@warp-drive/ember 0.0.0-beta.1 → 0.0.0-beta.10
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/README.md +13 -4
- package/dist/index.js +388 -148
- package/dist/index.js.map +1 -1
- package/package.json +43 -32
- package/unstable-preview-types/-private/await.d.ts.map +1 -1
- package/unstable-preview-types/-private/request.d.ts +120 -2
- package/unstable-preview-types/-private/request.d.ts.map +1 -1
- package/unstable-preview-types/index.d.ts +2 -2
package/dist/index.js
CHANGED
|
@@ -6,26 +6,7 @@ import { macroCondition, moduleExists, importSync, getGlobalConfig } from '@embr
|
|
|
6
6
|
import { EnableHydration } from '@warp-drive/core-types/request';
|
|
7
7
|
import { precompileTemplate } from '@ember/template-compilation';
|
|
8
8
|
import { setComponentTemplate } from '@ember/component';
|
|
9
|
-
|
|
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();
|
|
9
|
+
const deferred = /* @__PURE__ */new WeakMap();
|
|
29
10
|
function deferDecorator(proto, prop, desc) {
|
|
30
11
|
let map = deferred.get(proto);
|
|
31
12
|
if (!map) {
|
|
@@ -35,18 +16,16 @@ function deferDecorator(proto, prop, desc) {
|
|
|
35
16
|
map.set(prop, desc);
|
|
36
17
|
}
|
|
37
18
|
function findDeferredDecorator(target, prop) {
|
|
19
|
+
var _a;
|
|
38
20
|
let cursor = target.prototype;
|
|
39
21
|
while (cursor) {
|
|
40
|
-
let desc = deferred.get(cursor)
|
|
22
|
+
let desc = (_a = deferred.get(cursor)) == null ? void 0 : _a.get(prop);
|
|
41
23
|
if (desc) {
|
|
42
24
|
return desc;
|
|
43
25
|
}
|
|
44
26
|
cursor = cursor.prototype;
|
|
45
27
|
}
|
|
46
28
|
}
|
|
47
|
-
function decorateFieldV1(target, prop, decorators, initializer) {
|
|
48
|
-
return decorateFieldV2(target.prototype, prop, decorators, initializer);
|
|
49
|
-
}
|
|
50
29
|
function decorateFieldV2(prototype, prop, decorators, initializer) {
|
|
51
30
|
let desc = {
|
|
52
31
|
configurable: true,
|
|
@@ -66,11 +45,6 @@ function decorateFieldV2(prototype, prop, decorators, initializer) {
|
|
|
66
45
|
deferDecorator(prototype, prop, desc);
|
|
67
46
|
}
|
|
68
47
|
}
|
|
69
|
-
function decorateMethodV1({
|
|
70
|
-
prototype
|
|
71
|
-
}, prop, decorators) {
|
|
72
|
-
return decorateMethodV2(prototype, prop, decorators);
|
|
73
|
-
}
|
|
74
48
|
function decorateMethodV2(prototype, prop, decorators) {
|
|
75
49
|
const origDesc = Object.getOwnPropertyDescriptor(prototype, prop);
|
|
76
50
|
let desc = {
|
|
@@ -96,35 +70,6 @@ function initializeDeferredDecorator(target, prop) {
|
|
|
96
70
|
});
|
|
97
71
|
}
|
|
98
72
|
}
|
|
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
73
|
const RequestCache = new WeakMap();
|
|
129
74
|
function isAbortError(error) {
|
|
130
75
|
return error instanceof DOMException && error.name === 'AbortError';
|
|
@@ -542,10 +487,10 @@ function getPromiseState(promise) {
|
|
|
542
487
|
}
|
|
543
488
|
return state;
|
|
544
489
|
}
|
|
545
|
-
const and = (
|
|
490
|
+
const and = (x, y) => Boolean(x && y);
|
|
546
491
|
class Throw extends Component {
|
|
547
|
-
constructor(
|
|
548
|
-
super(
|
|
492
|
+
constructor(owner, args) {
|
|
493
|
+
super(owner, args);
|
|
549
494
|
// this error is opaque (user supplied) so we don't validate it
|
|
550
495
|
// as an Error instance.
|
|
551
496
|
// eslint-disable-next-line @typescript-eslint/no-throw-literal
|
|
@@ -577,33 +522,44 @@ class Await extends Component {
|
|
|
577
522
|
}), this);
|
|
578
523
|
}
|
|
579
524
|
}
|
|
580
|
-
function notNull(
|
|
525
|
+
function notNull(x) {
|
|
581
526
|
macroCondition(getGlobalConfig().WarpDrive.env.DEBUG) ? (test => {
|
|
582
527
|
if (!test) {
|
|
583
528
|
throw new Error('Expected a non-null value, but got null');
|
|
584
529
|
}
|
|
585
|
-
})(
|
|
586
|
-
return
|
|
530
|
+
})(x !== null) : {};
|
|
531
|
+
return x;
|
|
587
532
|
}
|
|
588
|
-
const not =
|
|
533
|
+
const not = x => !x;
|
|
589
534
|
// default to 30 seconds unavailable before we refresh
|
|
590
535
|
const DEFAULT_DEADLINE = 30_000;
|
|
591
|
-
|
|
536
|
+
const IdleBlockMissingError = new Error('No idle block provided for <Request> component, and no query or request was provided.');
|
|
537
|
+
let consume = service;
|
|
592
538
|
if (macroCondition(moduleExists('ember-provide-consume-context'))) {
|
|
593
539
|
const {
|
|
594
|
-
consume
|
|
540
|
+
consume: contextConsume
|
|
595
541
|
} = importSync('ember-provide-consume-context');
|
|
596
|
-
|
|
542
|
+
consume = contextConsume;
|
|
597
543
|
}
|
|
598
|
-
function isNeverString(
|
|
599
|
-
return
|
|
544
|
+
function isNeverString(val) {
|
|
545
|
+
return val;
|
|
600
546
|
}
|
|
547
|
+
/**
|
|
548
|
+
* The `<Request>` component is a powerful tool for managing data fetching and
|
|
549
|
+
* state in your Ember application. It provides declarative reactive control-flow
|
|
550
|
+
* for managing requests and state in your application.
|
|
551
|
+
*
|
|
552
|
+
* @typedoc
|
|
553
|
+
*/
|
|
601
554
|
class Request extends Component {
|
|
602
555
|
static {
|
|
603
|
-
decorateFieldV2(this.prototype, "_store", [
|
|
556
|
+
decorateFieldV2(this.prototype, "_store", [consume('store')]);
|
|
604
557
|
}
|
|
605
558
|
#_store = (initializeDeferredDecorator(this, "_store"), void 0);
|
|
606
559
|
/**
|
|
560
|
+
* The store instance to use for making requests. If contexts are available, this
|
|
561
|
+
* will be the `store` on the context, else it will be the store service.
|
|
562
|
+
*
|
|
607
563
|
* @internal
|
|
608
564
|
*/
|
|
609
565
|
static {
|
|
@@ -612,35 +568,255 @@ class Request extends Component {
|
|
|
612
568
|
});
|
|
613
569
|
}
|
|
614
570
|
#isOnline = (initializeDeferredDecorator(this, "isOnline"), void 0);
|
|
571
|
+
/**
|
|
572
|
+
* Whether the browser reports that the network is online.
|
|
573
|
+
*
|
|
574
|
+
* @internal
|
|
575
|
+
*/
|
|
615
576
|
static {
|
|
616
577
|
decorateFieldV2(this.prototype, "isHidden", [tracked], function () {
|
|
617
578
|
return true;
|
|
618
579
|
});
|
|
619
580
|
}
|
|
620
581
|
#isHidden = (initializeDeferredDecorator(this, "isHidden"), void 0);
|
|
582
|
+
/**
|
|
583
|
+
* Whether the browser reports that the tab is hidden.
|
|
584
|
+
*
|
|
585
|
+
* @internal
|
|
586
|
+
*/
|
|
621
587
|
static {
|
|
622
588
|
decorateFieldV2(this.prototype, "isRefreshing", [tracked], function () {
|
|
623
589
|
return false;
|
|
624
590
|
});
|
|
625
591
|
}
|
|
626
592
|
#isRefreshing = (initializeDeferredDecorator(this, "isRefreshing"), void 0);
|
|
593
|
+
/**
|
|
594
|
+
* Whether the component is currently refreshing the request.
|
|
595
|
+
*
|
|
596
|
+
* @internal
|
|
597
|
+
*/
|
|
627
598
|
static {
|
|
628
599
|
decorateFieldV2(this.prototype, "_localRequest", [tracked]);
|
|
629
600
|
}
|
|
630
601
|
#_localRequest = (initializeDeferredDecorator(this, "_localRequest"), void 0);
|
|
602
|
+
/**
|
|
603
|
+
* The most recent blocking request that was made, typically
|
|
604
|
+
* the result of a reload.
|
|
605
|
+
*
|
|
606
|
+
* This will never be the original request passed as an arg to
|
|
607
|
+
* the component.
|
|
608
|
+
*
|
|
609
|
+
* @internal
|
|
610
|
+
*/
|
|
631
611
|
static {
|
|
632
612
|
decorateFieldV2(this.prototype, "_latestRequest", [tracked]);
|
|
633
613
|
}
|
|
634
614
|
#_latestRequest = (initializeDeferredDecorator(this, "_latestRequest"), void 0);
|
|
615
|
+
/**
|
|
616
|
+
* The most recent request that was made, typically due to either a
|
|
617
|
+
* reload or a refresh.
|
|
618
|
+
*
|
|
619
|
+
* This will never be the original request passed as an arg to
|
|
620
|
+
* the component.
|
|
621
|
+
*
|
|
622
|
+
* @internal
|
|
623
|
+
*/
|
|
624
|
+
/**
|
|
625
|
+
* The time at which the network was reported as offline.
|
|
626
|
+
*
|
|
627
|
+
* @internal
|
|
628
|
+
*/
|
|
635
629
|
unavailableStart;
|
|
630
|
+
intervalStart;
|
|
631
|
+
nextInterval;
|
|
632
|
+
invalidated;
|
|
633
|
+
isUpdating;
|
|
634
|
+
/**
|
|
635
|
+
* The event listener for network status changes,
|
|
636
|
+
* cached to use the reference for removal.
|
|
637
|
+
*
|
|
638
|
+
* @internal
|
|
639
|
+
*/
|
|
636
640
|
onlineChanged;
|
|
641
|
+
/**
|
|
642
|
+
* The event listener for visibility status changes,
|
|
643
|
+
* cached to use the reference for removal.
|
|
644
|
+
*
|
|
645
|
+
* @internal
|
|
646
|
+
*/
|
|
637
647
|
backgroundChanged;
|
|
648
|
+
/**
|
|
649
|
+
* The last request passed as an arg to the component,
|
|
650
|
+
* cached for comparison.
|
|
651
|
+
*
|
|
652
|
+
* @internal
|
|
653
|
+
*/
|
|
638
654
|
_originalRequest;
|
|
655
|
+
/**
|
|
656
|
+
* The last query passed as an arg to the component,
|
|
657
|
+
* cached for comparison.
|
|
658
|
+
*
|
|
659
|
+
* @internal
|
|
660
|
+
*/
|
|
639
661
|
_originalQuery;
|
|
640
|
-
|
|
641
|
-
|
|
662
|
+
_subscription;
|
|
663
|
+
_subscribedTo;
|
|
664
|
+
constructor(owner, args) {
|
|
665
|
+
super(owner, args);
|
|
666
|
+
this._subscribedTo = null;
|
|
667
|
+
this._subscription = null;
|
|
668
|
+
this.intervalStart = null;
|
|
669
|
+
this.invalidated = false;
|
|
670
|
+
this.nextInterval = null;
|
|
642
671
|
this.installListeners();
|
|
672
|
+
void this.beginPolling();
|
|
673
|
+
}
|
|
674
|
+
async beginPolling() {
|
|
675
|
+
// await the initial request
|
|
676
|
+
try {
|
|
677
|
+
await this.request;
|
|
678
|
+
} catch {
|
|
679
|
+
// ignore errors here, we just want to wait for the request to finish
|
|
680
|
+
} finally {
|
|
681
|
+
if (!this.isDestroyed) {
|
|
682
|
+
void this.scheduleInterval();
|
|
683
|
+
}
|
|
684
|
+
}
|
|
685
|
+
}
|
|
686
|
+
get isIdle() {
|
|
687
|
+
const {
|
|
688
|
+
request,
|
|
689
|
+
query
|
|
690
|
+
} = this.args;
|
|
691
|
+
return Boolean(!request && !query);
|
|
692
|
+
}
|
|
693
|
+
static {
|
|
694
|
+
decorateMethodV2(this.prototype, "isIdle", [cached]);
|
|
695
|
+
}
|
|
696
|
+
get autorefreshTypes() {
|
|
697
|
+
const {
|
|
698
|
+
autorefresh
|
|
699
|
+
} = this.args;
|
|
700
|
+
let types;
|
|
701
|
+
if (autorefresh === true) {
|
|
702
|
+
types = ['online', 'invalid'];
|
|
703
|
+
} else if (typeof autorefresh === 'string') {
|
|
704
|
+
types = autorefresh.split(',');
|
|
705
|
+
} else {
|
|
706
|
+
types = [];
|
|
707
|
+
}
|
|
708
|
+
return new Set(types);
|
|
709
|
+
}
|
|
710
|
+
// we only run this function on component creation
|
|
711
|
+
// and when an update is triggered, so it does not
|
|
712
|
+
// react to changes in the autorefreshThreshold
|
|
713
|
+
// or autorefresh args.
|
|
714
|
+
//
|
|
715
|
+
// if we need to react to those changes, we can
|
|
716
|
+
// use a modifier or internal component or some
|
|
717
|
+
// such to trigger a re-run of this function.
|
|
718
|
+
static {
|
|
719
|
+
decorateMethodV2(this.prototype, "autorefreshTypes", [cached]);
|
|
720
|
+
}
|
|
721
|
+
async scheduleInterval() {
|
|
722
|
+
const {
|
|
723
|
+
autorefreshThreshold
|
|
724
|
+
} = this.args;
|
|
725
|
+
const hasValidThreshold = typeof autorefreshThreshold === 'number' && autorefreshThreshold > 0;
|
|
726
|
+
if (typeof window === 'undefined' ||
|
|
727
|
+
// dont schedule without a threshold
|
|
728
|
+
!hasValidThreshold ||
|
|
729
|
+
// dont schedule if we weren't told to
|
|
730
|
+
!this.autorefreshTypes.has('interval') ||
|
|
731
|
+
// dont schedule if we're already scheduled
|
|
732
|
+
this.intervalStart !== null) {
|
|
733
|
+
return;
|
|
734
|
+
}
|
|
735
|
+
// if we have a current request, wait for it to finish
|
|
736
|
+
// before scheduling the next one
|
|
737
|
+
if (this._latestRequest) {
|
|
738
|
+
try {
|
|
739
|
+
await this._latestRequest;
|
|
740
|
+
} catch {
|
|
741
|
+
// ignore errors here, we just want to wait for the request to finish
|
|
742
|
+
}
|
|
743
|
+
if (this.isDestroyed) {
|
|
744
|
+
return;
|
|
745
|
+
}
|
|
746
|
+
}
|
|
747
|
+
// setup the next interval
|
|
748
|
+
this.intervalStart = Date.now();
|
|
749
|
+
this.nextInterval = setTimeout(() => {
|
|
750
|
+
this.maybeUpdate();
|
|
751
|
+
}, autorefreshThreshold);
|
|
643
752
|
}
|
|
753
|
+
clearInterval() {
|
|
754
|
+
if (this.nextInterval) {
|
|
755
|
+
clearTimeout(this.nextInterval);
|
|
756
|
+
this.intervalStart = null;
|
|
757
|
+
}
|
|
758
|
+
}
|
|
759
|
+
updateSubscriptions() {
|
|
760
|
+
if (this.isIdle) {
|
|
761
|
+
return;
|
|
762
|
+
}
|
|
763
|
+
const requestId = this._request.lid;
|
|
764
|
+
// if we're already subscribed to this request, we don't need to do anything
|
|
765
|
+
if (this._subscribedTo === requestId) {
|
|
766
|
+
return;
|
|
767
|
+
}
|
|
768
|
+
// if we're subscribed to a different request, we need to unsubscribe
|
|
769
|
+
this.removeSubscriptions();
|
|
770
|
+
// if we have a request, we need to subscribe to it
|
|
771
|
+
if (requestId) {
|
|
772
|
+
this._subscribedTo = requestId;
|
|
773
|
+
this._subscription = this.store.notifications.subscribe(requestId, (_id, op) => {
|
|
774
|
+
// ignore subscription events that occur while our own component's request
|
|
775
|
+
// is ocurring
|
|
776
|
+
if (this.isUpdating) {
|
|
777
|
+
return;
|
|
778
|
+
}
|
|
779
|
+
switch (op) {
|
|
780
|
+
case 'invalidated':
|
|
781
|
+
{
|
|
782
|
+
// if we're subscribed to invalidations, we need to update
|
|
783
|
+
if (this.autorefreshTypes.has('invalid')) {
|
|
784
|
+
this.invalidated = true;
|
|
785
|
+
this.maybeUpdate();
|
|
786
|
+
}
|
|
787
|
+
break;
|
|
788
|
+
}
|
|
789
|
+
case 'state':
|
|
790
|
+
{
|
|
791
|
+
const latest = this.store.requestManager._deduped.get(requestId);
|
|
792
|
+
const priority = latest?.priority;
|
|
793
|
+
if (!priority) {
|
|
794
|
+
this.isRefreshing = false;
|
|
795
|
+
} else if (priority.blocking) {
|
|
796
|
+
// TODO should we just treat this as refreshing?
|
|
797
|
+
this.isRefreshing = false;
|
|
798
|
+
this.maybeUpdate('policy', true);
|
|
799
|
+
} else {
|
|
800
|
+
this.isRefreshing = true;
|
|
801
|
+
}
|
|
802
|
+
}
|
|
803
|
+
}
|
|
804
|
+
});
|
|
805
|
+
}
|
|
806
|
+
}
|
|
807
|
+
removeSubscriptions() {
|
|
808
|
+
if (this._subscription) {
|
|
809
|
+
this.store.notifications.unsubscribe(this._subscription);
|
|
810
|
+
this._subscribedTo = null;
|
|
811
|
+
this._subscription = null;
|
|
812
|
+
}
|
|
813
|
+
}
|
|
814
|
+
/**
|
|
815
|
+
* Install the event listeners for network and visibility changes.
|
|
816
|
+
* This is only done in browser environments with a global `window`.
|
|
817
|
+
*
|
|
818
|
+
* @internal
|
|
819
|
+
*/
|
|
644
820
|
installListeners() {
|
|
645
821
|
if (typeof window === 'undefined') {
|
|
646
822
|
return;
|
|
@@ -648,17 +824,17 @@ class Request extends Component {
|
|
|
648
824
|
this.isOnline = window.navigator.onLine;
|
|
649
825
|
this.unavailableStart = this.isOnline ? null : Date.now();
|
|
650
826
|
this.isHidden = document.visibilityState === 'hidden';
|
|
651
|
-
this.onlineChanged =
|
|
652
|
-
this.isOnline =
|
|
653
|
-
if (
|
|
827
|
+
this.onlineChanged = event => {
|
|
828
|
+
this.isOnline = event.type === 'online';
|
|
829
|
+
if (event.type === 'offline' && this.unavailableStart === null) {
|
|
654
830
|
this.unavailableStart = Date.now();
|
|
655
831
|
}
|
|
656
832
|
this.maybeUpdate();
|
|
657
833
|
};
|
|
658
834
|
this.backgroundChanged = () => {
|
|
659
|
-
const
|
|
660
|
-
this.isHidden =
|
|
661
|
-
if (
|
|
835
|
+
const isHidden = document.visibilityState === 'hidden';
|
|
836
|
+
this.isHidden = isHidden;
|
|
837
|
+
if (isHidden && this.unavailableStart === null) {
|
|
662
838
|
this.unavailableStart = Date.now();
|
|
663
839
|
}
|
|
664
840
|
this.maybeUpdate();
|
|
@@ -676,59 +852,112 @@ class Request extends Component {
|
|
|
676
852
|
capture: true
|
|
677
853
|
});
|
|
678
854
|
}
|
|
679
|
-
|
|
680
|
-
|
|
681
|
-
|
|
682
|
-
|
|
683
|
-
|
|
684
|
-
|
|
685
|
-
|
|
686
|
-
|
|
687
|
-
|
|
688
|
-
|
|
689
|
-
|
|
690
|
-
|
|
691
|
-
|
|
692
|
-
|
|
693
|
-
|
|
694
|
-
|
|
695
|
-
|
|
696
|
-
|
|
697
|
-
|
|
698
|
-
|
|
699
|
-
|
|
700
|
-
default:
|
|
701
|
-
throw new Error(`Invalid ${mode1 ? 'update mode' : '@autorefreshBehavior'} for <Request />: ${isNeverString(val1)}`);
|
|
702
|
-
}
|
|
703
|
-
const wasStoreRequest1 = request1[EnableHydration] === true;
|
|
704
|
-
macroCondition(getGlobalConfig().WarpDrive.env.DEBUG) ? (test => {
|
|
705
|
-
if (!test) {
|
|
706
|
-
throw new Error(`Cannot supply a different store via context than was used to create the request`);
|
|
707
|
-
}
|
|
708
|
-
})(!request1.store || request1.store === this.store) : {};
|
|
709
|
-
this._latestRequest = wasStoreRequest1 ? this.store.request(request1) : this.store.requestManager.request(request1);
|
|
710
|
-
if (val1 !== 'refresh') {
|
|
711
|
-
this._localRequest = this._latestRequest;
|
|
712
|
-
}
|
|
713
|
-
return;
|
|
855
|
+
/**
|
|
856
|
+
* If the network is online and the tab is visible, either reload or refresh the request
|
|
857
|
+
* based on the component's configuration and the requested update mode.
|
|
858
|
+
*
|
|
859
|
+
* Valid modes are:
|
|
860
|
+
*
|
|
861
|
+
* - `'reload'`: Force a reload of the request.
|
|
862
|
+
* - `'refresh'`: Refresh the request in the background.
|
|
863
|
+
* - `'policy'`: Make the request, letting the store's configured CachePolicy decide whether to reload, refresh, or do nothing.
|
|
864
|
+
* - `undefined`: Make the request using the component's autorefreshBehavior setting if the autorefreshThreshold has passed.
|
|
865
|
+
*
|
|
866
|
+
* @internal
|
|
867
|
+
*/
|
|
868
|
+
maybeUpdate(mode, silent) {
|
|
869
|
+
if (this.isIdle) {
|
|
870
|
+
return;
|
|
871
|
+
}
|
|
872
|
+
const canAttempt = Boolean(this.isOnline && !this.isHidden && (mode || this.autorefreshTypes.size));
|
|
873
|
+
if (!canAttempt) {
|
|
874
|
+
if (!silent && mode && mode !== 'invalidated') {
|
|
875
|
+
throw new Error(`Reload not available: the network is not online or the tab is hidden`);
|
|
714
876
|
}
|
|
877
|
+
return;
|
|
715
878
|
}
|
|
716
|
-
|
|
717
|
-
|
|
879
|
+
const {
|
|
880
|
+
autorefreshTypes
|
|
881
|
+
} = this;
|
|
882
|
+
let shouldAttempt = this.invalidated || Boolean(mode);
|
|
883
|
+
if (!shouldAttempt && autorefreshTypes.has('online')) {
|
|
884
|
+
const {
|
|
885
|
+
unavailableStart
|
|
886
|
+
} = this;
|
|
887
|
+
const {
|
|
888
|
+
autorefreshThreshold
|
|
889
|
+
} = this.args;
|
|
890
|
+
const deadline = typeof autorefreshThreshold === 'number' ? autorefreshThreshold : DEFAULT_DEADLINE;
|
|
891
|
+
shouldAttempt = Boolean(unavailableStart && Date.now() - unavailableStart > deadline);
|
|
892
|
+
}
|
|
893
|
+
if (!shouldAttempt && autorefreshTypes.has('interval')) {
|
|
894
|
+
const {
|
|
895
|
+
intervalStart
|
|
896
|
+
} = this;
|
|
897
|
+
const {
|
|
898
|
+
autorefreshThreshold
|
|
899
|
+
} = this.args;
|
|
900
|
+
if (intervalStart && typeof autorefreshThreshold === 'number' && autorefreshThreshold > 0) {
|
|
901
|
+
shouldAttempt = Boolean(Date.now() - intervalStart >= autorefreshThreshold);
|
|
902
|
+
}
|
|
903
|
+
}
|
|
904
|
+
this.unavailableStart = null;
|
|
905
|
+
this.invalidated = false;
|
|
906
|
+
if (shouldAttempt) {
|
|
907
|
+
this.clearInterval();
|
|
908
|
+
const request = Object.assign({}, this.reqState.request);
|
|
909
|
+
const realMode = mode === 'invalidated' ? null : mode;
|
|
910
|
+
const val = realMode ?? this.args.autorefreshBehavior ?? 'policy';
|
|
911
|
+
switch (val) {
|
|
912
|
+
case 'reload':
|
|
913
|
+
request.cacheOptions = Object.assign({}, request.cacheOptions, {
|
|
914
|
+
reload: true
|
|
915
|
+
});
|
|
916
|
+
break;
|
|
917
|
+
case 'refresh':
|
|
918
|
+
request.cacheOptions = Object.assign({}, request.cacheOptions, {
|
|
919
|
+
backgroundReload: true
|
|
920
|
+
});
|
|
921
|
+
break;
|
|
922
|
+
case 'policy':
|
|
923
|
+
break;
|
|
924
|
+
default:
|
|
925
|
+
throw new Error(`Invalid ${mode ? 'update mode' : '@autorefreshBehavior'} for <Request />: ${isNeverString(val)}`);
|
|
926
|
+
}
|
|
927
|
+
const wasStoreRequest = request[EnableHydration] === true;
|
|
928
|
+
macroCondition(getGlobalConfig().WarpDrive.env.DEBUG) ? (test => {
|
|
929
|
+
if (!test) {
|
|
930
|
+
throw new Error(`Cannot supply a different store via context than was used to create the request`);
|
|
931
|
+
}
|
|
932
|
+
})(!request.store || request.store === this.store) : {};
|
|
933
|
+
this.isUpdating = true;
|
|
934
|
+
this._latestRequest = wasStoreRequest ? this.store.request(request) : this.store.requestManager.request(request);
|
|
935
|
+
if (val !== 'refresh') {
|
|
936
|
+
this._localRequest = this._latestRequest;
|
|
937
|
+
}
|
|
938
|
+
void this.scheduleInterval();
|
|
939
|
+
void this._latestRequest.finally(() => {
|
|
940
|
+
this.isUpdating = false;
|
|
941
|
+
});
|
|
718
942
|
}
|
|
719
943
|
}
|
|
944
|
+
/**
|
|
945
|
+
* Retry the request, reloading it from the server.
|
|
946
|
+
*
|
|
947
|
+
* @internal
|
|
948
|
+
*/
|
|
720
949
|
retry = async () => {
|
|
721
950
|
this.maybeUpdate('reload');
|
|
722
951
|
await this._localRequest;
|
|
723
952
|
};
|
|
953
|
+
/**
|
|
954
|
+
* Refresh the request, updating it in the background.
|
|
955
|
+
*
|
|
956
|
+
* @internal
|
|
957
|
+
*/
|
|
724
958
|
refresh = async () => {
|
|
725
|
-
this.isRefreshing = true;
|
|
726
959
|
this.maybeUpdate('refresh');
|
|
727
|
-
|
|
728
|
-
await this._latestRequest;
|
|
729
|
-
} finally {
|
|
730
|
-
this.isRefreshing = false;
|
|
731
|
-
}
|
|
960
|
+
await this._latestRequest;
|
|
732
961
|
};
|
|
733
962
|
get errorFeatures() {
|
|
734
963
|
return {
|
|
@@ -741,7 +970,7 @@ class Request extends Component {
|
|
|
741
970
|
decorateMethodV2(this.prototype, "errorFeatures", [cached]);
|
|
742
971
|
}
|
|
743
972
|
get contentFeatures() {
|
|
744
|
-
const
|
|
973
|
+
const feat = {
|
|
745
974
|
isHidden: this.isHidden,
|
|
746
975
|
isOnline: this.isOnline,
|
|
747
976
|
reload: this.retry,
|
|
@@ -749,20 +978,22 @@ class Request extends Component {
|
|
|
749
978
|
isRefreshing: this.isRefreshing,
|
|
750
979
|
latestRequest: this._latestRequest
|
|
751
980
|
};
|
|
752
|
-
if (
|
|
753
|
-
|
|
981
|
+
if (feat.isRefreshing) {
|
|
982
|
+
feat.abort = () => {
|
|
754
983
|
this._latestRequest?.abort();
|
|
755
984
|
};
|
|
756
985
|
}
|
|
757
|
-
return
|
|
986
|
+
return feat;
|
|
758
987
|
}
|
|
759
988
|
static {
|
|
760
989
|
decorateMethodV2(this.prototype, "contentFeatures", [cached]);
|
|
761
990
|
}
|
|
762
991
|
willDestroy() {
|
|
992
|
+
this.removeSubscriptions();
|
|
763
993
|
if (typeof window === 'undefined') {
|
|
764
994
|
return;
|
|
765
995
|
}
|
|
996
|
+
this.clearInterval();
|
|
766
997
|
window.removeEventListener('online', this.onlineChanged, {
|
|
767
998
|
passive: true,
|
|
768
999
|
capture: true
|
|
@@ -776,49 +1007,57 @@ class Request extends Component {
|
|
|
776
1007
|
capture: true
|
|
777
1008
|
});
|
|
778
1009
|
}
|
|
779
|
-
get
|
|
1010
|
+
get _request() {
|
|
780
1011
|
const {
|
|
781
|
-
request
|
|
782
|
-
query
|
|
1012
|
+
request,
|
|
1013
|
+
query
|
|
783
1014
|
} = this.args;
|
|
784
1015
|
macroCondition(getGlobalConfig().WarpDrive.env.DEBUG) ? (test => {
|
|
785
1016
|
if (!test) {
|
|
786
1017
|
throw new Error(`Cannot use both @request and @query args with the <Request> component`);
|
|
787
1018
|
}
|
|
788
|
-
})(!
|
|
1019
|
+
})(!request || !query) : {};
|
|
789
1020
|
const {
|
|
790
|
-
_localRequest
|
|
791
|
-
_originalRequest
|
|
792
|
-
_originalQuery
|
|
1021
|
+
_localRequest,
|
|
1022
|
+
_originalRequest,
|
|
1023
|
+
_originalQuery
|
|
793
1024
|
} = this;
|
|
794
|
-
const
|
|
795
|
-
if (
|
|
796
|
-
return
|
|
1025
|
+
const isOriginalRequest = request === _originalRequest && query === _originalQuery;
|
|
1026
|
+
if (_localRequest && isOriginalRequest) {
|
|
1027
|
+
return _localRequest;
|
|
797
1028
|
}
|
|
798
1029
|
// update state checks for the next time
|
|
799
|
-
this._originalQuery =
|
|
800
|
-
this._originalRequest =
|
|
801
|
-
if (
|
|
802
|
-
return
|
|
1030
|
+
this._originalQuery = query;
|
|
1031
|
+
this._originalRequest = request;
|
|
1032
|
+
if (request) {
|
|
1033
|
+
return request;
|
|
803
1034
|
}
|
|
804
1035
|
macroCondition(getGlobalConfig().WarpDrive.env.DEBUG) ? (test => {
|
|
805
1036
|
if (!test) {
|
|
806
1037
|
throw new Error(`You must provide either @request or an @query arg with the <Request> component`);
|
|
807
1038
|
}
|
|
808
|
-
})(
|
|
809
|
-
return this.store.request(
|
|
1039
|
+
})(query) : {};
|
|
1040
|
+
return this.store.request(query);
|
|
1041
|
+
}
|
|
1042
|
+
static {
|
|
1043
|
+
decorateMethodV2(this.prototype, "_request", [cached]);
|
|
1044
|
+
}
|
|
1045
|
+
get request() {
|
|
1046
|
+
const request = this._request;
|
|
1047
|
+
this.updateSubscriptions();
|
|
1048
|
+
return request;
|
|
810
1049
|
}
|
|
811
1050
|
static {
|
|
812
1051
|
decorateMethodV2(this.prototype, "request", [cached]);
|
|
813
1052
|
}
|
|
814
1053
|
get store() {
|
|
815
|
-
const
|
|
1054
|
+
const store = this.args.store || this._store;
|
|
816
1055
|
macroCondition(getGlobalConfig().WarpDrive.env.DEBUG) ? (test => {
|
|
817
1056
|
if (!test) {
|
|
818
1057
|
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.`);
|
|
819
1058
|
}
|
|
820
|
-
})(
|
|
821
|
-
return
|
|
1059
|
+
})(store) : {};
|
|
1060
|
+
return store;
|
|
822
1061
|
}
|
|
823
1062
|
get reqState() {
|
|
824
1063
|
return getRequestState(this.request);
|
|
@@ -827,13 +1066,14 @@ class Request extends Component {
|
|
|
827
1066
|
return this.reqState.result;
|
|
828
1067
|
}
|
|
829
1068
|
static {
|
|
830
|
-
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 ", {
|
|
1069
|
+
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 ", {
|
|
831
1070
|
strictMode: true,
|
|
832
1071
|
scope: () => ({
|
|
833
1072
|
and,
|
|
1073
|
+
Throw,
|
|
1074
|
+
IdleBlockMissingError,
|
|
834
1075
|
notNull,
|
|
835
|
-
not
|
|
836
|
-
Throw
|
|
1076
|
+
not
|
|
837
1077
|
})
|
|
838
1078
|
}), this);
|
|
839
1079
|
}
|