@luvio/environments 0.71.0 → 0.73.2
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/es/es2018/DurableStore.d.ts +0 -1
- package/dist/es/es2018/environments.js +41 -375
- package/dist/umd/es2018/DurableStore.d.ts +0 -1
- package/dist/umd/es2018/environments.js +40 -374
- package/dist/umd/es5/DurableStore.d.ts +0 -1
- package/dist/umd/es5/environments.js +39 -384
- package/package.json +2 -2
- package/dist/es/es2018/makeDurable/cachepolicies/cache-and-network.d.ts +0 -3
- package/dist/es/es2018/makeDurable/cachepolicies/cache-then-network.d.ts +0 -3
- package/dist/es/es2018/makeDurable/cachepolicies/index.d.ts +0 -6
- package/dist/es/es2018/makeDurable/cachepolicies/no-cache.d.ts +0 -3
- package/dist/es/es2018/makeDurable/cachepolicies/only-if-cached.d.ts +0 -3
- package/dist/es/es2018/makeDurable/cachepolicies/stale-while-revalidate.d.ts +0 -3
- package/dist/es/es2018/makeDurable/cachepolicies/utils.d.ts +0 -9
- package/dist/es/es2018/makeDurable/cachepolicies/valid-at.d.ts +0 -3
- package/dist/umd/es2018/makeDurable/cachepolicies/cache-and-network.d.ts +0 -3
- package/dist/umd/es2018/makeDurable/cachepolicies/cache-then-network.d.ts +0 -3
- package/dist/umd/es2018/makeDurable/cachepolicies/index.d.ts +0 -6
- package/dist/umd/es2018/makeDurable/cachepolicies/no-cache.d.ts +0 -3
- package/dist/umd/es2018/makeDurable/cachepolicies/only-if-cached.d.ts +0 -3
- package/dist/umd/es2018/makeDurable/cachepolicies/stale-while-revalidate.d.ts +0 -3
- package/dist/umd/es2018/makeDurable/cachepolicies/utils.d.ts +0 -9
- package/dist/umd/es2018/makeDurable/cachepolicies/valid-at.d.ts +0 -3
- package/dist/umd/es5/makeDurable/cachepolicies/cache-and-network.d.ts +0 -3
- package/dist/umd/es5/makeDurable/cachepolicies/cache-then-network.d.ts +0 -3
- package/dist/umd/es5/makeDurable/cachepolicies/index.d.ts +0 -6
- package/dist/umd/es5/makeDurable/cachepolicies/no-cache.d.ts +0 -3
- package/dist/umd/es5/makeDurable/cachepolicies/only-if-cached.d.ts +0 -3
- package/dist/umd/es5/makeDurable/cachepolicies/stale-while-revalidate.d.ts +0 -3
- package/dist/umd/es5/makeDurable/cachepolicies/utils.d.ts +0 -9
- package/dist/umd/es5/makeDurable/cachepolicies/valid-at.d.ts +0 -3
|
@@ -21,292 +21,6 @@
|
|
|
21
21
|
const { keys, create, assign, freeze } = Object;
|
|
22
22
|
const { isArray } = Array;
|
|
23
23
|
|
|
24
|
-
function appendTTLStrategy(storeLookup, ttlStrategy) {
|
|
25
|
-
const returnStoreLookup = (sel, refresh) => storeLookup(sel, refresh, ttlStrategy);
|
|
26
|
-
// append ttlStrategy to storeLookup function (in cases where custom adapter
|
|
27
|
-
// wants to perform it's own lookup)
|
|
28
|
-
returnStoreLookup.ttlStrategy = ttlStrategy;
|
|
29
|
-
return returnStoreLookup;
|
|
30
|
-
}
|
|
31
|
-
function buildNetworkSnapshot(args) {
|
|
32
|
-
const { buildNetworkSnapshot, buildSnapshotContext, coercedAdapterRequestContext } = args;
|
|
33
|
-
return buildNetworkSnapshot(buildSnapshotContext, coercedAdapterRequestContext).then((snapshot) => snapshot.state === 'Pending' ? args.resolvePendingSnapshot(snapshot) : snapshot);
|
|
34
|
-
}
|
|
35
|
-
function buildTTLStrategy(staleDurationMilliseconds = 0) {
|
|
36
|
-
return (timestamp, metadata, valueIsError) => {
|
|
37
|
-
if (metadata !== undefined) {
|
|
38
|
-
const { expirationTimestamp } = metadata;
|
|
39
|
-
if (timestamp > expirationTimestamp) {
|
|
40
|
-
if (timestamp <= expirationTimestamp + staleDurationMilliseconds &&
|
|
41
|
-
valueIsError !== true) {
|
|
42
|
-
return engine.StoreResolveResultState.Stale;
|
|
43
|
-
}
|
|
44
|
-
return engine.StoreResolveResultState.NotPresent;
|
|
45
|
-
}
|
|
46
|
-
}
|
|
47
|
-
if (valueIsError === true) {
|
|
48
|
-
return engine.StoreResolveResultState.Error;
|
|
49
|
-
}
|
|
50
|
-
return engine.StoreResolveResultState.Found;
|
|
51
|
-
};
|
|
52
|
-
}
|
|
53
|
-
// TODO - update userland-facing APIs to return `AvailableSnapshot` instead of `Snapshot`
|
|
54
|
-
// and then the signatures here can be updated as well
|
|
55
|
-
function buildAvailableSnapshotFromCachedSnapshotResponse(cachedSnapshot, availableSnapshotFunc) {
|
|
56
|
-
if (isPromise(cachedSnapshot)) {
|
|
57
|
-
return cachedSnapshot.then(availableSnapshotFunc);
|
|
58
|
-
}
|
|
59
|
-
return availableSnapshotFunc(cachedSnapshot);
|
|
60
|
-
}
|
|
61
|
-
function isPromise(value) {
|
|
62
|
-
if (value === undefined) {
|
|
63
|
-
return false;
|
|
64
|
-
}
|
|
65
|
-
// check for Thenable due to test frameworks using custom Promise impls
|
|
66
|
-
return value.then !== undefined;
|
|
67
|
-
}
|
|
68
|
-
|
|
69
|
-
function buildCacheAndNetworkImplementation(funcs, staleDurationSeconds = 0) {
|
|
70
|
-
return function (args) {
|
|
71
|
-
funcs.validateNotDisposed();
|
|
72
|
-
const { buildCachedSnapshot, buildNetworkSnapshot: buildNetworkSnapshot$1, buildSnapshotContext, storeLookup, coercedAdapterRequestContext, } = args;
|
|
73
|
-
const staleDurationMilliseconds = staleDurationSeconds * 1000;
|
|
74
|
-
const cachePolicyStoreLookup = appendTTLStrategy(storeLookup, buildTTLStrategy(staleDurationMilliseconds));
|
|
75
|
-
const cachedSnapshot = buildCachedSnapshot(buildSnapshotContext, cachePolicyStoreLookup);
|
|
76
|
-
return buildAvailableSnapshotFromCachedSnapshotResponse(cachedSnapshot, (snapshot) => {
|
|
77
|
-
if (snapshot !== undefined) {
|
|
78
|
-
// data found in L1 cache
|
|
79
|
-
if (snapshot.state === 'Fulfilled' ||
|
|
80
|
-
snapshot.state === 'Error' ||
|
|
81
|
-
snapshot.state === 'Stale') {
|
|
82
|
-
// kick off network request, do not await it
|
|
83
|
-
buildNetworkSnapshot$1(buildSnapshotContext, coercedAdapterRequestContext);
|
|
84
|
-
// return the cached snapshot to caller
|
|
85
|
-
return snapshot;
|
|
86
|
-
}
|
|
87
|
-
// network request outstanding
|
|
88
|
-
if (snapshot.state === 'Pending') {
|
|
89
|
-
// kick off another network request, do not await it
|
|
90
|
-
buildNetworkSnapshot$1(buildSnapshotContext, coercedAdapterRequestContext);
|
|
91
|
-
return args.resolvePendingSnapshot(snapshot);
|
|
92
|
-
}
|
|
93
|
-
// if unfulfilled we have enough info to do an L2 lookup
|
|
94
|
-
if (snapshot.state === 'Unfulfilled') {
|
|
95
|
-
return funcs
|
|
96
|
-
.reviveSnapshotWithCachePolicy(snapshot, cachePolicyStoreLookup)
|
|
97
|
-
.then((revivedSnapshot) => {
|
|
98
|
-
// data found in L2 cache
|
|
99
|
-
if (revivedSnapshot.state === 'Fulfilled' ||
|
|
100
|
-
revivedSnapshot.state === 'Error' ||
|
|
101
|
-
revivedSnapshot.state === 'Stale') {
|
|
102
|
-
// kick off network request, do not await it
|
|
103
|
-
buildNetworkSnapshot$1(buildSnapshotContext, coercedAdapterRequestContext);
|
|
104
|
-
// return the L2 cached snapshot to caller
|
|
105
|
-
return revivedSnapshot;
|
|
106
|
-
}
|
|
107
|
-
if (revivedSnapshot.state === 'Pending') {
|
|
108
|
-
// kick off network request, do not await it
|
|
109
|
-
buildNetworkSnapshot$1(buildSnapshotContext, coercedAdapterRequestContext);
|
|
110
|
-
return args.resolvePendingSnapshot(revivedSnapshot);
|
|
111
|
-
}
|
|
112
|
-
// data not found in L2 cache, go to the network
|
|
113
|
-
return buildNetworkSnapshot(args);
|
|
114
|
-
});
|
|
115
|
-
}
|
|
116
|
-
}
|
|
117
|
-
return buildNetworkSnapshot(args);
|
|
118
|
-
});
|
|
119
|
-
};
|
|
120
|
-
}
|
|
121
|
-
|
|
122
|
-
function buildCacheThenNetworkImplementation(funcs) {
|
|
123
|
-
return function (args) {
|
|
124
|
-
funcs.validateNotDisposed();
|
|
125
|
-
const { buildCachedSnapshot, buildSnapshotContext, storeLookup } = args;
|
|
126
|
-
const cachePolicyStoreLookup = appendTTLStrategy(storeLookup, buildTTLStrategy());
|
|
127
|
-
const cachedSnapshot = buildCachedSnapshot(buildSnapshotContext, cachePolicyStoreLookup);
|
|
128
|
-
return buildAvailableSnapshotFromCachedSnapshotResponse(cachedSnapshot, (snapshot) => {
|
|
129
|
-
if (snapshot !== undefined) {
|
|
130
|
-
// data found in L1 cache
|
|
131
|
-
if (snapshot.state === 'Fulfilled' || snapshot.state === 'Error') {
|
|
132
|
-
return snapshot;
|
|
133
|
-
}
|
|
134
|
-
if (snapshot.state === 'Pending') {
|
|
135
|
-
return args.resolvePendingSnapshot(snapshot);
|
|
136
|
-
}
|
|
137
|
-
// if unfulfilled we have enough info to do an L2 lookup
|
|
138
|
-
if (snapshot.state === 'Unfulfilled') {
|
|
139
|
-
return funcs
|
|
140
|
-
.reviveSnapshotWithCachePolicy(snapshot, cachePolicyStoreLookup)
|
|
141
|
-
.then((revivedSnapshot) => {
|
|
142
|
-
// data found in L2 cache
|
|
143
|
-
if (revivedSnapshot.state === 'Fulfilled' ||
|
|
144
|
-
revivedSnapshot.state === 'Error') {
|
|
145
|
-
return revivedSnapshot;
|
|
146
|
-
}
|
|
147
|
-
if (revivedSnapshot.state === 'Pending') {
|
|
148
|
-
return args.resolvePendingSnapshot(revivedSnapshot);
|
|
149
|
-
}
|
|
150
|
-
// data not found in L2 cache, go to the network
|
|
151
|
-
return buildNetworkSnapshot(args);
|
|
152
|
-
});
|
|
153
|
-
}
|
|
154
|
-
}
|
|
155
|
-
// L1 lookup could not find enough information to even construct a snapshot, go to the network
|
|
156
|
-
return buildNetworkSnapshot(args);
|
|
157
|
-
});
|
|
158
|
-
};
|
|
159
|
-
}
|
|
160
|
-
|
|
161
|
-
function buildNoCacheImplementation(funcs) {
|
|
162
|
-
return function (args) {
|
|
163
|
-
funcs.validateNotDisposed();
|
|
164
|
-
return buildNetworkSnapshot(args);
|
|
165
|
-
};
|
|
166
|
-
}
|
|
167
|
-
|
|
168
|
-
function deepFreeze(value) {
|
|
169
|
-
// No need to freeze primitives
|
|
170
|
-
if (typeof value !== 'object' || value === null) {
|
|
171
|
-
return;
|
|
172
|
-
}
|
|
173
|
-
if (isArray(value)) {
|
|
174
|
-
for (let i = 0, len = value.length; i < len; i += 1) {
|
|
175
|
-
deepFreeze(value[i]);
|
|
176
|
-
}
|
|
177
|
-
}
|
|
178
|
-
else {
|
|
179
|
-
const keys$1 = keys(value);
|
|
180
|
-
for (let i = 0, len = keys$1.length; i < len; i += 1) {
|
|
181
|
-
deepFreeze(value[keys$1[i]]);
|
|
182
|
-
}
|
|
183
|
-
}
|
|
184
|
-
freeze(value);
|
|
185
|
-
}
|
|
186
|
-
|
|
187
|
-
// TODO[@W-10165595]: consolidate this code with the corresponding logic in the default environment's only-if-cached.ts
|
|
188
|
-
function buildNotCachedErrorSnapshot() {
|
|
189
|
-
const error = {
|
|
190
|
-
body: undefined,
|
|
191
|
-
headers: {},
|
|
192
|
-
ok: false,
|
|
193
|
-
status: engine.HttpStatusCode.GatewayTimeout,
|
|
194
|
-
statusText: 'Gateway Timeout',
|
|
195
|
-
};
|
|
196
|
-
deepFreeze(error);
|
|
197
|
-
return {
|
|
198
|
-
error,
|
|
199
|
-
state: 'Error',
|
|
200
|
-
data: undefined,
|
|
201
|
-
// TODO[@W-10164067]: copy refresh data from the snapshot returned by buildCachedSnapshot (if any)
|
|
202
|
-
// refresh: ...
|
|
203
|
-
};
|
|
204
|
-
}
|
|
205
|
-
function buildOnlyIfCachedImplementation(funcs) {
|
|
206
|
-
return function (args) {
|
|
207
|
-
funcs.validateNotDisposed();
|
|
208
|
-
const { buildCachedSnapshot, buildSnapshotContext, storeLookup } = args;
|
|
209
|
-
const cachePolicyStoreLookup = appendTTLStrategy(storeLookup, buildTTLStrategy());
|
|
210
|
-
const cachedSnapshot = buildCachedSnapshot(buildSnapshotContext, cachePolicyStoreLookup);
|
|
211
|
-
return buildAvailableSnapshotFromCachedSnapshotResponse(cachedSnapshot, (snapshot) => {
|
|
212
|
-
if (snapshot !== undefined) {
|
|
213
|
-
// data found in L1 cache
|
|
214
|
-
if (snapshot.state === 'Fulfilled' || snapshot.state === 'Error') {
|
|
215
|
-
return snapshot;
|
|
216
|
-
}
|
|
217
|
-
// network request outstanding, data is not cached
|
|
218
|
-
if (snapshot.state === 'Pending') {
|
|
219
|
-
return buildNotCachedErrorSnapshot();
|
|
220
|
-
}
|
|
221
|
-
// if unfulfilled we have enough info to do an L2 lookup
|
|
222
|
-
if (snapshot.state === 'Unfulfilled') {
|
|
223
|
-
return funcs
|
|
224
|
-
.reviveSnapshotWithCachePolicy(snapshot, cachePolicyStoreLookup)
|
|
225
|
-
.then((revivedSnapshot) => {
|
|
226
|
-
// data found in L2 cache
|
|
227
|
-
if (revivedSnapshot.state === 'Fulfilled' ||
|
|
228
|
-
revivedSnapshot.state === 'Error') {
|
|
229
|
-
return revivedSnapshot;
|
|
230
|
-
}
|
|
231
|
-
// data is not cached
|
|
232
|
-
return buildNotCachedErrorSnapshot();
|
|
233
|
-
});
|
|
234
|
-
}
|
|
235
|
-
}
|
|
236
|
-
return buildNotCachedErrorSnapshot();
|
|
237
|
-
});
|
|
238
|
-
};
|
|
239
|
-
}
|
|
240
|
-
|
|
241
|
-
function buildStaleWhileRevalidateImplementation(funcs, staleDurationSeconds) {
|
|
242
|
-
return function (args) {
|
|
243
|
-
funcs.validateNotDisposed();
|
|
244
|
-
const { buildCachedSnapshot, buildSnapshotContext, storeLookup } = args;
|
|
245
|
-
const cachePolicyStoreLookup = appendTTLStrategy(storeLookup, buildTTLStrategy(staleDurationSeconds * 1000));
|
|
246
|
-
const cachedSnapshot = buildCachedSnapshot(buildSnapshotContext, cachePolicyStoreLookup);
|
|
247
|
-
return buildAvailableSnapshotFromCachedSnapshotResponse(cachedSnapshot, (snapshot) => {
|
|
248
|
-
if (snapshot !== undefined) {
|
|
249
|
-
// data found in L1 cache
|
|
250
|
-
if (snapshot.state === 'Fulfilled' || snapshot.state === 'Error') {
|
|
251
|
-
return snapshot;
|
|
252
|
-
}
|
|
253
|
-
if (snapshot.state === 'Pending') {
|
|
254
|
-
return args.resolvePendingSnapshot(snapshot);
|
|
255
|
-
}
|
|
256
|
-
// stale data found in L1 cache
|
|
257
|
-
if (snapshot.state === 'Stale') {
|
|
258
|
-
buildNetworkSnapshot(args);
|
|
259
|
-
return snapshot;
|
|
260
|
-
}
|
|
261
|
-
// data not found in L1 cache, try L2 cache
|
|
262
|
-
return funcs
|
|
263
|
-
.reviveSnapshotWithCachePolicy(snapshot, cachePolicyStoreLookup)
|
|
264
|
-
.then((revivedSnapshot) => {
|
|
265
|
-
// data found in L2 cache
|
|
266
|
-
if (revivedSnapshot.state === 'Fulfilled' ||
|
|
267
|
-
revivedSnapshot.state === 'Error') {
|
|
268
|
-
return revivedSnapshot;
|
|
269
|
-
}
|
|
270
|
-
if (revivedSnapshot.state === 'Pending') {
|
|
271
|
-
return args.resolvePendingSnapshot(revivedSnapshot);
|
|
272
|
-
}
|
|
273
|
-
// stale data found in L2 cache
|
|
274
|
-
if (revivedSnapshot.state === 'Stale') {
|
|
275
|
-
buildNetworkSnapshot(args);
|
|
276
|
-
return revivedSnapshot;
|
|
277
|
-
}
|
|
278
|
-
// data not found in L2 cache, go to the network
|
|
279
|
-
return buildNetworkSnapshot(args);
|
|
280
|
-
});
|
|
281
|
-
}
|
|
282
|
-
// L1 lookup could not find enough information to even construct a snapshot, go to the network
|
|
283
|
-
return buildNetworkSnapshot(args);
|
|
284
|
-
});
|
|
285
|
-
};
|
|
286
|
-
}
|
|
287
|
-
|
|
288
|
-
function buildValidAtImplementation(funcs, basePolicyImplementation, timestamp) {
|
|
289
|
-
return function validAtImplementation(args) {
|
|
290
|
-
funcs.validateNotDisposed();
|
|
291
|
-
// This somewhat convoluted code is used to force the basePolicyImplementation's
|
|
292
|
-
// TTLStrategy to use the the valid-at cache policy's timestamp. The flow goes:
|
|
293
|
-
//
|
|
294
|
-
// Environment.applyCachePolicy => validAtImplementation (this function) =>
|
|
295
|
-
// basePolicyImplementation => adapter's buildCachedSnapshot =>
|
|
296
|
-
// basePolicyImplementation's storeLookup => validAtStoreLookup (below) =>
|
|
297
|
-
// Environment.applyCachePolicy's storeLookup => Store/Reader code =>
|
|
298
|
-
// valid-at TTLStrategy (below) =>
|
|
299
|
-
// basePolicyImplementation's TTLStrategy (with valid-at timestamp)
|
|
300
|
-
const validAtStoreLookup = (sel, refresh, ttlStrategy) => args.storeLookup(sel, refresh, (_readerTimestamp, metadata, valueIsError) => ttlStrategy(timestamp, metadata, valueIsError));
|
|
301
|
-
// let basePolicy make all the decisions, but have it use our storeLookup
|
|
302
|
-
// so we can override the timestamp passed to the basePolicy's TTLStrategy
|
|
303
|
-
return basePolicyImplementation({
|
|
304
|
-
...args,
|
|
305
|
-
storeLookup: validAtStoreLookup,
|
|
306
|
-
});
|
|
307
|
-
};
|
|
308
|
-
}
|
|
309
|
-
|
|
310
24
|
//Durable store error instrumentation key
|
|
311
25
|
const DURABLE_STORE_ERROR = 'durable-store-error';
|
|
312
26
|
/**
|
|
@@ -331,6 +45,25 @@
|
|
|
331
45
|
};
|
|
332
46
|
}
|
|
333
47
|
|
|
48
|
+
function deepFreeze(value) {
|
|
49
|
+
// No need to freeze primitives
|
|
50
|
+
if (typeof value !== 'object' || value === null) {
|
|
51
|
+
return;
|
|
52
|
+
}
|
|
53
|
+
if (isArray(value)) {
|
|
54
|
+
for (let i = 0, len = value.length; i < len; i += 1) {
|
|
55
|
+
deepFreeze(value[i]);
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
else {
|
|
59
|
+
const keys$1 = keys(value);
|
|
60
|
+
for (let i = 0, len = keys$1.length; i < len; i += 1) {
|
|
61
|
+
deepFreeze(value[keys$1[i]]);
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
freeze(value);
|
|
65
|
+
}
|
|
66
|
+
|
|
334
67
|
const SELECTOR_PAGINATION_TOKEN = 'tokenDataKey';
|
|
335
68
|
function isFragmentUnionSelection(sel) {
|
|
336
69
|
return sel.union === true;
|
|
@@ -700,6 +433,15 @@
|
|
|
700
433
|
return contextReturn();
|
|
701
434
|
});
|
|
702
435
|
}
|
|
436
|
+
function isUnfulfilledSnapshot(cachedSnapshotResult) {
|
|
437
|
+
if (cachedSnapshotResult === undefined) {
|
|
438
|
+
return false;
|
|
439
|
+
}
|
|
440
|
+
if ('then' in cachedSnapshotResult) {
|
|
441
|
+
return false;
|
|
442
|
+
}
|
|
443
|
+
return cachedSnapshotResult.state === 'Unfulfilled';
|
|
444
|
+
}
|
|
703
445
|
/**
|
|
704
446
|
* Configures the environment to persist data into a durable store and attempt to resolve
|
|
705
447
|
* data from the persistent store before hitting the network.
|
|
@@ -857,28 +599,6 @@
|
|
|
857
599
|
}
|
|
858
600
|
return environment.wrapNormalizedGraphNode(normalized, ingestStagingStore);
|
|
859
601
|
};
|
|
860
|
-
const resolveSnapshot = function (snapshot, refresh) {
|
|
861
|
-
validateNotDisposed();
|
|
862
|
-
// if the snapshot is already pending then no need to kick off another
|
|
863
|
-
// revive, just wait for the pending refresh to broadcast
|
|
864
|
-
if (snapshot.state === 'Pending') {
|
|
865
|
-
return environment.resolvePendingSnapshot(snapshot);
|
|
866
|
-
}
|
|
867
|
-
const { resolve, config } = refresh;
|
|
868
|
-
const refreshFunc = () => resolve(config);
|
|
869
|
-
// if the snapshot is unfulfilled we can do an L2 lookup
|
|
870
|
-
if (snapshot.state === 'Unfulfilled') {
|
|
871
|
-
return reviveSnapshot(environment, durableStore, snapshot, durableStoreErrorHandler, () => environment.storeLookup(snapshot.select, environment.createSnapshot, snapshot.refresh)).then((durableSnapshot) => {
|
|
872
|
-
if (environment.snapshotAvailable(durableSnapshot)) {
|
|
873
|
-
// L2 cache hit
|
|
874
|
-
return durableSnapshot;
|
|
875
|
-
}
|
|
876
|
-
// else have to hit network
|
|
877
|
-
return refreshFunc();
|
|
878
|
-
});
|
|
879
|
-
}
|
|
880
|
-
return refreshFunc();
|
|
881
|
-
};
|
|
882
602
|
const rebuildSnapshot = function (snapshot, records, storeMetadataMap, redirects, onAsyncRebuild) {
|
|
883
603
|
validateNotDisposed();
|
|
884
604
|
// try rebuilding from memory
|
|
@@ -941,73 +661,20 @@
|
|
|
941
661
|
validateNotDisposed();
|
|
942
662
|
return durableTTLStore.getDurableTTLOverrides();
|
|
943
663
|
};
|
|
944
|
-
// reviveSnapshot wrapper to let cache policies revive data from L2 and
|
|
945
|
-
// access the revived data via their own storeLookups
|
|
946
|
-
const reviveSnapshotWithCachePolicy = (unavailableSnapshot, storeLookup) => reviveSnapshot(environment, durableStore, unavailableSnapshot, durableStoreErrorHandler, () => storeLookup(unavailableSnapshot.select, unavailableSnapshot.refresh));
|
|
947
|
-
const defaultCachePolicy = buildStaleWhileRevalidateImplementation({
|
|
948
|
-
validateNotDisposed,
|
|
949
|
-
reviveSnapshotWithCachePolicy,
|
|
950
|
-
}, Number.MAX_SAFE_INTEGER);
|
|
951
|
-
function resolveCachePolicy(cachePolicy) {
|
|
952
|
-
if (cachePolicy === undefined) {
|
|
953
|
-
return defaultCachePolicy;
|
|
954
|
-
}
|
|
955
|
-
switch (cachePolicy.type) {
|
|
956
|
-
case 'stale-while-revalidate':
|
|
957
|
-
return buildStaleWhileRevalidateImplementation({
|
|
958
|
-
validateNotDisposed,
|
|
959
|
-
reviveSnapshotWithCachePolicy,
|
|
960
|
-
}, cachePolicy.staleDurationSeconds);
|
|
961
|
-
case 'cache-and-network':
|
|
962
|
-
return buildCacheAndNetworkImplementation({
|
|
963
|
-
validateNotDisposed,
|
|
964
|
-
reviveSnapshotWithCachePolicy,
|
|
965
|
-
}, cachePolicy.staleDurationSeconds);
|
|
966
|
-
case 'cache-then-network':
|
|
967
|
-
return buildCacheThenNetworkImplementation({
|
|
968
|
-
validateNotDisposed,
|
|
969
|
-
reviveSnapshotWithCachePolicy,
|
|
970
|
-
});
|
|
971
|
-
case 'no-cache':
|
|
972
|
-
return buildNoCacheImplementation({
|
|
973
|
-
validateNotDisposed,
|
|
974
|
-
reviveSnapshotWithCachePolicy,
|
|
975
|
-
});
|
|
976
|
-
case 'only-if-cached':
|
|
977
|
-
return buildOnlyIfCachedImplementation({
|
|
978
|
-
validateNotDisposed,
|
|
979
|
-
reviveSnapshotWithCachePolicy,
|
|
980
|
-
});
|
|
981
|
-
case 'valid-at': {
|
|
982
|
-
const basePolicy = resolveCachePolicy(cachePolicy.basePolicy);
|
|
983
|
-
return buildValidAtImplementation({
|
|
984
|
-
validateNotDisposed,
|
|
985
|
-
reviveSnapshotWithCachePolicy,
|
|
986
|
-
}, basePolicy, cachePolicy.timestamp);
|
|
987
|
-
}
|
|
988
|
-
default: {
|
|
989
|
-
if (process.env.NODE_ENV !== 'production') {
|
|
990
|
-
throw new Error(`unrecognized cache policy: ${JSON.stringify(cachePolicy)}`);
|
|
991
|
-
}
|
|
992
|
-
return defaultCachePolicy;
|
|
993
|
-
}
|
|
994
|
-
}
|
|
995
|
-
}
|
|
996
664
|
const applyCachePolicy = function (adapterRequestContext, buildSnapshotContext, buildCachedSnapshot, buildNetworkSnapshot) {
|
|
997
665
|
validateNotDisposed();
|
|
998
|
-
const
|
|
999
|
-
|
|
1000
|
-
|
|
1001
|
-
|
|
666
|
+
const wrappedCacheLookup = (buildSnapshotContext, storeLookup) => {
|
|
667
|
+
const snapshot = buildCachedSnapshot(buildSnapshotContext, storeLookup);
|
|
668
|
+
// if the adapter attempted to do an L1 lookup and it was unfulfilled
|
|
669
|
+
// then we can attempt an L2 lookup
|
|
670
|
+
if (isUnfulfilledSnapshot(snapshot)) {
|
|
671
|
+
return reviveSnapshot(environment, durableStore, snapshot, durableStoreErrorHandler, () => storeLookup(snapshot.select, snapshot.refresh));
|
|
672
|
+
}
|
|
673
|
+
// otherwise just return what buildCachedSnapshot gave us
|
|
674
|
+
return snapshot;
|
|
675
|
+
};
|
|
1002
676
|
const applyCachePolicy = () => {
|
|
1003
|
-
return
|
|
1004
|
-
buildCachedSnapshot,
|
|
1005
|
-
buildNetworkSnapshot,
|
|
1006
|
-
buildSnapshotContext,
|
|
1007
|
-
resolvePendingSnapshot,
|
|
1008
|
-
storeLookup,
|
|
1009
|
-
coercedAdapterRequestContext: engine.coerceAdapterRequestContext(adapterRequestContext),
|
|
1010
|
-
});
|
|
677
|
+
return environment.applyCachePolicy(adapterRequestContext, buildSnapshotContext, wrappedCacheLookup, buildNetworkSnapshot);
|
|
1011
678
|
};
|
|
1012
679
|
return isRevivingTTLOverrides !== undefined
|
|
1013
680
|
? isRevivingTTLOverrides.then(applyCachePolicy)
|
|
@@ -1043,6 +710,7 @@
|
|
|
1043
710
|
// if snapshot from staging store lookup is unfulfilled then do an L2 lookup
|
|
1044
711
|
return reviveSnapshot(environment, durableStore, snapshotFromMemoryIngest, durableStoreErrorHandler, () => environment.storeLookup(snapshotFromMemoryIngest.select, environment.createSnapshot, snapshotFromMemoryIngest.refresh));
|
|
1045
712
|
};
|
|
713
|
+
environment.defaultCachePolicy = engine.buildStaleWhileRevalidateImplementation(Number.MAX_SAFE_INTEGER);
|
|
1046
714
|
return create(environment, {
|
|
1047
715
|
publishStoreMetadata: { value: publishStoreMetadata },
|
|
1048
716
|
storeIngest: { value: storeIngest },
|
|
@@ -1052,7 +720,6 @@
|
|
|
1052
720
|
storeEvict: { value: storeEvict },
|
|
1053
721
|
wrapNormalizedGraphNode: { value: wrapNormalizedGraphNode },
|
|
1054
722
|
getNode: { value: getNode },
|
|
1055
|
-
resolveSnapshot: { value: resolveSnapshot },
|
|
1056
723
|
rebuildSnapshot: { value: rebuildSnapshot },
|
|
1057
724
|
withContext: { value: withContext },
|
|
1058
725
|
storeSetTTLOverride: { value: storeSetTTLOverride },
|
|
@@ -1062,7 +729,6 @@
|
|
|
1062
729
|
dispose: { value: dispose },
|
|
1063
730
|
publishChangesToDurableStore: { value: publishChangesToDurableStore },
|
|
1064
731
|
getDurableTTLOverrides: { value: getDurableTTLOverrides },
|
|
1065
|
-
defaultCachePolicy: { value: defaultCachePolicy },
|
|
1066
732
|
applyCachePolicy: { value: applyCachePolicy },
|
|
1067
733
|
getIngestStagingStoreRecords: { value: getIngestStagingStoreRecords },
|
|
1068
734
|
getIngestStagingStoreMetadata: { value: getIngestStagingStoreMetadata },
|