@luvio/environments 0.72.0 → 0.73.3
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/environments.js +62 -388
- package/dist/umd/es2018/environments.js +61 -387
- package/dist/umd/es5/environments.js +61 -398
- 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.
|
|
@@ -810,12 +552,9 @@
|
|
|
810
552
|
};
|
|
811
553
|
const storeBroadcast = function (_rebuildSnapshot, _snapshotDataAvailable) {
|
|
812
554
|
validateNotDisposed();
|
|
813
|
-
//
|
|
814
|
-
//
|
|
815
|
-
//
|
|
816
|
-
// don't await the DS write - DS implementation will take care of R/W
|
|
817
|
-
// synchronization
|
|
818
|
-
publishChangesToDurableStore();
|
|
555
|
+
// no-op here and wait for the L2 flush to happen in handleSuccessResponse,
|
|
556
|
+
// that flush will cause the onChanged handler to fire which will revive
|
|
557
|
+
// records to the main L1 store and call the base storeBroadcast
|
|
819
558
|
};
|
|
820
559
|
const publishChangesToDurableStore = function () {
|
|
821
560
|
validateNotDisposed();
|
|
@@ -857,28 +596,6 @@
|
|
|
857
596
|
}
|
|
858
597
|
return environment.wrapNormalizedGraphNode(normalized, ingestStagingStore);
|
|
859
598
|
};
|
|
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
599
|
const rebuildSnapshot = function (snapshot, records, storeMetadataMap, redirects, onAsyncRebuild) {
|
|
883
600
|
validateNotDisposed();
|
|
884
601
|
// try rebuilding from memory
|
|
@@ -941,73 +658,20 @@
|
|
|
941
658
|
validateNotDisposed();
|
|
942
659
|
return durableTTLStore.getDurableTTLOverrides();
|
|
943
660
|
};
|
|
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
661
|
const applyCachePolicy = function (adapterRequestContext, buildSnapshotContext, buildCachedSnapshot, buildNetworkSnapshot) {
|
|
997
662
|
validateNotDisposed();
|
|
998
|
-
const
|
|
999
|
-
|
|
1000
|
-
|
|
1001
|
-
|
|
663
|
+
const wrappedCacheLookup = (buildSnapshotContext, storeLookup) => {
|
|
664
|
+
const snapshot = buildCachedSnapshot(buildSnapshotContext, storeLookup);
|
|
665
|
+
// if the adapter attempted to do an L1 lookup and it was unfulfilled
|
|
666
|
+
// then we can attempt an L2 lookup
|
|
667
|
+
if (isUnfulfilledSnapshot(snapshot)) {
|
|
668
|
+
return reviveSnapshot(environment, durableStore, snapshot, durableStoreErrorHandler, () => storeLookup(snapshot.select, snapshot.refresh));
|
|
669
|
+
}
|
|
670
|
+
// otherwise just return what buildCachedSnapshot gave us
|
|
671
|
+
return snapshot;
|
|
672
|
+
};
|
|
1002
673
|
const applyCachePolicy = () => {
|
|
1003
|
-
return
|
|
1004
|
-
buildCachedSnapshot,
|
|
1005
|
-
buildNetworkSnapshot,
|
|
1006
|
-
buildSnapshotContext,
|
|
1007
|
-
resolvePendingSnapshot,
|
|
1008
|
-
storeLookup,
|
|
1009
|
-
coercedAdapterRequestContext: engine.coerceAdapterRequestContext(adapterRequestContext),
|
|
1010
|
-
});
|
|
674
|
+
return environment.applyCachePolicy(adapterRequestContext, buildSnapshotContext, wrappedCacheLookup, buildNetworkSnapshot);
|
|
1011
675
|
};
|
|
1012
676
|
return isRevivingTTLOverrides !== undefined
|
|
1013
677
|
? isRevivingTTLOverrides.then(applyCachePolicy)
|
|
@@ -1034,15 +698,26 @@
|
|
|
1034
698
|
ingestStagingStore.records = existingRecords;
|
|
1035
699
|
}
|
|
1036
700
|
const snapshotFromMemoryIngest = ingestAndBroadcastFunc();
|
|
1037
|
-
|
|
1038
|
-
|
|
1039
|
-
|
|
1040
|
-
|
|
701
|
+
// now that ingestion is done flush staging store values to L2
|
|
702
|
+
return publishChangesToDurableStore().then(() => {
|
|
703
|
+
if (snapshotFromMemoryIngest === undefined) {
|
|
704
|
+
return undefined;
|
|
705
|
+
}
|
|
706
|
+
if (snapshotFromMemoryIngest.state !== 'Unfulfilled') {
|
|
707
|
+
return snapshotFromMemoryIngest;
|
|
708
|
+
}
|
|
709
|
+
// if snapshot from staging store lookup is unfulfilled then do an L2 lookup
|
|
710
|
+
return reviveSnapshot(environment, durableStore, snapshotFromMemoryIngest, durableStoreErrorHandler, () => environment.storeLookup(snapshotFromMemoryIngest.select, environment.createSnapshot, snapshotFromMemoryIngest.refresh));
|
|
711
|
+
});
|
|
712
|
+
};
|
|
713
|
+
const handleErrorResponse = function (ingestAndBroadcastFunc) {
|
|
714
|
+
validateNotDisposed();
|
|
715
|
+
const snapshotFromMemoryIngest = ingestAndBroadcastFunc();
|
|
716
|
+
return publishChangesToDurableStore().then(() => {
|
|
1041
717
|
return snapshotFromMemoryIngest;
|
|
1042
|
-
}
|
|
1043
|
-
// if snapshot from staging store lookup is unfulfilled then do an L2 lookup
|
|
1044
|
-
return reviveSnapshot(environment, durableStore, snapshotFromMemoryIngest, durableStoreErrorHandler, () => environment.storeLookup(snapshotFromMemoryIngest.select, environment.createSnapshot, snapshotFromMemoryIngest.refresh));
|
|
718
|
+
});
|
|
1045
719
|
};
|
|
720
|
+
environment.defaultCachePolicy = engine.buildStaleWhileRevalidateImplementation(Number.MAX_SAFE_INTEGER);
|
|
1046
721
|
return create(environment, {
|
|
1047
722
|
publishStoreMetadata: { value: publishStoreMetadata },
|
|
1048
723
|
storeIngest: { value: storeIngest },
|
|
@@ -1052,7 +727,6 @@
|
|
|
1052
727
|
storeEvict: { value: storeEvict },
|
|
1053
728
|
wrapNormalizedGraphNode: { value: wrapNormalizedGraphNode },
|
|
1054
729
|
getNode: { value: getNode },
|
|
1055
|
-
resolveSnapshot: { value: resolveSnapshot },
|
|
1056
730
|
rebuildSnapshot: { value: rebuildSnapshot },
|
|
1057
731
|
withContext: { value: withContext },
|
|
1058
732
|
storeSetTTLOverride: { value: storeSetTTLOverride },
|
|
@@ -1062,11 +736,11 @@
|
|
|
1062
736
|
dispose: { value: dispose },
|
|
1063
737
|
publishChangesToDurableStore: { value: publishChangesToDurableStore },
|
|
1064
738
|
getDurableTTLOverrides: { value: getDurableTTLOverrides },
|
|
1065
|
-
defaultCachePolicy: { value: defaultCachePolicy },
|
|
1066
739
|
applyCachePolicy: { value: applyCachePolicy },
|
|
1067
740
|
getIngestStagingStoreRecords: { value: getIngestStagingStoreRecords },
|
|
1068
741
|
getIngestStagingStoreMetadata: { value: getIngestStagingStoreMetadata },
|
|
1069
742
|
handleSuccessResponse: { value: handleSuccessResponse },
|
|
743
|
+
handleErrorResponse: { value: handleErrorResponse },
|
|
1070
744
|
});
|
|
1071
745
|
}
|
|
1072
746
|
|