Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(react): add state support #35

Merged
merged 1 commit into from
May 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/silent-papayas-trade.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@coldwired/react": minor
---

add state support
22 changes: 17 additions & 5 deletions packages/react/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@
"name": "@coldwired/react",
"description": "React support for @coldwired",
"license": "MIT",
"files": ["dist"],
"files": [
"dist"
],
"main": "./dist/index.cjs.js",
"module": "./dist/index.es.js",
"types": "./dist/types/index.d.ts",
Expand Down Expand Up @@ -32,12 +34,13 @@
"@coldwired/utils": "^0.13.0"
},
"devDependencies": {
"react-error-boundary": "^4.0.13",
"@coldwired/actions": "*",
"@types/react": "^18.2.45",
"@types/react-dom": "^18.2.18",
"html-entities": "^2.4.0",
"react-aria-components": "^1.2.0",
"react-error-boundary": "^4.0.13",
"react-fast-compare": "^3.2.2",
"zod": "^3.23.4"
},
"peerDependencies": {
Expand All @@ -55,15 +58,24 @@
"eslintConfig": {
"root": true,
"parser": "@typescript-eslint/parser",
"plugins": ["@typescript-eslint"],
"extends": ["eslint:recommended", "plugin:@typescript-eslint/recommended", "prettier"],
"plugins": [
"@typescript-eslint"
],
"extends": [
"eslint:recommended",
"plugin:@typescript-eslint/recommended",
"prettier"
],
"rules": {
"@typescript-eslint/no-explicit-any": "off",
"@typescript-eslint/no-redeclare": "off"
},
"overrides": [
{
"files": ["vite.config.js", "vitest.config.ts"],
"files": [
"vite.config.js",
"vitest.config.ts"
],
"env": {
"node": true
}
Expand Down
3 changes: 2 additions & 1 deletion packages/react/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
export * from './root';
export * from './plugin';
export { hydrate, preload, createReactTree } from './react-tree-builder';
export * from './observable';
export { hydrate, preload, createReactTree, createState, type State } from './react-tree-builder';
70 changes: 70 additions & 0 deletions packages/react/src/observable.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
export interface Observer<T> {
next: NextChannel<T>;
}

type Connect<T> = (observer: Observer<T>) => Disconnect;
type Disconnect = () => void;

export type NextChannel<T> = (value: T) => void;
export type ObserverOrNext<T> = Observer<T> | NextChannel<T>;

export type Unsubscribe = () => void;
export type Subscription = { unsubscribe: Unsubscribe };

/**
* `Observable` is a standard interface that's useful for modeling multiple,
* asynchronous events.
*/
export class Observable<T> implements Observable<T> {
#connect: Connect<T>;

/**
* The provided function should receive an observer and connect that
* observer's `next` method to an event source (for instance,
* `element.addEventListener('click', observer.next)`).
*
* It must return a function that will disconnect the observer from the event
* source.
*/
constructor(connect: Connect<T>) {
this.#connect = connect;
}

/**
* `subscribe` uses the function supplied to the constructor to connect an
* observer to an event source. Each observer is connected independently:
* each call to `subscribe` calls `connect` with the new observer.
*
* To disconnect the observer from the event source, call `unsubscribe` on the
* returned subscription.
*
* Note: `subscribe` accepts either a function or an object with a
* next method.
*/
subscribe(observerOrNext: ObserverOrNext<T>): Subscription {
// For simplicity's sake, `subscribe` accepts `next` either as either an
// anonymous function or wrapped in an object (the observer). Since
// `connect` always expects to receive an observer, wrap any loose
// functions in an object.
const observer = wrapWithObserver<T>(observerOrNext);

let disconnect: Disconnect | undefined = this.#connect(observer);

return {
unsubscribe() {
if (disconnect) {
disconnect();
disconnect = undefined;
}
},
};
}
}

function wrapWithObserver<T>(listener: ObserverOrNext<T>): Observer<T> {
if (typeof listener == 'function') {
return { next: listener };
} else {
return listener;
}
}
4 changes: 4 additions & 0 deletions packages/react/src/react-tree-builder.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import {
hydrate,
preload,
defaultSchema,
createNullState,
type ReactComponent,
} from './react-tree-builder';

Expand Down Expand Up @@ -61,6 +62,7 @@ describe('@coldwired/react', () => {
const tree = createReactTree(
{ tagName: 'div', attributes: { className: 'title' }, children: 'Hello' },
{},
createNullState(),
);
const html = renderToStaticMarkup(tree);
expect(html).toBe('<div class="title">Hello</div>');
Expand All @@ -79,6 +81,7 @@ describe('@coldwired/react', () => {
],
},
{},
createNullState(),
);
const html = renderToStaticMarkup(tree);
expect(html).toBe(
Expand Down Expand Up @@ -122,6 +125,7 @@ describe('@coldwired/react', () => {
},
],
{ Greeting, FieldSet },
createNullState(),
);
const html = renderToStaticMarkup(tree);
expect(html).toBe(
Expand Down
Loading