@sylwellsoftware/glue 0.8.1 → 0.9.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/dist/index.js CHANGED
@@ -1,7 +1,7 @@
1
- var H = Object.defineProperty;
2
- var F = (t, r, e) => r in t ? H(t, r, { enumerable: !0, configurable: !0, writable: !0, value: e }) : t[r] = e;
3
- var n = (t, r, e) => F(t, typeof r != "symbol" ? r + "" : r, e);
4
- let x = 1;
1
+ var U = Object.defineProperty;
2
+ var H = (t, r, e) => r in t ? U(t, r, { enumerable: !0, configurable: !0, writable: !0, value: e }) : t[r] = e;
3
+ var n = (t, r, e) => H(t, typeof r != "symbol" ? r + "" : r, e);
4
+ let F = 1;
5
5
  class b {
6
6
  constructor(r = {}) {
7
7
  n(this, "id");
@@ -18,9 +18,9 @@ class b {
18
18
  var s;
19
19
  if (r == null || typeof r != "object" || Array.isArray(r))
20
20
  throw new TypeError("EventBubble options must be an object");
21
- this.id = `event-${x++}`, this.timestamp = r.timestamp ?? Date.now(), this.purpose = r.purpose ?? "unspecified", this.cause = r.cause ?? null, this.value = r.value, this.parent = r.parent ?? null, this.parentBubble = this.parent;
21
+ this.id = `event-${F++}`, this.timestamp = r.timestamp ?? Date.now(), this.purpose = r.purpose ?? "unspecified", this.cause = r.cause ?? null, this.value = r.value, this.parent = r.parent ?? null, this.parentBubble = this.parent;
22
22
  const e = r.owner;
23
- this.ownerType = e == null ? null : $(e) ?? typeof e, B(e) && typeof WeakRef == "function" ? (this.ownerRef = new WeakRef(e), this.strongOwner = void 0) : (this.ownerRef = null, this.strongOwner = e), (s = this.parent) == null || s.registerChild(this);
23
+ this.ownerType = e == null ? null : _(e) ?? typeof e, B(e) && typeof WeakRef == "function" ? (this.ownerRef = new WeakRef(e), this.strongOwner = void 0) : (this.ownerRef = null, this.strongOwner = e), (s = this.parent) == null || s.registerChild(this);
24
24
  }
25
25
  get owner() {
26
26
  var r;
@@ -35,7 +35,7 @@ class b {
35
35
  function B(t) {
36
36
  return typeof t == "object" && t !== null || typeof t == "function";
37
37
  }
38
- function $(t) {
38
+ function _(t) {
39
39
  const r = Reflect.get(t, "constructor");
40
40
  if (r == null || typeof r != "object" && typeof r != "function")
41
41
  return;
@@ -70,23 +70,23 @@ const w = Object.freeze({
70
70
  get subscriberCount() {
71
71
  return m.size;
72
72
  }
73
- }), h = Object.freeze({
73
+ }), a = Object.freeze({
74
74
  Error: "error",
75
75
  Loading: "loading",
76
76
  Initial: "initial",
77
77
  Ready: "ready"
78
78
  }), S = /* @__PURE__ */ new Map([
79
- [h.Ready, 1],
80
- [h.Initial, 2],
81
- [h.Loading, 3],
82
- [h.Error, 4]
79
+ [a.Ready, 1],
80
+ [a.Initial, 2],
81
+ [a.Loading, 3],
82
+ [a.Error, 4]
83
83
  ]);
84
- function j(t) {
85
- let r = h.Ready;
84
+ function T(t) {
85
+ let r = a.Ready;
86
86
  for (const e of t) {
87
87
  if (!S.has(e))
88
88
  throw new TypeError(`Unknown fetch state: ${String(e)}`);
89
- if ((S.get(e) ?? 0) > (S.get(r) ?? 0) && (r = e), r === h.Error) break;
89
+ if ((S.get(e) ?? 0) > (S.get(r) ?? 0) && (r = e), r === a.Error) break;
90
90
  }
91
91
  return r;
92
92
  }
@@ -103,7 +103,7 @@ class g {
103
103
  n(this, "trace");
104
104
  if (e == null || typeof e != "object" || Array.isArray(e))
105
105
  throw new TypeError("Emitter options must be an object");
106
- if (this.value = r, this.fetchState = e.fetchState ?? h.Ready, this.error = e.error ?? null, this.equals = e.equals ?? Object.is, this.owner = e.owner, this.purpose = e.purpose ?? this.constructor.name, this.trace = e.trace ?? !1, typeof this.equals != "function")
106
+ if (this.value = r, this.fetchState = e.fetchState ?? a.Ready, this.error = e.error ?? null, this.equals = e.equals ?? Object.is, this.owner = e.owner, this.purpose = e.purpose ?? this.constructor.name, this.trace = e.trace ?? !1, typeof this.equals != "function")
107
107
  throw new TypeError("Emitter equals option must be a function");
108
108
  R(this.fetchState);
109
109
  }
@@ -113,14 +113,14 @@ class g {
113
113
  throw new TypeError("subscribe requires a function");
114
114
  if (e == null || typeof e != "object" || Array.isArray(e))
115
115
  throw new TypeError("subscribe options must be an object");
116
- const { emitCurrent: s = !0, parentEvent: i = null } = e;
116
+ const { emitCurrent: s = !0, parentEvent: o = null } = e;
117
117
  if (this.subscribers.add(r), s) {
118
- const u = this.createEvent("subscribed", i, this.value);
119
- r(this.notification(u));
118
+ const c = this.createEvent("subscribed", o, this.value);
119
+ r(this.notification(c));
120
120
  }
121
- let o = !0;
121
+ let i = !0;
122
122
  return () => {
123
- o && (o = !1, this.subscribers.delete(r));
123
+ i && (i = !1, this.subscribers.delete(r));
124
124
  };
125
125
  }
126
126
  subscribeFutureValues(r) {
@@ -143,7 +143,7 @@ class g {
143
143
  }
144
144
  map(r, e = {}) {
145
145
  if (typeof r != "function") throw new TypeError("map requires a function");
146
- return new D(
146
+ return new x(
147
147
  [this],
148
148
  ([s]) => r(s),
149
149
  {
@@ -156,11 +156,11 @@ class g {
156
156
  }
157
157
  mapEach(r, e = {}) {
158
158
  if (typeof r != "function") throw new TypeError("mapEach requires a function");
159
- return new D([this], ([s]) => {
159
+ return new x([this], ([s]) => {
160
160
  if (s == null) return s;
161
161
  if (!Array.isArray(s))
162
162
  throw new TypeError("mapEach source value must be an array");
163
- return s.map((o, u) => o == null ? o : r(o, u));
163
+ return s.map((i, c) => i == null ? i : r(i, c));
164
164
  }, {
165
165
  ...e,
166
166
  purpose: e.purpose ?? `${this.purpose}:mapEach`
@@ -184,47 +184,47 @@ class g {
184
184
  }
185
185
  setSnapshot(r = {}) {
186
186
  if (this.isDisposed) return !1;
187
- const e = Object.hasOwn(r, "value") ? r.value : this.value, s = r.fetchState ?? this.fetchState, i = Object.hasOwn(r, "error") ? r.error : this.error, o = r.cause ?? "state changed", u = r.parentEvent ?? null;
188
- if (R(s), !(!this.equals(this.value, e) || this.fetchState !== s || !Object.is(this.error, i))) return !1;
189
- this.value = e, this.fetchState = s, this.error = i;
190
- const c = this.createEvent(o, u, e);
191
- return this.notify(c), !0;
187
+ const e = Object.hasOwn(r, "value") ? r.value : this.value, s = r.fetchState ?? this.fetchState, o = Object.hasOwn(r, "error") ? r.error : this.error, i = r.cause ?? "state changed", c = r.parentEvent ?? null;
188
+ if (R(s), !(!this.equals(this.value, e) || this.fetchState !== s || !Object.is(this.error, o))) return !1;
189
+ this.value = e, this.fetchState = s, this.error = o;
190
+ const u = this.createEvent(i, c, e);
191
+ return this.notify(u), !0;
192
192
  }
193
193
  createEvent(r, e = null, s = this.value) {
194
194
  if (!this.trace && !e && !w.hasSubscribers) return null;
195
- const i = new b({
195
+ const o = new b({
196
196
  owner: this.owner ?? this,
197
197
  purpose: this.purpose,
198
198
  value: s,
199
199
  cause: r,
200
200
  parent: e
201
201
  });
202
- return e || w.emit(i), i;
202
+ return e || w.emit(o), o;
203
203
  }
204
204
  }
205
- class D extends g {
206
- constructor(e, s, i = {}) {
207
- const { computeFetchState: o = j, ...u } = i;
205
+ class x extends g {
206
+ constructor(e, s, o = {}) {
207
+ const { computeFetchState: i = T, ...c } = o;
208
208
  super(void 0, {
209
- ...u,
210
- fetchState: h.Initial,
209
+ ...c,
210
+ fetchState: a.Initial,
211
211
  error: null
212
212
  });
213
213
  n(this, "sources");
214
214
  n(this, "sourceUnsubscribers", []);
215
215
  n(this, "compute");
216
216
  n(this, "computeFetchState");
217
- this.sources = [], this.compute = s, this.computeFetchState = o, this.setSourcesAndCompute(e, s, { notify: !1 });
217
+ this.sources = [], this.compute = s, this.computeFetchState = i, this.setSourcesAndCompute(e, s, { notify: !1 });
218
218
  }
219
- setSourcesAndCompute(e, s = this.compute, i = {}) {
219
+ setSourcesAndCompute(e, s = this.compute, o = {}) {
220
220
  if (this.isDisposed) throw new Error("Cannot update a disposed derived emitter");
221
- if (!Array.isArray(e) || e.some((o) => !k(o)))
221
+ if (!Array.isArray(e) || e.some((i) => !k(i)))
222
222
  throw new TypeError("DerivedEmitter sources must be an array of emitters");
223
223
  if (typeof s != "function")
224
224
  throw new TypeError("DerivedEmitter compute must be a function");
225
225
  return this.releaseSources(), this.sources = [...e], this.compute = s, this.sourceUnsubscribers = this.sources.map(
226
- (o) => o.subscribe(({ event: u }) => this.recompute(u), { emitCurrent: !1 })
227
- ), this.recompute(i.parentEvent ?? null, i.notify ?? !0), this;
226
+ (i) => i.subscribe(({ event: c }) => this.recompute(c), { emitCurrent: !1 })
227
+ ), this.recompute(o.parentEvent ?? null, o.notify ?? !0), this;
228
228
  }
229
229
  dispose() {
230
230
  this.isDisposed || (this.releaseSources(), super.dispose());
@@ -234,21 +234,21 @@ class D extends g {
234
234
  this.sourceUnsubscribers = [];
235
235
  }
236
236
  recompute(e = null, s = !0) {
237
- const i = this.sources.map((d) => d.get()), o = this.sources.map((d) => d.getFetchState()), u = this.sources.map((d, v) => ({ sourceIndex: v, error: d.getError() })).filter((d) => d.error != null);
238
- let a, c = null;
237
+ const o = this.sources.map((d) => d.get()), i = this.sources.map((d) => d.getFetchState()), c = this.sources.map((d, v) => ({ sourceIndex: v, error: d.getError() })).filter((d) => d.error != null);
238
+ let f, u = null;
239
239
  try {
240
- a = this.compute(i);
240
+ f = this.compute(o);
241
241
  } catch (d) {
242
- c = d, a = void 0;
242
+ u = d, f = void 0;
243
243
  }
244
- const f = c == null ? u : [...u, { sourceIndex: null, error: c }], p = _(this.error, f);
245
- let l = this.computeFetchState(o);
246
- if (R(l), (c != null || u.length > 0) && (l = h.Error), !s) {
247
- this.value = a, this.fetchState = l, this.error = p;
244
+ const h = u == null ? c : [...c, { sourceIndex: null, error: u }], p = $(this.error, h);
245
+ let l = this.computeFetchState(i);
246
+ if (R(l), (u != null || c.length > 0) && (l = a.Error), !s) {
247
+ this.value = f, this.fetchState = l, this.error = p;
248
248
  return;
249
249
  }
250
250
  this.setSnapshot({
251
- value: a,
251
+ value: f,
252
252
  fetchState: l,
253
253
  error: p,
254
254
  cause: "derived source changed",
@@ -256,28 +256,28 @@ class D extends g {
256
256
  });
257
257
  }
258
258
  }
259
- function _(t, r) {
259
+ function $(t, r) {
260
260
  return r.length === 0 ? null : t != null && t.length === r.length && t.every((e, s) => {
261
- var i, o;
262
- return e.sourceIndex === ((i = r[s]) == null ? void 0 : i.sourceIndex) && Object.is(e.error, (o = r[s]) == null ? void 0 : o.error);
261
+ var o, i;
262
+ return e.sourceIndex === ((o = r[s]) == null ? void 0 : o.sourceIndex) && Object.is(e.error, (i = r[s]) == null ? void 0 : i.error);
263
263
  }) ? t : Object.freeze(r.map((e) => Object.freeze(e)));
264
264
  }
265
265
  function k(t) {
266
266
  return (typeof t != "object" || t === null) && typeof t != "function" ? !1 : typeof Reflect.get(t, "get") == "function" && typeof Reflect.get(t, "getFetchState") == "function" && typeof Reflect.get(t, "getError") == "function" && typeof Reflect.get(t, "subscribe") == "function";
267
267
  }
268
268
  function R(t) {
269
- j([t]);
269
+ T([t]);
270
270
  }
271
- class U extends g {
271
+ class z extends g {
272
272
  constructor(r, e = {}) {
273
273
  super(r, e);
274
274
  }
275
- setWithState(r, e = h.Ready, s = null, i = null) {
276
- const o = i instanceof b ? i : null, u = o ? "set called" : i ?? "set called";
277
- return this.setSnapshot({ value: r, fetchState: e, error: s, cause: u, parentEvent: o });
275
+ setWithState(r, e = a.Ready, s = null, o = null) {
276
+ const i = o instanceof b ? o : null, c = i ? "set called" : o ?? "set called";
277
+ return this.setSnapshot({ value: r, fetchState: e, error: s, cause: c, parentEvent: i });
278
278
  }
279
279
  set(r, e = null) {
280
- return this.setWithState(r, h.Ready, null, e);
280
+ return this.setWithState(r, a.Ready, null, e);
281
281
  }
282
282
  }
283
283
  class M extends Error {
@@ -285,25 +285,25 @@ class M extends Error {
285
285
  super("AsyncCommand is already running"), this.name = "AsyncCommandConcurrencyError";
286
286
  }
287
287
  }
288
- class fe extends g {
288
+ class pe extends g {
289
289
  constructor(e) {
290
290
  N(e);
291
291
  const {
292
292
  execute: s,
293
- concurrency: i = "ignore",
294
- mapError: o = (f) => f,
295
- owner: u,
296
- purpose: a = "async command",
297
- trace: c
293
+ concurrency: o = "ignore",
294
+ mapError: i = (h) => h,
295
+ owner: c,
296
+ purpose: f = "async command",
297
+ trace: u
298
298
  } = e;
299
- if (W(i), typeof o != "function")
299
+ if (W(o), typeof i != "function")
300
300
  throw new TypeError("AsyncCommand mapError must be a function");
301
301
  super(void 0, {
302
- fetchState: h.Initial,
302
+ fetchState: a.Initial,
303
303
  error: null,
304
- owner: u,
305
- purpose: a,
306
- ...c === void 0 ? {} : { trace: c }
304
+ owner: c,
305
+ purpose: f,
306
+ ...u === void 0 ? {} : { trace: u }
307
307
  });
308
308
  n(this, "execute");
309
309
  n(this, "concurrency");
@@ -316,10 +316,10 @@ class fe extends g {
316
316
  n(this, "hasSuccessfulValue", !1);
317
317
  /** Exposed for deterministic tests; consumers should use run()/abort(). */
318
318
  n(this, "_activeRequest", null);
319
- this.execute = s, this.concurrency = i, this.mapError = o, this.runningEmitter = new U(!1, {
320
- owner: u ?? this,
321
- purpose: `${a}:running`,
322
- ...c === void 0 ? {} : { trace: c }
319
+ this.execute = s, this.concurrency = o, this.mapError = i, this.runningEmitter = new z(!1, {
320
+ owner: c ?? this,
321
+ purpose: `${f}:running`,
322
+ ...u === void 0 ? {} : { trace: u }
323
323
  }), this.isRunning = this.runningEmitter;
324
324
  }
325
325
  run(e, s = "command run") {
@@ -331,51 +331,51 @@ class fe extends g {
331
331
  return Promise.reject(new M());
332
332
  (p = this.abortController) == null || p.abort();
333
333
  }
334
- const i = ++this.requestId, o = G();
335
- this.abortController = o;
336
- const u = s instanceof b ? s : null, a = u ? "parent command requested" : s;
334
+ const o = ++this.requestId, i = G();
335
+ this.abortController = i;
336
+ const c = s instanceof b ? s : null, f = c ? "parent command requested" : s;
337
337
  this.setSnapshot({
338
338
  value: this.lastSuccessfulValue,
339
- fetchState: h.Loading,
339
+ fetchState: a.Loading,
340
340
  error: null,
341
- cause: a,
342
- parentEvent: u
341
+ cause: f,
342
+ parentEvent: c
343
343
  });
344
- const c = this.createEvent("command execute", u, e);
345
- this.runningEmitter.set(!0, c ?? a);
346
- const f = Promise.resolve().then(() => this.execute(e, {
347
- signal: o.signal,
348
- event: c
344
+ const u = this.createEvent("command execute", c, e);
345
+ this.runningEmitter.set(!0, u ?? f);
346
+ const h = Promise.resolve().then(() => this.execute(e, {
347
+ signal: i.signal,
348
+ event: u
349
349
  })).then((l) => {
350
- if (this.isCurrentRequest(i, o))
350
+ if (this.isCurrentRequest(o, i))
351
351
  return this.lastSuccessfulValue = l, this.hasSuccessfulValue = !0, this.setSnapshot({
352
352
  value: l,
353
- fetchState: h.Ready,
353
+ fetchState: a.Ready,
354
354
  error: null,
355
355
  cause: "command succeeded",
356
- parentEvent: c
356
+ parentEvent: u
357
357
  }), l;
358
358
  }).catch((l) => {
359
- this.isCurrentRequest(i, o) && this.setSnapshot({
359
+ this.isCurrentRequest(o, i) && this.setSnapshot({
360
360
  value: this.lastSuccessfulValue,
361
- fetchState: h.Error,
361
+ fetchState: a.Error,
362
362
  error: this.mapCommandError(l),
363
363
  cause: "command failed",
364
- parentEvent: c
364
+ parentEvent: u
365
365
  });
366
366
  }).finally(() => {
367
- this.isCurrentRequest(i, o) && (this.abortController = null, this._activeRequest = null, this.runningEmitter.set(!1, c ?? "command settled"));
367
+ this.isCurrentRequest(o, i) && (this.abortController = null, this._activeRequest = null, this.runningEmitter.set(!1, u ?? "command settled"));
368
368
  });
369
- return this._activeRequest = f, f;
369
+ return this._activeRequest = h, h;
370
370
  }
371
371
  abort(e = "command aborted") {
372
372
  if (this.isDisposed || this.abortController == null) return !1;
373
- const s = e instanceof b ? e : null, i = s ? "parent command aborted" : e;
374
- return this.requestId += 1, this.abortController.abort(), this.abortController = null, this._activeRequest = null, this.runningEmitter.set(!1, s ?? i), this.setSnapshot({
373
+ const s = e instanceof b ? e : null, o = s ? "parent command aborted" : e;
374
+ return this.requestId += 1, this.abortController.abort(), this.abortController = null, this._activeRequest = null, this.runningEmitter.set(!1, s ?? o), this.setSnapshot({
375
375
  value: this.lastSuccessfulValue,
376
- fetchState: this.hasSuccessfulValue ? h.Ready : h.Initial,
376
+ fetchState: this.hasSuccessfulValue ? a.Ready : a.Initial,
377
377
  error: null,
378
- cause: i,
378
+ cause: o,
379
379
  parentEvent: s
380
380
  }), !0;
381
381
  }
@@ -385,7 +385,7 @@ class fe extends g {
385
385
  const s = e instanceof b ? e : null;
386
386
  this.setSnapshot({
387
387
  value: void 0,
388
- fetchState: h.Initial,
388
+ fetchState: a.Initial,
389
389
  error: null,
390
390
  cause: s ? "parent command reset" : e,
391
391
  parentEvent: s
@@ -422,36 +422,40 @@ function G() {
422
422
  throw new Error("AsyncCommand requires AbortController in this runtime");
423
423
  return new t();
424
424
  }
425
- class I extends g {
425
+ class D extends g {
426
426
  constructor(e) {
427
427
  if (e == null || typeof e != "object" || Array.isArray(e))
428
428
  throw new TypeError("LiveQuery options must be an object");
429
429
  const {
430
430
  handler: s,
431
- args: i = {},
432
- autoFetch: o = !0,
433
- keepPreviousValue: u = !0,
434
- polling: a,
435
- owner: c,
436
- purpose: f = "live query",
437
- trace: p
431
+ args: o = {},
432
+ execution: i,
433
+ autoFetch: c = !0,
434
+ keepPreviousValue: f = !0,
435
+ polling: u,
436
+ owner: h,
437
+ purpose: p = "live query",
438
+ trace: l
438
439
  } = e;
439
440
  if (s == null || typeof s.fetch != "function")
440
441
  throw new TypeError("LiveQuery handler must implement fetch()");
441
- X(i), a != null && K(a);
442
+ Y(o), u != null && K(u), X(i, e.autoFetch, u);
442
443
  super(void 0, {
443
- fetchState: h.Initial,
444
+ fetchState: a.Initial,
444
445
  error: null,
445
- owner: c,
446
- purpose: f,
447
- ...p === void 0 ? {} : { trace: p }
446
+ owner: h,
447
+ purpose: p,
448
+ ...l === void 0 ? {} : { trace: l }
448
449
  });
449
450
  n(this, "handler");
450
451
  n(this, "args");
451
452
  n(this, "keepPreviousValue");
453
+ n(this, "execution");
452
454
  n(this, "lastSuccessfulValue");
453
455
  n(this, "hasSuccessfulValue", !1);
454
- n(this, "argumentUnsubscribers");
456
+ n(this, "automaticTriggersInitialized", !1);
457
+ n(this, "activated");
458
+ n(this, "argumentUnsubscribers", []);
455
459
  n(this, "pollingUnsubscribers", []);
456
460
  n(this, "polling");
457
461
  n(this, "pollingScheduler");
@@ -460,56 +464,68 @@ class I extends g {
460
464
  n(this, "abortController", null);
461
465
  /** Exposed for deterministic tests; consumers should use refresh()/retry(). */
462
466
  n(this, "_activeRequest", null);
463
- this.handler = s, this.args = { ...i }, this.keepPreviousValue = u, this.polling = a, this.pollingScheduler = (a == null ? void 0 : a.scheduler) ?? J, this.lastSuccessfulValue = void 0, this.argumentUnsubscribers = Object.values(this.args).map(
464
- (l) => l.subscribe(({ event: d }) => {
465
- this.refresh(d);
466
- }, { emitCurrent: !1 })
467
- ), a != null && this.initializePolling(a), o && (this._activeRequest = this.refresh("initial fetch")), this.scheduleNextPoll();
467
+ this.handler = s, this.args = { ...o }, this.keepPreviousValue = f, this.execution = i ?? null, this.activated = i !== "deferred", this.polling = u, this.pollingScheduler = (u == null ? void 0 : u.scheduler) ?? J, this.lastSuccessfulValue = void 0, i !== "deferred" && i !== "explicit" && this.initializeAutomaticTriggers(), (i === "immediate" || i == null && c) && (this._activeRequest = this.executeRequest("initial fetch")), this.scheduleNextPoll();
468
468
  }
469
469
  get argumentValues() {
470
470
  return Object.fromEntries(
471
471
  Object.entries(this.args).map(([e, s]) => [e, s.get()])
472
472
  );
473
473
  }
474
+ /**
475
+ * Activates a deferred query exactly once. Concurrent callers share the
476
+ * first request; later calls return the current value without reloading.
477
+ */
478
+ activate(e = "query activated") {
479
+ if (this.isDisposed) return Promise.resolve(void 0);
480
+ if (this.execution === "explicit")
481
+ return Promise.reject(new Error("Explicit LiveQuery execution cannot be activated"));
482
+ if (this.activated)
483
+ return this._activeRequest ?? Promise.resolve(this.get());
484
+ this.activated = !0, this.initializeAutomaticTriggers();
485
+ const s = this.executeRequest(e);
486
+ return this.scheduleNextPoll(), s;
487
+ }
474
488
  refresh(e = "refresh") {
489
+ return this.isDisposed ? Promise.resolve(void 0) : this.activated ? this.executeRequest(e) : this.activate(e);
490
+ }
491
+ executeRequest(e) {
475
492
  var p;
476
- if (this.isDisposed) return Promise.resolve(void 0);
477
493
  const s = ++this.requestId;
478
494
  (p = this.abortController) == null || p.abort();
479
- const i = Y();
480
- this.abortController = i;
481
- const o = e instanceof b ? e : null, u = o ? "query arguments changed" : e, a = this.keepPreviousValue ? this.lastSuccessfulValue : void 0;
495
+ const o = Z();
496
+ this.abortController = o;
497
+ const i = e instanceof b ? e : null, c = i ? "query arguments changed" : e, f = this.keepPreviousValue ? this.lastSuccessfulValue : void 0;
482
498
  this.setSnapshot({
483
- value: a,
484
- fetchState: h.Loading,
499
+ value: f,
500
+ fetchState: a.Loading,
485
501
  error: null,
486
- cause: u,
487
- parentEvent: o
502
+ cause: c,
503
+ parentEvent: i
488
504
  });
489
- const c = this.createEvent("query fetch", o, this.argumentValues), f = Promise.resolve().then(() => this.handler.fetch(this.argumentValues, {
490
- signal: i.signal,
491
- event: c
505
+ const u = this.createEvent("query fetch", i, this.argumentValues), h = Promise.resolve().then(() => this.handler.fetch(this.argumentValues, {
506
+ signal: o.signal,
507
+ event: u
492
508
  })).then((l) => {
493
- if (this.isCurrentRequest(s, i))
509
+ if (this.isCurrentRequest(s, o))
494
510
  return this.lastSuccessfulValue = l, this.hasSuccessfulValue = !0, this.setSnapshot({
495
511
  value: l,
496
- fetchState: h.Ready,
512
+ fetchState: a.Ready,
497
513
  error: null,
498
514
  cause: "query succeeded",
499
- parentEvent: c
515
+ parentEvent: u
500
516
  }), l;
501
517
  }).catch((l) => {
502
- !this.isCurrentRequest(s, i) || Z(l) || this.setSnapshot({
518
+ !this.isCurrentRequest(s, o) || ee(l) || this.setSnapshot({
503
519
  value: this.keepPreviousValue ? this.lastSuccessfulValue : void 0,
504
- fetchState: h.Error,
520
+ fetchState: a.Error,
505
521
  error: l,
506
522
  cause: "query failed",
507
- parentEvent: c
523
+ parentEvent: u
508
524
  });
509
525
  }).finally(() => {
510
- this.isCurrentRequest(s, i) && (this.abortController = null, this._activeRequest = null);
526
+ this.isCurrentRequest(s, o) && (this.abortController = null, this._activeRequest = null);
511
527
  });
512
- return this._activeRequest = f, f;
528
+ return this._activeRequest = h, h;
513
529
  }
514
530
  retry(e = "retry") {
515
531
  return this.refresh(e);
@@ -520,7 +536,7 @@ class I extends g {
520
536
  const s = e instanceof b ? e : null;
521
537
  this.setSnapshot({
522
538
  value: this.hasSuccessfulValue ? this.lastSuccessfulValue : void 0,
523
- fetchState: this.hasSuccessfulValue ? h.Ready : h.Initial,
539
+ fetchState: this.hasSuccessfulValue ? a.Ready : a.Initial,
524
540
  error: null,
525
541
  cause: s == null ? e : "query aborted",
526
542
  parentEvent: s
@@ -536,14 +552,21 @@ class I extends g {
536
552
  this.pollingUnsubscribers = [], super.dispose();
537
553
  }
538
554
  }
555
+ initializeAutomaticTriggers() {
556
+ this.automaticTriggersInitialized || this.execution === "explicit" || (this.automaticTriggersInitialized = !0, this.argumentUnsubscribers = Object.values(this.args).map(
557
+ (e) => e.subscribe(({ event: s }) => {
558
+ this.refresh(s);
559
+ }, { emitCurrent: !1 })
560
+ ), this.polling != null && this.initializePolling(this.polling));
561
+ }
539
562
  initializePolling(e) {
540
563
  for (const s of [e.intervalMs, e.enabled])
541
- T(s) && this.pollingUnsubscribers.push(s.subscribe(() => {
564
+ j(s) && this.pollingUnsubscribers.push(s.subscribe(() => {
542
565
  q(y(e.intervalMs)), Q(y(e.enabled, !0)), this.cancelScheduledPoll(), this.scheduleNextPoll();
543
566
  }, { emitCurrent: !1 }));
544
567
  }
545
568
  scheduleNextPoll() {
546
- if (this.isDisposed || this.polling == null || this.pollingHandle != null || !y(this.polling.enabled, !0)) return;
569
+ if (this.isDisposed || !this.automaticTriggersInitialized || this.polling == null || this.pollingHandle != null || !y(this.polling.enabled, !0)) return;
547
570
  const e = y(this.polling.intervalMs);
548
571
  q(e), this.pollingHandle = this.pollingScheduler.schedule(() => {
549
572
  this.pollingHandle = null, this.scheduleNextPoll(), this._activeRequest == null && this.refresh("poll");
@@ -568,21 +591,29 @@ const J = {
568
591
  typeof r == "function" && Reflect.apply(r, globalThis, [t]);
569
592
  }
570
593
  };
571
- function T(t) {
594
+ function j(t) {
572
595
  return t != null && (typeof t == "object" || typeof t == "function") && typeof Reflect.get(t, "get") == "function" && typeof Reflect.get(t, "subscribe") == "function";
573
596
  }
574
- function O(t, r) {
575
- if (typeof t != "number" && typeof t != "boolean" && !T(t))
597
+ function I(t, r) {
598
+ if (typeof t != "number" && typeof t != "boolean" && !j(t))
576
599
  throw new TypeError(`LiveQuery polling ${r} must be a value or emitter`);
577
600
  }
578
601
  function K(t) {
579
602
  if (t == null || typeof t != "object" || Array.isArray(t))
580
603
  throw new TypeError("LiveQuery polling options must be an object");
581
- if (O(t.intervalMs, "intervalMs"), t.enabled !== void 0 && O(t.enabled, "enabled"), q(y(t.intervalMs)), Q(y(t.enabled, !0)), t.scheduler != null && (typeof t.scheduler.schedule != "function" || typeof t.scheduler.cancel != "function"))
604
+ if (I(t.intervalMs, "intervalMs"), t.enabled !== void 0 && I(t.enabled, "enabled"), q(y(t.intervalMs)), Q(y(t.enabled, !0)), t.scheduler != null && (typeof t.scheduler.schedule != "function" || typeof t.scheduler.cancel != "function"))
582
605
  throw new TypeError("LiveQuery polling scheduler must implement schedule() and cancel()");
583
606
  }
607
+ function X(t, r, e) {
608
+ if (t !== void 0 && t !== "immediate" && t !== "deferred" && t !== "explicit")
609
+ throw new TypeError("LiveQuery execution must be immediate, deferred, or explicit");
610
+ if (t !== void 0 && r !== void 0)
611
+ throw new TypeError("LiveQuery execution and autoFetch are mutually exclusive");
612
+ if (t === "explicit" && e !== void 0)
613
+ throw new TypeError("Explicit LiveQuery execution does not support polling");
614
+ }
584
615
  function y(t, r) {
585
- return t === void 0 ? r : T(t) ? t.get() : t;
616
+ return t === void 0 ? r : j(t) ? t.get() : t;
586
617
  }
587
618
  function q(t) {
588
619
  if (!Number.isFinite(t) || t <= 0)
@@ -592,42 +623,42 @@ function Q(t) {
592
623
  if (typeof t != "boolean")
593
624
  throw new TypeError("LiveQuery polling enabled must be a boolean");
594
625
  }
595
- function X(t) {
626
+ function Y(t) {
596
627
  if (t == null || typeof t != "object" || Array.isArray(t))
597
628
  throw new TypeError("LiveQuery args must be a named record of emitters");
598
629
  for (const [r, e] of Object.entries(t))
599
630
  if (e == null || typeof Reflect.get(e, "get") != "function" || typeof Reflect.get(e, "subscribe") != "function")
600
631
  throw new TypeError(`LiveQuery argument ${r} must be an emitter`);
601
632
  }
602
- function Y() {
633
+ function Z() {
603
634
  const t = Reflect.get(globalThis, "AbortController");
604
635
  if (typeof t != "function")
605
636
  throw new Error("LiveQuery requires AbortController in this runtime");
606
637
  return new t();
607
638
  }
608
- function Z(t) {
639
+ function ee(t) {
609
640
  return t instanceof Error && t.name === "AbortError";
610
641
  }
611
- class pe extends U {
612
- constructor(e, s, i = {}) {
642
+ class de extends z {
643
+ constructor(e, s, o = {}) {
613
644
  if (typeof e != "string" || e.length === 0)
614
645
  throw new TypeError("QueryArg name must be a non-empty string");
615
646
  if (s == null || typeof s.get != "function" || typeof s.subscribe != "function")
616
647
  throw new TypeError("QueryArg source must be an emitter");
617
648
  super(s.get(), {
618
- ...i,
649
+ ...o,
619
650
  fetchState: s.getFetchState(),
620
651
  error: s.getError(),
621
- purpose: i.purpose ?? `query argument: ${e}`
652
+ purpose: o.purpose ?? `query argument: ${e}`
622
653
  });
623
654
  n(this, "name");
624
655
  n(this, "sourceUnsubscribe");
625
- this.name = e, this.sourceUnsubscribe = s.subscribe((o) => {
656
+ this.name = e, this.sourceUnsubscribe = s.subscribe((i) => {
626
657
  this.setWithState(
627
- o.value,
628
- o.fetchState,
629
- o.error,
630
- o.event
658
+ i.value,
659
+ i.fetchState,
660
+ i.error,
661
+ i.event
631
662
  );
632
663
  }, { emitCurrent: !1 });
633
664
  }
@@ -636,19 +667,19 @@ class pe extends U {
636
667
  this.isDisposed || ((e = this.sourceUnsubscribe) == null || e.call(this), this.sourceUnsubscribe = null, super.dispose());
637
668
  }
638
669
  }
639
- class ee {
670
+ class te {
640
671
  fetch(r, e = {}) {
641
672
  throw new Error("fetch() must be implemented");
642
673
  }
643
674
  }
644
- class te extends Error {
675
+ class re extends Error {
645
676
  constructor(e) {
646
677
  super(`HTTP ${e ?? "unknown"}`);
647
678
  n(this, "status");
648
679
  this.name = "HttpError", this.status = e;
649
680
  }
650
681
  }
651
- class re extends ee {
682
+ class se extends te {
652
683
  constructor(e) {
653
684
  super();
654
685
  n(this, "url");
@@ -662,7 +693,7 @@ class re extends ee {
662
693
  throw new TypeError("RestQueryHandler requires a URL or options object");
663
694
  if (typeof s.url != "string" || s.url.length === 0)
664
695
  throw new TypeError("RestQueryHandler url must be a non-empty string");
665
- if (this.url = s.url, this.baseUrl = s.baseUrl ?? ie(), this.fetchImplementation = s.fetch ?? oe(), this.serialize = s.serialize ?? se, this.parseResult = s.parseResult, this.trace = s.trace ?? !1, typeof this.fetchImplementation != "function")
696
+ if (this.url = s.url, this.baseUrl = s.baseUrl ?? oe(), this.fetchImplementation = s.fetch ?? ue(), this.serialize = s.serialize ?? ne, this.parseResult = s.parseResult, this.trace = s.trace ?? !1, typeof this.fetchImplementation != "function")
666
697
  throw new TypeError("RestQueryHandler requires an injected fetch implementation");
667
698
  if (typeof this.serialize != "function")
668
699
  throw new TypeError("RestQueryHandler serialize must be a function");
@@ -672,27 +703,27 @@ class re extends ee {
672
703
  async fetch(e, s = {}) {
673
704
  if (e == null || typeof e != "object" || Array.isArray(e))
674
705
  throw new TypeError("REST query args must be a named record");
675
- const i = ne(this.url, this.baseUrl), o = this.serialize(i, e) ?? i, u = P();
676
- if (!(o instanceof u))
706
+ const o = ie(this.url, this.baseUrl), i = this.serialize(o, e) ?? o, c = O();
707
+ if (!(i instanceof c))
677
708
  throw new TypeError("REST serializer must mutate or return a URL");
678
- const a = s.event ?? null;
679
- if (this.trace || a || w.hasSubscribers) {
709
+ const f = s.event ?? null;
710
+ if (this.trace || f || w.hasSubscribers) {
680
711
  const l = new b({
681
712
  owner: this,
682
713
  purpose: "REST fetch",
683
- value: o.toString(),
714
+ value: i.toString(),
684
715
  cause: "fetch called",
685
- parent: a
716
+ parent: f
686
717
  });
687
- a || w.emit(l);
718
+ f || w.emit(l);
688
719
  }
689
- const c = s.signal === void 0 ? {} : { signal: s.signal }, f = await this.fetchImplementation(o.toString(), c);
690
- if (!(f != null && f.ok)) throw new te(f == null ? void 0 : f.status);
691
- const p = await f.json();
720
+ const u = s.signal === void 0 ? {} : { signal: s.signal }, h = await this.fetchImplementation(i.toString(), u);
721
+ if (!(h != null && h.ok)) throw new re(h == null ? void 0 : h.status);
722
+ const p = await h.json();
692
723
  return this.parseResult == null ? p : this.parseResult(p);
693
724
  }
694
725
  }
695
- function se(t, r) {
726
+ function ne(t, r) {
696
727
  for (const [e, s] of Object.entries(r))
697
728
  L(t.searchParams, e, s);
698
729
  return t;
@@ -714,8 +745,8 @@ function L(t, r, e) {
714
745
  t.append(r, String(e));
715
746
  }
716
747
  }
717
- function ne(t, r) {
718
- const e = P();
748
+ function ie(t, r) {
749
+ const e = O();
719
750
  try {
720
751
  return new e(t);
721
752
  } catch (s) {
@@ -724,23 +755,23 @@ function ne(t, r) {
724
755
  return new e(t, r);
725
756
  }
726
757
  }
727
- function P() {
758
+ function O() {
728
759
  const t = Reflect.get(globalThis, "URL");
729
760
  if (typeof t != "function")
730
761
  throw new Error("RestQueryHandler requires URL in this runtime");
731
762
  return t;
732
763
  }
733
- function ie() {
764
+ function oe() {
734
765
  const t = Reflect.get(globalThis, "location");
735
766
  if (t == null || typeof t != "object" && typeof t != "function")
736
767
  return;
737
768
  const r = Reflect.get(t, "href");
738
769
  return typeof r == "string" ? r : void 0;
739
770
  }
740
- function oe() {
771
+ function ue() {
741
772
  return Reflect.get(globalThis, "fetch");
742
773
  }
743
- class ue {
774
+ class ce {
744
775
  constructor(r) {
745
776
  n(this, "handler");
746
777
  n(this, "queryOptions");
@@ -752,7 +783,7 @@ class ue {
752
783
  E(e), this.handler = r.handler, this.queryOptions = Object.freeze({ ...e }), Object.freeze(this);
753
784
  }
754
785
  open(r, e = {}) {
755
- return A(r), E(e), new I({
786
+ return A(r), E(e), new D({
756
787
  handler: this.handler,
757
788
  args: r,
758
789
  ...this.queryOptions,
@@ -760,20 +791,20 @@ class ue {
760
791
  });
761
792
  }
762
793
  }
763
- function de(t) {
764
- return new ue(t);
794
+ function be(t) {
795
+ return new ce(t);
765
796
  }
766
- class ce {
797
+ class le {
767
798
  constructor(r) {
768
799
  n(this, "handler");
769
800
  n(this, "queryOptions");
770
801
  if (r == null || typeof r != "object" || Array.isArray(r))
771
802
  throw new TypeError("RestEndpoint options must be an object");
772
803
  const { query: e = {}, ...s } = r;
773
- E(e, "RestEndpoint query options"), this.handler = new re(s), this.queryOptions = Object.freeze({ ...e }), Object.freeze(this);
804
+ E(e, "RestEndpoint query options"), this.handler = new se(s), this.queryOptions = Object.freeze({ ...e }), Object.freeze(this);
774
805
  }
775
806
  open(r, e = {}) {
776
- return A(r), E(e), new I({
807
+ return A(r), E(e), new D({
777
808
  handler: this.handler,
778
809
  args: r,
779
810
  ...this.queryOptions,
@@ -781,10 +812,10 @@ class ce {
781
812
  });
782
813
  }
783
814
  }
784
- function be(t) {
785
- return new ce(t);
815
+ function ye(t) {
816
+ return new le(t);
786
817
  }
787
- class le {
818
+ class ae {
788
819
  constructor(r) {
789
820
  n(this, "apply");
790
821
  n(this, "purpose");
@@ -795,26 +826,26 @@ class le {
795
826
  this.apply = r.apply, this.purpose = r.purpose ?? "derived endpoint", Object.freeze(this);
796
827
  }
797
828
  open(r) {
798
- return new ae({
829
+ return new he({
799
830
  ...r,
800
831
  apply: this.apply,
801
832
  purpose: r.purpose ?? this.purpose
802
833
  });
803
834
  }
804
835
  }
805
- function ye(t) {
806
- return new le(t);
836
+ function me(t) {
837
+ return new ae(t);
807
838
  }
808
- class ae extends g {
839
+ class he extends g {
809
840
  constructor(e) {
810
841
  if (e == null || typeof e != "object" || Array.isArray(e))
811
842
  throw new TypeError("DerivedLiveResult options must be an object");
812
- if (A(e.args), !z(e.source))
843
+ if (A(e.args), !P(e.source))
813
844
  throw new TypeError("DerivedLiveResult source must be an emitter");
814
845
  if (typeof e.apply != "function")
815
846
  throw new TypeError("DerivedLiveResult apply must be a function");
816
847
  super(void 0, {
817
- fetchState: h.Initial,
848
+ fetchState: a.Initial,
818
849
  error: null,
819
850
  owner: e.owner,
820
851
  ...e.purpose === void 0 ? {} : { purpose: e.purpose },
@@ -830,8 +861,8 @@ class ae extends g {
830
861
  this.source,
831
862
  ...Object.values(this.args)
832
863
  ];
833
- this.unsubscribers = s.map((i) => i.subscribe(({ event: o }) => {
834
- this.recompute(o);
864
+ this.unsubscribers = s.map((o) => o.subscribe(({ event: i }) => {
865
+ this.recompute(i);
835
866
  }, { emitCurrent: !1 })), this.recompute(null, !1);
836
867
  }
837
868
  dispose() {
@@ -842,31 +873,31 @@ class ae extends g {
842
873
  }
843
874
  recompute(e, s = !0) {
844
875
  var p;
845
- const i = [
876
+ const o = [
846
877
  this.source,
847
878
  ...Object.values(this.args)
848
- ], o = ((p = i.find((l) => l.getError() != null)) == null ? void 0 : p.getError()) ?? null;
849
- let u = j(
850
- i.map((l) => l.getFetchState())
879
+ ], i = ((p = o.find((l) => l.getError() != null)) == null ? void 0 : p.getError()) ?? null;
880
+ let c = T(
881
+ o.map((l) => l.getFetchState())
851
882
  );
852
- const a = this.source.get();
853
- let c = this.lastSuccessfulValue, f = o;
883
+ const f = this.source.get();
884
+ let u = this.lastSuccessfulValue, h = i;
854
885
  try {
855
886
  const l = Object.fromEntries(
856
887
  Object.entries(this.args).map(([d, v]) => [d, v.get()])
857
888
  );
858
- c = this.apply(a, l), this.lastSuccessfulValue = c;
889
+ u = this.apply(f, l), this.lastSuccessfulValue = u;
859
890
  } catch (l) {
860
- f = l, u = h.Error;
891
+ h = l, c = a.Error;
861
892
  }
862
- if (o != null && (f = o, u = h.Error), !s) {
863
- this.value = c, this.fetchState = u, this.error = f;
893
+ if (i != null && (h = i, c = a.Error), !s) {
894
+ this.value = u, this.fetchState = c, this.error = h;
864
895
  return;
865
896
  }
866
897
  this.setSnapshot({
867
- value: c,
868
- fetchState: u,
869
- error: f,
898
+ value: u,
899
+ fetchState: c,
900
+ error: h,
870
901
  cause: "derived endpoint source changed",
871
902
  parentEvent: e
872
903
  });
@@ -880,33 +911,33 @@ function A(t) {
880
911
  if (t == null || typeof t != "object" || Array.isArray(t))
881
912
  throw new TypeError("Endpoint args must be a named record of emitters");
882
913
  for (const [r, e] of Object.entries(t))
883
- if (!z(e))
914
+ if (!P(e))
884
915
  throw new TypeError(`Endpoint argument ${r} must be an emitter`);
885
916
  }
886
- function z(t) {
917
+ function P(t) {
887
918
  return t != null && (typeof t == "object" || typeof t == "function") && typeof Reflect.get(t, "get") == "function" && typeof Reflect.get(t, "getFetchState") == "function" && typeof Reflect.get(t, "getError") == "function" && typeof Reflect.get(t, "subscribe") == "function";
888
919
  }
889
920
  export {
890
- fe as AsyncCommand,
921
+ pe as AsyncCommand,
891
922
  M as AsyncCommandConcurrencyError,
892
923
  g as BaseEmitter,
893
- D as DerivedEmitter,
894
- le as DerivedEndpoint,
895
- ae as DerivedLiveResult,
896
- U as Emitter,
924
+ x as DerivedEmitter,
925
+ ae as DerivedEndpoint,
926
+ he as DerivedLiveResult,
927
+ z as Emitter,
897
928
  b as EventBubble,
898
929
  w as EventBus,
899
- h as FetchState,
930
+ a as FetchState,
900
931
  S as FetchStateValues,
901
- I as LiveQuery,
902
- pe as QueryArg,
903
- ue as QueryEndpoint,
904
- ee as QueryHandler,
905
- ce as RestEndpoint,
906
- re as RestQueryHandler,
907
- j as combineFetchStates,
908
- ye as derivedEndpoint,
909
- de as queryEndpoint,
910
- be as restEndpoint
932
+ D as LiveQuery,
933
+ de as QueryArg,
934
+ ce as QueryEndpoint,
935
+ te as QueryHandler,
936
+ le as RestEndpoint,
937
+ se as RestQueryHandler,
938
+ T as combineFetchStates,
939
+ me as derivedEndpoint,
940
+ be as queryEndpoint,
941
+ ye as restEndpoint
911
942
  };
912
943
  //# sourceMappingURL=index.js.map