@vielzeug/ripple 26.10.14 → 26.10.16

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/ripple.js CHANGED
@@ -1,2 +1,484 @@
1
- var RippleError=class extends Error{constructor(e,t){super(e,t),this.name=new.target.name,Object.setPrototypeOf(this,new.target.prototype)}},RippleCycleError=class extends RippleError{},RippleDisposedError=class extends RippleError{},e=Symbol.for(`@vielzeug/ripple/reactive`),t=100,n=Symbol(`ripple.unset`),ReactiveNode=class{[e]=!0;dependents=new Set;name;runtime;constructor(e,t){this.runtime=e,this.name=t}subscribe(e){this.runtime.assertActive(),this.peek();let t={listener:e,onDependencyChanged:()=>this.runtime.enqueueListener(t)};return this.dependents.add(t),()=>this.dependents.delete(t)}notify(){for(let e of[...this.dependents])e.onDependencyChanged()}},SignalNode=class extends ReactiveNode{current;equals;constructor(e,t,n){super(e,n?.name),this.current=t,this.equals=n?.equals??Object.is}get value(){return this.runtime.track(this),this.current}set value(e){this.runtime.assertActive(),!this.equals(this.current,e)&&(this.current=e,this.runtime.propagate(()=>this.notify()))}peek(){return this.current}update(e){this.value=e(this.current)}},ComputedNode=class extends ReactiveNode{dependencies=new Set;computing=!1;disposed=!1;dirty=!0;current=n;derive;equals;owner;constructor(e,t,n){super(e,n?.name),this.derive=t,this.equals=n?.equals??Object.is}get value(){return this.runtime.track(this),this.refresh(),this.evaluated()}peek(){return this.refresh(),this.evaluated()}evaluated(){if(this.current===n)throw new RippleDisposedError(`computed${this.name===void 0?``:` "${this.name}"`} was disposed before it was ever evaluated`);return this.current}onDependencyChanged(){if(!this.disposed&&(this.dirty||=!0,this.dependents.size>0))try{this.refresh()&&this.notify()}catch(e){this.runtime.report(e,{kind:`computed`,name:this.name})}}setOwner(e){this.owner=e}dispose(){this.disposed||(this.disposed=!0,this.owner?.release(this),this.owner=void 0,this.runtime.clearDependencies(this),this.dependents.clear())}refresh(){if(!this.dirty||this.disposed)return!1;if(this.computing)throw new RippleCycleError(`computed cycle detected${this.name===void 0?``:` "${this.name}"`}`);this.computing=!0;try{let e=this.runtime.collect(this,this.derive),t=this.current===n||!this.equals(this.current,e);return this.current=e,this.dirty=!1,t}finally{this.computing=!1}}},ScopeNode=class{disposalController=new AbortController;owned=new Set;name;isDisposed=!1;owner;runtime;constructor(e,t){this.runtime=e,this.name=t}get disposed(){return this.isDisposed}get disposalSignal(){return this.disposalController.signal}own(e){e.setOwner(this),this.owned.add(e)}release(e){this.owned.delete(e)}setOwner(e){this.owner=e}run(e){if(this.isDisposed)throw new RippleDisposedError(`Cannot run a disposed scope.`);return this.runtime.withScope(this,e)}dispose(){if(!this.isDisposed){this.isDisposed=!0,this.owner?.release(this),this.owner=void 0;for(let e of[...this.owned].reverse())e.dispose();this.owned.clear(),this.disposalController.abort()}}[Symbol.dispose](){this.dispose()}},EffectNode=class{dependencies=new Set;disposalController=new AbortController;cleanup;isDisposed=!1;ownershipScope;runScope;callback;options;runtime;constructor(e,t,n){this.runtime=e,this.callback=t,this.options=n}get disposed(){return this.isDisposed}get disposalSignal(){return this.disposalController.signal}setOwner(e){this.ownershipScope=e}onDependencyChanged(){this.isDisposed||this.runtime.enqueue(this)}run(){if(this.isDisposed)return;this.runScope?.dispose(),this.runScope=void 0,this.runCleanup();let e=new ScopeNode(this.runtime);try{let t=this.runtime.withEffectScope(e,()=>this.runtime.collectEffect(this,this.callback));this.runScope=e,this.cleanup=typeof t==`function`?t:void 0,this.isDisposed&&(this.runScope.dispose(),this.runScope=void 0,this.runCleanup())}catch(t){e.dispose(),this.runtime.report(t,{kind:`effect`,name:this.options?.name})}}dispose(){this.isDisposed||(this.isDisposed=!0,this.ownershipScope?.release(this),this.ownershipScope=void 0,this.runScope?.dispose(),this.runScope=void 0,this.runtime.clearDependencies(this),this.runCleanup(),this.disposalController.abort())}[Symbol.dispose](){this.dispose()}runCleanup(){let e=this.cleanup;if(this.cleanup=void 0,e!==void 0)try{e()}catch(e){this.runtime.report(e,{kind:`cleanup`,name:this.options?.name})}}},ReactiveRuntime=class{activeEffectScope;activeObserver;activeScope;isDisposed=!1;flushDepth=0;flushing=!1;pending=new Set;listeners=new Set;rootScope;onError;constructor(e){this.onError=e?.onError??(e=>{queueMicrotask(()=>{throw e})}),this.rootScope=new ScopeNode(this,`runtime`),this.activeScope=this.rootScope}get disposed(){return this.isDisposed}get disposalSignal(){return this.rootScope.disposalSignal}signal=(e,t)=>(this.assertActive(),new SignalNode(this,e,t));computed=(e,t)=>{this.assertActive();let n=new ComputedNode(this,e,t);return(this.activeEffectScope??this.activeScope).own(n),n};effect=(e,t)=>{this.assertActive();let n=new EffectNode(this,e,t);return(this.activeEffectScope??this.activeScope).own(n),n.run(),n};createScope=e=>{this.assertActive();let t=new ScopeNode(this,e);return this.activeScope.own(t),t};batch=e=>(this.assertActive(),this.propagate(e));untrack=e=>(this.assertActive(),this.withObserver(void 0,e));dispose(){this.isDisposed||(this.isDisposed=!0,this.rootScope.dispose())}assertActive(){if(this.isDisposed)throw new RippleDisposedError(`Cannot use a disposed Ripple runtime.`)}track(e){let t=this.activeObserver;t?.collecting!==void 0&&t.collecting.add(e)}clearDependencies(e){for(let t of e.dependencies)t.dependents.delete(e);e.dependencies.clear()}collect(e,t){return this.collectWith(e,t,e.dependencies.size===0)}collectEffect(e,t){return this.collectWith(e,t,!0)}withEffectScope(e,t){let n=this.activeEffectScope;this.activeEffectScope=e;try{return t()}finally{this.activeEffectScope=n}}withObserver(e,t){let n=this.activeObserver;this.activeObserver=e;try{return t()}finally{this.activeObserver=n}}withScope(e,t){let n=this.activeEffectScope,r=this.activeScope;this.activeEffectScope=void 0,this.activeScope=e;try{return t()}finally{this.activeEffectScope=n,this.activeScope=r}}enqueue(e){this.pending.add(e),this.flushDepth===0&&this.flush()}enqueueListener(e){this.listeners.add(e),this.flushDepth===0&&this.flush()}propagate(e){this.flushDepth++;try{return e()}finally{this.flushDepth--,this.flushDepth===0&&this.flush()}}report(e,t){try{this.onError(e,t)}catch{}}collectWith(e,t,n){let r=this.activeObserver,i=new Set;e.collecting=i,this.activeObserver=e;try{let n=t();return this.commitDependencies(e,i),n}catch(t){throw n&&this.commitDependencies(e,i),t}finally{e.collecting=void 0,this.activeObserver=r}}commitDependencies(e,t){for(let n of e.dependencies)t.has(n)||n.dependents.delete(e);for(let n of t)e.dependencies.has(n)||n.dependents.add(e);e.dependencies.clear();for(let n of t)e.dependencies.add(n)}flush(){if(this.flushing)return;this.flushing=!0;let e=0;try{for(;this.pending.size>0||this.listeners.size>0;){if(++e>t){this.report(new RippleCycleError(`infinite reactive flush (>${t} iterations)`),{kind:`flush`}),this.pending.clear(),this.listeners.clear();break}let n=[...this.pending],r=[...this.listeners];this.pending.clear(),this.listeners.clear();for(let e of n)e.run();for(let e of r)try{e.listener()}catch(e){this.report(e,{kind:`listener`})}}}finally{this.flushing=!1}}},isReactive=t=>{if(typeof t!=`object`||!t||t[e]!==!0)return!1;let n=t;return`value`in n&&typeof n.peek==`function`&&typeof n.subscribe==`function`},createResource=t=>(n,r,i)=>{let a=t.signal({status:`pending`},{name:i?.name}),o=t.signal(0),s,c=!1,l,run=()=>{o.value,s?.abort();let e;try{e=n()}catch(e){a.value=c?{error:e,previous:l,status:`error`}:{error:e,status:`error`};return}let t=new AbortController;s=t,a.value=c?{previous:l,status:`pending`}:{status:`pending`};let i;try{i=Promise.resolve(r(e,{signal:t.signal}))}catch(e){i=Promise.reject(e)}i.then(e=>{!u.disposed&&!t.signal.aborted&&(c=!0,l=e,a.value={status:`success`,value:e})},e=>{!u.disposed&&!t.signal.aborted&&(a.value=c?{error:e,previous:l,status:`error`}:{error:e,status:`error`})})},u=t.effect(()=>(run(),()=>s?.abort()),{name:i?.name}),assertActive=()=>{if(u.disposed)throw new RippleDisposedError(`Cannot use a disposed resource.`)},d={get disposalSignal(){return u.disposalSignal},dispose:()=>u.dispose(),get disposed(){return u.disposed},get name(){return a.name},peek:()=>a.peek(),reload:()=>{assertActive(),o.value=o.peek()+1},subscribe:e=>(assertActive(),a.subscribe(e)),[Symbol.dispose](){this.dispose()},get value(){return a.value}};return Object.defineProperty(d,e,{value:!0}),d},createFromSubscribable=t=>(n,r)=>{let i=t.signal(n.getSnapshot(),{name:r?.name}),a=t.effect(()=>{let sync=()=>{i.value=n.getSnapshot()},e;try{e=n.subscribe(sync),sync()}catch(t){throw e?.(),t}return e},{name:r?.name}),abort=()=>a.dispose();r?.signal?.addEventListener(`abort`,abort,{once:!0}),r?.signal?.aborted&&a.dispose();let assertActive=()=>{if(a.disposed)throw new RippleError(`Cannot subscribe to a disposed bridge.`)},dispose=()=>{r?.signal?.removeEventListener(`abort`,abort),a.dispose()},o={get disposalSignal(){return a.disposalSignal},dispose,get disposed(){return a.disposed},get name(){return i.name},peek:()=>i.peek(),subscribe(e){return assertActive(),i.subscribe(e)},[Symbol.dispose]:dispose,get value(){return i.value}};return Object.defineProperty(o,e,{value:!0}),o},createWatch=e=>(t,n,r)=>{let i=typeof t==`function`?t:()=>t.value,a=r?.equals??Object.is,o=!0,s;return e.effect(()=>{let t=i();if(o){o=!1,s=t,r?.immediate&&e.untrack(()=>n(t,void 0));return}if(a(s,t))return;let c=s;s=t,e.untrack(()=>n(t,c))},{name:r?.name})},createRipple=e=>{let t=new ReactiveRuntime(e),n=createFromSubscribable(t),r=createResource(t);return{batch:t.batch,computed:t.computed,createScope:t.createScope,get disposalSignal(){return t.disposalSignal},dispose:()=>t.dispose(),get disposed(){return t.disposed},effect:t.effect,fromSubscribable:n,resource:r,signal:t.signal,untrack:t.untrack,watch:createWatch(t),[Symbol.dispose]:()=>t.dispose()}},r=createRipple(),i=r.signal,a=r.computed,o=r.effect,s=r.batch,c=r.createScope,l=r.untrack,u=r.watch,d=r.resource,f=r.fromSubscribable;export{RippleCycleError,RippleDisposedError,RippleError,s as batch,a as computed,createRipple,c as createScope,o as effect,f as fromSubscribable,isReactive,d as resource,i as signal,l as untrack,u as watch};
1
+ //#region src/errors.ts
2
+ var RippleError = class extends Error {
3
+ constructor(e, t) {
4
+ super(e, t), this.name = new.target.name, Object.setPrototypeOf(this, new.target.prototype);
5
+ }
6
+ }, RippleCycleError = class extends RippleError {}, RippleDisposedError = class extends RippleError {}, e = Symbol.for("@vielzeug/ripple/reactive"), t = 100, n = Symbol("ripple.unset"), ReactiveNode = class {
7
+ [e] = !0;
8
+ dependents = /* @__PURE__ */ new Set();
9
+ name;
10
+ runtime;
11
+ constructor(e, t) {
12
+ this.runtime = e, this.name = t;
13
+ }
14
+ subscribe(e) {
15
+ this.runtime.assertActive(), this.peek();
16
+ let t = {
17
+ listener: e,
18
+ onDependencyChanged: () => this.runtime.enqueueListener(t)
19
+ };
20
+ return this.dependents.add(t), () => this.dependents.delete(t);
21
+ }
22
+ notify() {
23
+ for (let e of [...this.dependents]) e.onDependencyChanged();
24
+ }
25
+ }, SignalNode = class extends ReactiveNode {
26
+ current;
27
+ equals;
28
+ constructor(e, t, n) {
29
+ super(e, n?.name), this.current = t, this.equals = n?.equals ?? Object.is;
30
+ }
31
+ get value() {
32
+ return this.runtime.track(this), this.current;
33
+ }
34
+ set value(e) {
35
+ this.runtime.assertActive(), !this.equals(this.current, e) && (this.current = e, this.runtime.propagate(() => this.notify()));
36
+ }
37
+ peek() {
38
+ return this.current;
39
+ }
40
+ update(e) {
41
+ this.value = e(this.current);
42
+ }
43
+ }, ComputedNode = class extends ReactiveNode {
44
+ dependencies = /* @__PURE__ */ new Set();
45
+ computing = !1;
46
+ disposed = !1;
47
+ dirty = !0;
48
+ current = n;
49
+ derive;
50
+ equals;
51
+ owner;
52
+ constructor(e, t, n) {
53
+ super(e, n?.name), this.derive = t, this.equals = n?.equals ?? Object.is;
54
+ }
55
+ get value() {
56
+ return this.runtime.track(this), this.refresh(), this.evaluated();
57
+ }
58
+ peek() {
59
+ return this.refresh(), this.evaluated();
60
+ }
61
+ evaluated() {
62
+ if (this.current === n) throw new RippleDisposedError(`computed${this.name === void 0 ? "" : ` "${this.name}"`} was disposed before it was ever evaluated`);
63
+ return this.current;
64
+ }
65
+ onDependencyChanged() {
66
+ if (!this.disposed && (this.dirty ||= !0, this.dependents.size > 0)) try {
67
+ this.refresh() && this.notify();
68
+ } catch (e) {
69
+ this.runtime.report(e, {
70
+ kind: "computed",
71
+ name: this.name
72
+ });
73
+ }
74
+ }
75
+ setOwner(e) {
76
+ this.owner = e;
77
+ }
78
+ dispose() {
79
+ this.disposed || (this.disposed = !0, this.owner?.release(this), this.owner = void 0, this.runtime.clearDependencies(this), this.dependents.clear());
80
+ }
81
+ refresh() {
82
+ if (!this.dirty || this.disposed) return !1;
83
+ if (this.computing) throw new RippleCycleError(`computed cycle detected${this.name === void 0 ? "" : ` "${this.name}"`}`);
84
+ this.computing = !0;
85
+ try {
86
+ let e = this.runtime.collect(this, this.derive), t = this.current === n || !this.equals(this.current, e);
87
+ return this.current = e, this.dirty = !1, t;
88
+ } finally {
89
+ this.computing = !1;
90
+ }
91
+ }
92
+ }, ScopeNode = class {
93
+ disposalController = new AbortController();
94
+ owned = /* @__PURE__ */ new Set();
95
+ name;
96
+ isDisposed = !1;
97
+ owner;
98
+ runtime;
99
+ constructor(e, t) {
100
+ this.runtime = e, this.name = t;
101
+ }
102
+ get disposed() {
103
+ return this.isDisposed;
104
+ }
105
+ get disposalSignal() {
106
+ return this.disposalController.signal;
107
+ }
108
+ own(e) {
109
+ e.setOwner(this), this.owned.add(e);
110
+ }
111
+ release(e) {
112
+ this.owned.delete(e);
113
+ }
114
+ setOwner(e) {
115
+ this.owner = e;
116
+ }
117
+ run(e) {
118
+ if (this.isDisposed) throw new RippleDisposedError("Cannot run a disposed scope.");
119
+ return this.runtime.withScope(this, e);
120
+ }
121
+ dispose() {
122
+ if (!this.isDisposed) {
123
+ this.isDisposed = !0, this.owner?.release(this), this.owner = void 0;
124
+ for (let e of [...this.owned].reverse()) e.dispose();
125
+ this.owned.clear(), this.disposalController.abort();
126
+ }
127
+ }
128
+ [Symbol.dispose]() {
129
+ this.dispose();
130
+ }
131
+ }, EffectNode = class {
132
+ dependencies = /* @__PURE__ */ new Set();
133
+ disposalController = new AbortController();
134
+ cleanup;
135
+ isDisposed = !1;
136
+ ownershipScope;
137
+ runScope;
138
+ callback;
139
+ options;
140
+ runtime;
141
+ constructor(e, t, n) {
142
+ this.runtime = e, this.callback = t, this.options = n;
143
+ }
144
+ get disposed() {
145
+ return this.isDisposed;
146
+ }
147
+ get disposalSignal() {
148
+ return this.disposalController.signal;
149
+ }
150
+ setOwner(e) {
151
+ this.ownershipScope = e;
152
+ }
153
+ onDependencyChanged() {
154
+ this.isDisposed || this.runtime.enqueue(this);
155
+ }
156
+ run() {
157
+ if (this.isDisposed) return;
158
+ this.runScope?.dispose(), this.runScope = void 0, this.runCleanup();
159
+ let e = new ScopeNode(this.runtime);
160
+ try {
161
+ let t = this.runtime.withEffectScope(e, () => this.runtime.collectEffect(this, this.callback));
162
+ this.runScope = e, this.cleanup = typeof t == "function" ? t : void 0, this.isDisposed && (this.runScope.dispose(), this.runScope = void 0, this.runCleanup());
163
+ } catch (t) {
164
+ e.dispose(), this.runtime.report(t, {
165
+ kind: "effect",
166
+ name: this.options?.name
167
+ });
168
+ }
169
+ }
170
+ dispose() {
171
+ this.isDisposed || (this.isDisposed = !0, this.ownershipScope?.release(this), this.ownershipScope = void 0, this.runScope?.dispose(), this.runScope = void 0, this.runtime.clearDependencies(this), this.runCleanup(), this.disposalController.abort());
172
+ }
173
+ [Symbol.dispose]() {
174
+ this.dispose();
175
+ }
176
+ runCleanup() {
177
+ let e = this.cleanup;
178
+ if (this.cleanup = void 0, e !== void 0) try {
179
+ e();
180
+ } catch (e) {
181
+ this.runtime.report(e, {
182
+ kind: "cleanup",
183
+ name: this.options?.name
184
+ });
185
+ }
186
+ }
187
+ }, ReactiveRuntime = class {
188
+ activeEffectScope;
189
+ activeObserver;
190
+ activeScope;
191
+ isDisposed = !1;
192
+ flushDepth = 0;
193
+ flushing = !1;
194
+ pending = /* @__PURE__ */ new Set();
195
+ listeners = /* @__PURE__ */ new Set();
196
+ rootScope;
197
+ onError;
198
+ constructor(e) {
199
+ this.onError = e?.onError ?? ((e) => {
200
+ queueMicrotask(() => {
201
+ throw e;
202
+ });
203
+ }), this.rootScope = new ScopeNode(this, "runtime"), this.activeScope = this.rootScope;
204
+ }
205
+ get disposed() {
206
+ return this.isDisposed;
207
+ }
208
+ get disposalSignal() {
209
+ return this.rootScope.disposalSignal;
210
+ }
211
+ signal = (e, t) => (this.assertActive(), new SignalNode(this, e, t));
212
+ computed = (e, t) => {
213
+ this.assertActive();
214
+ let n = new ComputedNode(this, e, t);
215
+ return (this.activeEffectScope ?? this.activeScope).own(n), n;
216
+ };
217
+ effect = (e, t) => {
218
+ this.assertActive();
219
+ let n = new EffectNode(this, e, t);
220
+ return (this.activeEffectScope ?? this.activeScope).own(n), n.run(), n;
221
+ };
222
+ createScope = (e) => {
223
+ this.assertActive();
224
+ let t = new ScopeNode(this, e);
225
+ return this.activeScope.own(t), t;
226
+ };
227
+ batch = (e) => (this.assertActive(), this.propagate(e));
228
+ untrack = (e) => (this.assertActive(), this.withObserver(void 0, e));
229
+ dispose() {
230
+ this.isDisposed || (this.isDisposed = !0, this.rootScope.dispose());
231
+ }
232
+ assertActive() {
233
+ if (this.isDisposed) throw new RippleDisposedError("Cannot use a disposed Ripple runtime.");
234
+ }
235
+ track(e) {
236
+ let t = this.activeObserver;
237
+ t?.collecting !== void 0 && t.collecting.add(e);
238
+ }
239
+ clearDependencies(e) {
240
+ for (let t of e.dependencies) t.dependents.delete(e);
241
+ e.dependencies.clear();
242
+ }
243
+ collect(e, t) {
244
+ return this.collectWith(e, t, e.dependencies.size === 0);
245
+ }
246
+ collectEffect(e, t) {
247
+ return this.collectWith(e, t, !0);
248
+ }
249
+ withEffectScope(e, t) {
250
+ let n = this.activeEffectScope;
251
+ this.activeEffectScope = e;
252
+ try {
253
+ return t();
254
+ } finally {
255
+ this.activeEffectScope = n;
256
+ }
257
+ }
258
+ withObserver(e, t) {
259
+ let n = this.activeObserver;
260
+ this.activeObserver = e;
261
+ try {
262
+ return t();
263
+ } finally {
264
+ this.activeObserver = n;
265
+ }
266
+ }
267
+ withScope(e, t) {
268
+ let n = this.activeEffectScope, r = this.activeScope;
269
+ this.activeEffectScope = void 0, this.activeScope = e;
270
+ try {
271
+ return t();
272
+ } finally {
273
+ this.activeEffectScope = n, this.activeScope = r;
274
+ }
275
+ }
276
+ enqueue(e) {
277
+ this.pending.add(e), this.flushDepth === 0 && this.flush();
278
+ }
279
+ enqueueListener(e) {
280
+ this.listeners.add(e), this.flushDepth === 0 && this.flush();
281
+ }
282
+ propagate(e) {
283
+ this.flushDepth++;
284
+ try {
285
+ return e();
286
+ } finally {
287
+ this.flushDepth--, this.flushDepth === 0 && this.flush();
288
+ }
289
+ }
290
+ report(e, t) {
291
+ try {
292
+ this.onError(e, t);
293
+ } catch {}
294
+ }
295
+ collectWith(e, t, n) {
296
+ let r = this.activeObserver, i = /* @__PURE__ */ new Set();
297
+ e.collecting = i, this.activeObserver = e;
298
+ try {
299
+ let n = t();
300
+ return this.commitDependencies(e, i), n;
301
+ } catch (t) {
302
+ throw n && this.commitDependencies(e, i), t;
303
+ } finally {
304
+ e.collecting = void 0, this.activeObserver = r;
305
+ }
306
+ }
307
+ commitDependencies(e, t) {
308
+ for (let n of e.dependencies) t.has(n) || n.dependents.delete(e);
309
+ for (let n of t) e.dependencies.has(n) || n.dependents.add(e);
310
+ e.dependencies.clear();
311
+ for (let n of t) e.dependencies.add(n);
312
+ }
313
+ flush() {
314
+ if (this.flushing) return;
315
+ this.flushing = !0;
316
+ let e = 0;
317
+ try {
318
+ for (; this.pending.size > 0 || this.listeners.size > 0;) {
319
+ if (++e > t) {
320
+ this.report(new RippleCycleError(`infinite reactive flush (>${t} iterations)`), { kind: "flush" }), this.pending.clear(), this.listeners.clear();
321
+ break;
322
+ }
323
+ let n = [...this.pending], r = [...this.listeners];
324
+ this.pending.clear(), this.listeners.clear();
325
+ for (let e of n) e.run();
326
+ for (let e of r) try {
327
+ e.listener();
328
+ } catch (e) {
329
+ this.report(e, { kind: "listener" });
330
+ }
331
+ }
332
+ } finally {
333
+ this.flushing = !1;
334
+ }
335
+ }
336
+ }, isReactive = (t) => {
337
+ if (typeof t != "object" || !t || t[e] !== !0) return !1;
338
+ let n = t;
339
+ return "value" in n && typeof n.peek == "function" && typeof n.subscribe == "function";
340
+ }, createResource = (t) => (n, r, i) => {
341
+ let a = t.signal({ status: "pending" }, { name: i?.name }), o = t.signal(0), s, c = !1, l, run = () => {
342
+ o.value, s?.abort();
343
+ let e;
344
+ try {
345
+ e = n();
346
+ } catch (e) {
347
+ a.value = c ? {
348
+ error: e,
349
+ previous: l,
350
+ status: "error"
351
+ } : {
352
+ error: e,
353
+ status: "error"
354
+ };
355
+ return;
356
+ }
357
+ let t = new AbortController();
358
+ s = t, a.value = c ? {
359
+ previous: l,
360
+ status: "pending"
361
+ } : { status: "pending" };
362
+ let i;
363
+ try {
364
+ i = Promise.resolve(r(e, { signal: t.signal }));
365
+ } catch (e) {
366
+ i = Promise.reject(e);
367
+ }
368
+ i.then((e) => {
369
+ !u.disposed && !t.signal.aborted && (c = !0, l = e, a.value = {
370
+ status: "success",
371
+ value: e
372
+ });
373
+ }, (e) => {
374
+ !u.disposed && !t.signal.aborted && (a.value = c ? {
375
+ error: e,
376
+ previous: l,
377
+ status: "error"
378
+ } : {
379
+ error: e,
380
+ status: "error"
381
+ });
382
+ });
383
+ }, u = t.effect(() => (run(), () => s?.abort()), { name: i?.name }), assertActive = () => {
384
+ if (u.disposed) throw new RippleDisposedError("Cannot use a disposed resource.");
385
+ }, d = {
386
+ get disposalSignal() {
387
+ return u.disposalSignal;
388
+ },
389
+ dispose: () => u.dispose(),
390
+ get disposed() {
391
+ return u.disposed;
392
+ },
393
+ get name() {
394
+ return a.name;
395
+ },
396
+ peek: () => a.peek(),
397
+ reload: () => {
398
+ assertActive(), o.value = o.peek() + 1;
399
+ },
400
+ subscribe: (e) => (assertActive(), a.subscribe(e)),
401
+ [Symbol.dispose]() {
402
+ this.dispose();
403
+ },
404
+ get value() {
405
+ return a.value;
406
+ }
407
+ };
408
+ return Object.defineProperty(d, e, { value: !0 }), d;
409
+ }, createFromSubscribable = (t) => (n, r) => {
410
+ let i = t.signal(n.getSnapshot(), { name: r?.name }), a = t.effect(() => {
411
+ let sync = () => {
412
+ i.value = n.getSnapshot();
413
+ }, e;
414
+ try {
415
+ e = n.subscribe(sync), sync();
416
+ } catch (t) {
417
+ throw e?.(), t;
418
+ }
419
+ return e;
420
+ }, { name: r?.name }), abort = () => a.dispose();
421
+ r?.signal?.addEventListener("abort", abort, { once: !0 }), r?.signal?.aborted && a.dispose();
422
+ let assertActive = () => {
423
+ if (a.disposed) throw new RippleError("Cannot subscribe to a disposed bridge.");
424
+ }, dispose = () => {
425
+ r?.signal?.removeEventListener("abort", abort), a.dispose();
426
+ }, o = {
427
+ get disposalSignal() {
428
+ return a.disposalSignal;
429
+ },
430
+ dispose,
431
+ get disposed() {
432
+ return a.disposed;
433
+ },
434
+ get name() {
435
+ return i.name;
436
+ },
437
+ peek: () => i.peek(),
438
+ subscribe(e) {
439
+ return assertActive(), i.subscribe(e);
440
+ },
441
+ [Symbol.dispose]: dispose,
442
+ get value() {
443
+ return i.value;
444
+ }
445
+ };
446
+ return Object.defineProperty(o, e, { value: !0 }), o;
447
+ }, createWatch = (e) => (t, n, r) => {
448
+ let i = typeof t == "function" ? t : () => t.value, a = r?.equals ?? Object.is, o = !0, s;
449
+ return e.effect(() => {
450
+ let t = i();
451
+ if (o) {
452
+ o = !1, s = t, r?.immediate && e.untrack(() => n(t, void 0));
453
+ return;
454
+ }
455
+ if (a(s, t)) return;
456
+ let c = s;
457
+ s = t, e.untrack(() => n(t, c));
458
+ }, { name: r?.name });
459
+ }, createRipple = (e) => {
460
+ let t = new ReactiveRuntime(e), n = createFromSubscribable(t), r = createResource(t);
461
+ return {
462
+ batch: t.batch,
463
+ computed: t.computed,
464
+ createScope: t.createScope,
465
+ get disposalSignal() {
466
+ return t.disposalSignal;
467
+ },
468
+ dispose: () => t.dispose(),
469
+ get disposed() {
470
+ return t.disposed;
471
+ },
472
+ effect: t.effect,
473
+ fromSubscribable: n,
474
+ resource: r,
475
+ signal: t.signal,
476
+ untrack: t.untrack,
477
+ watch: createWatch(t),
478
+ [Symbol.dispose]: () => t.dispose()
479
+ };
480
+ }, r = createRipple(), i = r.signal, a = r.computed, o = r.effect, s = r.batch, c = r.createScope, l = r.untrack, u = r.watch, d = r.resource, f = r.fromSubscribable;
481
+ //#endregion
482
+ export { RippleCycleError, RippleDisposedError, RippleError, s as batch, a as computed, createRipple, c as createScope, o as effect, f as fromSubscribable, isReactive, d as resource, i as signal, l as untrack, u as watch };
483
+
2
484
  //# sourceMappingURL=ripple.js.map