@dougongjs/reactive 0.8.3 → 0.10.0

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/dist/index.js CHANGED
@@ -1,406 +1,626 @@
1
- function D(s) {
2
- if (typeof s != "function")
3
- throw new Error("Unsupported JavaScript runtime: Promise.withResolvers is required");
1
+ import { listenForAbort as e } from "#abort-listener";
2
+ // src/disposal-runtime.ts
3
+ function t(e) {
4
+ if (typeof e != "function") throw Error("Unsupported JavaScript runtime: Promise.withResolvers is required");
4
5
  }
5
- function b(s, e) {
6
- return s ?? Symbol.for(e);
6
+ function n(e, t) {
7
+ return e ?? Symbol.for(t);
7
8
  }
8
- D(Promise.withResolvers);
9
- const w = b(
10
- Symbol.dispose,
11
- "Symbol.dispose"
12
- ), y = b(
13
- Symbol.asyncDispose,
14
- "Symbol.asyncDispose"
15
- );
16
- function a(s, e) {
17
- const t = m(s);
18
- if (t)
19
- throw t.catch(() => {
20
- }), new TypeError(e);
9
+ t(Promise.withResolvers);
10
+ var r = n(Symbol.dispose, "Symbol.dispose"), i = n(Symbol.asyncDispose, "Symbol.asyncDispose"), a = Promise.prototype, o = a.then;
11
+ async function s(e) {
12
+ return l(e) ?? Object.freeze({ value: e });
21
13
  }
22
- function m(s) {
23
- if (s === null || typeof s != "object" && typeof s != "function") return;
24
- const e = s.then;
25
- if (typeof e == "function")
26
- return Promise.resolve().then(
27
- () => new Promise((t, i) => Reflect.apply(e, s, [t, i]))
28
- );
14
+ function c(e, t) {
15
+ let n = l(e);
16
+ if (n) throw n.then(() => void 0, () => void 0), TypeError(t);
29
17
  }
30
- let u = 0, p = !1;
31
- const f = /* @__PURE__ */ new Set(), o = /* @__PURE__ */ new Set();
32
- let c;
33
- const l = /* @__PURE__ */ new WeakMap();
34
- class g {
35
- #e;
36
- constructor(e) {
37
- this.#e = e, Object.freeze(this);
38
- }
39
- dispose() {
40
- const e = this.#e;
41
- this.#e = void 0, e?.();
42
- }
43
- [w]() {
44
- this.dispose();
45
- }
18
+ function l(e) {
19
+ if (e === null || typeof e != "object" && typeof e != "function") return;
20
+ if (u(e)) {
21
+ let t = Promise.withResolvers();
22
+ return Reflect.apply(o, e, [(e) => t.resolve(Object.freeze({ value: e })), t.reject]), t.promise;
23
+ }
24
+ let t = e.then;
25
+ if (typeof t != "function") return;
26
+ let n = Promise.withResolvers();
27
+ return queueMicrotask(() => {
28
+ try {
29
+ Reflect.apply(t, e, [n.resolve, n.reject]);
30
+ } catch (e) {
31
+ n.reject(e);
32
+ }
33
+ }), n.promise.then((e) => Object.freeze({ value: e }));
46
34
  }
47
- function O() {
48
- if (p) return;
49
- const s = [];
50
- p = !0;
51
- try {
52
- for (; o.size || f.size; ) {
53
- const e = o.size ? o : f, t = e.values().next().value;
54
- e.delete(t);
55
- const i = t.listener;
56
- if (i)
57
- try {
58
- a(i(), "Signal subscribers must be synchronous");
59
- } catch (r) {
60
- s.push(r);
61
- }
62
- }
63
- } finally {
64
- f.clear(), o.clear(), p = !1;
65
- }
66
- R(s);
35
+ function u(e) {
36
+ let t = /* @__PURE__ */ new Set();
37
+ for (let n = Object.getPrototypeOf(e); n !== null; n = Object.getPrototypeOf(n)) {
38
+ if (n === a) return !0;
39
+ if (t.has(n)) throw TypeError("Result has a cyclic prototype chain");
40
+ t.add(n);
41
+ }
42
+ return !1;
67
43
  }
68
- function S(s) {
69
- for (const e of s)
70
- (e.kind === "dependency" ? o : f).add(e);
71
- u || O();
44
+ // src/protocol.ts
45
+ function d(e, t) {
46
+ let n = e?.get, r = e?.subscribe;
47
+ if (typeof n != "function" || typeof r != "function") throw TypeError(`${t} expects a readable source`);
48
+ return Object.freeze({
49
+ get: () => Reflect.apply(n, e, []),
50
+ subscribe: (t) => Reflect.apply(r, e, [t])
51
+ });
72
52
  }
73
- function d(s, e, t) {
74
- k(e);
75
- const i = { kind: t, listener: e };
76
- return s.add(i), new g(() => {
77
- i.listener = void 0, s.delete(i);
78
- });
53
+ function f(e) {
54
+ let t = e?.[r];
55
+ return typeof t != "function" && m(e, "Readable.subscribe() must return a Disposable"), () => c(Reflect.apply(t, e, []), "Readable subscriptions must dispose synchronously");
79
56
  }
80
- function R(s) {
81
- if (s.length === 1) throw s[0];
82
- if (s.length > 1)
83
- throw new AggregateError(s, "Reactive subscribers failed");
57
+ function p(e, t) {
58
+ let n = e?.[i];
59
+ return typeof n != "function" && m(e, t), () => Reflect.apply(n, e, []);
84
60
  }
85
- function E(s) {
86
- c?.(s);
61
+ function m(e, t) {
62
+ throw c(e, t), TypeError(t);
87
63
  }
88
- function I(s) {
89
- if (typeof s != "function") throw new TypeError("batch() expects a function");
90
- u++;
91
- let e;
92
- try {
93
- const t = s();
94
- a(t, "Reactive batches must be synchronous"), e = { ok: !0, value: t };
95
- } catch (t) {
96
- e = { ok: !1, error: t };
97
- } finally {
98
- u--;
99
- }
100
- if (!u)
101
- try {
102
- O();
103
- } catch (t) {
104
- e = e.ok ? { ok: !1, error: t } : { ok: !1, error: new AggregateError([e.error, t], "Reactive batch failed") };
105
- }
106
- if (!e.ok) throw e.error;
107
- return e.value;
64
+ function h(e) {
65
+ if (typeof e != "function") throw TypeError("Signal subscriber must be a function");
108
66
  }
109
- function L(s) {
110
- return new z(s).view;
111
- }
112
- function q(s) {
113
- if (typeof s != "function") throw new TypeError("computed() expects a function");
114
- return new P(s).view;
67
+ // src/signals.ts
68
+ var g = 0, _ = 0, v = !1, y = !1, b = /* @__PURE__ */ new Set(), x = /* @__PURE__ */ new Set(), S, C = [], w = class {
69
+ #e;
70
+ constructor(e) {
71
+ this.#e = e, Object.freeze(this);
72
+ }
73
+ [r]() {
74
+ let e = this.#e;
75
+ this.#e = void 0, e?.();
76
+ }
77
+ };
78
+ function T() {
79
+ if (v) return;
80
+ let e = [];
81
+ v = !0;
82
+ try {
83
+ for (; b.size;) {
84
+ let t = b.values().next().value;
85
+ b.delete(t);
86
+ let n = t.listener;
87
+ if (n) try {
88
+ c(n(), "Signal subscribers must be synchronous");
89
+ } catch (t) {
90
+ e.push(t);
91
+ }
92
+ }
93
+ } finally {
94
+ b.clear(), v = !1;
95
+ }
96
+ O(e);
115
97
  }
116
- class z {
117
- #e = /* @__PURE__ */ new Set();
118
- #t;
119
- #s = 0;
120
- view;
121
- constructor(e) {
122
- this.#t = e, this.view = Object.freeze({
123
- get: () => (E(this.view), this.#t),
124
- set: (t) => this.#r(t),
125
- subscribe: (t) => d(this.#e, t, "observer")
126
- }), l.set(this.view, this);
127
- }
128
- get version() {
129
- return this.#s;
130
- }
131
- refresh() {
132
- }
133
- subscribeInvalidation(e) {
134
- return d(this.#e, e, "dependency");
135
- }
136
- #r(e) {
137
- if (c)
138
- throw new TypeError("Computed signal calculations cannot write signals");
139
- Object.is(this.#t, e) || (this.#t = e, this.#s++, S(this.#e));
140
- }
98
+ function E(e) {
99
+ for (let t of e) (t.kind === "dependency" ? x : b).add(t);
100
+ if (!y) {
101
+ y = !0;
102
+ try {
103
+ for (; x.size;) {
104
+ let e = x.values().next().value;
105
+ x.delete(e);
106
+ let t = e.listener;
107
+ t?.();
108
+ }
109
+ } finally {
110
+ y = !1, x.clear();
111
+ }
112
+ _ || T();
113
+ }
141
114
  }
142
- class P {
143
- #e;
144
- #t = /* @__PURE__ */ new Set();
145
- #s = /* @__PURE__ */ new Map();
146
- #r;
147
- #n = !1;
148
- #i = !0;
149
- #c = 0;
150
- view;
151
- constructor(e) {
152
- this.#e = e, this.view = Object.freeze({
153
- get: () => this.#o(),
154
- subscribe: (t) => this.#h(t)
155
- }), l.set(this.view, this);
156
- }
157
- get version() {
158
- return this.#c;
159
- }
160
- refresh() {
161
- if (this.#n) throw new TypeError("Circular computed signal");
162
- this.#n = !0;
163
- try {
164
- this.#v();
165
- } finally {
166
- this.#n = !1;
167
- }
168
- }
169
- #o() {
170
- return this.refresh(), E(this.view), this.#a();
171
- }
172
- #a() {
173
- const e = this.#r;
174
- if (e.phase === "error") throw e.error;
175
- return e.value;
176
- }
177
- subscribeInvalidation(e) {
178
- return this.#h(e, "dependency");
179
- }
180
- #h(e, t = "observer") {
181
- k(e);
182
- const i = { kind: t, listener: e }, r = this.#t.size === 0;
183
- if (this.#t.add(i), r)
184
- try {
185
- this.refresh(), t === "observer" && this.#a(), this.#f();
186
- } catch (n) {
187
- throw this.#t.delete(i), this.#t.size || this.#u(), n;
188
- }
189
- return new g(() => {
190
- i.listener = void 0, this.#t.delete(i), this.#t.size || this.#u();
191
- });
192
- }
193
- #d = () => {
194
- this.#i || (this.#i = !0, S(this.#t));
195
- };
196
- #u() {
197
- for (const e of this.#s.values())
198
- e.subscription?.dispose(), e.subscription = void 0;
199
- }
200
- #f() {
201
- for (const e of this.#s.values())
202
- e.subscription ??= e.node.subscribeInvalidation(this.#d);
203
- }
204
- #v() {
205
- const e = this.#r;
206
- if (e && !this.#i && !this.#p()) return;
207
- this.#i = !0;
208
- const t = /* @__PURE__ */ new Set(), i = this.#b(t);
209
- this.#l(t), (!e || (i.phase === "value" ? e.phase !== "value" || !Object.is(e.value, i.value) : e.phase !== "error" || !Object.is(e.error, i.error))) && this.#c++, this.#r = i, this.#i = !1, this.#t.size && this.#f();
210
- }
211
- #p() {
212
- for (const e of this.#s.values())
213
- if (e.node.refresh(), e.version !== e.node.version) return !0;
214
- return this.#i;
215
- }
216
- #b(e) {
217
- const t = c;
218
- c = (i) => e.add(i);
219
- try {
220
- const i = this.#e();
221
- return a(i, "Computed signal calculations must be synchronous"), { phase: "value", value: i };
222
- } catch (i) {
223
- return { phase: "error", error: i };
224
- } finally {
225
- c = t;
226
- }
227
- }
228
- #l(e) {
229
- for (const [t, i] of this.#s)
230
- e.has(t) || (i.subscription?.dispose(), this.#s.delete(t));
231
- for (const t of e) {
232
- const i = l.get(t);
233
- if (!i) continue;
234
- const r = this.#s.get(t) ?? {
235
- node: i,
236
- version: i.version,
237
- subscription: void 0
238
- };
239
- r.version = i.version, this.#s.set(t, r);
240
- }
241
- }
115
+ function D(e, t, n) {
116
+ h(t);
117
+ let r = {
118
+ kind: n,
119
+ listener: t
120
+ };
121
+ return e.add(r), new w(() => {
122
+ r.listener = void 0, e.delete(r);
123
+ });
242
124
  }
243
- function k(s) {
244
- if (typeof s != "function")
245
- throw new TypeError("Signal subscriber must be a function");
125
+ function O(e) {
126
+ if (e.length === 1) throw e[0];
127
+ if (e.length > 1) throw AggregateError(e, "Reactive subscribers failed");
246
128
  }
247
- class j {
248
- #e;
249
- #t;
250
- #s;
251
- #r = { present: !1 };
252
- #n = !1;
253
- #i;
254
- #c;
255
- #o;
256
- #a;
257
- constructor(e, t, i) {
258
- this.#e = { phase: "active", binding: { owner: e, source: t, observer: i } };
259
- }
260
- start() {
261
- const { owner: e, source: t } = this.#y(), i = t.subscribe(() => this.#d());
262
- if (x(i, "Readable.subscribe()"), this.#e.phase !== "active")
263
- throw v(i), new Error("Observation is not active");
264
- this.#t = i, this.#p();
265
- const r = e.spawn((C) => this.#u(C)), n = A(r);
266
- this.#i = r, n.then(
267
- () => this.#m(r),
268
- () => this.#m(r)
269
- );
270
- }
271
- attachOwnerCleanup(e) {
272
- this.#c = e;
273
- }
274
- dispose() {
275
- if (this.#a) return this.#a;
276
- const e = Promise.withResolvers();
277
- return this.#a = e.promise, this.#c = void 0, this.#g("disposed"), this.#o?.(), this.#h().then(e.resolve, e.reject), e.promise;
278
- }
279
- async #h() {
280
- const e = [];
281
- if (await this.#l(e), await h(this.#O(), e), await h(this.#w(), e), e.length === 1) throw e[0];
282
- if (e.length > 1) throw new AggregateError(e, "Observation cleanup failed");
283
- }
284
- #d() {
285
- this.#e.phase === "active" && (this.#n = !0, this.#o?.());
286
- }
287
- // Stop on drain failure because continuing would observe a value whose resource ownership never established.
288
- async #u(e) {
289
- try {
290
- for (; this.#e.phase === "active" && !e.aborted; )
291
- for (await this.#f(e); this.#e.phase === "active" && !e.aborted && this.#n; )
292
- this.#n = !1, await this.#v(e);
293
- } catch (t) {
294
- if (this.#e.phase !== "active") throw t;
295
- await this.#E(t);
296
- }
297
- }
298
- #f(e) {
299
- return this.#n || this.#e.phase !== "active" || e.aborted ? Promise.resolve() : new Promise((t) => {
300
- const i = () => {
301
- e.removeEventListener("abort", i), this.#o === i && (this.#o = void 0), t();
302
- };
303
- this.#o = i, e.addEventListener("abort", i, { once: !0 }), (this.#n || this.#e.phase !== "active" || e.aborted) && i();
304
- });
305
- }
306
- // Clear the observed marker during replacement so interruption cannot leave a value without its owner.
307
- async #v(e) {
308
- if (this.#e.phase !== "active" || e.aborted) return;
309
- const t = this.#e.binding.source.get();
310
- if (this.#r.present && Object.is(t, this.#r.value)) return;
311
- const i = this.#w();
312
- i && await i.dispose(), this.#r = { present: !1 }, !(this.#e.phase !== "active" || e.aborted) && this.#p();
313
- }
314
- #p() {
315
- const { owner: e, source: t } = this.#y(), i = e.lifetime("observation");
316
- T(i, "ObservationOwner.lifetime()"), this.#s = i;
317
- const r = t.get();
318
- this.#b(r, i), this.#e.phase === "active" && (this.#r = { present: !0, value: r });
319
- }
320
- #b(e, t) {
321
- const i = this.#y().observer(e, t);
322
- a(i, "Observers must be synchronous; use owner.spawn() for async work");
323
- }
324
- async #l(e) {
325
- const t = this.#t;
326
- if (this.#t = void 0, !!t)
327
- try {
328
- v(t);
329
- } catch (i) {
330
- e.push(i);
331
- }
332
- }
333
- #w() {
334
- const e = this.#s;
335
- return this.#s = void 0, e;
336
- }
337
- #O() {
338
- const e = this.#i;
339
- return this.#i = void 0, e;
340
- }
341
- #S() {
342
- const e = this.#c;
343
- return this.#c = void 0, e;
344
- }
345
- #m(e) {
346
- this.#i === e && (this.#i = void 0);
347
- }
348
- async #E(e) {
349
- const t = [e];
350
- throw this.#g("stopped"), this.#o?.(), await this.#l(t), await h(this.#w(), t), this.#i = void 0, await h(this.#S(), t), t.length === 1 ? t[0] : new AggregateError(t, "Observation stopped after a replacement failed");
351
- }
352
- #y() {
353
- const e = this.#e;
354
- if (e.phase !== "active") throw new Error("Observation is not active");
355
- return e.binding;
356
- }
357
- #g(e) {
358
- this.#e = { phase: e }, this.#r = { present: !1 }, this.#n = !1;
359
- }
129
+ function k(e) {
130
+ if (typeof e != "function") throw TypeError("batch() expects a function");
131
+ _++;
132
+ let t;
133
+ try {
134
+ let n = e();
135
+ c(n, "Reactive batches must be synchronous"), t = {
136
+ succeeded: !0,
137
+ value: n
138
+ };
139
+ } catch (e) {
140
+ t = {
141
+ succeeded: !1,
142
+ error: e
143
+ };
144
+ } finally {
145
+ _--;
146
+ }
147
+ if (!_) try {
148
+ T();
149
+ } catch (e) {
150
+ t = t.succeeded ? {
151
+ succeeded: !1,
152
+ error: e
153
+ } : {
154
+ succeeded: !1,
155
+ error: AggregateError([t.error, e], "Reactive batch failed")
156
+ };
157
+ }
158
+ if (!t.succeeded) throw t.error;
159
+ return t.value;
360
160
  }
361
- function B(s, e, t) {
362
- if (!e || typeof e.get != "function" || typeof e.subscribe != "function")
363
- throw new TypeError("observe() expects a readable source");
364
- if (typeof t != "function") throw new TypeError("Observer must be a function");
365
- if (!s || typeof s.cleanup != "function" || typeof s.lifetime != "function" || typeof s.spawn != "function")
366
- throw new TypeError("observe() expects an observation owner");
367
- const i = new j(s, e, t), r = s.cleanup(() => i.dispose());
368
- T(r, "ObservationOwner.cleanup()"), i.attachOwnerCleanup(r);
369
- try {
370
- return i.start(), r;
371
- } catch (n) {
372
- throw i.dispose().catch(() => {
373
- }), n;
374
- }
161
+ function A(e) {
162
+ return new M(e).view;
375
163
  }
376
- function x(s, e) {
377
- if (!s || typeof s.dispose != "function" || typeof s[w] != "function")
378
- throw new TypeError(`${e} must return a Disposable`);
164
+ function j(e) {
165
+ if (typeof e != "function") throw TypeError("computed() expects a function");
166
+ return new N(e).view;
379
167
  }
380
- function T(s, e) {
381
- if (!s || typeof s.dispose != "function" || typeof s[y] != "function")
382
- throw new TypeError(`${e} must return an AsyncDisposable`);
168
+ var M = class {
169
+ #e = /* @__PURE__ */ new Set();
170
+ #t;
171
+ #n = 0;
172
+ view;
173
+ constructor(e) {
174
+ this.#t = e, this.view = Object.freeze({
175
+ get: () => (S?.add(this), this.#t),
176
+ set: (e) => this.#r(e),
177
+ subscribe: (e) => D(this.#e, e, "observer")
178
+ });
179
+ }
180
+ get version() {
181
+ return this.#n;
182
+ }
183
+ refresh() {}
184
+ subscribeInvalidation(e) {
185
+ return D(this.#e, e, "dependency");
186
+ }
187
+ #r(e) {
188
+ if (S) throw TypeError("Computed signal calculations cannot write signals");
189
+ Object.is(this.#t, e) || (this.#t = e, g++, this.#n++, E(this.#e));
190
+ }
191
+ }, N = class e {
192
+ #e;
193
+ #t = /* @__PURE__ */ new Set();
194
+ #n = /* @__PURE__ */ new Map();
195
+ #r;
196
+ #i = -1;
197
+ #a;
198
+ #o;
199
+ #s = -1;
200
+ #c = 0;
201
+ view;
202
+ constructor(e) {
203
+ this.#e = e, this.view = Object.freeze({
204
+ get: () => this.#u(),
205
+ subscribe: (e) => this.#f(e)
206
+ });
207
+ }
208
+ get version() {
209
+ return this.#c;
210
+ }
211
+ refresh() {
212
+ if (this.#a !== void 0) throw S && this.#l(this.#a, S), TypeError("Circular computed signal");
213
+ this.#a = C.length, C.push(this);
214
+ try {
215
+ this.#g();
216
+ } finally {
217
+ C.pop(), this.#a = void 0;
218
+ }
219
+ }
220
+ #l(t, n) {
221
+ let r = /* @__PURE__ */ new Set();
222
+ for (let e of C.slice(t)) for (let t of e.#o ?? e.#n.keys()) r.add(t);
223
+ for (let t of r) if (t instanceof e) for (let e of t.#n.keys()) r.add(e);
224
+ else n.add(t);
225
+ }
226
+ #u() {
227
+ return this.refresh(), S?.add(this), this.#d();
228
+ }
229
+ #d() {
230
+ let e = this.#r;
231
+ if (e.phase === "error") throw e.error;
232
+ return e.value;
233
+ }
234
+ subscribeInvalidation(e) {
235
+ return this.#f(e, "dependency");
236
+ }
237
+ #f(e, t = "observer") {
238
+ h(e);
239
+ let n = {
240
+ kind: t,
241
+ listener: e
242
+ }, r = this.#t.size === 0;
243
+ if (this.#t.add(n), r || t === "observer") try {
244
+ this.refresh(), t === "observer" && this.#d(), r && this.#h();
245
+ } catch (e) {
246
+ throw this.#t.delete(n), this.#t.size || this.#m(), e;
247
+ }
248
+ return new w(() => {
249
+ n.listener = void 0, this.#t.delete(n), this.#t.size || this.#m();
250
+ });
251
+ }
252
+ #p = () => {
253
+ this.#s !== g && (this.#s = g, E(this.#t));
254
+ };
255
+ #m() {
256
+ for (let e of this.#n.values()) e.subscription?.[r](), e.subscription = void 0;
257
+ }
258
+ #h() {
259
+ for (let [e, t] of this.#n) t.subscription ??= e.subscribeInvalidation(this.#p);
260
+ }
261
+ #g() {
262
+ let e = this.#r;
263
+ if (e && this.#s <= this.#i && (this.#i === g || !this.#_())) {
264
+ this.#i = g;
265
+ return;
266
+ }
267
+ let t = /* @__PURE__ */ new Set(), n = this.#v(t);
268
+ this.#y(t), (!e || (n.phase === "value" ? e.phase !== "value" || !Object.is(e.value, n.value) : e.phase !== "error" || !Object.is(e.error, n.error))) && this.#c++, this.#r = n, this.#i = g, this.#t.size && this.#h();
269
+ }
270
+ #_() {
271
+ for (let [t, n] of this.#n) if (t instanceof e && t.#a !== void 0 || (t.refresh(), n.version !== t.version)) return !0;
272
+ return this.#s > this.#i;
273
+ }
274
+ #v(e) {
275
+ let t = S;
276
+ this.#o = e, S = e;
277
+ try {
278
+ let e = this.#e, t = e();
279
+ return c(t, "Computed signal calculations must be synchronous"), {
280
+ phase: "value",
281
+ value: t
282
+ };
283
+ } catch (e) {
284
+ return {
285
+ phase: "error",
286
+ error: e
287
+ };
288
+ } finally {
289
+ S = t, this.#o = void 0;
290
+ }
291
+ }
292
+ #y(e) {
293
+ for (let [t, n] of this.#n) e.has(t) || (n.subscription?.[r](), this.#n.delete(t));
294
+ for (let t of e) {
295
+ let e = this.#n.get(t);
296
+ e ? e.version = t.version : this.#n.set(t, {
297
+ version: t.version,
298
+ subscription: void 0
299
+ });
300
+ }
301
+ }
302
+ }, P = class {
303
+ #e;
304
+ #t;
305
+ #n;
306
+ #r;
307
+ view;
308
+ constructor(e) {
309
+ this.#e = A({
310
+ phase: "value",
311
+ source: e,
312
+ value: e.get()
313
+ }), this.view = Object.freeze({
314
+ get: () => {
315
+ let e = this.#e?.get();
316
+ if (!e) throw Error("Readable bridge has been disposed");
317
+ if (e.phase === "error") throw e.error;
318
+ return e.value;
319
+ },
320
+ subscribe: (e) => this.#o(e)
321
+ });
322
+ }
323
+ start(e) {
324
+ let t = !0, n;
325
+ try {
326
+ n = p(e.defer(() => (this[r](), t ? Promise.resolve().then(() => this.#i()) : void 0)), "BridgeOwner.defer() must return an AsyncDisposable");
327
+ let i = this.#e?.get();
328
+ if (!i) throw Error("Readable bridge has been disposed");
329
+ let a = f(i.source.subscribe(() => this.#a()));
330
+ if (this.#t = a, !this.#e?.get()) {
331
+ try {
332
+ a(), this.#t = void 0;
333
+ } catch (e) {
334
+ this.#r = { value: e };
335
+ }
336
+ throw Error("Readable bridge has been disposed");
337
+ }
338
+ this.#a(), this.view.get();
339
+ } catch (e) {
340
+ let t = n;
341
+ throw t && Promise.resolve().then(() => s(t())).catch(() => void 0), e;
342
+ } finally {
343
+ t = !1;
344
+ }
345
+ }
346
+ #i() {
347
+ if (this.#r) throw this.#r.value;
348
+ }
349
+ #a() {
350
+ let e = this.#e?.get();
351
+ if (!e) return;
352
+ let t = Symbol();
353
+ this.#n = t;
354
+ try {
355
+ let { source: n } = e, r;
356
+ try {
357
+ r = {
358
+ phase: "value",
359
+ source: n,
360
+ value: n.get()
361
+ };
362
+ } catch (e) {
363
+ r = {
364
+ phase: "error",
365
+ source: n,
366
+ error: e
367
+ };
368
+ }
369
+ if (this.#n !== t || this.#e?.get() !== e || e.phase === "value" && r.phase === "value" && Object.is(e.value, r.value) || e.phase === "error" && r.phase === "error" && Object.is(e.error, r.error)) return;
370
+ this.#e?.set(r);
371
+ } finally {
372
+ this.#n = void 0;
373
+ }
374
+ }
375
+ #o(e) {
376
+ let t = this.#e;
377
+ if (!t?.get()) throw Error("Readable bridge has been disposed");
378
+ h(e);
379
+ let n = t.subscribe(() => (this.#e?.get() || n[r](), e()));
380
+ return n;
381
+ }
382
+ [r]() {
383
+ let e = this.#e;
384
+ if (!e?.get()) return this.#i();
385
+ let t = [];
386
+ try {
387
+ e.set(void 0);
388
+ } catch (n) {
389
+ if (e.get()) throw n;
390
+ t.push(n);
391
+ }
392
+ this.#e = void 0;
393
+ try {
394
+ this.#t?.(), this.#t = void 0;
395
+ } catch (e) {
396
+ t.push(e);
397
+ }
398
+ if (t.length) {
399
+ let e = t.length === 1 ? t[0] : AggregateError(t, "Readable bridge cleanup failed");
400
+ throw this.#r = { value: e }, e;
401
+ }
402
+ }
403
+ };
404
+ function F(e, t) {
405
+ let n = e?.defer;
406
+ if (typeof n != "function") throw TypeError("bridge() expects a resource owner");
407
+ let i = d(t, "bridge()"), a = { defer: (t) => Reflect.apply(n, e, [t]) }, o = new P(i);
408
+ try {
409
+ return o.start(a), o.view;
410
+ } catch (e) {
411
+ try {
412
+ o[r]();
413
+ } catch (t) {
414
+ throw AggregateError([e, t], "Readable bridge construction failed");
415
+ }
416
+ throw e;
417
+ }
383
418
  }
384
- function A(s) {
385
- const e = s && typeof s.dispose == "function" && typeof s[y] == "function" ? m(s.result) : void 0;
386
- if (!e)
387
- throw new TypeError("ObservationOwner.spawn() must return an ObservationTask");
388
- return e;
419
+ // src/abort.ts
420
+ function I(e) {
421
+ return Reflect.get(AbortSignal.prototype, "aborted", e);
389
422
  }
390
- function v(s) {
391
- a(s.dispose(), "Readable subscriptions must dispose synchronously");
423
+ function L(e) {
424
+ return Reflect.get(AbortSignal.prototype, "reason", e);
392
425
  }
393
- async function h(s, e) {
394
- if (s)
395
- try {
396
- await s.dispose();
397
- } catch (t) {
398
- e.push(t);
399
- }
426
+ function R(t, n) {
427
+ if (typeof n != "function") throw TypeError("Abort listener must be a function");
428
+ if (I(t)) return n(L(t)), () => {};
429
+ let r = {
430
+ signal: t,
431
+ callback: n
432
+ }, i;
433
+ function a() {
434
+ r = void 0, i?.(), i = void 0;
435
+ }
436
+ function o() {
437
+ if (!r || !I(r.signal)) return;
438
+ let { signal: e, callback: t } = r, n = L(e);
439
+ a(), t(n);
440
+ }
441
+ return i = e(t, o), r ? o() : a(), a;
400
442
  }
401
- export {
402
- I as batch,
403
- q as computed,
404
- B as observe,
405
- L as signal
443
+ // src/observe.ts
444
+ var z = class {
445
+ #e;
446
+ #t;
447
+ #n;
448
+ #r;
449
+ #i = !1;
450
+ #a = { present: !1 };
451
+ #o = !1;
452
+ #s;
453
+ #c;
454
+ #l;
455
+ #u;
456
+ #d;
457
+ constructor(e, t, n) {
458
+ this.#e = {
459
+ phase: "active",
460
+ binding: {
461
+ owner: e,
462
+ source: t,
463
+ observer: n
464
+ }
465
+ };
466
+ }
467
+ start() {
468
+ this.#i = !0;
469
+ try {
470
+ this.#f();
471
+ } finally {
472
+ this.#i = !1;
473
+ }
474
+ }
475
+ #f() {
476
+ let { source: e } = this.#w();
477
+ this.#t = f(e.subscribe(() => this.#h())), this.#v();
478
+ let { owner: t } = this.#w(), n = t.spawn((e) => this.#g(e)), r = p(n, "ObservationOwner.spawn() must return an ObservationTask");
479
+ this.#s = r;
480
+ let i = l(n.result);
481
+ if (!i) throw TypeError("ObservationOwner.spawn() must return an ObservationTask");
482
+ i.then(() => this.#S(r), () => this.#S(r));
483
+ }
484
+ attachOwnerCleanup(e) {
485
+ this.#d || (this.#c = e);
486
+ }
487
+ handle = Object.freeze({ [i]: () => this.#p() });
488
+ #p() {
489
+ if (this.#l) return this.#l;
490
+ let e = this.#c;
491
+ if (!e) return this.dispose();
492
+ let t = Promise.withResolvers();
493
+ this.#l = t.promise;
494
+ try {
495
+ s(e()).then(() => t.resolve(), t.reject);
496
+ } catch (e) {
497
+ t.reject(e);
498
+ }
499
+ return t.promise;
500
+ }
501
+ dispose() {
502
+ if (this.#d) return this.#d;
503
+ let e = Promise.withResolvers();
504
+ return this.#d = e.promise, this.#c = void 0, this.#T("disposed"), this.#u?.(), (this.#i ? Promise.resolve().then(() => this.#m()) : this.#m()).then(e.resolve, e.reject), e.promise;
505
+ }
506
+ async #m() {
507
+ let e = [];
508
+ try {
509
+ this.#t?.(), this.#t = void 0;
510
+ } catch (t) {
511
+ e.push(t);
512
+ }
513
+ await V(this.#x(), e);
514
+ try {
515
+ await this.#b();
516
+ } catch (t) {
517
+ e.push(t);
518
+ }
519
+ if (e.length === 1) throw e[0];
520
+ if (e.length > 1) throw AggregateError(e, "Observation cleanup failed");
521
+ }
522
+ #h() {
523
+ this.#e.phase === "active" && (this.#o = !0, this.#u?.());
524
+ }
525
+ async #g(e) {
526
+ let t = R(e, () => this.#u?.());
527
+ try {
528
+ for (; this.#e.phase === "active" && !I(e);) {
529
+ if (!this.#o) {
530
+ let e = Promise.withResolvers();
531
+ this.#u = e.resolve, await e.promise, this.#u = void 0;
532
+ }
533
+ for (; this.#e.phase === "active" && !I(e) && this.#o;) this.#o = !1, await this.#_(e);
534
+ }
535
+ } catch (e) {
536
+ if (this.#e.phase !== "active") throw e;
537
+ try {
538
+ await this.#C();
539
+ } catch (t) {
540
+ throw AggregateError([e, t], "Observation stopped after a replacement failed");
541
+ }
542
+ throw e;
543
+ } finally {
544
+ t();
545
+ }
546
+ }
547
+ async #_(e) {
548
+ if (this.#e.phase !== "active" || I(e)) return;
549
+ let t = this.#e.binding.source.get();
550
+ if (!(this.#a.present && Object.is(t, this.#a.value))) {
551
+ try {
552
+ await this.#b();
553
+ } catch (e) {
554
+ throw this.#e.phase === "active" && await this.#C(), e;
555
+ }
556
+ this.#a = { present: !1 }, !(this.#e.phase !== "active" || I(e)) && this.#v(e);
557
+ }
558
+ }
559
+ #v(e) {
560
+ let { owner: t } = this.#w(), n = t.child("observation");
561
+ if (this.#n = p(n, "ObservationOwner.child() must return an AsyncDisposable"), e && I(e)) return;
562
+ let r = this.#w().source.get();
563
+ e && I(e) || (this.#y(r, n), this.#e.phase === "active" && (this.#a = {
564
+ present: !0,
565
+ value: r
566
+ }));
567
+ }
568
+ #y(e, t) {
569
+ let { observer: n } = this.#w();
570
+ c(n(e, t), "Observers must be synchronous; use owner.spawn() for async work");
571
+ }
572
+ #b() {
573
+ let e = this.#n;
574
+ if (e) return this.#r ??= Promise.resolve().then(() => s(e())).then(() => {
575
+ this.#n = void 0, this.#r = void 0;
576
+ });
577
+ }
578
+ #x() {
579
+ let e = this.#s;
580
+ return this.#s = void 0, e;
581
+ }
582
+ #S(e) {
583
+ this.#s === e && (this.#s = void 0);
584
+ }
585
+ async #C() {
586
+ this.#T("stopped"), this.#s = void 0, await this.#p();
587
+ }
588
+ #w() {
589
+ let e = this.#e;
590
+ if (e.phase !== "active") throw Error("Observation is not active");
591
+ return e.binding;
592
+ }
593
+ #T(e) {
594
+ this.#e = { phase: e }, this.#a = { present: !1 }, this.#o = !1;
595
+ }
406
596
  };
597
+ function B(e, t, n) {
598
+ let r = d(t, "observe()");
599
+ if (typeof n != "function") throw TypeError("Observer must be a function");
600
+ let i = e?.defer, a = e?.child, o = e?.spawn;
601
+ if (typeof i != "function" || typeof a != "function" || typeof o != "function") throw TypeError("observe() expects an observation owner");
602
+ let c = {
603
+ defer: (t) => Reflect.apply(i, e, [t]),
604
+ child: (t) => Reflect.apply(a, e, [t]),
605
+ spawn: (t) => Reflect.apply(o, e, [t])
606
+ }, l = new z(c, r, n), u = p(c.defer(() => l.dispose()), "ObservationOwner.defer() must return an AsyncDisposable");
607
+ l.attachOwnerCleanup(u);
608
+ try {
609
+ return l.start(), l.handle;
610
+ } catch (e) {
611
+ try {
612
+ s(u()).catch(() => void 0);
613
+ } catch (t) {
614
+ throw AggregateError([e, t], "Observation construction failed");
615
+ }
616
+ throw e;
617
+ }
618
+ }
619
+ async function V(e, t) {
620
+ if (e) try {
621
+ await s(e());
622
+ } catch (e) {
623
+ t.push(e);
624
+ }
625
+ }
626
+ export { i as asyncDisposeSymbol, k as batch, F as bridge, j as computed, r as disposeSymbol, B as observe, A as signal };