@matthesketh/utopia-core 0.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 Matt Hesketh
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,62 @@
1
+ # @matthesketh/utopia-core
2
+
3
+ Fine-grained signals reactivity system for UtopiaJS.
4
+
5
+ ## Install
6
+
7
+ ```bash
8
+ pnpm add @matthesketh/utopia-core
9
+ ```
10
+
11
+ ## Usage
12
+
13
+ ```ts
14
+ import { signal, computed, effect, batch, untrack } from '@matthesketh/utopia-core';
15
+
16
+ // Writable signal
17
+ const count = signal(0);
18
+ count(); // read (tracked)
19
+ count.value; // read (tracked)
20
+ count.peek(); // read (untracked)
21
+ count.set(1); // write
22
+ count.update(n => n + 1); // write via callback
23
+
24
+ // Lazy computed
25
+ const double = computed(() => count() * 2);
26
+ double(); // 4
27
+
28
+ // Reactive effect
29
+ const dispose = effect(() => {
30
+ console.log('count is', count());
31
+ return () => console.log('cleaning up');
32
+ });
33
+ dispose(); // stop watching
34
+
35
+ // Batch multiple writes
36
+ batch(() => {
37
+ count.set(10);
38
+ count.set(20);
39
+ }); // effects run once after batch
40
+
41
+ // Untracked reads
42
+ effect(() => {
43
+ const tracked = count();
44
+ const untracked_val = untrack(() => count());
45
+ });
46
+ ```
47
+
48
+ ## API
49
+
50
+ | Export | Description |
51
+ |--------|-------------|
52
+ | `signal(value)` | Writable reactive cell. Read via `()` or `.value`, write via `.set()` or `.update()`. |
53
+ | `computed(fn)` | Lazy derived value. Recomputes only when dependencies change and the value is read. |
54
+ | `effect(fn)` | Eager side-effect. Re-runs when dependencies change. Returns a dispose function. |
55
+ | `batch(fn)` | Groups multiple writes -- effects only run once after the batch completes. |
56
+ | `untrack(fn)` | Reads signals inside `fn` without creating dependency subscriptions. |
57
+
58
+ See [docs/architecture.md](../../docs/architecture.md) for full details on the reactivity system.
59
+
60
+ ## License
61
+
62
+ MIT
package/dist/index.cjs ADDED
@@ -0,0 +1,283 @@
1
+ "use strict";
2
+ var __defProp = Object.defineProperty;
3
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
+ var __getOwnPropNames = Object.getOwnPropertyNames;
5
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
6
+ var __export = (target, all) => {
7
+ for (var name in all)
8
+ __defProp(target, name, { get: all[name], enumerable: true });
9
+ };
10
+ var __copyProps = (to, from, except, desc) => {
11
+ if (from && typeof from === "object" || typeof from === "function") {
12
+ for (let key of __getOwnPropNames(from))
13
+ if (!__hasOwnProp.call(to, key) && key !== except)
14
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
15
+ }
16
+ return to;
17
+ };
18
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
19
+
20
+ // src/index.ts
21
+ var index_exports = {};
22
+ __export(index_exports, {
23
+ batch: () => batch,
24
+ computed: () => computed,
25
+ effect: () => effect,
26
+ signal: () => signal,
27
+ untrack: () => untrack
28
+ });
29
+ module.exports = __toCommonJS(index_exports);
30
+ var subscriberStack = [];
31
+ var currentSubscriber = null;
32
+ var batchDepth = 0;
33
+ var pendingEffects = /* @__PURE__ */ new Set();
34
+ function pushSubscriber(sub) {
35
+ subscriberStack.push(currentSubscriber);
36
+ currentSubscriber = sub;
37
+ }
38
+ function popSubscriber() {
39
+ currentSubscriber = subscriberStack.pop() ?? null;
40
+ }
41
+ var SignalNode = class {
42
+ /** Current stored value. */
43
+ _value;
44
+ /** Set of subscribers currently tracking this signal. */
45
+ _subscribers = /* @__PURE__ */ new Set();
46
+ constructor(value) {
47
+ this._value = value;
48
+ }
49
+ /** Read value, registering the current subscriber if any. */
50
+ _read() {
51
+ if (currentSubscriber !== null) {
52
+ this._subscribers.add(currentSubscriber);
53
+ currentSubscriber.dependencies.add(this);
54
+ }
55
+ return this._value;
56
+ }
57
+ /** Read value WITHOUT tracking. */
58
+ _peek() {
59
+ return this._value;
60
+ }
61
+ /** Write a new value. If changed, notify all subscribers. */
62
+ _write(newValue) {
63
+ if (Object.is(this._value, newValue)) {
64
+ return;
65
+ }
66
+ this._value = newValue;
67
+ batchDepth++;
68
+ try {
69
+ const subs = Array.from(this._subscribers);
70
+ for (let i = 0; i < subs.length; i++) {
71
+ subs[i].notify();
72
+ }
73
+ } finally {
74
+ batchDepth--;
75
+ if (batchDepth === 0) {
76
+ flushPendingEffects();
77
+ }
78
+ }
79
+ }
80
+ };
81
+ function signal(initialValue) {
82
+ const node = new SignalNode(initialValue);
83
+ const read = (() => node._read());
84
+ Object.defineProperty(read, "value", {
85
+ get() {
86
+ return node._read();
87
+ },
88
+ enumerable: true,
89
+ configurable: false
90
+ });
91
+ read.peek = () => node._peek();
92
+ read.set = (newValue) => {
93
+ node._write(newValue);
94
+ };
95
+ read.update = (fn) => {
96
+ node._write(fn(node._value));
97
+ };
98
+ return read;
99
+ }
100
+ var ComputedNode = class {
101
+ _fn;
102
+ _value;
103
+ _dirty = true;
104
+ _initialized = false;
105
+ _signalNode;
106
+ dependencies = /* @__PURE__ */ new Set();
107
+ /**
108
+ * Whether we are currently recomputing. Used to prevent infinite loops
109
+ * and to correctly propagate to downstream subscribers only after our
110
+ * own value has settled.
111
+ */
112
+ _computing = false;
113
+ constructor(fn) {
114
+ this._fn = fn;
115
+ this._signalNode = new SignalNode(void 0);
116
+ }
117
+ /** Subscriber interface — called when an upstream dependency changes. */
118
+ notify() {
119
+ if (!this._dirty) {
120
+ this._dirty = true;
121
+ const subs = Array.from(this._signalNode._subscribers);
122
+ for (let i = 0; i < subs.length; i++) {
123
+ subs[i].notify();
124
+ }
125
+ }
126
+ }
127
+ /** Recompute (if dirty) and return the value. */
128
+ _read() {
129
+ if (this._dirty && !this._computing) {
130
+ this._recompute();
131
+ }
132
+ return this._signalNode._read();
133
+ }
134
+ /** Read without tracking. */
135
+ _peek() {
136
+ if (this._dirty && !this._computing) {
137
+ this._recompute();
138
+ }
139
+ return this._signalNode._peek();
140
+ }
141
+ /** Unsubscribe from all current dependencies. */
142
+ _cleanup() {
143
+ for (const dep of this.dependencies) {
144
+ dep._subscribers.delete(this);
145
+ }
146
+ this.dependencies.clear();
147
+ }
148
+ /** Recompute the derived value. */
149
+ _recompute() {
150
+ this._computing = true;
151
+ this._cleanup();
152
+ pushSubscriber(this);
153
+ try {
154
+ const newValue = this._fn();
155
+ this._dirty = false;
156
+ this._computing = false;
157
+ if (!this._initialized || !Object.is(newValue, this._signalNode._value)) {
158
+ this._initialized = true;
159
+ this._signalNode._value = newValue;
160
+ }
161
+ } finally {
162
+ popSubscriber();
163
+ this._computing = false;
164
+ }
165
+ }
166
+ };
167
+ function computed(fn) {
168
+ const node = new ComputedNode(fn);
169
+ const read = (() => node._read());
170
+ Object.defineProperty(read, "value", {
171
+ get() {
172
+ return node._read();
173
+ },
174
+ enumerable: true,
175
+ configurable: false
176
+ });
177
+ read.peek = () => node._peek();
178
+ return read;
179
+ }
180
+ var EffectNode = class {
181
+ _fn;
182
+ _cleanupFn = void 0;
183
+ _disposed = false;
184
+ dependencies = /* @__PURE__ */ new Set();
185
+ /**
186
+ * Flag to prevent re-entrant notification. When an effect is already
187
+ * queued (or currently executing), additional notifications are ignored.
188
+ */
189
+ _queued = false;
190
+ constructor(fn) {
191
+ this._fn = fn;
192
+ }
193
+ /** Subscriber interface — called when an upstream dependency changes. */
194
+ notify() {
195
+ if (this._disposed || this._queued) {
196
+ return;
197
+ }
198
+ this._queued = true;
199
+ if (batchDepth > 0) {
200
+ pendingEffects.add(this);
201
+ } else {
202
+ this._run();
203
+ }
204
+ }
205
+ /** Execute the effect, cleaning up previous subscriptions first. */
206
+ _run() {
207
+ if (this._disposed) {
208
+ this._queued = false;
209
+ return;
210
+ }
211
+ if (this._cleanupFn) {
212
+ this._cleanupFn();
213
+ this._cleanupFn = void 0;
214
+ }
215
+ this._unsubscribe();
216
+ pushSubscriber(this);
217
+ try {
218
+ const result = this._fn();
219
+ this._cleanupFn = typeof result === "function" ? result : void 0;
220
+ } finally {
221
+ popSubscriber();
222
+ this._queued = false;
223
+ }
224
+ }
225
+ /** Unsubscribe from all tracked dependencies. */
226
+ _unsubscribe() {
227
+ for (const dep of this.dependencies) {
228
+ dep._subscribers.delete(this);
229
+ }
230
+ this.dependencies.clear();
231
+ }
232
+ /** Dispose the effect permanently — runs cleanup and unsubscribes. */
233
+ _dispose() {
234
+ this._disposed = true;
235
+ if (this._cleanupFn) {
236
+ this._cleanupFn();
237
+ this._cleanupFn = void 0;
238
+ }
239
+ this._unsubscribe();
240
+ pendingEffects.delete(this);
241
+ }
242
+ };
243
+ function effect(fn) {
244
+ const node = new EffectNode(fn);
245
+ node._run();
246
+ return () => node._dispose();
247
+ }
248
+ function batch(fn) {
249
+ batchDepth++;
250
+ try {
251
+ return fn();
252
+ } finally {
253
+ batchDepth--;
254
+ if (batchDepth === 0) {
255
+ flushPendingEffects();
256
+ }
257
+ }
258
+ }
259
+ function flushPendingEffects() {
260
+ while (pendingEffects.size > 0) {
261
+ const effects = Array.from(pendingEffects);
262
+ pendingEffects.clear();
263
+ for (let i = 0; i < effects.length; i++) {
264
+ effects[i]._run();
265
+ }
266
+ }
267
+ }
268
+ function untrack(fn) {
269
+ pushSubscriber(null);
270
+ try {
271
+ return fn();
272
+ } finally {
273
+ popSubscriber();
274
+ }
275
+ }
276
+ // Annotate the CommonJS export names for ESM import in node:
277
+ 0 && (module.exports = {
278
+ batch,
279
+ computed,
280
+ effect,
281
+ signal,
282
+ untrack
283
+ });
@@ -0,0 +1,83 @@
1
+ /** A read-only reactive signal. */
2
+ interface ReadonlySignal<T> {
3
+ /** Read the current value (tracks dependency). */
4
+ (): T;
5
+ /** Read the current value (tracks dependency). */
6
+ readonly value: T;
7
+ /** Read the current value WITHOUT tracking dependency. */
8
+ peek(): T;
9
+ }
10
+ /** A writable reactive signal. */
11
+ interface Signal<T> extends ReadonlySignal<T> {
12
+ /** Set a new value. */
13
+ set(newValue: T): void;
14
+ /** Update via callback: fn(currentValue) => newValue. */
15
+ update(fn: (current: T) => T): void;
16
+ }
17
+ /**
18
+ * Creates a writable reactive signal.
19
+ *
20
+ * ```ts
21
+ * const count = signal(0);
22
+ * count() // read (tracked)
23
+ * count.value // read (tracked)
24
+ * count.peek() // read (untracked)
25
+ * count.set(1) // write
26
+ * count.update(n => n + 1) // write via callback
27
+ * ```
28
+ */
29
+ declare function signal<T>(initialValue: T): Signal<T>;
30
+ /**
31
+ * Creates a lazy computed (derived) signal.
32
+ *
33
+ * ```ts
34
+ * const double = computed(() => count() * 2);
35
+ * double() // read (tracked)
36
+ * double.value // read (tracked)
37
+ * ```
38
+ */
39
+ declare function computed<T>(fn: () => T): ReadonlySignal<T>;
40
+ /**
41
+ * Creates a reactive side-effect that re-runs when its dependencies change.
42
+ *
43
+ * The callback may optionally return a cleanup function that is invoked
44
+ * before each re-execution and on disposal (like React useEffect).
45
+ *
46
+ * Returns a dispose function to stop the effect.
47
+ *
48
+ * ```ts
49
+ * const dispose = effect(() => {
50
+ * console.log('count is', count());
51
+ * return () => console.log('cleaning up');
52
+ * });
53
+ *
54
+ * dispose(); // stop watching
55
+ * ```
56
+ */
57
+ declare function effect(fn: () => void | (() => void)): () => void;
58
+ /**
59
+ * Batches multiple signal writes so that effects only run once after the
60
+ * batch completes.
61
+ *
62
+ * ```ts
63
+ * batch(() => {
64
+ * a.set(1);
65
+ * b.set(2);
66
+ * });
67
+ * // effects that depend on a AND b only run once
68
+ * ```
69
+ */
70
+ declare function batch<T>(fn: () => T): T;
71
+ /**
72
+ * Runs a function without tracking any signal reads as dependencies.
73
+ *
74
+ * ```ts
75
+ * effect(() => {
76
+ * const x = a(); // tracked
77
+ * const y = untrack(() => b()); // NOT tracked
78
+ * });
79
+ * ```
80
+ */
81
+ declare function untrack<T>(fn: () => T): T;
82
+
83
+ export { type ReadonlySignal, type Signal, batch, computed, effect, signal, untrack };
@@ -0,0 +1,83 @@
1
+ /** A read-only reactive signal. */
2
+ interface ReadonlySignal<T> {
3
+ /** Read the current value (tracks dependency). */
4
+ (): T;
5
+ /** Read the current value (tracks dependency). */
6
+ readonly value: T;
7
+ /** Read the current value WITHOUT tracking dependency. */
8
+ peek(): T;
9
+ }
10
+ /** A writable reactive signal. */
11
+ interface Signal<T> extends ReadonlySignal<T> {
12
+ /** Set a new value. */
13
+ set(newValue: T): void;
14
+ /** Update via callback: fn(currentValue) => newValue. */
15
+ update(fn: (current: T) => T): void;
16
+ }
17
+ /**
18
+ * Creates a writable reactive signal.
19
+ *
20
+ * ```ts
21
+ * const count = signal(0);
22
+ * count() // read (tracked)
23
+ * count.value // read (tracked)
24
+ * count.peek() // read (untracked)
25
+ * count.set(1) // write
26
+ * count.update(n => n + 1) // write via callback
27
+ * ```
28
+ */
29
+ declare function signal<T>(initialValue: T): Signal<T>;
30
+ /**
31
+ * Creates a lazy computed (derived) signal.
32
+ *
33
+ * ```ts
34
+ * const double = computed(() => count() * 2);
35
+ * double() // read (tracked)
36
+ * double.value // read (tracked)
37
+ * ```
38
+ */
39
+ declare function computed<T>(fn: () => T): ReadonlySignal<T>;
40
+ /**
41
+ * Creates a reactive side-effect that re-runs when its dependencies change.
42
+ *
43
+ * The callback may optionally return a cleanup function that is invoked
44
+ * before each re-execution and on disposal (like React useEffect).
45
+ *
46
+ * Returns a dispose function to stop the effect.
47
+ *
48
+ * ```ts
49
+ * const dispose = effect(() => {
50
+ * console.log('count is', count());
51
+ * return () => console.log('cleaning up');
52
+ * });
53
+ *
54
+ * dispose(); // stop watching
55
+ * ```
56
+ */
57
+ declare function effect(fn: () => void | (() => void)): () => void;
58
+ /**
59
+ * Batches multiple signal writes so that effects only run once after the
60
+ * batch completes.
61
+ *
62
+ * ```ts
63
+ * batch(() => {
64
+ * a.set(1);
65
+ * b.set(2);
66
+ * });
67
+ * // effects that depend on a AND b only run once
68
+ * ```
69
+ */
70
+ declare function batch<T>(fn: () => T): T;
71
+ /**
72
+ * Runs a function without tracking any signal reads as dependencies.
73
+ *
74
+ * ```ts
75
+ * effect(() => {
76
+ * const x = a(); // tracked
77
+ * const y = untrack(() => b()); // NOT tracked
78
+ * });
79
+ * ```
80
+ */
81
+ declare function untrack<T>(fn: () => T): T;
82
+
83
+ export { type ReadonlySignal, type Signal, batch, computed, effect, signal, untrack };
package/dist/index.js ADDED
@@ -0,0 +1,254 @@
1
+ // src/index.ts
2
+ var subscriberStack = [];
3
+ var currentSubscriber = null;
4
+ var batchDepth = 0;
5
+ var pendingEffects = /* @__PURE__ */ new Set();
6
+ function pushSubscriber(sub) {
7
+ subscriberStack.push(currentSubscriber);
8
+ currentSubscriber = sub;
9
+ }
10
+ function popSubscriber() {
11
+ currentSubscriber = subscriberStack.pop() ?? null;
12
+ }
13
+ var SignalNode = class {
14
+ /** Current stored value. */
15
+ _value;
16
+ /** Set of subscribers currently tracking this signal. */
17
+ _subscribers = /* @__PURE__ */ new Set();
18
+ constructor(value) {
19
+ this._value = value;
20
+ }
21
+ /** Read value, registering the current subscriber if any. */
22
+ _read() {
23
+ if (currentSubscriber !== null) {
24
+ this._subscribers.add(currentSubscriber);
25
+ currentSubscriber.dependencies.add(this);
26
+ }
27
+ return this._value;
28
+ }
29
+ /** Read value WITHOUT tracking. */
30
+ _peek() {
31
+ return this._value;
32
+ }
33
+ /** Write a new value. If changed, notify all subscribers. */
34
+ _write(newValue) {
35
+ if (Object.is(this._value, newValue)) {
36
+ return;
37
+ }
38
+ this._value = newValue;
39
+ batchDepth++;
40
+ try {
41
+ const subs = Array.from(this._subscribers);
42
+ for (let i = 0; i < subs.length; i++) {
43
+ subs[i].notify();
44
+ }
45
+ } finally {
46
+ batchDepth--;
47
+ if (batchDepth === 0) {
48
+ flushPendingEffects();
49
+ }
50
+ }
51
+ }
52
+ };
53
+ function signal(initialValue) {
54
+ const node = new SignalNode(initialValue);
55
+ const read = (() => node._read());
56
+ Object.defineProperty(read, "value", {
57
+ get() {
58
+ return node._read();
59
+ },
60
+ enumerable: true,
61
+ configurable: false
62
+ });
63
+ read.peek = () => node._peek();
64
+ read.set = (newValue) => {
65
+ node._write(newValue);
66
+ };
67
+ read.update = (fn) => {
68
+ node._write(fn(node._value));
69
+ };
70
+ return read;
71
+ }
72
+ var ComputedNode = class {
73
+ _fn;
74
+ _value;
75
+ _dirty = true;
76
+ _initialized = false;
77
+ _signalNode;
78
+ dependencies = /* @__PURE__ */ new Set();
79
+ /**
80
+ * Whether we are currently recomputing. Used to prevent infinite loops
81
+ * and to correctly propagate to downstream subscribers only after our
82
+ * own value has settled.
83
+ */
84
+ _computing = false;
85
+ constructor(fn) {
86
+ this._fn = fn;
87
+ this._signalNode = new SignalNode(void 0);
88
+ }
89
+ /** Subscriber interface — called when an upstream dependency changes. */
90
+ notify() {
91
+ if (!this._dirty) {
92
+ this._dirty = true;
93
+ const subs = Array.from(this._signalNode._subscribers);
94
+ for (let i = 0; i < subs.length; i++) {
95
+ subs[i].notify();
96
+ }
97
+ }
98
+ }
99
+ /** Recompute (if dirty) and return the value. */
100
+ _read() {
101
+ if (this._dirty && !this._computing) {
102
+ this._recompute();
103
+ }
104
+ return this._signalNode._read();
105
+ }
106
+ /** Read without tracking. */
107
+ _peek() {
108
+ if (this._dirty && !this._computing) {
109
+ this._recompute();
110
+ }
111
+ return this._signalNode._peek();
112
+ }
113
+ /** Unsubscribe from all current dependencies. */
114
+ _cleanup() {
115
+ for (const dep of this.dependencies) {
116
+ dep._subscribers.delete(this);
117
+ }
118
+ this.dependencies.clear();
119
+ }
120
+ /** Recompute the derived value. */
121
+ _recompute() {
122
+ this._computing = true;
123
+ this._cleanup();
124
+ pushSubscriber(this);
125
+ try {
126
+ const newValue = this._fn();
127
+ this._dirty = false;
128
+ this._computing = false;
129
+ if (!this._initialized || !Object.is(newValue, this._signalNode._value)) {
130
+ this._initialized = true;
131
+ this._signalNode._value = newValue;
132
+ }
133
+ } finally {
134
+ popSubscriber();
135
+ this._computing = false;
136
+ }
137
+ }
138
+ };
139
+ function computed(fn) {
140
+ const node = new ComputedNode(fn);
141
+ const read = (() => node._read());
142
+ Object.defineProperty(read, "value", {
143
+ get() {
144
+ return node._read();
145
+ },
146
+ enumerable: true,
147
+ configurable: false
148
+ });
149
+ read.peek = () => node._peek();
150
+ return read;
151
+ }
152
+ var EffectNode = class {
153
+ _fn;
154
+ _cleanupFn = void 0;
155
+ _disposed = false;
156
+ dependencies = /* @__PURE__ */ new Set();
157
+ /**
158
+ * Flag to prevent re-entrant notification. When an effect is already
159
+ * queued (or currently executing), additional notifications are ignored.
160
+ */
161
+ _queued = false;
162
+ constructor(fn) {
163
+ this._fn = fn;
164
+ }
165
+ /** Subscriber interface — called when an upstream dependency changes. */
166
+ notify() {
167
+ if (this._disposed || this._queued) {
168
+ return;
169
+ }
170
+ this._queued = true;
171
+ if (batchDepth > 0) {
172
+ pendingEffects.add(this);
173
+ } else {
174
+ this._run();
175
+ }
176
+ }
177
+ /** Execute the effect, cleaning up previous subscriptions first. */
178
+ _run() {
179
+ if (this._disposed) {
180
+ this._queued = false;
181
+ return;
182
+ }
183
+ if (this._cleanupFn) {
184
+ this._cleanupFn();
185
+ this._cleanupFn = void 0;
186
+ }
187
+ this._unsubscribe();
188
+ pushSubscriber(this);
189
+ try {
190
+ const result = this._fn();
191
+ this._cleanupFn = typeof result === "function" ? result : void 0;
192
+ } finally {
193
+ popSubscriber();
194
+ this._queued = false;
195
+ }
196
+ }
197
+ /** Unsubscribe from all tracked dependencies. */
198
+ _unsubscribe() {
199
+ for (const dep of this.dependencies) {
200
+ dep._subscribers.delete(this);
201
+ }
202
+ this.dependencies.clear();
203
+ }
204
+ /** Dispose the effect permanently — runs cleanup and unsubscribes. */
205
+ _dispose() {
206
+ this._disposed = true;
207
+ if (this._cleanupFn) {
208
+ this._cleanupFn();
209
+ this._cleanupFn = void 0;
210
+ }
211
+ this._unsubscribe();
212
+ pendingEffects.delete(this);
213
+ }
214
+ };
215
+ function effect(fn) {
216
+ const node = new EffectNode(fn);
217
+ node._run();
218
+ return () => node._dispose();
219
+ }
220
+ function batch(fn) {
221
+ batchDepth++;
222
+ try {
223
+ return fn();
224
+ } finally {
225
+ batchDepth--;
226
+ if (batchDepth === 0) {
227
+ flushPendingEffects();
228
+ }
229
+ }
230
+ }
231
+ function flushPendingEffects() {
232
+ while (pendingEffects.size > 0) {
233
+ const effects = Array.from(pendingEffects);
234
+ pendingEffects.clear();
235
+ for (let i = 0; i < effects.length; i++) {
236
+ effects[i]._run();
237
+ }
238
+ }
239
+ }
240
+ function untrack(fn) {
241
+ pushSubscriber(null);
242
+ try {
243
+ return fn();
244
+ } finally {
245
+ popSubscriber();
246
+ }
247
+ }
248
+ export {
249
+ batch,
250
+ computed,
251
+ effect,
252
+ signal,
253
+ untrack
254
+ };
package/package.json ADDED
@@ -0,0 +1,45 @@
1
+ {
2
+ "name": "@matthesketh/utopia-core",
3
+ "version": "0.0.1",
4
+ "description": "Fine-grained signals reactivity system for UtopiaJS",
5
+ "type": "module",
6
+ "license": "MIT",
7
+ "author": "Matt <matt@matthesketh.pro>",
8
+ "repository": {
9
+ "type": "git",
10
+ "url": "https://github.com/wrxck/utopiajs.git",
11
+ "directory": "packages/core"
12
+ },
13
+ "homepage": "https://github.com/wrxck/utopiajs/tree/main/packages/core",
14
+ "keywords": [
15
+ "signals",
16
+ "reactive",
17
+ "fine-grained",
18
+ "state-management",
19
+ "utopiajs"
20
+ ],
21
+ "engines": {
22
+ "node": ">=20.0.0"
23
+ },
24
+ "publishConfig": {
25
+ "access": "public"
26
+ },
27
+ "sideEffects": false,
28
+ "main": "./dist/index.cjs",
29
+ "module": "./dist/index.js",
30
+ "types": "./dist/index.d.ts",
31
+ "exports": {
32
+ ".": {
33
+ "types": "./dist/index.d.ts",
34
+ "import": "./dist/index.js",
35
+ "require": "./dist/index.cjs"
36
+ }
37
+ },
38
+ "files": [
39
+ "dist"
40
+ ],
41
+ "scripts": {
42
+ "build": "tsup src/index.ts --format esm,cjs --dts",
43
+ "dev": "tsup src/index.ts --format esm,cjs --dts --watch"
44
+ }
45
+ }