@spoosh/plugin-optimistic 0.4.1 → 0.4.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/README.md CHANGED
@@ -2,7 +2,7 @@
2
2
 
3
3
  Optimistic updates plugin for Spoosh - instant UI updates with automatic rollback on error.
4
4
 
5
- **[Documentation](https://spoosh.dev/docs/plugins/optimistic)** · **Requirements:** TypeScript >= 5.0 · **Peer Dependencies:** `@spoosh/core`, `@spoosh/plugin-invalidation`
5
+ **[Documentation](https://spoosh.dev/docs/react/plugins/optimistic)** · **Requirements:** TypeScript >= 5.0 · **Peer Dependencies:** `@spoosh/core`, `@spoosh/plugin-invalidation`
6
6
 
7
7
  ## Installation
8
8
 
package/dist/index.d.mts CHANGED
@@ -215,7 +215,6 @@ declare module "@spoosh/core" {
215
215
  }
216
216
  }
217
217
 
218
- declare const OPTIMISTIC_SNAPSHOTS_KEY = "optimistic:snapshots";
219
218
  /**
220
219
  * Enables optimistic updates for mutations.
221
220
  *
@@ -226,7 +225,7 @@ declare const OPTIMISTIC_SNAPSHOTS_KEY = "optimistic:snapshots";
226
225
  * unnecessary refetches that would override the optimistic data. You can override
227
226
  * this by explicitly setting the `invalidate` option with a mode string or array.
228
227
  *
229
- * @see {@link https://spoosh.dev/docs/plugins/optimistic | Optimistic Plugin Documentation}
228
+ * @see {@link https://spoosh.dev/docs/react/plugins/optimistic | Optimistic Plugin Documentation}
230
229
  *
231
230
  * @returns Optimistic plugin instance
232
231
  *
@@ -279,4 +278,4 @@ declare function optimisticPlugin(): SpooshPlugin<{
279
278
  writeResult: OptimisticWriteResult;
280
279
  }>;
281
280
 
282
- export { type AnyOptimisticTarget, OPTIMISTIC_SNAPSHOTS_KEY, type OptimisticApiHelper, type OptimisticBuilder, type OptimisticCallbackFn, type OptimisticInfiniteReadOptions, type OptimisticPluginConfig, type OptimisticReadOptions, type OptimisticReadResult, type OptimisticTarget, type OptimisticWriteOptions, type OptimisticWriteResult, optimisticPlugin };
281
+ export { type AnyOptimisticTarget, type OptimisticApiHelper, type OptimisticBuilder, type OptimisticCallbackFn, type OptimisticInfiniteReadOptions, type OptimisticPluginConfig, type OptimisticReadOptions, type OptimisticReadResult, type OptimisticTarget, type OptimisticWriteOptions, type OptimisticWriteResult, optimisticPlugin };
package/dist/index.d.ts CHANGED
@@ -215,7 +215,6 @@ declare module "@spoosh/core" {
215
215
  }
216
216
  }
217
217
 
218
- declare const OPTIMISTIC_SNAPSHOTS_KEY = "optimistic:snapshots";
219
218
  /**
220
219
  * Enables optimistic updates for mutations.
221
220
  *
@@ -226,7 +225,7 @@ declare const OPTIMISTIC_SNAPSHOTS_KEY = "optimistic:snapshots";
226
225
  * unnecessary refetches that would override the optimistic data. You can override
227
226
  * this by explicitly setting the `invalidate` option with a mode string or array.
228
227
  *
229
- * @see {@link https://spoosh.dev/docs/plugins/optimistic | Optimistic Plugin Documentation}
228
+ * @see {@link https://spoosh.dev/docs/react/plugins/optimistic | Optimistic Plugin Documentation}
230
229
  *
231
230
  * @returns Optimistic plugin instance
232
231
  *
@@ -279,4 +278,4 @@ declare function optimisticPlugin(): SpooshPlugin<{
279
278
  writeResult: OptimisticWriteResult;
280
279
  }>;
281
280
 
282
- export { type AnyOptimisticTarget, OPTIMISTIC_SNAPSHOTS_KEY, type OptimisticApiHelper, type OptimisticBuilder, type OptimisticCallbackFn, type OptimisticInfiniteReadOptions, type OptimisticPluginConfig, type OptimisticReadOptions, type OptimisticReadResult, type OptimisticTarget, type OptimisticWriteOptions, type OptimisticWriteResult, optimisticPlugin };
281
+ export { type AnyOptimisticTarget, type OptimisticApiHelper, type OptimisticBuilder, type OptimisticCallbackFn, type OptimisticInfiniteReadOptions, type OptimisticPluginConfig, type OptimisticReadOptions, type OptimisticReadResult, type OptimisticTarget, type OptimisticWriteOptions, type OptimisticWriteResult, optimisticPlugin };
package/dist/index.js CHANGED
@@ -20,7 +20,6 @@ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: tru
20
20
  // src/index.ts
21
21
  var src_exports = {};
22
22
  __export(src_exports, {
23
- OPTIMISTIC_SNAPSHOTS_KEY: () => OPTIMISTIC_SNAPSHOTS_KEY,
24
23
  optimisticPlugin: () => optimisticPlugin
25
24
  });
26
25
  module.exports = __toCommonJS(src_exports);
@@ -28,7 +27,6 @@ module.exports = __toCommonJS(src_exports);
28
27
  // src/plugin.ts
29
28
  var import_core = require("@spoosh/core");
30
29
  var import_plugin_invalidation = require("@spoosh/plugin-invalidation");
31
- var OPTIMISTIC_SNAPSHOTS_KEY = "optimistic:snapshots";
32
30
  function createBuilder(state) {
33
31
  return {
34
32
  ...state,
@@ -167,22 +165,16 @@ function optimisticPlugin() {
167
165
  const immediateTargets = targets.filter((t) => t.timing !== "onSuccess");
168
166
  const allSnapshots = [];
169
167
  for (const target of immediateTargets) {
170
- const snapshots2 = applyOptimisticUpdate(stateManager, target);
171
- allSnapshots.push(...snapshots2);
172
- }
173
- if (allSnapshots.length > 0) {
174
- context.metadata.set(OPTIMISTIC_SNAPSHOTS_KEY, allSnapshots);
168
+ const snapshots = applyOptimisticUpdate(stateManager, target);
169
+ allSnapshots.push(...snapshots);
175
170
  }
176
171
  const response = await next();
177
- const snapshots = context.metadata.get(
178
- OPTIMISTIC_SNAPSHOTS_KEY
179
- ) ?? [];
180
172
  if (response.error) {
181
173
  const shouldRollback = targets.some(
182
174
  (t) => t.rollbackOnError && t.timing !== "onSuccess"
183
175
  );
184
- if (shouldRollback && snapshots.length > 0) {
185
- rollbackOptimistic(stateManager, snapshots);
176
+ if (shouldRollback && allSnapshots.length > 0) {
177
+ rollbackOptimistic(stateManager, allSnapshots);
186
178
  }
187
179
  for (const target of targets) {
188
180
  if (target.onError) {
@@ -190,8 +182,8 @@ function optimisticPlugin() {
190
182
  }
191
183
  }
192
184
  } else {
193
- if (snapshots.length > 0) {
194
- confirmOptimistic(stateManager, snapshots);
185
+ if (allSnapshots.length > 0) {
186
+ confirmOptimistic(stateManager, allSnapshots);
195
187
  }
196
188
  const onSuccessTargets = targets.filter(
197
189
  (t) => t.timing === "onSuccess"
package/dist/index.mjs CHANGED
@@ -1,7 +1,6 @@
1
1
  // src/plugin.ts
2
2
  import { generateTags } from "@spoosh/core";
3
3
  import "@spoosh/plugin-invalidation";
4
- var OPTIMISTIC_SNAPSHOTS_KEY = "optimistic:snapshots";
5
4
  function createBuilder(state) {
6
5
  return {
7
6
  ...state,
@@ -140,22 +139,16 @@ function optimisticPlugin() {
140
139
  const immediateTargets = targets.filter((t) => t.timing !== "onSuccess");
141
140
  const allSnapshots = [];
142
141
  for (const target of immediateTargets) {
143
- const snapshots2 = applyOptimisticUpdate(stateManager, target);
144
- allSnapshots.push(...snapshots2);
145
- }
146
- if (allSnapshots.length > 0) {
147
- context.metadata.set(OPTIMISTIC_SNAPSHOTS_KEY, allSnapshots);
142
+ const snapshots = applyOptimisticUpdate(stateManager, target);
143
+ allSnapshots.push(...snapshots);
148
144
  }
149
145
  const response = await next();
150
- const snapshots = context.metadata.get(
151
- OPTIMISTIC_SNAPSHOTS_KEY
152
- ) ?? [];
153
146
  if (response.error) {
154
147
  const shouldRollback = targets.some(
155
148
  (t) => t.rollbackOnError && t.timing !== "onSuccess"
156
149
  );
157
- if (shouldRollback && snapshots.length > 0) {
158
- rollbackOptimistic(stateManager, snapshots);
150
+ if (shouldRollback && allSnapshots.length > 0) {
151
+ rollbackOptimistic(stateManager, allSnapshots);
159
152
  }
160
153
  for (const target of targets) {
161
154
  if (target.onError) {
@@ -163,8 +156,8 @@ function optimisticPlugin() {
163
156
  }
164
157
  }
165
158
  } else {
166
- if (snapshots.length > 0) {
167
- confirmOptimistic(stateManager, snapshots);
159
+ if (allSnapshots.length > 0) {
160
+ confirmOptimistic(stateManager, allSnapshots);
168
161
  }
169
162
  const onSuccessTargets = targets.filter(
170
163
  (t) => t.timing === "onSuccess"
@@ -205,6 +198,5 @@ function optimisticPlugin() {
205
198
  };
206
199
  }
207
200
  export {
208
- OPTIMISTIC_SNAPSHOTS_KEY,
209
201
  optimisticPlugin
210
202
  };
package/package.json CHANGED
@@ -1,17 +1,17 @@
1
1
  {
2
2
  "name": "@spoosh/plugin-optimistic",
3
- "version": "0.4.1",
3
+ "version": "0.4.3",
4
4
  "description": "Optimistic updates plugin for Spoosh - instant UI updates with automatic rollback",
5
5
  "license": "MIT",
6
6
  "repository": {
7
7
  "type": "git",
8
- "url": "git+https://github.com/nxnom/spoosh.git",
8
+ "url": "git+https://github.com/spooshdev/spoosh.git",
9
9
  "directory": "packages/plugin-optimistic"
10
10
  },
11
11
  "bugs": {
12
- "url": "https://github.com/nxnom/spoosh/issues"
12
+ "url": "https://github.com/spooshdev/spoosh/issues"
13
13
  },
14
- "homepage": "https://spoosh.dev/react/docs/plugins/optimistic",
14
+ "homepage": "https://spoosh.dev/docs/react/plugins/optimistic",
15
15
  "publishConfig": {
16
16
  "access": "public"
17
17
  },
@@ -33,13 +33,13 @@
33
33
  }
34
34
  },
35
35
  "peerDependencies": {
36
- "@spoosh/core": ">=0.9.0",
36
+ "@spoosh/core": ">=0.11.1",
37
37
  "@spoosh/plugin-invalidation": ">=0.4.0"
38
38
  },
39
39
  "devDependencies": {
40
- "@spoosh/core": "0.9.0",
41
- "@spoosh/test-utils": "0.1.5",
42
- "@spoosh/plugin-invalidation": "0.5.0"
40
+ "@spoosh/core": "0.11.1",
41
+ "@spoosh/test-utils": "0.1.6",
42
+ "@spoosh/plugin-invalidation": "0.5.6"
43
43
  },
44
44
  "scripts": {
45
45
  "dev": "tsup --watch",