@coaction/pinia 2.1.0 → 3.1.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/README.md CHANGED
@@ -2,6 +2,8 @@
2
2
 
3
3
  ![Node CI](https://github.com/coactionjs/coaction/workflows/Node%20CI/badge.svg) [![npm](https://img.shields.io/npm/v/@coaction/pinia.svg)](https://www.npmjs.com/package/@coaction/pinia) ![license](https://img.shields.io/npm/l/@coaction/pinia)
4
4
 
5
+ [English documentation](https://coactionjs.github.io/coaction/en/docs/) · [中文文档](https://coactionjs.github.io/coaction/zh/docs/)
6
+
5
7
  A Coaction integration tool for Pinia
6
8
 
7
9
  ## Installation
package/dist/index.js CHANGED
@@ -1,39 +1,52 @@
1
1
  Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
2
2
  //#endregion
3
- let coaction = require("coaction");
3
+ let coaction_adapter = require("coaction/adapter");
4
4
  let pinia = require("pinia");
5
5
  //#region packages/coaction-pinia/src/index.ts
6
6
  const instancesMap = /* @__PURE__ */ new WeakMap();
7
+ const reportAdapterStateError = (error) => {
8
+ if (process.env.NODE_ENV === "development") console.error(error);
9
+ };
7
10
  const handleStore = (store, state, _, internal) => {
8
11
  const rawState = state;
12
+ internal.getTransportState = () => (0, coaction_adapter.snapshotMutableAdapterPureState)(store);
9
13
  let isApplyingCoactionState = false;
10
14
  let lastSnapshot;
11
15
  const restoreClientState = (snapshot) => {
12
16
  const mutableStore = internal.toMutableRaw(rawState);
13
17
  if (!mutableStore) return;
18
+ const rollbackSnapshot = (0, coaction_adapter.toMutableAdapterSnapshot)(snapshot);
14
19
  isApplyingCoactionState = true;
15
20
  try {
16
- (0, coaction.replaceMutableAdapterState)(internal.rootState ?? rawState, mutableStore, store.getState(), snapshot);
21
+ (0, coaction_adapter.replaceMutableAdapterState)(internal.rootState ?? rawState, mutableStore, store.getState(), rollbackSnapshot);
17
22
  } finally {
18
- lastSnapshot = (0, coaction.snapshotMutableAdapterPureState)(store);
23
+ lastSnapshot = (0, coaction_adapter.snapshotMutableAdapterPureState)(store);
19
24
  isApplyingCoactionState = false;
20
25
  }
21
26
  };
22
27
  const syncSharedExternalChange = () => {
23
- const currentSnapshot = (0, coaction.snapshotMutableAdapterPureState)(store);
28
+ const currentSnapshot = (0, coaction_adapter.snapshotMutableAdapterPureState)(store);
24
29
  if (isApplyingCoactionState) {
25
30
  lastSnapshot = currentSnapshot;
26
31
  return "handled";
27
32
  }
28
33
  if (store.share === "client" && lastSnapshot) {
29
- if (!(0, coaction.isEqualMutableAdapterSnapshot)(currentSnapshot, lastSnapshot)) restoreClientState(lastSnapshot);
34
+ if (!(0, coaction_adapter.isEqualMutableAdapterSnapshot)(currentSnapshot, lastSnapshot)) restoreClientState(lastSnapshot);
30
35
  return "ignored";
31
36
  }
32
37
  if (store.share === "main" && lastSnapshot) {
33
38
  const rootState = internal.rootState;
34
39
  internal.rootState = lastSnapshot;
35
40
  try {
36
- (0, coaction.replaceExternalStoreState)(store, internal, currentSnapshot, { syncImmutable: false });
41
+ (0, coaction_adapter.replaceExternalStoreState)(store, internal, currentSnapshot, { syncImmutable: false });
42
+ } catch (error) {
43
+ try {
44
+ restoreClientState(lastSnapshot);
45
+ } catch (rollbackError) {
46
+ reportAdapterStateError(rollbackError);
47
+ }
48
+ reportAdapterStateError(error);
49
+ return "ignored";
37
50
  } finally {
38
51
  internal.rootState = rootState;
39
52
  }
@@ -68,16 +81,25 @@ const handleStore = (store, state, _, internal) => {
68
81
  };
69
82
  store.apply = (nextState = store.getPureState(), patches) => {
70
83
  internal.assertAlive?.("apply");
84
+ const previousSnapshot = lastSnapshot ?? (0, coaction_adapter.snapshotMutableAdapterPureState)(store);
71
85
  isApplyingCoactionState = true;
72
86
  try {
73
87
  if (!patches) {
74
88
  if (nextState === store.getState()) return;
75
- (0, coaction.replaceMutableAdapterState)(internal.rootState ?? rawState, internal.toMutableRaw(rawState), store.getState(), nextState);
89
+ internal.validateState?.((0, coaction_adapter.toMutableAdapterSnapshot)(nextState));
90
+ (0, coaction_adapter.replaceMutableAdapterState)(internal.rootState ?? rawState, internal.toMutableRaw(rawState), store.getState(), nextState);
76
91
  return;
77
92
  }
78
- (0, coaction.applyMutableAdapterPatches)(nextState, patches, internal.rootState ?? rawState, internal.toMutableRaw(rawState), store.getState());
93
+ (0, coaction_adapter.applyMutableAdapterPatches)(nextState, patches, internal.rootState ?? rawState, internal.toMutableRaw(rawState), store.getState(), internal.validateState);
94
+ } catch (error) {
95
+ try {
96
+ restoreClientState(previousSnapshot);
97
+ } catch (rollbackError) {
98
+ reportAdapterStateError(rollbackError);
99
+ }
100
+ throw error;
79
101
  } finally {
80
- lastSnapshot = (0, coaction.snapshotMutableAdapterPureState)(store);
102
+ lastSnapshot = (0, coaction_adapter.snapshotMutableAdapterPureState)(store);
81
103
  isApplyingCoactionState = false;
82
104
  internal.notifyStateChange?.();
83
105
  }
@@ -86,9 +108,9 @@ const handleStore = (store, state, _, internal) => {
86
108
  const mutableStore = internal.toMutableRaw(state);
87
109
  if (!mutableStore) throw new Error("Pinia store instance is not found");
88
110
  let stopWatch;
89
- const cancelReadySubscription = (0, coaction.onStoreReady)(store, () => {
90
- (0, coaction.replaceMutableAdapterState)(internal.rootState ?? rawState, mutableStore, store.getState(), (0, coaction.sanitizeInitialStateValue)((0, coaction.snapshotMutableAdapterPureState)(store)));
91
- lastSnapshot = (0, coaction.snapshotMutableAdapterPureState)(store);
111
+ const cancelReadySubscription = (0, coaction_adapter.onStoreReady)(store, () => {
112
+ (0, coaction_adapter.replaceMutableAdapterState)(internal.rootState ?? rawState, mutableStore, store.getState(), (0, coaction_adapter.sanitizeInitialStateValue)((0, coaction_adapter.snapshotMutableAdapterPureState)(store)));
113
+ lastSnapshot = (0, coaction_adapter.snapshotMutableAdapterPureState)(store);
92
114
  stopWatch = mutableStore.$subscribe((...args) => {
93
115
  const change = syncSharedExternalChange();
94
116
  if (change === "ignored") return;
@@ -106,7 +128,7 @@ const handleStore = (store, state, _, internal) => {
106
128
  /**
107
129
  * Bind a store to Pinia
108
130
  */
109
- const bindPinia = (0, coaction.createBinder)({
131
+ const bindPinia = (0, coaction_adapter.createBinder)({
110
132
  handleStore,
111
133
  handleState: ((options) => {
112
134
  const descriptors = {};
package/dist/index.mjs CHANGED
@@ -1,19 +1,24 @@
1
- import { applyMutableAdapterPatches, createBinder, isEqualMutableAdapterSnapshot, onStoreReady, replaceExternalStoreState, replaceMutableAdapterState, sanitizeInitialStateValue, snapshotMutableAdapterPureState } from "coaction";
1
+ import { applyMutableAdapterPatches, createBinder, isEqualMutableAdapterSnapshot, onStoreReady, replaceExternalStoreState, replaceMutableAdapterState, sanitizeInitialStateValue, snapshotMutableAdapterPureState, toMutableAdapterSnapshot } from "coaction/adapter";
2
2
  import { createPinia } from "pinia";
3
3
  export * from "pinia";
4
4
  //#endregion
5
5
  //#region packages/coaction-pinia/src/index.ts
6
6
  const instancesMap = /* @__PURE__ */ new WeakMap();
7
+ const reportAdapterStateError = (error) => {
8
+ if (process.env.NODE_ENV === "development") console.error(error);
9
+ };
7
10
  const handleStore = (store, state, _, internal) => {
8
11
  const rawState = state;
12
+ internal.getTransportState = () => snapshotMutableAdapterPureState(store);
9
13
  let isApplyingCoactionState = false;
10
14
  let lastSnapshot;
11
15
  const restoreClientState = (snapshot) => {
12
16
  const mutableStore = internal.toMutableRaw(rawState);
13
17
  if (!mutableStore) return;
18
+ const rollbackSnapshot = toMutableAdapterSnapshot(snapshot);
14
19
  isApplyingCoactionState = true;
15
20
  try {
16
- replaceMutableAdapterState(internal.rootState ?? rawState, mutableStore, store.getState(), snapshot);
21
+ replaceMutableAdapterState(internal.rootState ?? rawState, mutableStore, store.getState(), rollbackSnapshot);
17
22
  } finally {
18
23
  lastSnapshot = snapshotMutableAdapterPureState(store);
19
24
  isApplyingCoactionState = false;
@@ -34,6 +39,14 @@ const handleStore = (store, state, _, internal) => {
34
39
  internal.rootState = lastSnapshot;
35
40
  try {
36
41
  replaceExternalStoreState(store, internal, currentSnapshot, { syncImmutable: false });
42
+ } catch (error) {
43
+ try {
44
+ restoreClientState(lastSnapshot);
45
+ } catch (rollbackError) {
46
+ reportAdapterStateError(rollbackError);
47
+ }
48
+ reportAdapterStateError(error);
49
+ return "ignored";
37
50
  } finally {
38
51
  internal.rootState = rootState;
39
52
  }
@@ -68,14 +81,23 @@ const handleStore = (store, state, _, internal) => {
68
81
  };
69
82
  store.apply = (nextState = store.getPureState(), patches) => {
70
83
  internal.assertAlive?.("apply");
84
+ const previousSnapshot = lastSnapshot ?? snapshotMutableAdapterPureState(store);
71
85
  isApplyingCoactionState = true;
72
86
  try {
73
87
  if (!patches) {
74
88
  if (nextState === store.getState()) return;
89
+ internal.validateState?.(toMutableAdapterSnapshot(nextState));
75
90
  replaceMutableAdapterState(internal.rootState ?? rawState, internal.toMutableRaw(rawState), store.getState(), nextState);
76
91
  return;
77
92
  }
78
- applyMutableAdapterPatches(nextState, patches, internal.rootState ?? rawState, internal.toMutableRaw(rawState), store.getState());
93
+ applyMutableAdapterPatches(nextState, patches, internal.rootState ?? rawState, internal.toMutableRaw(rawState), store.getState(), internal.validateState);
94
+ } catch (error) {
95
+ try {
96
+ restoreClientState(previousSnapshot);
97
+ } catch (rollbackError) {
98
+ reportAdapterStateError(rollbackError);
99
+ }
100
+ throw error;
79
101
  } finally {
80
102
  lastSnapshot = snapshotMutableAdapterPureState(store);
81
103
  isApplyingCoactionState = false;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@coaction/pinia",
3
- "version": "2.1.0",
3
+ "version": "3.1.0",
4
4
  "description": "A Coaction integration tool for Pinia",
5
5
  "keywords": [
6
6
  "coaction",
@@ -50,10 +50,10 @@
50
50
  "data-transport": "^5.0.3",
51
51
  "mutative": "^1.3.0",
52
52
  "pinia": "^3.0.4",
53
- "coaction": "2.1.0"
53
+ "coaction": "3.1.0"
54
54
  },
55
55
  "peerDependencies": {
56
- "coaction": ">=2.1.0 <3",
56
+ "coaction": "^3.1.0",
57
57
  "pinia": "^2.0 || ^3.0.0"
58
58
  },
59
59
  "authors": [