@coaction/pinia 2.0.0 → 2.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 +24 -18
- package/dist/index.js +22 -110
- package/dist/index.mjs +24 -112
- package/package.json +11 -6
package/README.md
CHANGED
|
@@ -1,40 +1,40 @@
|
|
|
1
1
|
# @coaction/pinia
|
|
2
2
|
|
|
3
|
-

|
|
4
|
-
[](https://www.npmjs.com/package/@coaction/pinia)
|
|
5
|
-

|
|
3
|
+
 [](https://www.npmjs.com/package/@coaction/pinia) 
|
|
6
4
|
|
|
7
5
|
A Coaction integration tool for Pinia
|
|
8
6
|
|
|
9
7
|
## Installation
|
|
10
8
|
|
|
11
|
-
|
|
9
|
+
Install it with pnpm:
|
|
12
10
|
|
|
13
11
|
```sh
|
|
14
|
-
|
|
12
|
+
pnpm add coaction @coaction/pinia
|
|
15
13
|
```
|
|
16
14
|
|
|
17
15
|
## Usage
|
|
18
16
|
|
|
19
17
|
```js
|
|
20
18
|
import { create } from 'coaction';
|
|
21
|
-
import { bindPinia } from '@coaction/pinia';
|
|
19
|
+
import { adapt, bindPinia } from '@coaction/pinia';
|
|
22
20
|
import { defineStore } from 'pinia';
|
|
23
21
|
|
|
24
22
|
const useStore = create(() =>
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
state
|
|
23
|
+
adapt(
|
|
24
|
+
defineStore(
|
|
25
|
+
'test',
|
|
26
|
+
bindPinia({
|
|
27
|
+
state: () => ({ count: 0 }),
|
|
28
|
+
getters: {
|
|
29
|
+
double: (state) => state.count * 2
|
|
30
|
+
},
|
|
31
|
+
actions: {
|
|
32
|
+
increment(state) {
|
|
33
|
+
state.count += 1;
|
|
34
|
+
}
|
|
35
35
|
}
|
|
36
|
-
}
|
|
37
|
-
|
|
36
|
+
})
|
|
37
|
+
)
|
|
38
38
|
)
|
|
39
39
|
);
|
|
40
40
|
```
|
|
@@ -43,6 +43,12 @@ const useStore = create(() =>
|
|
|
43
43
|
|
|
44
44
|
- `@coaction/pinia` only supports binding a whole Pinia store.
|
|
45
45
|
- Coaction `Slices` mode is not supported in this adapter.
|
|
46
|
+
- `bindPinia()` does not set Pinia's global active instance. Use `adapt()` for
|
|
47
|
+
Coaction-owned Pinia definitions, or provide your own active Pinia when using
|
|
48
|
+
raw Pinia store definitions directly.
|
|
49
|
+
- Unknown root properties written directly to the Pinia store are ignored by
|
|
50
|
+
Coaction's fixed schema. They are not promoted into Coaction raw/public state,
|
|
51
|
+
and this adapter does not guarantee pruning them from the Pinia store.
|
|
46
52
|
|
|
47
53
|
## Documentation
|
|
48
54
|
|
package/dist/index.js
CHANGED
|
@@ -1,107 +1,9 @@
|
|
|
1
1
|
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
|
|
2
2
|
//#endregion
|
|
3
|
-
let mutability = require("mutability");
|
|
4
3
|
let coaction = require("coaction");
|
|
5
4
|
let pinia = require("pinia");
|
|
6
5
|
//#region packages/coaction-pinia/src/index.ts
|
|
7
6
|
const instancesMap = /* @__PURE__ */ new WeakMap();
|
|
8
|
-
const getOwnEnumerableKeys = (value) => Reflect.ownKeys(value).filter((key) => Object.prototype.propertyIsEnumerable.call(value, key));
|
|
9
|
-
const isUnsafeKey = (key) => typeof key === "string" && (key === "__proto__" || key === "prototype" || key === "constructor");
|
|
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;
|
|
104
|
-
};
|
|
105
7
|
const handleStore = (store, state, _, internal) => {
|
|
106
8
|
const rawState = state;
|
|
107
9
|
let isApplyingCoactionState = false;
|
|
@@ -111,20 +13,20 @@ const handleStore = (store, state, _, internal) => {
|
|
|
111
13
|
if (!mutableStore) return;
|
|
112
14
|
isApplyingCoactionState = true;
|
|
113
15
|
try {
|
|
114
|
-
|
|
16
|
+
(0, coaction.replaceMutableAdapterState)(internal.rootState ?? rawState, mutableStore, store.getState(), snapshot);
|
|
115
17
|
} finally {
|
|
116
|
-
lastSnapshot =
|
|
18
|
+
lastSnapshot = (0, coaction.snapshotMutableAdapterPureState)(store);
|
|
117
19
|
isApplyingCoactionState = false;
|
|
118
20
|
}
|
|
119
21
|
};
|
|
120
22
|
const syncSharedExternalChange = () => {
|
|
121
|
-
const currentSnapshot =
|
|
23
|
+
const currentSnapshot = (0, coaction.snapshotMutableAdapterPureState)(store);
|
|
122
24
|
if (isApplyingCoactionState) {
|
|
123
25
|
lastSnapshot = currentSnapshot;
|
|
124
26
|
return "handled";
|
|
125
27
|
}
|
|
126
28
|
if (store.share === "client" && lastSnapshot) {
|
|
127
|
-
if (!
|
|
29
|
+
if (!(0, coaction.isEqualMutableAdapterSnapshot)(currentSnapshot, lastSnapshot)) restoreClientState(lastSnapshot);
|
|
128
30
|
return "ignored";
|
|
129
31
|
}
|
|
130
32
|
if (store.share === "main" && lastSnapshot) {
|
|
@@ -144,6 +46,7 @@ const handleStore = (store, state, _, internal) => {
|
|
|
144
46
|
if (!internal.toMutableRaw) {
|
|
145
47
|
internal.toMutableRaw = (key) => instancesMap.get(key);
|
|
146
48
|
Object.assign(store, { subscribe: (callback) => {
|
|
49
|
+
internal.assertAlive?.("subscribe");
|
|
147
50
|
store._subscriptions.add(callback);
|
|
148
51
|
return () => {
|
|
149
52
|
store._subscriptions?.delete(callback);
|
|
@@ -163,17 +66,18 @@ const handleStore = (store, state, _, internal) => {
|
|
|
163
66
|
store._destroyers = void 0;
|
|
164
67
|
baseDestroy();
|
|
165
68
|
};
|
|
166
|
-
store.apply = (nextState = store.
|
|
69
|
+
store.apply = (nextState = store.getPureState(), patches) => {
|
|
70
|
+
internal.assertAlive?.("apply");
|
|
167
71
|
isApplyingCoactionState = true;
|
|
168
72
|
try {
|
|
169
73
|
if (!patches) {
|
|
170
74
|
if (nextState === store.getState()) return;
|
|
171
|
-
|
|
75
|
+
(0, coaction.replaceMutableAdapterState)(internal.rootState ?? rawState, internal.toMutableRaw(rawState), store.getState(), nextState);
|
|
172
76
|
return;
|
|
173
77
|
}
|
|
174
|
-
(0,
|
|
78
|
+
(0, coaction.applyMutableAdapterPatches)(nextState, patches, internal.rootState ?? rawState, internal.toMutableRaw(rawState), store.getState());
|
|
175
79
|
} finally {
|
|
176
|
-
lastSnapshot =
|
|
80
|
+
lastSnapshot = (0, coaction.snapshotMutableAdapterPureState)(store);
|
|
177
81
|
isApplyingCoactionState = false;
|
|
178
82
|
internal.notifyStateChange?.();
|
|
179
83
|
}
|
|
@@ -183,8 +87,8 @@ const handleStore = (store, state, _, internal) => {
|
|
|
183
87
|
if (!mutableStore) throw new Error("Pinia store instance is not found");
|
|
184
88
|
let stopWatch;
|
|
185
89
|
const cancelReadySubscription = (0, coaction.onStoreReady)(store, () => {
|
|
186
|
-
|
|
187
|
-
lastSnapshot =
|
|
90
|
+
(0, coaction.replaceMutableAdapterState)(internal.rootState ?? rawState, mutableStore, store.getState(), (0, coaction.sanitizeInitialStateValue)((0, coaction.snapshotMutableAdapterPureState)(store)));
|
|
91
|
+
lastSnapshot = (0, coaction.snapshotMutableAdapterPureState)(store);
|
|
188
92
|
stopWatch = mutableStore.$subscribe((...args) => {
|
|
189
93
|
const change = syncSharedExternalChange();
|
|
190
94
|
if (change === "ignored") return;
|
|
@@ -219,7 +123,6 @@ const bindPinia = (0, coaction.createBinder)({
|
|
|
219
123
|
...options.state?.(),
|
|
220
124
|
...options.actions
|
|
221
125
|
}, descriptors);
|
|
222
|
-
(0, pinia.setActivePinia)((0, pinia.createPinia)());
|
|
223
126
|
return {
|
|
224
127
|
copyState: options,
|
|
225
128
|
key: "actions",
|
|
@@ -233,7 +136,16 @@ const bindPinia = (0, coaction.createBinder)({
|
|
|
233
136
|
/**
|
|
234
137
|
* Adapt a store type to Pinia
|
|
235
138
|
*/
|
|
236
|
-
const adapt = (store) =>
|
|
139
|
+
const adapt = (store) => {
|
|
140
|
+
if (typeof store !== "function") return store;
|
|
141
|
+
const pinia$1 = (0, pinia.createPinia)();
|
|
142
|
+
const boundStore = ((...args) => {
|
|
143
|
+
const [piniaOverride, hot] = args;
|
|
144
|
+
return store(piniaOverride ?? pinia$1, hot);
|
|
145
|
+
});
|
|
146
|
+
Object.assign(boundStore, store);
|
|
147
|
+
return boundStore;
|
|
148
|
+
};
|
|
237
149
|
//#endregion
|
|
238
150
|
exports.adapt = adapt;
|
|
239
151
|
exports.bindPinia = bindPinia;
|
package/dist/index.mjs
CHANGED
|
@@ -1,107 +1,9 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import {
|
|
3
|
-
import { createPinia, setActivePinia } from "pinia";
|
|
1
|
+
import { applyMutableAdapterPatches, createBinder, isEqualMutableAdapterSnapshot, onStoreReady, replaceExternalStoreState, replaceMutableAdapterState, sanitizeInitialStateValue, snapshotMutableAdapterPureState } from "coaction";
|
|
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 getOwnEnumerableKeys = (value) => Reflect.ownKeys(value).filter((key) => Object.prototype.propertyIsEnumerable.call(value, key));
|
|
9
|
-
const isUnsafeKey = (key) => typeof key === "string" && (key === "__proto__" || key === "prototype" || key === "constructor");
|
|
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;
|
|
104
|
-
};
|
|
105
7
|
const handleStore = (store, state, _, internal) => {
|
|
106
8
|
const rawState = state;
|
|
107
9
|
let isApplyingCoactionState = false;
|
|
@@ -111,20 +13,20 @@ const handleStore = (store, state, _, internal) => {
|
|
|
111
13
|
if (!mutableStore) return;
|
|
112
14
|
isApplyingCoactionState = true;
|
|
113
15
|
try {
|
|
114
|
-
|
|
16
|
+
replaceMutableAdapterState(internal.rootState ?? rawState, mutableStore, store.getState(), snapshot);
|
|
115
17
|
} finally {
|
|
116
|
-
lastSnapshot =
|
|
18
|
+
lastSnapshot = snapshotMutableAdapterPureState(store);
|
|
117
19
|
isApplyingCoactionState = false;
|
|
118
20
|
}
|
|
119
21
|
};
|
|
120
22
|
const syncSharedExternalChange = () => {
|
|
121
|
-
const currentSnapshot =
|
|
23
|
+
const currentSnapshot = snapshotMutableAdapterPureState(store);
|
|
122
24
|
if (isApplyingCoactionState) {
|
|
123
25
|
lastSnapshot = currentSnapshot;
|
|
124
26
|
return "handled";
|
|
125
27
|
}
|
|
126
28
|
if (store.share === "client" && lastSnapshot) {
|
|
127
|
-
if (!
|
|
29
|
+
if (!isEqualMutableAdapterSnapshot(currentSnapshot, lastSnapshot)) restoreClientState(lastSnapshot);
|
|
128
30
|
return "ignored";
|
|
129
31
|
}
|
|
130
32
|
if (store.share === "main" && lastSnapshot) {
|
|
@@ -144,6 +46,7 @@ const handleStore = (store, state, _, internal) => {
|
|
|
144
46
|
if (!internal.toMutableRaw) {
|
|
145
47
|
internal.toMutableRaw = (key) => instancesMap.get(key);
|
|
146
48
|
Object.assign(store, { subscribe: (callback) => {
|
|
49
|
+
internal.assertAlive?.("subscribe");
|
|
147
50
|
store._subscriptions.add(callback);
|
|
148
51
|
return () => {
|
|
149
52
|
store._subscriptions?.delete(callback);
|
|
@@ -163,17 +66,18 @@ const handleStore = (store, state, _, internal) => {
|
|
|
163
66
|
store._destroyers = void 0;
|
|
164
67
|
baseDestroy();
|
|
165
68
|
};
|
|
166
|
-
store.apply = (nextState = store.
|
|
69
|
+
store.apply = (nextState = store.getPureState(), patches) => {
|
|
70
|
+
internal.assertAlive?.("apply");
|
|
167
71
|
isApplyingCoactionState = true;
|
|
168
72
|
try {
|
|
169
73
|
if (!patches) {
|
|
170
74
|
if (nextState === store.getState()) return;
|
|
171
|
-
|
|
75
|
+
replaceMutableAdapterState(internal.rootState ?? rawState, internal.toMutableRaw(rawState), store.getState(), nextState);
|
|
172
76
|
return;
|
|
173
77
|
}
|
|
174
|
-
|
|
78
|
+
applyMutableAdapterPatches(nextState, patches, internal.rootState ?? rawState, internal.toMutableRaw(rawState), store.getState());
|
|
175
79
|
} finally {
|
|
176
|
-
lastSnapshot =
|
|
80
|
+
lastSnapshot = snapshotMutableAdapterPureState(store);
|
|
177
81
|
isApplyingCoactionState = false;
|
|
178
82
|
internal.notifyStateChange?.();
|
|
179
83
|
}
|
|
@@ -183,8 +87,8 @@ const handleStore = (store, state, _, internal) => {
|
|
|
183
87
|
if (!mutableStore) throw new Error("Pinia store instance is not found");
|
|
184
88
|
let stopWatch;
|
|
185
89
|
const cancelReadySubscription = onStoreReady(store, () => {
|
|
186
|
-
|
|
187
|
-
lastSnapshot =
|
|
90
|
+
replaceMutableAdapterState(internal.rootState ?? rawState, mutableStore, store.getState(), sanitizeInitialStateValue(snapshotMutableAdapterPureState(store)));
|
|
91
|
+
lastSnapshot = snapshotMutableAdapterPureState(store);
|
|
188
92
|
stopWatch = mutableStore.$subscribe((...args) => {
|
|
189
93
|
const change = syncSharedExternalChange();
|
|
190
94
|
if (change === "ignored") return;
|
|
@@ -219,7 +123,6 @@ const bindPinia = createBinder({
|
|
|
219
123
|
...options.state?.(),
|
|
220
124
|
...options.actions
|
|
221
125
|
}, descriptors);
|
|
222
|
-
setActivePinia(createPinia());
|
|
223
126
|
return {
|
|
224
127
|
copyState: options,
|
|
225
128
|
key: "actions",
|
|
@@ -233,6 +136,15 @@ const bindPinia = createBinder({
|
|
|
233
136
|
/**
|
|
234
137
|
* Adapt a store type to Pinia
|
|
235
138
|
*/
|
|
236
|
-
const adapt = (store) =>
|
|
139
|
+
const adapt = (store) => {
|
|
140
|
+
if (typeof store !== "function") return store;
|
|
141
|
+
const pinia = createPinia();
|
|
142
|
+
const boundStore = ((...args) => {
|
|
143
|
+
const [piniaOverride, hot] = args;
|
|
144
|
+
return store(piniaOverride ?? pinia, hot);
|
|
145
|
+
});
|
|
146
|
+
Object.assign(boundStore, store);
|
|
147
|
+
return boundStore;
|
|
148
|
+
};
|
|
237
149
|
//#endregion
|
|
238
150
|
export { adapt, bindPinia };
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@coaction/pinia",
|
|
3
|
-
"version": "2.
|
|
3
|
+
"version": "2.1.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": "2.
|
|
53
|
+
"coaction": "2.1.0"
|
|
49
54
|
},
|
|
50
55
|
"peerDependencies": {
|
|
51
|
-
"coaction": "
|
|
56
|
+
"coaction": ">=2.1.0 <3",
|
|
52
57
|
"pinia": "^2.0 || ^3.0.0"
|
|
53
58
|
},
|
|
54
59
|
"authors": [
|