@spoosh/plugin-optimistic 0.8.1 → 0.9.0
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 +25 -17
- package/dist/index.mjs +25 -17
- package/package.json +5 -5
package/dist/index.js
CHANGED
|
@@ -104,6 +104,8 @@ var import_core3 = require("@spoosh/core");
|
|
|
104
104
|
function createBuilder(state, isConfirmed = false) {
|
|
105
105
|
return {
|
|
106
106
|
...state,
|
|
107
|
+
// Expose the internal state so the plugin can access predicates
|
|
108
|
+
__state: state,
|
|
107
109
|
filter(predicate) {
|
|
108
110
|
return createBuilder({ ...state, filter: predicate }, isConfirmed);
|
|
109
111
|
},
|
|
@@ -170,11 +172,12 @@ function applyUpdate(stateManager, target, updater, response, t) {
|
|
|
170
172
|
const snapshots = [];
|
|
171
173
|
const matchingEntries = getMatchingEntries(stateManager, target.path);
|
|
172
174
|
if (matchingEntries.length === 0) {
|
|
173
|
-
t?.skip(`Skipped ${target.path} (no cache entry)`);
|
|
174
175
|
return [];
|
|
175
176
|
}
|
|
176
177
|
for (const { key, entry, extractedParams } of matchingEntries) {
|
|
177
|
-
|
|
178
|
+
const targetWithState = target;
|
|
179
|
+
const filterPredicate = targetWithState.__state ? targetWithState.__state.filter : target.filter;
|
|
180
|
+
if (filterPredicate) {
|
|
178
181
|
const options = extractOptionsFromKey(key) ?? {};
|
|
179
182
|
const mergedOptions = {
|
|
180
183
|
...options,
|
|
@@ -184,10 +187,7 @@ function applyUpdate(stateManager, target, updater, response, t) {
|
|
|
184
187
|
}
|
|
185
188
|
};
|
|
186
189
|
try {
|
|
187
|
-
if (!
|
|
188
|
-
t?.skip(
|
|
189
|
-
`Skipped ${formatCacheKeyForTrace(key)} (filter not matched)`
|
|
190
|
-
);
|
|
190
|
+
if (!filterPredicate(mergedOptions)) {
|
|
191
191
|
continue;
|
|
192
192
|
}
|
|
193
193
|
} catch {
|
|
@@ -196,7 +196,6 @@ function applyUpdate(stateManager, target, updater, response, t) {
|
|
|
196
196
|
}
|
|
197
197
|
}
|
|
198
198
|
if (entry?.state.data === void 0) {
|
|
199
|
-
t?.skip(`Skipped ${formatCacheKeyForTrace(key)} (no cached data)`);
|
|
200
199
|
continue;
|
|
201
200
|
}
|
|
202
201
|
const afterData = updater(entry.state.data, response);
|
|
@@ -256,14 +255,16 @@ function optimisticPlugin() {
|
|
|
256
255
|
t?.skip("No optimistic targets");
|
|
257
256
|
return next();
|
|
258
257
|
}
|
|
259
|
-
context.plugins.get("spoosh:invalidation")?.
|
|
258
|
+
context.plugins.get("spoosh:invalidation")?.disableAutoInvalidate();
|
|
260
259
|
const allImmediateSnapshots = [];
|
|
261
260
|
for (const target of targets) {
|
|
262
|
-
|
|
261
|
+
const targetWithState = target;
|
|
262
|
+
const immediateUpdater = targetWithState.__state ? targetWithState.__state.immediateUpdater : target.immediateUpdater;
|
|
263
|
+
if (!immediateUpdater) continue;
|
|
263
264
|
const snapshots = applyUpdate(
|
|
264
265
|
stateManager,
|
|
265
266
|
target,
|
|
266
|
-
|
|
267
|
+
immediateUpdater,
|
|
267
268
|
void 0,
|
|
268
269
|
t
|
|
269
270
|
);
|
|
@@ -277,9 +278,12 @@ function optimisticPlugin() {
|
|
|
277
278
|
}
|
|
278
279
|
const response = await next();
|
|
279
280
|
if (response.error) {
|
|
280
|
-
const shouldRollback = targets.some(
|
|
281
|
-
|
|
282
|
-
|
|
281
|
+
const shouldRollback = targets.some((target) => {
|
|
282
|
+
const targetWithState = target;
|
|
283
|
+
const rollbackOnError = targetWithState.__state ? targetWithState.__state.rollbackOnError : target.rollbackOnError;
|
|
284
|
+
const immediateUpdater = targetWithState.__state ? targetWithState.__state.immediateUpdater : target.immediateUpdater;
|
|
285
|
+
return rollbackOnError && immediateUpdater;
|
|
286
|
+
});
|
|
283
287
|
if (shouldRollback && allImmediateSnapshots.length > 0) {
|
|
284
288
|
rollbackOptimistic(stateManager, allImmediateSnapshots);
|
|
285
289
|
for (const snapshot of allImmediateSnapshots) {
|
|
@@ -290,8 +294,10 @@ function optimisticPlugin() {
|
|
|
290
294
|
}
|
|
291
295
|
}
|
|
292
296
|
for (const target of targets) {
|
|
293
|
-
|
|
294
|
-
|
|
297
|
+
const targetWithState = target;
|
|
298
|
+
const onError = targetWithState.__state ? targetWithState.__state.onError : target.onError;
|
|
299
|
+
if (onError) {
|
|
300
|
+
onError(response.error);
|
|
295
301
|
}
|
|
296
302
|
}
|
|
297
303
|
} else {
|
|
@@ -299,11 +305,13 @@ function optimisticPlugin() {
|
|
|
299
305
|
confirmOptimistic(stateManager, allImmediateSnapshots);
|
|
300
306
|
}
|
|
301
307
|
for (const target of targets) {
|
|
302
|
-
|
|
308
|
+
const targetWithState = target;
|
|
309
|
+
const confirmedUpdater = targetWithState.__state ? targetWithState.__state.confirmedUpdater : target.confirmedUpdater;
|
|
310
|
+
if (!confirmedUpdater) continue;
|
|
303
311
|
const snapshots = applyUpdate(
|
|
304
312
|
stateManager,
|
|
305
313
|
target,
|
|
306
|
-
|
|
314
|
+
confirmedUpdater,
|
|
307
315
|
response.data,
|
|
308
316
|
t
|
|
309
317
|
);
|
package/dist/index.mjs
CHANGED
|
@@ -80,6 +80,8 @@ import { generateSelfTagFromKey as generateSelfTagFromKey2 } from "@spoosh/core"
|
|
|
80
80
|
function createBuilder(state, isConfirmed = false) {
|
|
81
81
|
return {
|
|
82
82
|
...state,
|
|
83
|
+
// Expose the internal state so the plugin can access predicates
|
|
84
|
+
__state: state,
|
|
83
85
|
filter(predicate) {
|
|
84
86
|
return createBuilder({ ...state, filter: predicate }, isConfirmed);
|
|
85
87
|
},
|
|
@@ -146,11 +148,12 @@ function applyUpdate(stateManager, target, updater, response, t) {
|
|
|
146
148
|
const snapshots = [];
|
|
147
149
|
const matchingEntries = getMatchingEntries(stateManager, target.path);
|
|
148
150
|
if (matchingEntries.length === 0) {
|
|
149
|
-
t?.skip(`Skipped ${target.path} (no cache entry)`);
|
|
150
151
|
return [];
|
|
151
152
|
}
|
|
152
153
|
for (const { key, entry, extractedParams } of matchingEntries) {
|
|
153
|
-
|
|
154
|
+
const targetWithState = target;
|
|
155
|
+
const filterPredicate = targetWithState.__state ? targetWithState.__state.filter : target.filter;
|
|
156
|
+
if (filterPredicate) {
|
|
154
157
|
const options = extractOptionsFromKey(key) ?? {};
|
|
155
158
|
const mergedOptions = {
|
|
156
159
|
...options,
|
|
@@ -160,10 +163,7 @@ function applyUpdate(stateManager, target, updater, response, t) {
|
|
|
160
163
|
}
|
|
161
164
|
};
|
|
162
165
|
try {
|
|
163
|
-
if (!
|
|
164
|
-
t?.skip(
|
|
165
|
-
`Skipped ${formatCacheKeyForTrace(key)} (filter not matched)`
|
|
166
|
-
);
|
|
166
|
+
if (!filterPredicate(mergedOptions)) {
|
|
167
167
|
continue;
|
|
168
168
|
}
|
|
169
169
|
} catch {
|
|
@@ -172,7 +172,6 @@ function applyUpdate(stateManager, target, updater, response, t) {
|
|
|
172
172
|
}
|
|
173
173
|
}
|
|
174
174
|
if (entry?.state.data === void 0) {
|
|
175
|
-
t?.skip(`Skipped ${formatCacheKeyForTrace(key)} (no cached data)`);
|
|
176
175
|
continue;
|
|
177
176
|
}
|
|
178
177
|
const afterData = updater(entry.state.data, response);
|
|
@@ -232,14 +231,16 @@ function optimisticPlugin() {
|
|
|
232
231
|
t?.skip("No optimistic targets");
|
|
233
232
|
return next();
|
|
234
233
|
}
|
|
235
|
-
context.plugins.get("spoosh:invalidation")?.
|
|
234
|
+
context.plugins.get("spoosh:invalidation")?.disableAutoInvalidate();
|
|
236
235
|
const allImmediateSnapshots = [];
|
|
237
236
|
for (const target of targets) {
|
|
238
|
-
|
|
237
|
+
const targetWithState = target;
|
|
238
|
+
const immediateUpdater = targetWithState.__state ? targetWithState.__state.immediateUpdater : target.immediateUpdater;
|
|
239
|
+
if (!immediateUpdater) continue;
|
|
239
240
|
const snapshots = applyUpdate(
|
|
240
241
|
stateManager,
|
|
241
242
|
target,
|
|
242
|
-
|
|
243
|
+
immediateUpdater,
|
|
243
244
|
void 0,
|
|
244
245
|
t
|
|
245
246
|
);
|
|
@@ -253,9 +254,12 @@ function optimisticPlugin() {
|
|
|
253
254
|
}
|
|
254
255
|
const response = await next();
|
|
255
256
|
if (response.error) {
|
|
256
|
-
const shouldRollback = targets.some(
|
|
257
|
-
|
|
258
|
-
|
|
257
|
+
const shouldRollback = targets.some((target) => {
|
|
258
|
+
const targetWithState = target;
|
|
259
|
+
const rollbackOnError = targetWithState.__state ? targetWithState.__state.rollbackOnError : target.rollbackOnError;
|
|
260
|
+
const immediateUpdater = targetWithState.__state ? targetWithState.__state.immediateUpdater : target.immediateUpdater;
|
|
261
|
+
return rollbackOnError && immediateUpdater;
|
|
262
|
+
});
|
|
259
263
|
if (shouldRollback && allImmediateSnapshots.length > 0) {
|
|
260
264
|
rollbackOptimistic(stateManager, allImmediateSnapshots);
|
|
261
265
|
for (const snapshot of allImmediateSnapshots) {
|
|
@@ -266,8 +270,10 @@ function optimisticPlugin() {
|
|
|
266
270
|
}
|
|
267
271
|
}
|
|
268
272
|
for (const target of targets) {
|
|
269
|
-
|
|
270
|
-
|
|
273
|
+
const targetWithState = target;
|
|
274
|
+
const onError = targetWithState.__state ? targetWithState.__state.onError : target.onError;
|
|
275
|
+
if (onError) {
|
|
276
|
+
onError(response.error);
|
|
271
277
|
}
|
|
272
278
|
}
|
|
273
279
|
} else {
|
|
@@ -275,11 +281,13 @@ function optimisticPlugin() {
|
|
|
275
281
|
confirmOptimistic(stateManager, allImmediateSnapshots);
|
|
276
282
|
}
|
|
277
283
|
for (const target of targets) {
|
|
278
|
-
|
|
284
|
+
const targetWithState = target;
|
|
285
|
+
const confirmedUpdater = targetWithState.__state ? targetWithState.__state.confirmedUpdater : target.confirmedUpdater;
|
|
286
|
+
if (!confirmedUpdater) continue;
|
|
279
287
|
const snapshots = applyUpdate(
|
|
280
288
|
stateManager,
|
|
281
289
|
target,
|
|
282
|
-
|
|
290
|
+
confirmedUpdater,
|
|
283
291
|
response.data,
|
|
284
292
|
t
|
|
285
293
|
);
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@spoosh/plugin-optimistic",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.9.0",
|
|
4
4
|
"description": "Optimistic updates plugin for Spoosh - instant UI updates with automatic rollback",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"repository": {
|
|
@@ -33,12 +33,12 @@
|
|
|
33
33
|
}
|
|
34
34
|
},
|
|
35
35
|
"peerDependencies": {
|
|
36
|
-
"@spoosh/core": ">=0.
|
|
37
|
-
"@spoosh/plugin-invalidation": ">=0.
|
|
36
|
+
"@spoosh/core": ">=0.18.0",
|
|
37
|
+
"@spoosh/plugin-invalidation": ">=0.11.0"
|
|
38
38
|
},
|
|
39
39
|
"devDependencies": {
|
|
40
|
-
"@spoosh/core": "0.
|
|
41
|
-
"@spoosh/plugin-invalidation": "0.
|
|
40
|
+
"@spoosh/core": "0.18.0",
|
|
41
|
+
"@spoosh/plugin-invalidation": "0.11.0",
|
|
42
42
|
"@spoosh/test-utils": "0.3.0"
|
|
43
43
|
},
|
|
44
44
|
"scripts": {
|