@spoosh/plugin-optimistic 0.4.2 → 0.4.4
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.d.mts +1 -2
- package/dist/index.d.ts +1 -2
- package/dist/index.js +6 -14
- package/dist/index.mjs +6 -14
- package/package.json +5 -5
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
|
*
|
|
@@ -279,4 +278,4 @@ declare function optimisticPlugin(): SpooshPlugin<{
|
|
|
279
278
|
writeResult: OptimisticWriteResult;
|
|
280
279
|
}>;
|
|
281
280
|
|
|
282
|
-
export { type AnyOptimisticTarget,
|
|
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
|
*
|
|
@@ -279,4 +278,4 @@ declare function optimisticPlugin(): SpooshPlugin<{
|
|
|
279
278
|
writeResult: OptimisticWriteResult;
|
|
280
279
|
}>;
|
|
281
280
|
|
|
282
|
-
export { type AnyOptimisticTarget,
|
|
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
|
|
171
|
-
allSnapshots.push(...
|
|
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 &&
|
|
185
|
-
rollbackOptimistic(stateManager,
|
|
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 (
|
|
194
|
-
confirmOptimistic(stateManager,
|
|
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
|
|
144
|
-
allSnapshots.push(...
|
|
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 &&
|
|
158
|
-
rollbackOptimistic(stateManager,
|
|
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 (
|
|
167
|
-
confirmOptimistic(stateManager,
|
|
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,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@spoosh/plugin-optimistic",
|
|
3
|
-
"version": "0.4.
|
|
3
|
+
"version": "0.4.4",
|
|
4
4
|
"description": "Optimistic updates plugin for Spoosh - instant UI updates with automatic rollback",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"repository": {
|
|
@@ -33,13 +33,13 @@
|
|
|
33
33
|
}
|
|
34
34
|
},
|
|
35
35
|
"peerDependencies": {
|
|
36
|
-
"@spoosh/core": ">=0.
|
|
36
|
+
"@spoosh/core": ">=0.12.0",
|
|
37
37
|
"@spoosh/plugin-invalidation": ">=0.4.0"
|
|
38
38
|
},
|
|
39
39
|
"devDependencies": {
|
|
40
|
-
"@spoosh/
|
|
41
|
-
"@spoosh/
|
|
42
|
-
"@spoosh/
|
|
40
|
+
"@spoosh/test-utils": "0.1.8",
|
|
41
|
+
"@spoosh/core": "0.12.0",
|
|
42
|
+
"@spoosh/plugin-invalidation": "0.5.7"
|
|
43
43
|
},
|
|
44
44
|
"scripts": {
|
|
45
45
|
"dev": "tsup --watch",
|