@warp-drive/ember 0.0.0-alpha.7 → 0.0.0-alpha.70

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 ADDED
@@ -0,0 +1,1138 @@
1
+ import { tracked, cached } from '@glimmer/tracking';
2
+ import { getPromiseResult, setPromiseResult } from '@ember-data/request';
3
+ import { service } from '@ember/service';
4
+ import Component from '@glimmer/component';
5
+ import { macroCondition, moduleExists, importSync, getGlobalConfig } from '@embroider/macros';
6
+ import { EnableHydration } from '@warp-drive/core-types/request';
7
+ import { precompileTemplate } from '@ember/template-compilation';
8
+ import { setComponentTemplate } from '@ember/component';
9
+ var __defProp = Object.defineProperty;
10
+ var __export = (target, all) => {
11
+ for (var name in all) __defProp(target, name, {
12
+ get: all[name],
13
+ enumerable: true
14
+ });
15
+ };
16
+
17
+ // src/runtime.ts
18
+ var runtime_exports = {};
19
+ __export(runtime_exports, {
20
+ c: () => decorateClass,
21
+ f: () => decorateFieldV1,
22
+ g: () => decorateFieldV2,
23
+ i: () => initializeDeferredDecorator,
24
+ m: () => decorateMethodV1,
25
+ n: () => decorateMethodV2,
26
+ p: () => decoratePOJO
27
+ });
28
+ var deferred = /* @__PURE__ */new WeakMap();
29
+ function deferDecorator(proto, prop, desc) {
30
+ let map = deferred.get(proto);
31
+ if (!map) {
32
+ map = /* @__PURE__ */new Map();
33
+ deferred.set(proto, map);
34
+ }
35
+ map.set(prop, desc);
36
+ }
37
+ function findDeferredDecorator(target, prop) {
38
+ let cursor = target.prototype;
39
+ while (cursor) {
40
+ let desc = deferred.get(cursor)?.get(prop);
41
+ if (desc) {
42
+ return desc;
43
+ }
44
+ cursor = cursor.prototype;
45
+ }
46
+ }
47
+ function decorateFieldV1(target, prop, decorators, initializer) {
48
+ return decorateFieldV2(target.prototype, prop, decorators, initializer);
49
+ }
50
+ function decorateFieldV2(prototype, prop, decorators, initializer) {
51
+ let desc = {
52
+ configurable: true,
53
+ enumerable: true,
54
+ writable: true,
55
+ initializer: null
56
+ };
57
+ if (initializer) {
58
+ desc.initializer = initializer;
59
+ }
60
+ for (let decorator of decorators) {
61
+ desc = decorator(prototype, prop, desc) || desc;
62
+ }
63
+ if (desc.initializer === void 0) {
64
+ Object.defineProperty(prototype, prop, desc);
65
+ } else {
66
+ deferDecorator(prototype, prop, desc);
67
+ }
68
+ }
69
+ function decorateMethodV1({
70
+ prototype
71
+ }, prop, decorators) {
72
+ return decorateMethodV2(prototype, prop, decorators);
73
+ }
74
+ function decorateMethodV2(prototype, prop, decorators) {
75
+ const origDesc = Object.getOwnPropertyDescriptor(prototype, prop);
76
+ let desc = {
77
+ ...origDesc
78
+ };
79
+ for (let decorator of decorators) {
80
+ desc = decorator(prototype, prop, desc) || desc;
81
+ }
82
+ if (desc.initializer !== void 0) {
83
+ desc.value = desc.initializer ? desc.initializer.call(prototype) : void 0;
84
+ desc.initializer = void 0;
85
+ }
86
+ Object.defineProperty(prototype, prop, desc);
87
+ }
88
+ function initializeDeferredDecorator(target, prop) {
89
+ let desc = findDeferredDecorator(target.constructor, prop);
90
+ if (desc) {
91
+ Object.defineProperty(target, prop, {
92
+ enumerable: desc.enumerable,
93
+ configurable: desc.configurable,
94
+ writable: desc.writable,
95
+ value: desc.initializer ? desc.initializer.call(target) : void 0
96
+ });
97
+ }
98
+ }
99
+ function decorateClass(target, decorators) {
100
+ return decorators.reduce((accum, decorator) => decorator(accum) || accum, target);
101
+ }
102
+ function decoratePOJO(pojo, decorated) {
103
+ for (let [type, prop, decorators] of decorated) {
104
+ if (type === "field") {
105
+ decoratePojoField(pojo, prop, decorators);
106
+ } else {
107
+ decorateMethodV2(pojo, prop, decorators);
108
+ }
109
+ }
110
+ return pojo;
111
+ }
112
+ function decoratePojoField(pojo, prop, decorators) {
113
+ let desc = {
114
+ configurable: true,
115
+ enumerable: true,
116
+ writable: true,
117
+ initializer: () => Object.getOwnPropertyDescriptor(pojo, prop)?.value
118
+ };
119
+ for (let decorator of decorators) {
120
+ desc = decorator(pojo, prop, desc) || desc;
121
+ }
122
+ if (desc.initializer) {
123
+ desc.value = desc.initializer.call(pojo);
124
+ delete desc.initializer;
125
+ }
126
+ Object.defineProperty(pojo, prop, desc);
127
+ }
128
+ const RequestCache = new WeakMap();
129
+ function isAbortError(error) {
130
+ return error instanceof DOMException && error.name === 'AbortError';
131
+ }
132
+ async function watchStream(stream, state) {
133
+ const reader = stream.getReader();
134
+ let bytesLoaded = 0;
135
+ let shouldForward = state._stream !== null && state._stream.readable.locked;
136
+ let isForwarding = shouldForward;
137
+ let writer = state._stream?.writable.getWriter();
138
+ const buffer = [];
139
+ state._isPending = false;
140
+ state._isStarted = true;
141
+ state._startTime = performance.now();
142
+
143
+ // eslint-disable-next-line no-constant-condition
144
+ while (true) {
145
+ const {
146
+ value,
147
+ done
148
+ } = await reader.read();
149
+ if (done) {
150
+ break;
151
+ }
152
+ bytesLoaded += value.byteLength;
153
+ state._bytesLoaded = bytesLoaded;
154
+ state._lastPacketTime = performance.now();
155
+ shouldForward = shouldForward || state._stream !== null && state._stream.readable.locked;
156
+ if (shouldForward) {
157
+ if (!isForwarding) {
158
+ isForwarding = true;
159
+ writer = state._stream.writable.getWriter();
160
+ for (const item of buffer) {
161
+ await writer.ready;
162
+ await writer.write(item);
163
+ }
164
+ buffer.length = 0;
165
+ }
166
+ await writer.ready;
167
+ await writer.write(value);
168
+ } else {
169
+ buffer.push(value);
170
+ }
171
+ }
172
+
173
+ // if we are still forwarding, we need to close the writer
174
+ if (isForwarding) {
175
+ await writer.ready;
176
+ await writer.close();
177
+ } else if (state._stream) {
178
+ // if we are not forwarding, we need to cancel the stream
179
+ await state._stream.readable.cancel('The Stream Has Already Ended');
180
+ state._stream = null;
181
+ }
182
+ const endTime = performance.now();
183
+ state._endTime = endTime;
184
+ state._isComplete = true;
185
+ state._isStarted = false;
186
+ }
187
+ class RequestLoadingState {
188
+ _stream = null;
189
+ _future;
190
+ _triggered = false;
191
+ _trigger() {
192
+ if (this._triggered) {
193
+ return;
194
+ }
195
+ this._triggered = true;
196
+ const future = this._future;
197
+ const promise = future.getStream();
198
+ if (promise.sizeHint) {
199
+ this._sizeHint = promise.sizeHint;
200
+ }
201
+ this.promise = promise.then(stream => {
202
+ if (!stream) {
203
+ this._isPending = false;
204
+ this._isComplete = true;
205
+ return;
206
+ }
207
+ return watchStream(stream, this);
208
+ }, error => {
209
+ this._isPending = false;
210
+ this._isStarted = false;
211
+ if (isAbortError(error)) {
212
+ this._isCancelled = true;
213
+ this._isComplete = true;
214
+ }
215
+ this._isErrored = true;
216
+ this._error = error;
217
+ });
218
+ }
219
+ promise = null;
220
+ static {
221
+ decorateFieldV2(this.prototype, "_sizeHint", [tracked], function () {
222
+ return 0;
223
+ });
224
+ }
225
+ #_sizeHint = (initializeDeferredDecorator(this, "_sizeHint"), void 0);
226
+ static {
227
+ decorateFieldV2(this.prototype, "_bytesLoaded", [tracked], function () {
228
+ return 0;
229
+ });
230
+ }
231
+ #_bytesLoaded = (initializeDeferredDecorator(this, "_bytesLoaded"), void 0);
232
+ static {
233
+ decorateFieldV2(this.prototype, "_startTime", [tracked], function () {
234
+ return 0;
235
+ });
236
+ }
237
+ #_startTime = (initializeDeferredDecorator(this, "_startTime"), void 0);
238
+ static {
239
+ decorateFieldV2(this.prototype, "_endTime", [tracked], function () {
240
+ return 0;
241
+ });
242
+ }
243
+ #_endTime = (initializeDeferredDecorator(this, "_endTime"), void 0);
244
+ static {
245
+ decorateFieldV2(this.prototype, "_lastPacketTime", [tracked], function () {
246
+ return 0;
247
+ });
248
+ }
249
+ #_lastPacketTime = (initializeDeferredDecorator(this, "_lastPacketTime"), void 0);
250
+ static {
251
+ decorateFieldV2(this.prototype, "_isPending", [tracked], function () {
252
+ return true;
253
+ });
254
+ }
255
+ #_isPending = (initializeDeferredDecorator(this, "_isPending"), void 0);
256
+ static {
257
+ decorateFieldV2(this.prototype, "_isStarted", [tracked], function () {
258
+ return false;
259
+ });
260
+ }
261
+ #_isStarted = (initializeDeferredDecorator(this, "_isStarted"), void 0);
262
+ static {
263
+ decorateFieldV2(this.prototype, "_isComplete", [tracked], function () {
264
+ return false;
265
+ });
266
+ }
267
+ #_isComplete = (initializeDeferredDecorator(this, "_isComplete"), void 0);
268
+ static {
269
+ decorateFieldV2(this.prototype, "_isCancelled", [tracked], function () {
270
+ return false;
271
+ });
272
+ }
273
+ #_isCancelled = (initializeDeferredDecorator(this, "_isCancelled"), void 0);
274
+ static {
275
+ decorateFieldV2(this.prototype, "_isErrored", [tracked], function () {
276
+ return false;
277
+ });
278
+ }
279
+ #_isErrored = (initializeDeferredDecorator(this, "_isErrored"), void 0);
280
+ static {
281
+ decorateFieldV2(this.prototype, "_error", [tracked], function () {
282
+ return null;
283
+ });
284
+ }
285
+ #_error = (initializeDeferredDecorator(this, "_error"), void 0);
286
+ get isPending() {
287
+ this._trigger();
288
+ return this._isPending;
289
+ }
290
+ get sizeHint() {
291
+ this._trigger();
292
+ return this._sizeHint;
293
+ }
294
+ get stream() {
295
+ this._trigger();
296
+ if (!this._stream) {
297
+ if (this._isComplete || this._isCancelled || this._isErrored) {
298
+ return null;
299
+ }
300
+ this._stream = new TransformStream();
301
+ }
302
+ return this._stream.readable;
303
+ }
304
+ get isStarted() {
305
+ this._trigger();
306
+ return this._isStarted;
307
+ }
308
+ get bytesLoaded() {
309
+ this._trigger();
310
+ return this._bytesLoaded;
311
+ }
312
+ get startTime() {
313
+ this._trigger();
314
+ return this._startTime;
315
+ }
316
+ get endTime() {
317
+ this._trigger();
318
+ return this._endTime;
319
+ }
320
+ get lastPacketTime() {
321
+ this._trigger();
322
+ return this._lastPacketTime;
323
+ }
324
+ get isComplete() {
325
+ this._trigger();
326
+ return this._isComplete;
327
+ }
328
+ get isCancelled() {
329
+ this._trigger();
330
+ return this._isCancelled;
331
+ }
332
+ get isErrored() {
333
+ this._trigger();
334
+ return this._isErrored;
335
+ }
336
+ get error() {
337
+ this._trigger();
338
+ return this._error;
339
+ }
340
+ get elapsedTime() {
341
+ return (this.endTime || this.lastPacketTime) - this.startTime;
342
+ }
343
+ get completedRatio() {
344
+ return this.sizeHint ? this.bytesLoaded / this.sizeHint : 0;
345
+ }
346
+ get remainingRatio() {
347
+ return 1 - this.completedRatio;
348
+ }
349
+ get duration() {
350
+ return this.endTime - this.startTime;
351
+ }
352
+ get speed() {
353
+ // bytes per second
354
+ return this.bytesLoaded / (this.elapsedTime / 1000);
355
+ }
356
+ constructor(future) {
357
+ this._future = future;
358
+ }
359
+ abort = () => {
360
+ this._future.abort();
361
+ };
362
+ }
363
+ class RequestState {
364
+ #request;
365
+ #loadingState = null;
366
+ static {
367
+ decorateFieldV2(this.prototype, "result", [tracked], function () {
368
+ return null;
369
+ });
370
+ }
371
+ #result = (initializeDeferredDecorator(this, "result"), void 0);
372
+ static {
373
+ decorateFieldV2(this.prototype, "error", [tracked], function () {
374
+ return null;
375
+ });
376
+ }
377
+ #error = (initializeDeferredDecorator(this, "error"), void 0);
378
+ static {
379
+ decorateFieldV2(this.prototype, "isLoading", [tracked], function () {
380
+ return true;
381
+ });
382
+ }
383
+ #isLoading = (initializeDeferredDecorator(this, "isLoading"), void 0);
384
+ static {
385
+ decorateFieldV2(this.prototype, "isSuccess", [tracked], function () {
386
+ return false;
387
+ });
388
+ }
389
+ #isSuccess = (initializeDeferredDecorator(this, "isSuccess"), void 0);
390
+ static {
391
+ decorateFieldV2(this.prototype, "isError", [tracked], function () {
392
+ return false;
393
+ });
394
+ }
395
+ #isError = (initializeDeferredDecorator(this, "isError"), void 0);
396
+ static {
397
+ decorateFieldV2(this.prototype, "request", [tracked], function () {
398
+ return null;
399
+ });
400
+ }
401
+ #request_ = (initializeDeferredDecorator(this, "request"), void 0);
402
+ static {
403
+ decorateFieldV2(this.prototype, "response", [tracked], function () {
404
+ return null;
405
+ });
406
+ }
407
+ #response = (initializeDeferredDecorator(this, "response"), void 0);
408
+ get isCancelled() {
409
+ return this.isError && isAbortError(this.error);
410
+ }
411
+ get loadingState() {
412
+ if (!this.#loadingState) {
413
+ this.#loadingState = new RequestLoadingState(this.#request);
414
+ }
415
+ return this.#loadingState;
416
+ }
417
+ constructor(future) {
418
+ this.#request = future;
419
+ const state = getPromiseResult(future);
420
+ if (state) {
421
+ this.request = state.result.request;
422
+ this.response = state.result.response;
423
+ this.isLoading = false;
424
+ if (state.isError) {
425
+ this.error = state.result;
426
+ this.isError = true;
427
+ } else {
428
+ this.result = state.result.content;
429
+ this.isSuccess = true;
430
+ }
431
+ } else {
432
+ void future.then(result => {
433
+ setPromiseResult(future, {
434
+ isError: false,
435
+ result
436
+ });
437
+ this.result = result.content;
438
+ this.isSuccess = true;
439
+ this.isLoading = false;
440
+ this.request = result.request;
441
+ this.response = result.response;
442
+ }, error => {
443
+ setPromiseResult(future, {
444
+ isError: true,
445
+ result: error
446
+ });
447
+ this.error = error;
448
+ this.isError = true;
449
+ this.isLoading = false;
450
+ this.request = error.request;
451
+ this.response = error.response;
452
+ });
453
+ }
454
+ }
455
+ }
456
+ function getRequestState(future) {
457
+ let state = RequestCache.get(future);
458
+ if (!state) {
459
+ state = new RequestState(future);
460
+ RequestCache.set(future, state);
461
+ }
462
+ return state;
463
+ }
464
+ const PromiseCache = new WeakMap();
465
+ class PromiseState {
466
+ static {
467
+ decorateFieldV2(this.prototype, "result", [tracked], function () {
468
+ return null;
469
+ });
470
+ }
471
+ #result = (initializeDeferredDecorator(this, "result"), void 0);
472
+ static {
473
+ decorateFieldV2(this.prototype, "error", [tracked], function () {
474
+ return null;
475
+ });
476
+ }
477
+ #error = (initializeDeferredDecorator(this, "error"), void 0);
478
+ static {
479
+ decorateFieldV2(this.prototype, "isPending", [tracked], function () {
480
+ return true;
481
+ });
482
+ }
483
+ #isPending = (initializeDeferredDecorator(this, "isPending"), void 0);
484
+ static {
485
+ decorateFieldV2(this.prototype, "isSuccess", [tracked], function () {
486
+ return false;
487
+ });
488
+ }
489
+ #isSuccess = (initializeDeferredDecorator(this, "isSuccess"), void 0);
490
+ static {
491
+ decorateFieldV2(this.prototype, "isError", [tracked], function () {
492
+ return false;
493
+ });
494
+ }
495
+ #isError = (initializeDeferredDecorator(this, "isError"), void 0);
496
+ constructor(promise) {
497
+ const state = getPromiseResult(promise);
498
+ if (state) {
499
+ if (state.isError) {
500
+ this.error = state.result;
501
+ this.isError = true;
502
+ this.isPending = false;
503
+ } else {
504
+ this.result = state.result;
505
+ this.isSuccess = true;
506
+ this.isPending = false;
507
+ }
508
+ } else {
509
+ void promise.then(result => {
510
+ setPromiseResult(promise, {
511
+ isError: false,
512
+ result
513
+ });
514
+ this.result = result;
515
+ this.isSuccess = true;
516
+ this.isPending = false;
517
+ }, error => {
518
+ setPromiseResult(promise, {
519
+ isError: true,
520
+ result: error
521
+ });
522
+ this.error = error;
523
+ this.isError = true;
524
+ this.isPending = false;
525
+ });
526
+ }
527
+ }
528
+ }
529
+ const LegacyPromiseProxy = Symbol.for('LegacyPromiseProxy');
530
+ function isLegacyAwaitable(promise) {
531
+ return LegacyPromiseProxy in promise && 'promise' in promise && promise[LegacyPromiseProxy] === true;
532
+ }
533
+ function getPromise(promise) {
534
+ return isLegacyAwaitable(promise) ? promise.promise : promise;
535
+ }
536
+ function getPromiseState(promise) {
537
+ const _promise = getPromise(promise);
538
+ let state = PromiseCache.get(_promise);
539
+ if (!state) {
540
+ state = new PromiseState(_promise);
541
+ PromiseCache.set(_promise, state);
542
+ }
543
+ return state;
544
+ }
545
+ const and = (x1, y1) => Boolean(x1 && y1);
546
+ class Throw extends Component {
547
+ constructor(owner1, args1) {
548
+ super(owner1, args1);
549
+ // this error is opaque (user supplied) so we don't validate it
550
+ // as an Error instance.
551
+ // eslint-disable-next-line @typescript-eslint/no-throw-literal
552
+ throw this.args.error;
553
+ }
554
+ static {
555
+ setComponentTemplate(precompileTemplate("", {
556
+ strictMode: true
557
+ }), this);
558
+ }
559
+ }
560
+ class Await extends Component {
561
+ get state() {
562
+ return getPromiseState(this.args.promise);
563
+ }
564
+ get error() {
565
+ return this.state.error;
566
+ }
567
+ get result() {
568
+ return this.state.result;
569
+ }
570
+ static {
571
+ setComponentTemplate(precompileTemplate("\n {{#if this.state.isPending}}\n {{yield to=\"pending\"}}\n {{else if (and this.state.isError (has-block \"error\"))}}\n {{yield this.error to=\"error\"}}\n {{else if this.state.isSuccess}}\n {{yield this.result to=\"success\"}}\n {{else}}\n <Throw @error={{this.error}} />\n {{/if}}\n ", {
572
+ strictMode: true,
573
+ scope: () => ({
574
+ and,
575
+ Throw
576
+ })
577
+ }), this);
578
+ }
579
+ }
580
+ function notNull(x1) {
581
+ macroCondition(getGlobalConfig().WarpDrive.env.DEBUG) ? (test => {
582
+ if (!test) {
583
+ throw new Error('Expected a non-null value, but got null');
584
+ }
585
+ })(x1 !== null) : {};
586
+ return x1;
587
+ }
588
+ const not = x1 => !x1;
589
+ // default to 30 seconds unavailable before we refresh
590
+ const DEFAULT_DEADLINE = 30_000;
591
+ const IdleBlockMissingError = new Error('No idle block provided for <Request> component, and no query or request was provided.');
592
+ let consume = service;
593
+ if (macroCondition(moduleExists('ember-provide-consume-context'))) {
594
+ const {
595
+ consume: contextConsume
596
+ } = importSync('ember-provide-consume-context');
597
+ consume = contextConsume;
598
+ }
599
+ function isNeverString(val1) {
600
+ return val1;
601
+ }
602
+ /**
603
+ * The `<Request>` component is a powerful tool for managing data fetching and
604
+ * state in your Ember application. It provides declarative reactive control-flow
605
+ * for managing requests and state in your application.
606
+ *
607
+ * @typedoc
608
+ */
609
+ class Request extends Component {
610
+ static {
611
+ decorateFieldV2(this.prototype, "_store", [consume('store')]);
612
+ }
613
+ #_store = (initializeDeferredDecorator(this, "_store"), void 0);
614
+ /**
615
+ * The store instance to use for making requests. If contexts are available, this
616
+ * will be the `store` on the context, else it will be the store service.
617
+ *
618
+ * @internal
619
+ */
620
+ static {
621
+ decorateFieldV2(this.prototype, "isOnline", [tracked], function () {
622
+ return true;
623
+ });
624
+ }
625
+ #isOnline = (initializeDeferredDecorator(this, "isOnline"), void 0);
626
+ /**
627
+ * Whether the browser reports that the network is online.
628
+ *
629
+ * @internal
630
+ */
631
+ static {
632
+ decorateFieldV2(this.prototype, "isHidden", [tracked], function () {
633
+ return true;
634
+ });
635
+ }
636
+ #isHidden = (initializeDeferredDecorator(this, "isHidden"), void 0);
637
+ /**
638
+ * Whether the browser reports that the tab is hidden.
639
+ *
640
+ * @internal
641
+ */
642
+ static {
643
+ decorateFieldV2(this.prototype, "isRefreshing", [tracked], function () {
644
+ return false;
645
+ });
646
+ }
647
+ #isRefreshing = (initializeDeferredDecorator(this, "isRefreshing"), void 0);
648
+ /**
649
+ * Whether the component is currently refreshing the request.
650
+ *
651
+ * @internal
652
+ */
653
+ static {
654
+ decorateFieldV2(this.prototype, "_localRequest", [tracked]);
655
+ }
656
+ #_localRequest = (initializeDeferredDecorator(this, "_localRequest"), void 0);
657
+ /**
658
+ * The most recent blocking request that was made, typically
659
+ * the result of a reload.
660
+ *
661
+ * This will never be the original request passed as an arg to
662
+ * the component.
663
+ *
664
+ * @internal
665
+ */
666
+ static {
667
+ decorateFieldV2(this.prototype, "_latestRequest", [tracked]);
668
+ }
669
+ #_latestRequest = (initializeDeferredDecorator(this, "_latestRequest"), void 0);
670
+ /**
671
+ * The most recent request that was made, typically due to either a
672
+ * reload or a refresh.
673
+ *
674
+ * This will never be the original request passed as an arg to
675
+ * the component.
676
+ *
677
+ * @internal
678
+ */
679
+ /**
680
+ * The time at which the network was reported as offline.
681
+ *
682
+ * @internal
683
+ */
684
+ unavailableStart;
685
+ intervalStart;
686
+ nextInterval;
687
+ invalidated;
688
+ isUpdating;
689
+ /**
690
+ * The event listener for network status changes,
691
+ * cached to use the reference for removal.
692
+ *
693
+ * @internal
694
+ */
695
+ onlineChanged;
696
+ /**
697
+ * The event listener for visibility status changes,
698
+ * cached to use the reference for removal.
699
+ *
700
+ * @internal
701
+ */
702
+ backgroundChanged;
703
+ /**
704
+ * The last request passed as an arg to the component,
705
+ * cached for comparison.
706
+ *
707
+ * @internal
708
+ */
709
+ _originalRequest;
710
+ /**
711
+ * The last query passed as an arg to the component,
712
+ * cached for comparison.
713
+ *
714
+ * @internal
715
+ */
716
+ _originalQuery;
717
+ _subscription;
718
+ _subscribedTo;
719
+ constructor(owner1, args1) {
720
+ super(owner1, args1);
721
+ this._subscribedTo = null;
722
+ this._subscription = null;
723
+ this.intervalStart = null;
724
+ this.invalidated = false;
725
+ this.nextInterval = null;
726
+ this.installListeners();
727
+ void this.beginPolling();
728
+ }
729
+ async beginPolling() {
730
+ // await the initial request
731
+ try {
732
+ await this.request;
733
+ } catch {
734
+ // ignore errors here, we just want to wait for the request to finish
735
+ } finally {
736
+ if (!this.isDestroyed) {
737
+ void this.scheduleInterval();
738
+ }
739
+ }
740
+ }
741
+ get isIdle() {
742
+ const {
743
+ request: request1,
744
+ query: query1
745
+ } = this.args;
746
+ return Boolean(!request1 && !query1);
747
+ }
748
+ static {
749
+ decorateMethodV2(this.prototype, "isIdle", [cached]);
750
+ }
751
+ get autorefreshTypes() {
752
+ const {
753
+ autorefresh: autorefresh1
754
+ } = this.args;
755
+ let types1;
756
+ if (autorefresh1 === true) {
757
+ types1 = ['online', 'invalid'];
758
+ } else if (typeof autorefresh1 === 'string') {
759
+ types1 = autorefresh1.split(',');
760
+ } else {
761
+ types1 = [];
762
+ }
763
+ return new Set(types1);
764
+ }
765
+ // we only run this function on component creation
766
+ // and when an update is triggered, so it does not
767
+ // react to changes in the autorefreshThreshold
768
+ // or autorefresh args.
769
+ //
770
+ // if we need to react to those changes, we can
771
+ // use a modifier or internal component or some
772
+ // such to trigger a re-run of this function.
773
+ static {
774
+ decorateMethodV2(this.prototype, "autorefreshTypes", [cached]);
775
+ }
776
+ async scheduleInterval() {
777
+ const {
778
+ autorefreshThreshold: autorefreshThreshold1
779
+ } = this.args;
780
+ const hasValidThreshold1 = typeof autorefreshThreshold1 === 'number' && autorefreshThreshold1 > 0;
781
+ if (
782
+ // dont schedule in SSR
783
+ typeof window === 'undefined' ||
784
+ // dont schedule without a threshold
785
+ !hasValidThreshold1 ||
786
+ // dont schedule if we weren't told to
787
+ !this.autorefreshTypes.has('interval') ||
788
+ // dont schedule if we're already scheduled
789
+ this.intervalStart !== null) {
790
+ return;
791
+ }
792
+ // if we have a current request, wait for it to finish
793
+ // before scheduling the next one
794
+ if (this._latestRequest) {
795
+ try {
796
+ await this._latestRequest;
797
+ } catch {
798
+ // ignore errors here, we just want to wait for the request to finish
799
+ }
800
+ if (this.isDestroyed) {
801
+ return;
802
+ }
803
+ }
804
+ // setup the next interval
805
+ this.intervalStart = Date.now();
806
+ this.nextInterval = setTimeout(() => {
807
+ this.maybeUpdate();
808
+ }, autorefreshThreshold1);
809
+ }
810
+ clearInterval() {
811
+ if (this.nextInterval) {
812
+ clearTimeout(this.nextInterval);
813
+ this.intervalStart = null;
814
+ }
815
+ }
816
+ updateSubscriptions() {
817
+ if (this.isIdle) {
818
+ return;
819
+ }
820
+ const requestId1 = this._request.lid;
821
+ // if we're already subscribed to this request, we don't need to do anything
822
+ if (this._subscribedTo === requestId1) {
823
+ return;
824
+ }
825
+ // if we're subscribed to a different request, we need to unsubscribe
826
+ this.removeSubscriptions();
827
+ // if we have a request, we need to subscribe to it
828
+ if (requestId1) {
829
+ this._subscribedTo = requestId1;
830
+ this._subscription = this.store.notifications.subscribe(requestId1, (_id1, op1) => {
831
+ // ignore subscription events that occur while our own component's request
832
+ // is ocurring
833
+ if (this.isUpdating) {
834
+ return;
835
+ }
836
+ switch (op1) {
837
+ case 'invalidated':
838
+ {
839
+ // if we're subscribed to invalidations, we need to update
840
+ if (this.autorefreshTypes.has('invalid')) {
841
+ this.invalidated = true;
842
+ this.maybeUpdate();
843
+ }
844
+ break;
845
+ }
846
+ case 'state':
847
+ {
848
+ const latest1 = this.store.requestManager._deduped.get(requestId1);
849
+ const priority1 = latest1?.priority;
850
+ if (!priority1) {
851
+ this.isRefreshing = false;
852
+ } else if (priority1.blocking) {
853
+ // TODO should we just treat this as refreshing?
854
+ this.isRefreshing = false;
855
+ this.maybeUpdate('policy', true);
856
+ } else {
857
+ this.isRefreshing = true;
858
+ }
859
+ }
860
+ }
861
+ });
862
+ }
863
+ }
864
+ removeSubscriptions() {
865
+ if (this._subscription) {
866
+ this.store.notifications.unsubscribe(this._subscription);
867
+ this._subscribedTo = null;
868
+ this._subscription = null;
869
+ }
870
+ }
871
+ /**
872
+ * Install the event listeners for network and visibility changes.
873
+ * This is only done in browser environments with a global `window`.
874
+ *
875
+ * @internal
876
+ */
877
+ installListeners() {
878
+ if (typeof window === 'undefined') {
879
+ return;
880
+ }
881
+ this.isOnline = window.navigator.onLine;
882
+ this.unavailableStart = this.isOnline ? null : Date.now();
883
+ this.isHidden = document.visibilityState === 'hidden';
884
+ this.onlineChanged = event1 => {
885
+ this.isOnline = event1.type === 'online';
886
+ if (event1.type === 'offline' && this.unavailableStart === null) {
887
+ this.unavailableStart = Date.now();
888
+ }
889
+ this.maybeUpdate();
890
+ };
891
+ this.backgroundChanged = () => {
892
+ const isHidden1 = document.visibilityState === 'hidden';
893
+ this.isHidden = isHidden1;
894
+ if (isHidden1 && this.unavailableStart === null) {
895
+ this.unavailableStart = Date.now();
896
+ }
897
+ this.maybeUpdate();
898
+ };
899
+ window.addEventListener('online', this.onlineChanged, {
900
+ passive: true,
901
+ capture: true
902
+ });
903
+ window.addEventListener('offline', this.onlineChanged, {
904
+ passive: true,
905
+ capture: true
906
+ });
907
+ document.addEventListener('visibilitychange', this.backgroundChanged, {
908
+ passive: true,
909
+ capture: true
910
+ });
911
+ }
912
+ /**
913
+ * If the network is online and the tab is visible, either reload or refresh the request
914
+ * based on the component's configuration and the requested update mode.
915
+ *
916
+ * Valid modes are:
917
+ *
918
+ * - `'reload'`: Force a reload of the request.
919
+ * - `'refresh'`: Refresh the request in the background.
920
+ * - `'policy'`: Make the request, letting the store's configured CachePolicy decide whether to reload, refresh, or do nothing.
921
+ * - `undefined`: Make the request using the component's autorefreshBehavior setting if the autorefreshThreshold has passed.
922
+ *
923
+ * @internal
924
+ */
925
+ maybeUpdate(mode1, silent1) {
926
+ if (this.isIdle) {
927
+ return;
928
+ }
929
+ const canAttempt1 = Boolean(this.isOnline && !this.isHidden && (mode1 || this.autorefreshTypes.size));
930
+ if (!canAttempt1) {
931
+ if (!silent1 && mode1 && mode1 !== 'invalidated') {
932
+ throw new Error(`Reload not available: the network is not online or the tab is hidden`);
933
+ }
934
+ return;
935
+ }
936
+ const {
937
+ autorefreshTypes: autorefreshTypes1
938
+ } = this;
939
+ let shouldAttempt1 = this.invalidated || Boolean(mode1);
940
+ if (!shouldAttempt1 && autorefreshTypes1.has('online')) {
941
+ const {
942
+ unavailableStart: unavailableStart1
943
+ } = this;
944
+ const {
945
+ autorefreshThreshold: autorefreshThreshold1
946
+ } = this.args;
947
+ const deadline1 = typeof autorefreshThreshold1 === 'number' ? autorefreshThreshold1 : DEFAULT_DEADLINE;
948
+ shouldAttempt1 = Boolean(unavailableStart1 && Date.now() - unavailableStart1 > deadline1);
949
+ }
950
+ if (!shouldAttempt1 && autorefreshTypes1.has('interval')) {
951
+ const {
952
+ intervalStart: intervalStart1
953
+ } = this;
954
+ const {
955
+ autorefreshThreshold: autorefreshThreshold1
956
+ } = this.args;
957
+ if (intervalStart1 && typeof autorefreshThreshold1 === 'number' && autorefreshThreshold1 > 0) {
958
+ shouldAttempt1 = Boolean(Date.now() - intervalStart1 >= autorefreshThreshold1);
959
+ }
960
+ }
961
+ this.unavailableStart = null;
962
+ this.invalidated = false;
963
+ if (shouldAttempt1) {
964
+ this.clearInterval();
965
+ const request1 = Object.assign({}, this.reqState.request);
966
+ const realMode1 = mode1 === 'invalidated' ? null : mode1;
967
+ const val1 = realMode1 ?? this.args.autorefreshBehavior ?? 'policy';
968
+ switch (val1) {
969
+ case 'reload':
970
+ request1.cacheOptions = Object.assign({}, request1.cacheOptions, {
971
+ reload: true
972
+ });
973
+ break;
974
+ case 'refresh':
975
+ request1.cacheOptions = Object.assign({}, request1.cacheOptions, {
976
+ backgroundReload: true
977
+ });
978
+ break;
979
+ case 'policy':
980
+ break;
981
+ default:
982
+ throw new Error(`Invalid ${mode1 ? 'update mode' : '@autorefreshBehavior'} for <Request />: ${isNeverString(val1)}`);
983
+ }
984
+ const wasStoreRequest1 = request1[EnableHydration] === true;
985
+ macroCondition(getGlobalConfig().WarpDrive.env.DEBUG) ? (test => {
986
+ if (!test) {
987
+ throw new Error(`Cannot supply a different store via context than was used to create the request`);
988
+ }
989
+ })(!request1.store || request1.store === this.store) : {};
990
+ this.isUpdating = true;
991
+ this._latestRequest = wasStoreRequest1 ? this.store.request(request1) : this.store.requestManager.request(request1);
992
+ if (val1 !== 'refresh') {
993
+ this._localRequest = this._latestRequest;
994
+ }
995
+ void this.scheduleInterval();
996
+ void this._latestRequest.finally(() => {
997
+ this.isUpdating = false;
998
+ });
999
+ }
1000
+ }
1001
+ /**
1002
+ * Retry the request, reloading it from the server.
1003
+ *
1004
+ * @internal
1005
+ */
1006
+ retry = async () => {
1007
+ this.maybeUpdate('reload');
1008
+ await this._localRequest;
1009
+ };
1010
+ /**
1011
+ * Refresh the request, updating it in the background.
1012
+ *
1013
+ * @internal
1014
+ */
1015
+ refresh = async () => {
1016
+ this.maybeUpdate('refresh');
1017
+ await this._latestRequest;
1018
+ };
1019
+ get errorFeatures() {
1020
+ return {
1021
+ isHidden: this.isHidden,
1022
+ isOnline: this.isOnline,
1023
+ retry: this.retry
1024
+ };
1025
+ }
1026
+ static {
1027
+ decorateMethodV2(this.prototype, "errorFeatures", [cached]);
1028
+ }
1029
+ get contentFeatures() {
1030
+ const feat1 = {
1031
+ isHidden: this.isHidden,
1032
+ isOnline: this.isOnline,
1033
+ reload: this.retry,
1034
+ refresh: this.refresh,
1035
+ isRefreshing: this.isRefreshing,
1036
+ latestRequest: this._latestRequest
1037
+ };
1038
+ if (feat1.isRefreshing) {
1039
+ feat1.abort = () => {
1040
+ this._latestRequest?.abort();
1041
+ };
1042
+ }
1043
+ return feat1;
1044
+ }
1045
+ static {
1046
+ decorateMethodV2(this.prototype, "contentFeatures", [cached]);
1047
+ }
1048
+ willDestroy() {
1049
+ this.removeSubscriptions();
1050
+ if (typeof window === 'undefined') {
1051
+ return;
1052
+ }
1053
+ this.clearInterval();
1054
+ window.removeEventListener('online', this.onlineChanged, {
1055
+ passive: true,
1056
+ capture: true
1057
+ });
1058
+ window.removeEventListener('offline', this.onlineChanged, {
1059
+ passive: true,
1060
+ capture: true
1061
+ });
1062
+ document.removeEventListener('visibilitychange', this.backgroundChanged, {
1063
+ passive: true,
1064
+ capture: true
1065
+ });
1066
+ }
1067
+ get _request() {
1068
+ const {
1069
+ request: request1,
1070
+ query: query1
1071
+ } = this.args;
1072
+ macroCondition(getGlobalConfig().WarpDrive.env.DEBUG) ? (test => {
1073
+ if (!test) {
1074
+ throw new Error(`Cannot use both @request and @query args with the <Request> component`);
1075
+ }
1076
+ })(!request1 || !query1) : {};
1077
+ const {
1078
+ _localRequest: _localRequest1,
1079
+ _originalRequest: _originalRequest1,
1080
+ _originalQuery: _originalQuery1
1081
+ } = this;
1082
+ const isOriginalRequest1 = request1 === _originalRequest1 && query1 === _originalQuery1;
1083
+ if (_localRequest1 && isOriginalRequest1) {
1084
+ return _localRequest1;
1085
+ }
1086
+ // update state checks for the next time
1087
+ this._originalQuery = query1;
1088
+ this._originalRequest = request1;
1089
+ if (request1) {
1090
+ return request1;
1091
+ }
1092
+ macroCondition(getGlobalConfig().WarpDrive.env.DEBUG) ? (test => {
1093
+ if (!test) {
1094
+ throw new Error(`You must provide either @request or an @query arg with the <Request> component`);
1095
+ }
1096
+ })(query1) : {};
1097
+ return this.store.request(query1);
1098
+ }
1099
+ static {
1100
+ decorateMethodV2(this.prototype, "_request", [cached]);
1101
+ }
1102
+ get request() {
1103
+ const request1 = this._request;
1104
+ this.updateSubscriptions();
1105
+ return request1;
1106
+ }
1107
+ static {
1108
+ decorateMethodV2(this.prototype, "request", [cached]);
1109
+ }
1110
+ get store() {
1111
+ const store1 = this.args.store || this._store;
1112
+ macroCondition(getGlobalConfig().WarpDrive.env.DEBUG) ? (test => {
1113
+ if (!test) {
1114
+ throw new Error(moduleExists('ember-provide-consume-context') ? `No store was provided to the <Request> component. Either provide a store via the @store arg or via the context API provided by ember-provide-consume-context.` : `No store was provided to the <Request> component. Either provide a store via the @store arg or by registering a store service.`);
1115
+ }
1116
+ })(store1) : {};
1117
+ return store1;
1118
+ }
1119
+ get reqState() {
1120
+ return getRequestState(this.request);
1121
+ }
1122
+ get result() {
1123
+ return this.reqState.result;
1124
+ }
1125
+ static {
1126
+ setComponentTemplate(precompileTemplate("\n {{#if (and this.isIdle (has-block \"idle\"))}}\n {{yield to=\"idle\"}}\n {{else if this.isIdle}}\n <Throw @error={{IdleBlockMissingError}} />\n {{else if this.reqState.isLoading}}\n {{yield this.reqState.loadingState to=\"loading\"}}\n {{else if (and this.reqState.isCancelled (has-block \"cancelled\"))}}\n {{yield (notNull this.reqState.error) this.errorFeatures to=\"cancelled\"}}\n {{else if (and this.reqState.isError (has-block \"error\"))}}\n {{yield (notNull this.reqState.error) this.errorFeatures to=\"error\"}}\n {{else if this.reqState.isSuccess}}\n {{yield this.result this.contentFeatures to=\"content\"}}\n {{else if (not this.reqState.isCancelled)}}\n <Throw @error={{(notNull this.reqState.error)}} />\n {{/if}}\n {{yield this.reqState to=\"always\"}}\n ", {
1127
+ strictMode: true,
1128
+ scope: () => ({
1129
+ and,
1130
+ Throw,
1131
+ IdleBlockMissingError,
1132
+ notNull,
1133
+ not
1134
+ })
1135
+ }), this);
1136
+ }
1137
+ }
1138
+ export { Await, Request, Throw, getPromiseState, getRequestState };