@coaction/pinia 2.0.0 → 3.0.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 +26 -18
- package/dist/index.js +47 -113
- package/dist/index.mjs +45 -111
- package/package.json +11 -6
package/README.md
CHANGED
|
@@ -1,40 +1,42 @@
|
|
|
1
1
|
# @coaction/pinia
|
|
2
2
|
|
|
3
|
-

|
|
4
|
-
|
|
5
|
-
|
|
3
|
+
 [](https://www.npmjs.com/package/@coaction/pinia) 
|
|
4
|
+
|
|
5
|
+
[English documentation](https://coactionjs.github.io/coaction/en/docs/) · [中文文档](https://coactionjs.github.io/coaction/zh/docs/)
|
|
6
6
|
|
|
7
7
|
A Coaction integration tool for Pinia
|
|
8
8
|
|
|
9
9
|
## Installation
|
|
10
10
|
|
|
11
|
-
|
|
11
|
+
Install it with pnpm:
|
|
12
12
|
|
|
13
13
|
```sh
|
|
14
|
-
|
|
14
|
+
pnpm add coaction @coaction/pinia
|
|
15
15
|
```
|
|
16
16
|
|
|
17
17
|
## Usage
|
|
18
18
|
|
|
19
19
|
```js
|
|
20
20
|
import { create } from 'coaction';
|
|
21
|
-
import { bindPinia } from '@coaction/pinia';
|
|
21
|
+
import { adapt, bindPinia } from '@coaction/pinia';
|
|
22
22
|
import { defineStore } from 'pinia';
|
|
23
23
|
|
|
24
24
|
const useStore = create(() =>
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
state
|
|
25
|
+
adapt(
|
|
26
|
+
defineStore(
|
|
27
|
+
'test',
|
|
28
|
+
bindPinia({
|
|
29
|
+
state: () => ({ count: 0 }),
|
|
30
|
+
getters: {
|
|
31
|
+
double: (state) => state.count * 2
|
|
32
|
+
},
|
|
33
|
+
actions: {
|
|
34
|
+
increment(state) {
|
|
35
|
+
state.count += 1;
|
|
36
|
+
}
|
|
35
37
|
}
|
|
36
|
-
}
|
|
37
|
-
|
|
38
|
+
})
|
|
39
|
+
)
|
|
38
40
|
)
|
|
39
41
|
);
|
|
40
42
|
```
|
|
@@ -43,6 +45,12 @@ const useStore = create(() =>
|
|
|
43
45
|
|
|
44
46
|
- `@coaction/pinia` only supports binding a whole Pinia store.
|
|
45
47
|
- Coaction `Slices` mode is not supported in this adapter.
|
|
48
|
+
- `bindPinia()` does not set Pinia's global active instance. Use `adapt()` for
|
|
49
|
+
Coaction-owned Pinia definitions, or provide your own active Pinia when using
|
|
50
|
+
raw Pinia store definitions directly.
|
|
51
|
+
- Unknown root properties written directly to the Pinia store are ignored by
|
|
52
|
+
Coaction's fixed schema. They are not promoted into Coaction raw/public state,
|
|
53
|
+
and this adapter does not guarantee pruning them from the Pinia store.
|
|
46
54
|
|
|
47
55
|
## Documentation
|
|
48
56
|
|
package/dist/index.js
CHANGED
|
@@ -1,137 +1,52 @@
|
|
|
1
1
|
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
|
|
2
2
|
//#endregion
|
|
3
|
-
let
|
|
4
|
-
let coaction = require("coaction");
|
|
3
|
+
let coaction_adapter = require("coaction/adapter");
|
|
5
4
|
let pinia = require("pinia");
|
|
6
5
|
//#region packages/coaction-pinia/src/index.ts
|
|
7
6
|
const instancesMap = /* @__PURE__ */ new WeakMap();
|
|
8
|
-
const
|
|
9
|
-
|
|
10
|
-
const isArrayIndexKey = (key) => {
|
|
11
|
-
if (typeof key !== "string") return false;
|
|
12
|
-
const index = Number(key);
|
|
13
|
-
return Number.isInteger(index) && index >= 0 && index < 2 ** 32 - 1 && String(index) === key;
|
|
14
|
-
};
|
|
15
|
-
const isObjectRecord = (value) => Object.prototype.toString.call(value) === "[object Object]";
|
|
16
|
-
const replaceMutableState = (rawState, mutableState, publicState, source) => {
|
|
17
|
-
const nextKeys = /* @__PURE__ */ new Set();
|
|
18
|
-
for (const key of getOwnEnumerableKeys(source)) {
|
|
19
|
-
if (isUnsafeKey(key)) continue;
|
|
20
|
-
if (typeof source[key] === "function") continue;
|
|
21
|
-
nextKeys.add(key);
|
|
22
|
-
}
|
|
23
|
-
for (const key of getOwnEnumerableKeys(rawState)) {
|
|
24
|
-
if (isUnsafeKey(key)) {
|
|
25
|
-
delete rawState[key];
|
|
26
|
-
delete mutableState[key];
|
|
27
|
-
delete publicState[key];
|
|
28
|
-
continue;
|
|
29
|
-
}
|
|
30
|
-
if (typeof rawState[key] === "function") continue;
|
|
31
|
-
if (!nextKeys.has(key)) {
|
|
32
|
-
delete rawState[key];
|
|
33
|
-
delete mutableState[key];
|
|
34
|
-
delete publicState[key];
|
|
35
|
-
}
|
|
36
|
-
}
|
|
37
|
-
const rawSeen = /* @__PURE__ */ new WeakMap();
|
|
38
|
-
const mutableSeen = /* @__PURE__ */ new WeakMap();
|
|
39
|
-
const publicSeen = /* @__PURE__ */ new WeakMap();
|
|
40
|
-
rawSeen.set(source, rawState);
|
|
41
|
-
mutableSeen.set(source, mutableState);
|
|
42
|
-
publicSeen.set(source, publicState);
|
|
43
|
-
nextKeys.forEach((key) => {
|
|
44
|
-
rawState[key] = (0, coaction.sanitizeReplacementState)(source[key], rawSeen);
|
|
45
|
-
mutableState[key] = (0, coaction.sanitizeReplacementState)(source[key], mutableSeen);
|
|
46
|
-
publicState[key] = (0, coaction.sanitizeReplacementState)(source[key], publicSeen);
|
|
47
|
-
});
|
|
48
|
-
};
|
|
49
|
-
const toSnapshot = (value, visited = /* @__PURE__ */ new WeakMap()) => {
|
|
50
|
-
if (Array.isArray(value)) {
|
|
51
|
-
if (visited.has(value)) return visited.get(value);
|
|
52
|
-
const next = [];
|
|
53
|
-
next.length = value.length;
|
|
54
|
-
visited.set(value, next);
|
|
55
|
-
for (let index = 0; index < value.length; index += 1) if (Object.prototype.hasOwnProperty.call(value, index)) next[index] = toSnapshot(value[index], visited);
|
|
56
|
-
const source = value;
|
|
57
|
-
const target = next;
|
|
58
|
-
for (const key of getOwnEnumerableKeys(value)) {
|
|
59
|
-
if (isArrayIndexKey(key) || isUnsafeKey(key)) continue;
|
|
60
|
-
const child = source[key];
|
|
61
|
-
if (typeof child !== "function") target[key] = toSnapshot(child, visited);
|
|
62
|
-
}
|
|
63
|
-
return next;
|
|
64
|
-
}
|
|
65
|
-
if (typeof value === "object" && value !== null) {
|
|
66
|
-
if (!isObjectRecord(value)) return value;
|
|
67
|
-
if (visited.has(value)) return visited.get(value);
|
|
68
|
-
const next = {};
|
|
69
|
-
visited.set(value, next);
|
|
70
|
-
for (const key of getOwnEnumerableKeys(value)) {
|
|
71
|
-
if (isUnsafeKey(key)) continue;
|
|
72
|
-
const child = value[key];
|
|
73
|
-
if (typeof child !== "function") next[key] = toSnapshot(child, visited);
|
|
74
|
-
}
|
|
75
|
-
return next;
|
|
76
|
-
}
|
|
77
|
-
return value;
|
|
78
|
-
};
|
|
79
|
-
const snapshotPureState = (store) => toSnapshot(store.getPureState());
|
|
80
|
-
const isEqualSnapshot = (left, right, visited = /* @__PURE__ */ new WeakMap()) => {
|
|
81
|
-
if (Object.is(left, right)) return true;
|
|
82
|
-
if (typeof left !== "object" || left === null || typeof right !== "object" || right === null) return false;
|
|
83
|
-
const leftIsArray = Array.isArray(left);
|
|
84
|
-
const rightIsArray = Array.isArray(right);
|
|
85
|
-
if (leftIsArray || rightIsArray) {
|
|
86
|
-
if (!leftIsArray || !rightIsArray || left.length !== right.length) return false;
|
|
87
|
-
} else if (!isObjectRecord(left) || !isObjectRecord(right)) return false;
|
|
88
|
-
let seenTargets = visited.get(left);
|
|
89
|
-
if (!seenTargets) {
|
|
90
|
-
seenTargets = /* @__PURE__ */ new WeakSet();
|
|
91
|
-
visited.set(left, seenTargets);
|
|
92
|
-
} else if (seenTargets.has(right)) return true;
|
|
93
|
-
seenTargets.add(right);
|
|
94
|
-
const leftRecord = left;
|
|
95
|
-
const rightRecord = right;
|
|
96
|
-
const leftKeys = getOwnEnumerableKeys(left);
|
|
97
|
-
const rightKeys = getOwnEnumerableKeys(right);
|
|
98
|
-
if (leftKeys.length !== rightKeys.length) return false;
|
|
99
|
-
for (const key of leftKeys) {
|
|
100
|
-
if (!Object.prototype.hasOwnProperty.call(rightRecord, key)) return false;
|
|
101
|
-
if (!isEqualSnapshot(leftRecord[key], rightRecord[key], visited)) return false;
|
|
102
|
-
}
|
|
103
|
-
return true;
|
|
7
|
+
const reportAdapterStateError = (error) => {
|
|
8
|
+
if (process.env.NODE_ENV === "development") console.error(error);
|
|
104
9
|
};
|
|
105
10
|
const handleStore = (store, state, _, internal) => {
|
|
106
11
|
const rawState = state;
|
|
12
|
+
internal.getTransportState = () => (0, coaction_adapter.snapshotMutableAdapterPureState)(store);
|
|
107
13
|
let isApplyingCoactionState = false;
|
|
108
14
|
let lastSnapshot;
|
|
109
15
|
const restoreClientState = (snapshot) => {
|
|
110
16
|
const mutableStore = internal.toMutableRaw(rawState);
|
|
111
17
|
if (!mutableStore) return;
|
|
18
|
+
const rollbackSnapshot = (0, coaction_adapter.toMutableAdapterSnapshot)(snapshot);
|
|
112
19
|
isApplyingCoactionState = true;
|
|
113
20
|
try {
|
|
114
|
-
|
|
21
|
+
(0, coaction_adapter.replaceMutableAdapterState)(internal.rootState ?? rawState, mutableStore, store.getState(), rollbackSnapshot);
|
|
115
22
|
} finally {
|
|
116
|
-
lastSnapshot =
|
|
23
|
+
lastSnapshot = (0, coaction_adapter.snapshotMutableAdapterPureState)(store);
|
|
117
24
|
isApplyingCoactionState = false;
|
|
118
25
|
}
|
|
119
26
|
};
|
|
120
27
|
const syncSharedExternalChange = () => {
|
|
121
|
-
const currentSnapshot =
|
|
28
|
+
const currentSnapshot = (0, coaction_adapter.snapshotMutableAdapterPureState)(store);
|
|
122
29
|
if (isApplyingCoactionState) {
|
|
123
30
|
lastSnapshot = currentSnapshot;
|
|
124
31
|
return "handled";
|
|
125
32
|
}
|
|
126
33
|
if (store.share === "client" && lastSnapshot) {
|
|
127
|
-
if (!
|
|
34
|
+
if (!(0, coaction_adapter.isEqualMutableAdapterSnapshot)(currentSnapshot, lastSnapshot)) restoreClientState(lastSnapshot);
|
|
128
35
|
return "ignored";
|
|
129
36
|
}
|
|
130
37
|
if (store.share === "main" && lastSnapshot) {
|
|
131
38
|
const rootState = internal.rootState;
|
|
132
39
|
internal.rootState = lastSnapshot;
|
|
133
40
|
try {
|
|
134
|
-
(0,
|
|
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";
|
|
135
50
|
} finally {
|
|
136
51
|
internal.rootState = rootState;
|
|
137
52
|
}
|
|
@@ -144,6 +59,7 @@ const handleStore = (store, state, _, internal) => {
|
|
|
144
59
|
if (!internal.toMutableRaw) {
|
|
145
60
|
internal.toMutableRaw = (key) => instancesMap.get(key);
|
|
146
61
|
Object.assign(store, { subscribe: (callback) => {
|
|
62
|
+
internal.assertAlive?.("subscribe");
|
|
147
63
|
store._subscriptions.add(callback);
|
|
148
64
|
return () => {
|
|
149
65
|
store._subscriptions?.delete(callback);
|
|
@@ -163,17 +79,27 @@ const handleStore = (store, state, _, internal) => {
|
|
|
163
79
|
store._destroyers = void 0;
|
|
164
80
|
baseDestroy();
|
|
165
81
|
};
|
|
166
|
-
store.apply = (nextState = store.
|
|
82
|
+
store.apply = (nextState = store.getPureState(), patches) => {
|
|
83
|
+
internal.assertAlive?.("apply");
|
|
84
|
+
const previousSnapshot = lastSnapshot ?? (0, coaction_adapter.snapshotMutableAdapterPureState)(store);
|
|
167
85
|
isApplyingCoactionState = true;
|
|
168
86
|
try {
|
|
169
87
|
if (!patches) {
|
|
170
88
|
if (nextState === store.getState()) return;
|
|
171
|
-
|
|
89
|
+
internal.validateState?.((0, coaction_adapter.toMutableAdapterSnapshot)(nextState));
|
|
90
|
+
(0, coaction_adapter.replaceMutableAdapterState)(internal.rootState ?? rawState, internal.toMutableRaw(rawState), store.getState(), nextState);
|
|
172
91
|
return;
|
|
173
92
|
}
|
|
174
|
-
(0,
|
|
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;
|
|
175
101
|
} finally {
|
|
176
|
-
lastSnapshot =
|
|
102
|
+
lastSnapshot = (0, coaction_adapter.snapshotMutableAdapterPureState)(store);
|
|
177
103
|
isApplyingCoactionState = false;
|
|
178
104
|
internal.notifyStateChange?.();
|
|
179
105
|
}
|
|
@@ -182,9 +108,9 @@ const handleStore = (store, state, _, internal) => {
|
|
|
182
108
|
const mutableStore = internal.toMutableRaw(state);
|
|
183
109
|
if (!mutableStore) throw new Error("Pinia store instance is not found");
|
|
184
110
|
let stopWatch;
|
|
185
|
-
const cancelReadySubscription = (0,
|
|
186
|
-
|
|
187
|
-
lastSnapshot =
|
|
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);
|
|
188
114
|
stopWatch = mutableStore.$subscribe((...args) => {
|
|
189
115
|
const change = syncSharedExternalChange();
|
|
190
116
|
if (change === "ignored") return;
|
|
@@ -202,7 +128,7 @@ const handleStore = (store, state, _, internal) => {
|
|
|
202
128
|
/**
|
|
203
129
|
* Bind a store to Pinia
|
|
204
130
|
*/
|
|
205
|
-
const bindPinia = (0,
|
|
131
|
+
const bindPinia = (0, coaction_adapter.createBinder)({
|
|
206
132
|
handleStore,
|
|
207
133
|
handleState: ((options) => {
|
|
208
134
|
const descriptors = {};
|
|
@@ -219,7 +145,6 @@ const bindPinia = (0, coaction.createBinder)({
|
|
|
219
145
|
...options.state?.(),
|
|
220
146
|
...options.actions
|
|
221
147
|
}, descriptors);
|
|
222
|
-
(0, pinia.setActivePinia)((0, pinia.createPinia)());
|
|
223
148
|
return {
|
|
224
149
|
copyState: options,
|
|
225
150
|
key: "actions",
|
|
@@ -233,7 +158,16 @@ const bindPinia = (0, coaction.createBinder)({
|
|
|
233
158
|
/**
|
|
234
159
|
* Adapt a store type to Pinia
|
|
235
160
|
*/
|
|
236
|
-
const adapt = (store) =>
|
|
161
|
+
const adapt = (store) => {
|
|
162
|
+
if (typeof store !== "function") return store;
|
|
163
|
+
const pinia$1 = (0, pinia.createPinia)();
|
|
164
|
+
const boundStore = ((...args) => {
|
|
165
|
+
const [piniaOverride, hot] = args;
|
|
166
|
+
return store(piniaOverride ?? pinia$1, hot);
|
|
167
|
+
});
|
|
168
|
+
Object.assign(boundStore, store);
|
|
169
|
+
return boundStore;
|
|
170
|
+
};
|
|
237
171
|
//#endregion
|
|
238
172
|
exports.adapt = adapt;
|
|
239
173
|
exports.bindPinia = bindPinia;
|
package/dist/index.mjs
CHANGED
|
@@ -1,130 +1,37 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import {
|
|
3
|
-
import { createPinia, setActivePinia } from "pinia";
|
|
1
|
+
import { applyMutableAdapterPatches, createBinder, isEqualMutableAdapterSnapshot, onStoreReady, replaceExternalStoreState, replaceMutableAdapterState, sanitizeInitialStateValue, snapshotMutableAdapterPureState, toMutableAdapterSnapshot } from "coaction/adapter";
|
|
2
|
+
import { createPinia } from "pinia";
|
|
4
3
|
export * from "pinia";
|
|
5
4
|
//#endregion
|
|
6
5
|
//#region packages/coaction-pinia/src/index.ts
|
|
7
6
|
const instancesMap = /* @__PURE__ */ new WeakMap();
|
|
8
|
-
const
|
|
9
|
-
|
|
10
|
-
const isArrayIndexKey = (key) => {
|
|
11
|
-
if (typeof key !== "string") return false;
|
|
12
|
-
const index = Number(key);
|
|
13
|
-
return Number.isInteger(index) && index >= 0 && index < 2 ** 32 - 1 && String(index) === key;
|
|
14
|
-
};
|
|
15
|
-
const isObjectRecord = (value) => Object.prototype.toString.call(value) === "[object Object]";
|
|
16
|
-
const replaceMutableState = (rawState, mutableState, publicState, source) => {
|
|
17
|
-
const nextKeys = /* @__PURE__ */ new Set();
|
|
18
|
-
for (const key of getOwnEnumerableKeys(source)) {
|
|
19
|
-
if (isUnsafeKey(key)) continue;
|
|
20
|
-
if (typeof source[key] === "function") continue;
|
|
21
|
-
nextKeys.add(key);
|
|
22
|
-
}
|
|
23
|
-
for (const key of getOwnEnumerableKeys(rawState)) {
|
|
24
|
-
if (isUnsafeKey(key)) {
|
|
25
|
-
delete rawState[key];
|
|
26
|
-
delete mutableState[key];
|
|
27
|
-
delete publicState[key];
|
|
28
|
-
continue;
|
|
29
|
-
}
|
|
30
|
-
if (typeof rawState[key] === "function") continue;
|
|
31
|
-
if (!nextKeys.has(key)) {
|
|
32
|
-
delete rawState[key];
|
|
33
|
-
delete mutableState[key];
|
|
34
|
-
delete publicState[key];
|
|
35
|
-
}
|
|
36
|
-
}
|
|
37
|
-
const rawSeen = /* @__PURE__ */ new WeakMap();
|
|
38
|
-
const mutableSeen = /* @__PURE__ */ new WeakMap();
|
|
39
|
-
const publicSeen = /* @__PURE__ */ new WeakMap();
|
|
40
|
-
rawSeen.set(source, rawState);
|
|
41
|
-
mutableSeen.set(source, mutableState);
|
|
42
|
-
publicSeen.set(source, publicState);
|
|
43
|
-
nextKeys.forEach((key) => {
|
|
44
|
-
rawState[key] = sanitizeReplacementState(source[key], rawSeen);
|
|
45
|
-
mutableState[key] = sanitizeReplacementState(source[key], mutableSeen);
|
|
46
|
-
publicState[key] = sanitizeReplacementState(source[key], publicSeen);
|
|
47
|
-
});
|
|
48
|
-
};
|
|
49
|
-
const toSnapshot = (value, visited = /* @__PURE__ */ new WeakMap()) => {
|
|
50
|
-
if (Array.isArray(value)) {
|
|
51
|
-
if (visited.has(value)) return visited.get(value);
|
|
52
|
-
const next = [];
|
|
53
|
-
next.length = value.length;
|
|
54
|
-
visited.set(value, next);
|
|
55
|
-
for (let index = 0; index < value.length; index += 1) if (Object.prototype.hasOwnProperty.call(value, index)) next[index] = toSnapshot(value[index], visited);
|
|
56
|
-
const source = value;
|
|
57
|
-
const target = next;
|
|
58
|
-
for (const key of getOwnEnumerableKeys(value)) {
|
|
59
|
-
if (isArrayIndexKey(key) || isUnsafeKey(key)) continue;
|
|
60
|
-
const child = source[key];
|
|
61
|
-
if (typeof child !== "function") target[key] = toSnapshot(child, visited);
|
|
62
|
-
}
|
|
63
|
-
return next;
|
|
64
|
-
}
|
|
65
|
-
if (typeof value === "object" && value !== null) {
|
|
66
|
-
if (!isObjectRecord(value)) return value;
|
|
67
|
-
if (visited.has(value)) return visited.get(value);
|
|
68
|
-
const next = {};
|
|
69
|
-
visited.set(value, next);
|
|
70
|
-
for (const key of getOwnEnumerableKeys(value)) {
|
|
71
|
-
if (isUnsafeKey(key)) continue;
|
|
72
|
-
const child = value[key];
|
|
73
|
-
if (typeof child !== "function") next[key] = toSnapshot(child, visited);
|
|
74
|
-
}
|
|
75
|
-
return next;
|
|
76
|
-
}
|
|
77
|
-
return value;
|
|
78
|
-
};
|
|
79
|
-
const snapshotPureState = (store) => toSnapshot(store.getPureState());
|
|
80
|
-
const isEqualSnapshot = (left, right, visited = /* @__PURE__ */ new WeakMap()) => {
|
|
81
|
-
if (Object.is(left, right)) return true;
|
|
82
|
-
if (typeof left !== "object" || left === null || typeof right !== "object" || right === null) return false;
|
|
83
|
-
const leftIsArray = Array.isArray(left);
|
|
84
|
-
const rightIsArray = Array.isArray(right);
|
|
85
|
-
if (leftIsArray || rightIsArray) {
|
|
86
|
-
if (!leftIsArray || !rightIsArray || left.length !== right.length) return false;
|
|
87
|
-
} else if (!isObjectRecord(left) || !isObjectRecord(right)) return false;
|
|
88
|
-
let seenTargets = visited.get(left);
|
|
89
|
-
if (!seenTargets) {
|
|
90
|
-
seenTargets = /* @__PURE__ */ new WeakSet();
|
|
91
|
-
visited.set(left, seenTargets);
|
|
92
|
-
} else if (seenTargets.has(right)) return true;
|
|
93
|
-
seenTargets.add(right);
|
|
94
|
-
const leftRecord = left;
|
|
95
|
-
const rightRecord = right;
|
|
96
|
-
const leftKeys = getOwnEnumerableKeys(left);
|
|
97
|
-
const rightKeys = getOwnEnumerableKeys(right);
|
|
98
|
-
if (leftKeys.length !== rightKeys.length) return false;
|
|
99
|
-
for (const key of leftKeys) {
|
|
100
|
-
if (!Object.prototype.hasOwnProperty.call(rightRecord, key)) return false;
|
|
101
|
-
if (!isEqualSnapshot(leftRecord[key], rightRecord[key], visited)) return false;
|
|
102
|
-
}
|
|
103
|
-
return true;
|
|
7
|
+
const reportAdapterStateError = (error) => {
|
|
8
|
+
if (process.env.NODE_ENV === "development") console.error(error);
|
|
104
9
|
};
|
|
105
10
|
const handleStore = (store, state, _, internal) => {
|
|
106
11
|
const rawState = state;
|
|
12
|
+
internal.getTransportState = () => snapshotMutableAdapterPureState(store);
|
|
107
13
|
let isApplyingCoactionState = false;
|
|
108
14
|
let lastSnapshot;
|
|
109
15
|
const restoreClientState = (snapshot) => {
|
|
110
16
|
const mutableStore = internal.toMutableRaw(rawState);
|
|
111
17
|
if (!mutableStore) return;
|
|
18
|
+
const rollbackSnapshot = toMutableAdapterSnapshot(snapshot);
|
|
112
19
|
isApplyingCoactionState = true;
|
|
113
20
|
try {
|
|
114
|
-
|
|
21
|
+
replaceMutableAdapterState(internal.rootState ?? rawState, mutableStore, store.getState(), rollbackSnapshot);
|
|
115
22
|
} finally {
|
|
116
|
-
lastSnapshot =
|
|
23
|
+
lastSnapshot = snapshotMutableAdapterPureState(store);
|
|
117
24
|
isApplyingCoactionState = false;
|
|
118
25
|
}
|
|
119
26
|
};
|
|
120
27
|
const syncSharedExternalChange = () => {
|
|
121
|
-
const currentSnapshot =
|
|
28
|
+
const currentSnapshot = snapshotMutableAdapterPureState(store);
|
|
122
29
|
if (isApplyingCoactionState) {
|
|
123
30
|
lastSnapshot = currentSnapshot;
|
|
124
31
|
return "handled";
|
|
125
32
|
}
|
|
126
33
|
if (store.share === "client" && lastSnapshot) {
|
|
127
|
-
if (!
|
|
34
|
+
if (!isEqualMutableAdapterSnapshot(currentSnapshot, lastSnapshot)) restoreClientState(lastSnapshot);
|
|
128
35
|
return "ignored";
|
|
129
36
|
}
|
|
130
37
|
if (store.share === "main" && lastSnapshot) {
|
|
@@ -132,6 +39,14 @@ const handleStore = (store, state, _, internal) => {
|
|
|
132
39
|
internal.rootState = lastSnapshot;
|
|
133
40
|
try {
|
|
134
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";
|
|
135
50
|
} finally {
|
|
136
51
|
internal.rootState = rootState;
|
|
137
52
|
}
|
|
@@ -144,6 +59,7 @@ const handleStore = (store, state, _, internal) => {
|
|
|
144
59
|
if (!internal.toMutableRaw) {
|
|
145
60
|
internal.toMutableRaw = (key) => instancesMap.get(key);
|
|
146
61
|
Object.assign(store, { subscribe: (callback) => {
|
|
62
|
+
internal.assertAlive?.("subscribe");
|
|
147
63
|
store._subscriptions.add(callback);
|
|
148
64
|
return () => {
|
|
149
65
|
store._subscriptions?.delete(callback);
|
|
@@ -163,17 +79,27 @@ const handleStore = (store, state, _, internal) => {
|
|
|
163
79
|
store._destroyers = void 0;
|
|
164
80
|
baseDestroy();
|
|
165
81
|
};
|
|
166
|
-
store.apply = (nextState = store.
|
|
82
|
+
store.apply = (nextState = store.getPureState(), patches) => {
|
|
83
|
+
internal.assertAlive?.("apply");
|
|
84
|
+
const previousSnapshot = lastSnapshot ?? snapshotMutableAdapterPureState(store);
|
|
167
85
|
isApplyingCoactionState = true;
|
|
168
86
|
try {
|
|
169
87
|
if (!patches) {
|
|
170
88
|
if (nextState === store.getState()) return;
|
|
171
|
-
|
|
89
|
+
internal.validateState?.(toMutableAdapterSnapshot(nextState));
|
|
90
|
+
replaceMutableAdapterState(internal.rootState ?? rawState, internal.toMutableRaw(rawState), store.getState(), nextState);
|
|
172
91
|
return;
|
|
173
92
|
}
|
|
174
|
-
|
|
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;
|
|
175
101
|
} finally {
|
|
176
|
-
lastSnapshot =
|
|
102
|
+
lastSnapshot = snapshotMutableAdapterPureState(store);
|
|
177
103
|
isApplyingCoactionState = false;
|
|
178
104
|
internal.notifyStateChange?.();
|
|
179
105
|
}
|
|
@@ -183,8 +109,8 @@ const handleStore = (store, state, _, internal) => {
|
|
|
183
109
|
if (!mutableStore) throw new Error("Pinia store instance is not found");
|
|
184
110
|
let stopWatch;
|
|
185
111
|
const cancelReadySubscription = onStoreReady(store, () => {
|
|
186
|
-
|
|
187
|
-
lastSnapshot =
|
|
112
|
+
replaceMutableAdapterState(internal.rootState ?? rawState, mutableStore, store.getState(), sanitizeInitialStateValue(snapshotMutableAdapterPureState(store)));
|
|
113
|
+
lastSnapshot = snapshotMutableAdapterPureState(store);
|
|
188
114
|
stopWatch = mutableStore.$subscribe((...args) => {
|
|
189
115
|
const change = syncSharedExternalChange();
|
|
190
116
|
if (change === "ignored") return;
|
|
@@ -219,7 +145,6 @@ const bindPinia = createBinder({
|
|
|
219
145
|
...options.state?.(),
|
|
220
146
|
...options.actions
|
|
221
147
|
}, descriptors);
|
|
222
|
-
setActivePinia(createPinia());
|
|
223
148
|
return {
|
|
224
149
|
copyState: options,
|
|
225
150
|
key: "actions",
|
|
@@ -233,6 +158,15 @@ const bindPinia = createBinder({
|
|
|
233
158
|
/**
|
|
234
159
|
* Adapt a store type to Pinia
|
|
235
160
|
*/
|
|
236
|
-
const adapt = (store) =>
|
|
161
|
+
const adapt = (store) => {
|
|
162
|
+
if (typeof store !== "function") return store;
|
|
163
|
+
const pinia = createPinia();
|
|
164
|
+
const boundStore = ((...args) => {
|
|
165
|
+
const [piniaOverride, hot] = args;
|
|
166
|
+
return store(piniaOverride ?? pinia, hot);
|
|
167
|
+
});
|
|
168
|
+
Object.assign(boundStore, store);
|
|
169
|
+
return boundStore;
|
|
170
|
+
};
|
|
237
171
|
//#endregion
|
|
238
172
|
export { adapt, bindPinia };
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@coaction/pinia",
|
|
3
|
-
"version": "
|
|
3
|
+
"version": "3.0.0",
|
|
4
4
|
"description": "A Coaction integration tool for Pinia",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"coaction",
|
|
@@ -27,9 +27,14 @@
|
|
|
27
27
|
"types": "dist/index.d.ts",
|
|
28
28
|
"exports": {
|
|
29
29
|
".": {
|
|
30
|
-
"
|
|
31
|
-
|
|
32
|
-
|
|
30
|
+
"import": {
|
|
31
|
+
"types": "./dist/index.d.mts",
|
|
32
|
+
"default": "./dist/index.mjs"
|
|
33
|
+
},
|
|
34
|
+
"require": {
|
|
35
|
+
"types": "./dist/index.d.ts",
|
|
36
|
+
"default": "./dist/index.js"
|
|
37
|
+
},
|
|
33
38
|
"default": "./dist/index.mjs"
|
|
34
39
|
},
|
|
35
40
|
"./package.json": "./package.json"
|
|
@@ -45,10 +50,10 @@
|
|
|
45
50
|
"data-transport": "^5.0.3",
|
|
46
51
|
"mutative": "^1.3.0",
|
|
47
52
|
"pinia": "^3.0.4",
|
|
48
|
-
"coaction": "
|
|
53
|
+
"coaction": "3.0.0"
|
|
49
54
|
},
|
|
50
55
|
"peerDependencies": {
|
|
51
|
-
"coaction": "^
|
|
56
|
+
"coaction": "^3.0.0",
|
|
52
57
|
"pinia": "^2.0 || ^3.0.0"
|
|
53
58
|
},
|
|
54
59
|
"authors": [
|