@spoosh/plugin-optimistic 0.8.1 → 0.8.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/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
- if (target.filter) {
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 (!target.filter(mergedOptions)) {
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);
@@ -259,11 +258,13 @@ function optimisticPlugin() {
259
258
  context.plugins.get("spoosh:invalidation")?.setDefaultMode("none");
260
259
  const allImmediateSnapshots = [];
261
260
  for (const target of targets) {
262
- if (!target.immediateUpdater) continue;
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
- target.immediateUpdater,
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
- (target) => target.rollbackOnError && target.immediateUpdater
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
- if (target.onError) {
294
- target.onError(response.error);
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
- if (!target.confirmedUpdater) continue;
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
- target.confirmedUpdater,
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
- if (target.filter) {
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 (!target.filter(mergedOptions)) {
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);
@@ -235,11 +234,13 @@ function optimisticPlugin() {
235
234
  context.plugins.get("spoosh:invalidation")?.setDefaultMode("none");
236
235
  const allImmediateSnapshots = [];
237
236
  for (const target of targets) {
238
- if (!target.immediateUpdater) continue;
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
- target.immediateUpdater,
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
- (target) => target.rollbackOnError && target.immediateUpdater
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
- if (target.onError) {
270
- target.onError(response.error);
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
- if (!target.confirmedUpdater) continue;
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
- target.confirmedUpdater,
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.8.1",
3
+ "version": "0.8.2",
4
4
  "description": "Optimistic updates plugin for Spoosh - instant UI updates with automatic rollback",
5
5
  "license": "MIT",
6
6
  "repository": {
@@ -38,8 +38,8 @@
38
38
  },
39
39
  "devDependencies": {
40
40
  "@spoosh/core": "0.17.1",
41
- "@spoosh/plugin-invalidation": "0.10.0",
42
- "@spoosh/test-utils": "0.3.0"
41
+ "@spoosh/test-utils": "0.3.0",
42
+ "@spoosh/plugin-invalidation": "0.10.0"
43
43
  },
44
44
  "scripts": {
45
45
  "dev": "tsup --watch",