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

Add tc39 stage 0 proposal #12

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
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
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
"oby": "14.3.1",
"preact": "^10.17.1",
"s-js": "^0.4.9",
"signal-polyfill": "^0.1.0",
"solid-js": "^1.7.11",
"usignal": "^0.9.0"
},
Expand Down
7 changes: 7 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { angularFramework } from "./frameworks/angularSignals";
import { compostateFramework } from "./frameworks/compostate";
import { kairoFramework } from "./frameworks/kairo";
import { mobxFramework } from "./frameworks/mobx";
import { tc39SignalsProposalStage0 } from './frameworks/tc39-proposal-signals-stage-0';
import { molWireFramework } from "./frameworks/molWire";
import { obyFramework } from "./frameworks/oby";
import { preactSignalFramework } from "./frameworks/preactSignals";
Expand All @@ -17,6 +18,7 @@ import { xReactivityFramework } from "./frameworks/xReactivity";
export const frameworkInfo: FrameworkInfo[] = [
{ framework: angularFramework, testPullCounts: true },
{ framework: compostateFramework, testPullCounts: true },
{ framework: tc39SignalsProposalStage0, testPullCounts: true },
// { framework: kairoFramework, testPullCounts: true },
// { framework: mobxFramework, testPullCounts: true },
{ framework: molWireFramework, testPullCounts: true },
Expand Down
66 changes: 66 additions & 0 deletions src/frameworks/tc39-proposal-signals-stage-0.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
import { ReactiveFramework } from "../util/reactiveFramework";
import { Signal } from "signal-polyfill";


export const tc39SignalsProposalStage0: ReactiveFramework = {
name: "TC39 Signals Proposal: Stage 0",
signal: (initialValue) => {
const s = new Signal.State(initialValue);
return {
write: (v) => s.set(v),
read: () => s.get(),
};
},
computed: (fn) => {
const c = new Signal.Computed(fn);
return {
read: () => c.get(),
};
},
effect: (fn) => effect(fn),
withBatch: (fn) => {
fn();
processPending();
},
withBuild: (fn) => fn(),
};

let needsEnqueue = false;

const w = new Signal.subtle.Watcher(() => {
if (needsEnqueue) {
needsEnqueue = false;
(async () => {
await Promise.resolve();
// next micro queue
processPending();
})();
}
});

function processPending() {
needsEnqueue = true;

for (const s of w.getPending()) {
s.get();
}

w.watch();
}

export function effect(callback) {
let cleanup;

const computed = new Signal.Computed(() => {
typeof cleanup === "function" && cleanup();
cleanup = callback();
});

w.watch(computed);
computed.get();

return () => {
w.unwatch(computed);
typeof cleanup === "function" && cleanup();
};
}