@warp-drive/ember 0.0.0-alpha.6 → 0.0.0-alpha.61

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,1090 @@
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
+ let consume = service;
592
+ if (macroCondition(moduleExists('ember-provide-consume-context'))) {
593
+ const {
594
+ consume: contextConsume
595
+ } = importSync('ember-provide-consume-context');
596
+ consume = contextConsume;
597
+ }
598
+ function isNeverString(val1) {
599
+ return val1;
600
+ }
601
+ /**
602
+ * The `<Request>` component is a powerful tool for managing data fetching and
603
+ * state in your Ember application. It provides declarative reactive control-flow
604
+ * for managing requests and state in your application.
605
+ *
606
+ * @typedoc
607
+ */
608
+ class Request extends Component {
609
+ static {
610
+ decorateFieldV2(this.prototype, "_store", [consume('store')]);
611
+ }
612
+ #_store = (initializeDeferredDecorator(this, "_store"), void 0);
613
+ /**
614
+ * The store instance to use for making requests. If contexts are available, this
615
+ * will be the `store` on the context, else it will be the store service.
616
+ *
617
+ * @internal
618
+ */
619
+ static {
620
+ decorateFieldV2(this.prototype, "isOnline", [tracked], function () {
621
+ return true;
622
+ });
623
+ }
624
+ #isOnline = (initializeDeferredDecorator(this, "isOnline"), void 0);
625
+ /**
626
+ * Whether the browser reports that the network is online.
627
+ *
628
+ * @internal
629
+ */
630
+ static {
631
+ decorateFieldV2(this.prototype, "isHidden", [tracked], function () {
632
+ return true;
633
+ });
634
+ }
635
+ #isHidden = (initializeDeferredDecorator(this, "isHidden"), void 0);
636
+ /**
637
+ * Whether the browser reports that the tab is hidden.
638
+ *
639
+ * @internal
640
+ */
641
+ static {
642
+ decorateFieldV2(this.prototype, "isRefreshing", [tracked], function () {
643
+ return false;
644
+ });
645
+ }
646
+ #isRefreshing = (initializeDeferredDecorator(this, "isRefreshing"), void 0);
647
+ /**
648
+ * Whether the component is currently refreshing the request.
649
+ *
650
+ * @internal
651
+ */
652
+ static {
653
+ decorateFieldV2(this.prototype, "_localRequest", [tracked]);
654
+ }
655
+ #_localRequest = (initializeDeferredDecorator(this, "_localRequest"), void 0);
656
+ /**
657
+ * The most recent blocking request that was made, typically
658
+ * the result of a reload.
659
+ *
660
+ * This will never be the original request passed as an arg to
661
+ * the component.
662
+ *
663
+ * @internal
664
+ */
665
+ static {
666
+ decorateFieldV2(this.prototype, "_latestRequest", [tracked]);
667
+ }
668
+ #_latestRequest = (initializeDeferredDecorator(this, "_latestRequest"), void 0);
669
+ /**
670
+ * The most recent request that was made, typically due to either a
671
+ * reload or a refresh.
672
+ *
673
+ * This will never be the original request passed as an arg to
674
+ * the component.
675
+ *
676
+ * @internal
677
+ */
678
+ /**
679
+ * The time at which the network was reported as offline.
680
+ *
681
+ * @internal
682
+ */
683
+ unavailableStart;
684
+ intervalStart;
685
+ nextInterval;
686
+ invalidated;
687
+ /**
688
+ * The event listener for network status changes,
689
+ * cached to use the reference for removal.
690
+ *
691
+ * @internal
692
+ */
693
+ onlineChanged;
694
+ /**
695
+ * The event listener for visibility status changes,
696
+ * cached to use the reference for removal.
697
+ *
698
+ * @internal
699
+ */
700
+ backgroundChanged;
701
+ /**
702
+ * The last request passed as an arg to the component,
703
+ * cached for comparison.
704
+ *
705
+ * @internal
706
+ */
707
+ _originalRequest;
708
+ /**
709
+ * The last query passed as an arg to the component,
710
+ * cached for comparison.
711
+ *
712
+ * @internal
713
+ */
714
+ _originalQuery;
715
+ _subscription;
716
+ _subscribedTo;
717
+ constructor(owner1, args1) {
718
+ super(owner1, args1);
719
+ this._subscribedTo = null;
720
+ this._subscription = null;
721
+ this.intervalStart = null;
722
+ this.invalidated = false;
723
+ this.nextInterval = null;
724
+ this.installListeners();
725
+ this.updateSubscriptions();
726
+ void this.scheduleInterval();
727
+ }
728
+ get autorefreshTypes() {
729
+ const {
730
+ autorefresh: autorefresh1
731
+ } = this.args;
732
+ let types1;
733
+ if (autorefresh1 === true) {
734
+ types1 = ['online', 'invalid'];
735
+ } else if (typeof autorefresh1 === 'string') {
736
+ types1 = autorefresh1.split(',');
737
+ } else {
738
+ types1 = [];
739
+ }
740
+ return new Set(types1);
741
+ }
742
+ // we only run this function on component creation
743
+ // and when an update is triggered, so it does not
744
+ // react to changes in the autorefreshThreshold
745
+ // or autorefresh args.
746
+ //
747
+ // if we need to react to those changes, we can
748
+ // use a modifier or internal component or some
749
+ // such to trigger a re-run of this function.
750
+ static {
751
+ decorateMethodV2(this.prototype, "autorefreshTypes", [cached]);
752
+ }
753
+ async scheduleInterval() {
754
+ const {
755
+ autorefreshThreshold: autorefreshThreshold1
756
+ } = this.args;
757
+ const hasValidThreshold1 = typeof autorefreshThreshold1 === 'number' && autorefreshThreshold1 > 0;
758
+ if (
759
+ // dont schedule in SSR
760
+ typeof window === 'undefined' ||
761
+ // dont schedule without a threshold
762
+ !hasValidThreshold1 ||
763
+ // dont schedule if we weren't told to
764
+ !this.autorefreshTypes.has('interval') ||
765
+ // dont schedule if we're already scheduled
766
+ this.intervalStart !== null) {
767
+ return;
768
+ }
769
+ // if we have a current request, wait for it to finish
770
+ // before scheduling the next one
771
+ if (this._latestRequest) {
772
+ try {
773
+ await this._latestRequest;
774
+ } catch {
775
+ // ignore errors here, we just want to wait for the request to finish
776
+ }
777
+ if (this.isDestroyed) {
778
+ return;
779
+ }
780
+ }
781
+ // setup the next interval
782
+ this.intervalStart = Date.now();
783
+ this.nextInterval = setTimeout(() => {
784
+ this.maybeUpdate();
785
+ }, autorefreshThreshold1);
786
+ }
787
+ clearInterval() {
788
+ if (this.nextInterval) {
789
+ clearTimeout(this.nextInterval);
790
+ this.intervalStart = null;
791
+ }
792
+ }
793
+ updateSubscriptions() {
794
+ const requestId1 = this.request.lid;
795
+ // if we're already subscribed to this request, we don't need to do anything
796
+ if (this._subscribedTo === requestId1) {
797
+ return;
798
+ }
799
+ // if we're subscribed to a different request, we need to unsubscribe
800
+ this.removeSubscriptions();
801
+ // if we have a request, we need to subscribe to it
802
+ if (requestId1) {
803
+ this._subscription = this.store.notifications.subscribe(requestId1, (_id1, op1) => {
804
+ switch (op1) {
805
+ case 'invalidated':
806
+ {
807
+ // if we're subscribed to invalidations, we need to update
808
+ if (this.autorefreshTypes.has('invalid')) {
809
+ this.invalidated = true;
810
+ this.maybeUpdate();
811
+ }
812
+ break;
813
+ }
814
+ case 'state':
815
+ {
816
+ const latest1 = this.store.requestManager._deduped.get(requestId1);
817
+ const priority1 = latest1?.priority;
818
+ if (!priority1) {
819
+ this.isRefreshing = false;
820
+ } else if (priority1.blocking) {
821
+ // TODO should we just treat this as refreshing?
822
+ this.isRefreshing = false;
823
+ this.maybeUpdate('policy', true);
824
+ } else {
825
+ this.isRefreshing = true;
826
+ }
827
+ }
828
+ }
829
+ });
830
+ }
831
+ }
832
+ removeSubscriptions() {
833
+ if (this._subscription) {
834
+ this.store.notifications.unsubscribe(this._subscription);
835
+ this._subscribedTo = null;
836
+ this._subscription = null;
837
+ }
838
+ }
839
+ /**
840
+ * Install the event listeners for network and visibility changes.
841
+ * This is only done in browser environments with a global `window`.
842
+ *
843
+ * @internal
844
+ */
845
+ installListeners() {
846
+ if (typeof window === 'undefined') {
847
+ return;
848
+ }
849
+ this.isOnline = window.navigator.onLine;
850
+ this.unavailableStart = this.isOnline ? null : Date.now();
851
+ this.isHidden = document.visibilityState === 'hidden';
852
+ this.onlineChanged = event1 => {
853
+ this.isOnline = event1.type === 'online';
854
+ if (event1.type === 'offline' && this.unavailableStart === null) {
855
+ this.unavailableStart = Date.now();
856
+ }
857
+ this.maybeUpdate();
858
+ };
859
+ this.backgroundChanged = () => {
860
+ const isHidden1 = document.visibilityState === 'hidden';
861
+ this.isHidden = isHidden1;
862
+ if (isHidden1 && this.unavailableStart === null) {
863
+ this.unavailableStart = Date.now();
864
+ }
865
+ this.maybeUpdate();
866
+ };
867
+ window.addEventListener('online', this.onlineChanged, {
868
+ passive: true,
869
+ capture: true
870
+ });
871
+ window.addEventListener('offline', this.onlineChanged, {
872
+ passive: true,
873
+ capture: true
874
+ });
875
+ document.addEventListener('visibilitychange', this.backgroundChanged, {
876
+ passive: true,
877
+ capture: true
878
+ });
879
+ }
880
+ /**
881
+ * If the network is online and the tab is visible, either reload or refresh the request
882
+ * based on the component's configuration and the requested update mode.
883
+ *
884
+ * Valid modes are:
885
+ *
886
+ * - `'reload'`: Force a reload of the request.
887
+ * - `'refresh'`: Refresh the request in the background.
888
+ * - `'policy'`: Make the request, letting the store's configured CachePolicy decide whether to reload, refresh, or do nothing.
889
+ * - `undefined`: Make the request using the component's autorefreshBehavior setting if the autorefreshThreshold has passed.
890
+ *
891
+ * @internal
892
+ */
893
+ maybeUpdate(mode1, silent1) {
894
+ const canAttempt1 = Boolean(this.isOnline && !this.isHidden && (mode1 || this.autorefreshTypes.size));
895
+ if (!canAttempt1) {
896
+ if (!silent1 && mode1 && mode1 !== 'invalidated') {
897
+ throw new Error(`Reload not available: the network is not online or the tab is hidden`);
898
+ }
899
+ return;
900
+ }
901
+ const {
902
+ autorefreshTypes: autorefreshTypes1
903
+ } = this;
904
+ let shouldAttempt1 = this.invalidated || Boolean(mode1);
905
+ if (!shouldAttempt1 && autorefreshTypes1.has('online')) {
906
+ const {
907
+ unavailableStart: unavailableStart1
908
+ } = this;
909
+ const {
910
+ autorefreshThreshold: autorefreshThreshold1
911
+ } = this.args;
912
+ const deadline1 = typeof autorefreshThreshold1 === 'number' ? autorefreshThreshold1 : DEFAULT_DEADLINE;
913
+ shouldAttempt1 = Boolean(unavailableStart1 && Date.now() - unavailableStart1 > deadline1);
914
+ }
915
+ if (!shouldAttempt1 && autorefreshTypes1.has('interval')) {
916
+ const {
917
+ intervalStart: intervalStart1
918
+ } = this;
919
+ const {
920
+ autorefreshThreshold: autorefreshThreshold1
921
+ } = this.args;
922
+ if (intervalStart1 && typeof autorefreshThreshold1 === 'number' && autorefreshThreshold1 > 0) {
923
+ shouldAttempt1 = Boolean(Date.now() - intervalStart1 > autorefreshThreshold1);
924
+ }
925
+ }
926
+ this.unavailableStart = null;
927
+ this.invalidated = false;
928
+ if (shouldAttempt1) {
929
+ this.clearInterval();
930
+ const request1 = Object.assign({}, this.reqState.request);
931
+ const realMode1 = mode1 === 'invalidated' ? null : mode1;
932
+ const val1 = realMode1 ?? this.args.autorefreshBehavior ?? 'policy';
933
+ switch (val1) {
934
+ case 'reload':
935
+ request1.cacheOptions = Object.assign({}, request1.cacheOptions, {
936
+ reload: true
937
+ });
938
+ break;
939
+ case 'refresh':
940
+ request1.cacheOptions = Object.assign({}, request1.cacheOptions, {
941
+ backgroundReload: true
942
+ });
943
+ break;
944
+ case 'policy':
945
+ break;
946
+ default:
947
+ throw new Error(`Invalid ${mode1 ? 'update mode' : '@autorefreshBehavior'} for <Request />: ${isNeverString(val1)}`);
948
+ }
949
+ const wasStoreRequest1 = request1[EnableHydration] === true;
950
+ macroCondition(getGlobalConfig().WarpDrive.env.DEBUG) ? (test => {
951
+ if (!test) {
952
+ throw new Error(`Cannot supply a different store via context than was used to create the request`);
953
+ }
954
+ })(!request1.store || request1.store === this.store) : {};
955
+ this._latestRequest = wasStoreRequest1 ? this.store.request(request1) : this.store.requestManager.request(request1);
956
+ if (val1 !== 'refresh') {
957
+ this._localRequest = this._latestRequest;
958
+ }
959
+ void this.scheduleInterval();
960
+ }
961
+ }
962
+ /**
963
+ * Retry the request, reloading it from the server.
964
+ *
965
+ * @internal
966
+ */
967
+ retry = async () => {
968
+ this.maybeUpdate('reload');
969
+ await this._localRequest;
970
+ };
971
+ /**
972
+ * Refresh the request, updating it in the background.
973
+ *
974
+ * @internal
975
+ */
976
+ refresh = async () => {
977
+ this.maybeUpdate('refresh');
978
+ await this._latestRequest;
979
+ };
980
+ get errorFeatures() {
981
+ return {
982
+ isHidden: this.isHidden,
983
+ isOnline: this.isOnline,
984
+ retry: this.retry
985
+ };
986
+ }
987
+ static {
988
+ decorateMethodV2(this.prototype, "errorFeatures", [cached]);
989
+ }
990
+ get contentFeatures() {
991
+ const feat1 = {
992
+ isHidden: this.isHidden,
993
+ isOnline: this.isOnline,
994
+ reload: this.retry,
995
+ refresh: this.refresh,
996
+ isRefreshing: this.isRefreshing,
997
+ latestRequest: this._latestRequest
998
+ };
999
+ if (feat1.isRefreshing) {
1000
+ feat1.abort = () => {
1001
+ this._latestRequest?.abort();
1002
+ };
1003
+ }
1004
+ return feat1;
1005
+ }
1006
+ static {
1007
+ decorateMethodV2(this.prototype, "contentFeatures", [cached]);
1008
+ }
1009
+ willDestroy() {
1010
+ this.removeSubscriptions();
1011
+ if (typeof window === 'undefined') {
1012
+ return;
1013
+ }
1014
+ this.clearInterval();
1015
+ window.removeEventListener('online', this.onlineChanged, {
1016
+ passive: true,
1017
+ capture: true
1018
+ });
1019
+ window.removeEventListener('offline', this.onlineChanged, {
1020
+ passive: true,
1021
+ capture: true
1022
+ });
1023
+ document.removeEventListener('visibilitychange', this.backgroundChanged, {
1024
+ passive: true,
1025
+ capture: true
1026
+ });
1027
+ }
1028
+ get request() {
1029
+ const {
1030
+ request: request1,
1031
+ query: query1
1032
+ } = this.args;
1033
+ macroCondition(getGlobalConfig().WarpDrive.env.DEBUG) ? (test => {
1034
+ if (!test) {
1035
+ throw new Error(`Cannot use both @request and @query args with the <Request> component`);
1036
+ }
1037
+ })(!request1 || !query1) : {};
1038
+ const {
1039
+ _localRequest: _localRequest1,
1040
+ _originalRequest: _originalRequest1,
1041
+ _originalQuery: _originalQuery1
1042
+ } = this;
1043
+ const isOriginalRequest1 = request1 === _originalRequest1 && query1 === _originalQuery1;
1044
+ if (_localRequest1 && isOriginalRequest1) {
1045
+ return _localRequest1;
1046
+ }
1047
+ // update state checks for the next time
1048
+ this._originalQuery = query1;
1049
+ this._originalRequest = request1;
1050
+ if (request1) {
1051
+ return request1;
1052
+ }
1053
+ macroCondition(getGlobalConfig().WarpDrive.env.DEBUG) ? (test => {
1054
+ if (!test) {
1055
+ throw new Error(`You must provide either @request or an @query arg with the <Request> component`);
1056
+ }
1057
+ })(query1) : {};
1058
+ return this.store.request(query1);
1059
+ }
1060
+ static {
1061
+ decorateMethodV2(this.prototype, "request", [cached]);
1062
+ }
1063
+ get store() {
1064
+ const store1 = this.args.store || this._store;
1065
+ macroCondition(getGlobalConfig().WarpDrive.env.DEBUG) ? (test => {
1066
+ if (!test) {
1067
+ 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.`);
1068
+ }
1069
+ })(store1) : {};
1070
+ return store1;
1071
+ }
1072
+ get reqState() {
1073
+ return getRequestState(this.request);
1074
+ }
1075
+ get result() {
1076
+ return this.reqState.result;
1077
+ }
1078
+ static {
1079
+ setComponentTemplate(precompileTemplate("\n {{#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 ", {
1080
+ strictMode: true,
1081
+ scope: () => ({
1082
+ and,
1083
+ notNull,
1084
+ not,
1085
+ Throw
1086
+ })
1087
+ }), this);
1088
+ }
1089
+ }
1090
+ export { Await, Request, Throw, getPromiseState, getRequestState };