@coaction/pinia 1.5.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 +25 -19
- package/dist/index.d.mts +14 -23
- package/dist/index.d.ts +14 -23
- package/dist/index.js +155 -126
- package/dist/index.mjs +147 -124
- package/package.json +40 -47
package/README.md
CHANGED
|
@@ -1,40 +1,40 @@
|
|
|
1
1
|
# @coaction/pinia
|
|
2
2
|
|
|
3
|
-
](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,7 +43,13 @@ 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
|
|
|
49
|
-
You can find the documentation [here](https://github.com/
|
|
55
|
+
You can find the documentation [here](https://github.com/coactionjs/coaction).
|
package/dist/index.d.mts
CHANGED
|
@@ -1,25 +1,13 @@
|
|
|
1
|
-
import {
|
|
2
|
-
export * from
|
|
1
|
+
import { DefineStoreOptions, StateTree, StoreDefinition, _GettersTree } from "pinia";
|
|
2
|
+
export * from "pinia";
|
|
3
3
|
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
type
|
|
8
|
-
type
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
}, Readonly<{
|
|
12
|
-
[P in K]: T[P];
|
|
13
|
-
}>> extends true ? K : never;
|
|
14
|
-
}[keyof T];
|
|
15
|
-
type IStore<T extends object> = [
|
|
16
|
-
string,
|
|
17
|
-
Pick<T, Exclude<keyof T, ReadonlyKeys<T> | FunctionKeys<T>>>,
|
|
18
|
-
{
|
|
19
|
-
[K in ReadonlyKeys<T>]: (state: Pick<T, Exclude<keyof T, ReadonlyKeys<T> | FunctionKeys<T>>>) => T[K];
|
|
20
|
-
},
|
|
21
|
-
Pick<T, FunctionKeys<T>>
|
|
22
|
-
];
|
|
4
|
+
//#region \0rolldown/runtime.js
|
|
5
|
+
//#endregion
|
|
6
|
+
//#region packages/coaction-pinia/src/index.d.ts
|
|
7
|
+
type FunctionKeys<T> = { [K in keyof T]: T[K] extends ((...args: any[]) => any) ? K : never }[keyof T];
|
|
8
|
+
type Equal<X, Y> = (<T>() => T extends X ? 1 : 2) extends (<T>() => T extends Y ? 1 : 2) ? true : false;
|
|
9
|
+
type ReadonlyKeys<T> = { [K in keyof T]: Equal<{ [P in K]: T[P] }, Readonly<{ [P in K]: T[P] }>> extends true ? K : never }[keyof T];
|
|
10
|
+
type IStore<T extends object> = [string, Pick<T, Exclude<keyof T, ReadonlyKeys<T> | FunctionKeys<T>>>, { [K in ReadonlyKeys<T>]: (state: Pick<T, Exclude<keyof T, ReadonlyKeys<T> | FunctionKeys<T>>>) => T[K] }, Pick<T, FunctionKeys<T>>];
|
|
23
11
|
/**
|
|
24
12
|
* Bind a store to Pinia
|
|
25
13
|
*/
|
|
@@ -29,5 +17,8 @@ declare const bindPinia: <Id extends string, S extends StateTree = {}, G extends
|
|
|
29
17
|
*/
|
|
30
18
|
declare const adapt: <T extends object>(store: StoreDefinition<IStore<T>[0], IStore<T>[1], IStore<T>[2], IStore<T>[3]>) => T;
|
|
31
19
|
type PiniaStore<T extends object> = StoreDefinition<IStore<T>[0], IStore<T>[1], IStore<T>[2], IStore<T>[3]>;
|
|
32
|
-
|
|
33
|
-
export {
|
|
20
|
+
declare namespace index_d_exports {
|
|
21
|
+
export { IStore, PiniaStore, adapt, bindPinia };
|
|
22
|
+
}
|
|
23
|
+
//#endregion
|
|
24
|
+
export { IStore, PiniaStore, adapt, bindPinia };
|
package/dist/index.d.ts
CHANGED
|
@@ -1,25 +1,13 @@
|
|
|
1
|
-
import {
|
|
2
|
-
export * from
|
|
1
|
+
import { DefineStoreOptions, StateTree, StoreDefinition, _GettersTree } from "pinia";
|
|
2
|
+
export * from "pinia";
|
|
3
3
|
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
type
|
|
8
|
-
type
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
}, Readonly<{
|
|
12
|
-
[P in K]: T[P];
|
|
13
|
-
}>> extends true ? K : never;
|
|
14
|
-
}[keyof T];
|
|
15
|
-
type IStore<T extends object> = [
|
|
16
|
-
string,
|
|
17
|
-
Pick<T, Exclude<keyof T, ReadonlyKeys<T> | FunctionKeys<T>>>,
|
|
18
|
-
{
|
|
19
|
-
[K in ReadonlyKeys<T>]: (state: Pick<T, Exclude<keyof T, ReadonlyKeys<T> | FunctionKeys<T>>>) => T[K];
|
|
20
|
-
},
|
|
21
|
-
Pick<T, FunctionKeys<T>>
|
|
22
|
-
];
|
|
4
|
+
//#region \0rolldown/runtime.js
|
|
5
|
+
//#endregion
|
|
6
|
+
//#region packages/coaction-pinia/src/index.d.ts
|
|
7
|
+
type FunctionKeys<T> = { [K in keyof T]: T[K] extends ((...args: any[]) => any) ? K : never }[keyof T];
|
|
8
|
+
type Equal<X, Y> = (<T>() => T extends X ? 1 : 2) extends (<T>() => T extends Y ? 1 : 2) ? true : false;
|
|
9
|
+
type ReadonlyKeys<T> = { [K in keyof T]: Equal<{ [P in K]: T[P] }, Readonly<{ [P in K]: T[P] }>> extends true ? K : never }[keyof T];
|
|
10
|
+
type IStore<T extends object> = [string, Pick<T, Exclude<keyof T, ReadonlyKeys<T> | FunctionKeys<T>>>, { [K in ReadonlyKeys<T>]: (state: Pick<T, Exclude<keyof T, ReadonlyKeys<T> | FunctionKeys<T>>>) => T[K] }, Pick<T, FunctionKeys<T>>];
|
|
23
11
|
/**
|
|
24
12
|
* Bind a store to Pinia
|
|
25
13
|
*/
|
|
@@ -29,5 +17,8 @@ declare const bindPinia: <Id extends string, S extends StateTree = {}, G extends
|
|
|
29
17
|
*/
|
|
30
18
|
declare const adapt: <T extends object>(store: StoreDefinition<IStore<T>[0], IStore<T>[1], IStore<T>[2], IStore<T>[3]>) => T;
|
|
31
19
|
type PiniaStore<T extends object> = StoreDefinition<IStore<T>[0], IStore<T>[1], IStore<T>[2], IStore<T>[3]>;
|
|
32
|
-
|
|
33
|
-
export {
|
|
20
|
+
declare namespace index_d_exports {
|
|
21
|
+
export { IStore, PiniaStore, adapt, bindPinia };
|
|
22
|
+
}
|
|
23
|
+
//#endregion
|
|
24
|
+
export { IStore, PiniaStore, adapt, bindPinia };
|
package/dist/index.js
CHANGED
|
@@ -1,130 +1,159 @@
|
|
|
1
|
-
"
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
1
|
+
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
|
|
2
|
+
//#endregion
|
|
3
|
+
let coaction = require("coaction");
|
|
4
|
+
let pinia = require("pinia");
|
|
5
|
+
//#region packages/coaction-pinia/src/index.ts
|
|
6
|
+
const instancesMap = /* @__PURE__ */ new WeakMap();
|
|
7
|
+
const handleStore = (store, state, _, internal) => {
|
|
8
|
+
const rawState = state;
|
|
9
|
+
let isApplyingCoactionState = false;
|
|
10
|
+
let lastSnapshot;
|
|
11
|
+
const restoreClientState = (snapshot) => {
|
|
12
|
+
const mutableStore = internal.toMutableRaw(rawState);
|
|
13
|
+
if (!mutableStore) return;
|
|
14
|
+
isApplyingCoactionState = true;
|
|
15
|
+
try {
|
|
16
|
+
(0, coaction.replaceMutableAdapterState)(internal.rootState ?? rawState, mutableStore, store.getState(), snapshot);
|
|
17
|
+
} finally {
|
|
18
|
+
lastSnapshot = (0, coaction.snapshotMutableAdapterPureState)(store);
|
|
19
|
+
isApplyingCoactionState = false;
|
|
20
|
+
}
|
|
21
|
+
};
|
|
22
|
+
const syncSharedExternalChange = () => {
|
|
23
|
+
const currentSnapshot = (0, coaction.snapshotMutableAdapterPureState)(store);
|
|
24
|
+
if (isApplyingCoactionState) {
|
|
25
|
+
lastSnapshot = currentSnapshot;
|
|
26
|
+
return "handled";
|
|
27
|
+
}
|
|
28
|
+
if (store.share === "client" && lastSnapshot) {
|
|
29
|
+
if (!(0, coaction.isEqualMutableAdapterSnapshot)(currentSnapshot, lastSnapshot)) restoreClientState(lastSnapshot);
|
|
30
|
+
return "ignored";
|
|
31
|
+
}
|
|
32
|
+
if (store.share === "main" && lastSnapshot) {
|
|
33
|
+
const rootState = internal.rootState;
|
|
34
|
+
internal.rootState = lastSnapshot;
|
|
35
|
+
try {
|
|
36
|
+
(0, coaction.replaceExternalStoreState)(store, internal, currentSnapshot, { syncImmutable: false });
|
|
37
|
+
} finally {
|
|
38
|
+
internal.rootState = rootState;
|
|
39
|
+
}
|
|
40
|
+
lastSnapshot = currentSnapshot;
|
|
41
|
+
return "handled";
|
|
42
|
+
}
|
|
43
|
+
lastSnapshot = currentSnapshot;
|
|
44
|
+
return "external";
|
|
45
|
+
};
|
|
46
|
+
if (!internal.toMutableRaw) {
|
|
47
|
+
internal.toMutableRaw = (key) => instancesMap.get(key);
|
|
48
|
+
Object.assign(store, { subscribe: (callback) => {
|
|
49
|
+
internal.assertAlive?.("subscribe");
|
|
50
|
+
store._subscriptions.add(callback);
|
|
51
|
+
return () => {
|
|
52
|
+
store._subscriptions?.delete(callback);
|
|
53
|
+
};
|
|
54
|
+
} });
|
|
55
|
+
store._subscriptions = /* @__PURE__ */ new Set();
|
|
56
|
+
store._destroyers = /* @__PURE__ */ new Set();
|
|
57
|
+
const baseDestroy = store.destroy;
|
|
58
|
+
let destroyed = false;
|
|
59
|
+
store.destroy = () => {
|
|
60
|
+
if (destroyed) return;
|
|
61
|
+
destroyed = true;
|
|
62
|
+
store._subscriptions?.clear();
|
|
63
|
+
store._subscriptions = void 0;
|
|
64
|
+
store._destroyers?.forEach((destroy) => destroy());
|
|
65
|
+
store._destroyers?.clear();
|
|
66
|
+
store._destroyers = void 0;
|
|
67
|
+
baseDestroy();
|
|
68
|
+
};
|
|
69
|
+
store.apply = (nextState = store.getPureState(), patches) => {
|
|
70
|
+
internal.assertAlive?.("apply");
|
|
71
|
+
isApplyingCoactionState = true;
|
|
72
|
+
try {
|
|
73
|
+
if (!patches) {
|
|
74
|
+
if (nextState === store.getState()) return;
|
|
75
|
+
(0, coaction.replaceMutableAdapterState)(internal.rootState ?? rawState, internal.toMutableRaw(rawState), store.getState(), nextState);
|
|
76
|
+
return;
|
|
77
|
+
}
|
|
78
|
+
(0, coaction.applyMutableAdapterPatches)(nextState, patches, internal.rootState ?? rawState, internal.toMutableRaw(rawState), store.getState());
|
|
79
|
+
} finally {
|
|
80
|
+
lastSnapshot = (0, coaction.snapshotMutableAdapterPureState)(store);
|
|
81
|
+
isApplyingCoactionState = false;
|
|
82
|
+
internal.notifyStateChange?.();
|
|
83
|
+
}
|
|
84
|
+
};
|
|
85
|
+
}
|
|
86
|
+
const mutableStore = internal.toMutableRaw(state);
|
|
87
|
+
if (!mutableStore) throw new Error("Pinia store instance is not found");
|
|
88
|
+
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);
|
|
92
|
+
stopWatch = mutableStore.$subscribe((...args) => {
|
|
93
|
+
const change = syncSharedExternalChange();
|
|
94
|
+
if (change === "ignored") return;
|
|
95
|
+
if (change === "external") internal.notifyStateChange?.();
|
|
96
|
+
store._subscriptions.forEach((callback) => callback(...args));
|
|
97
|
+
});
|
|
98
|
+
});
|
|
99
|
+
const destroy = () => {
|
|
100
|
+
cancelReadySubscription();
|
|
101
|
+
instancesMap.delete(state);
|
|
102
|
+
stopWatch?.();
|
|
103
|
+
};
|
|
104
|
+
store._destroyers.add(destroy);
|
|
9
105
|
};
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
};
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
106
|
+
/**
|
|
107
|
+
* Bind a store to Pinia
|
|
108
|
+
*/
|
|
109
|
+
const bindPinia = (0, coaction.createBinder)({
|
|
110
|
+
handleStore,
|
|
111
|
+
handleState: ((options) => {
|
|
112
|
+
const descriptors = {};
|
|
113
|
+
options.getters = options.getters ?? {};
|
|
114
|
+
options.actions = options.actions ?? {};
|
|
115
|
+
for (const key of Object.keys(options.getters)) {
|
|
116
|
+
const getter = options.getters[key];
|
|
117
|
+
if (typeof getter !== "function") continue;
|
|
118
|
+
descriptors[key] = { get() {
|
|
119
|
+
return getter.call(this, this);
|
|
120
|
+
} };
|
|
121
|
+
}
|
|
122
|
+
const rawState = Object.defineProperties({
|
|
123
|
+
...options.state?.(),
|
|
124
|
+
...options.actions
|
|
125
|
+
}, descriptors);
|
|
126
|
+
return {
|
|
127
|
+
copyState: options,
|
|
128
|
+
key: "actions",
|
|
129
|
+
bind: (state) => {
|
|
130
|
+
instancesMap.set(rawState, state);
|
|
131
|
+
return rawState;
|
|
132
|
+
}
|
|
133
|
+
};
|
|
134
|
+
})
|
|
34
135
|
});
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
}
|
|
48
|
-
});
|
|
49
|
-
store._subscriptions = /* @__PURE__ */ new Set();
|
|
50
|
-
store._destroyers = /* @__PURE__ */ new Set();
|
|
51
|
-
const baseDestroy = store.destroy;
|
|
52
|
-
let destroyed = false;
|
|
53
|
-
store.destroy = () => {
|
|
54
|
-
if (destroyed) {
|
|
55
|
-
return;
|
|
56
|
-
}
|
|
57
|
-
destroyed = true;
|
|
58
|
-
baseDestroy();
|
|
59
|
-
store._subscriptions.clear();
|
|
60
|
-
store._subscriptions = void 0;
|
|
61
|
-
store._destroyers.forEach((destroy2) => destroy2());
|
|
62
|
-
store._destroyers = void 0;
|
|
63
|
-
};
|
|
64
|
-
store.apply = (state2 = store.getState(), patches) => {
|
|
65
|
-
if (!patches) {
|
|
66
|
-
if (state2 === store.getState()) return;
|
|
67
|
-
Object.assign(store.getState(), state2);
|
|
68
|
-
return;
|
|
69
|
-
}
|
|
70
|
-
(0, import_mutability.apply)(state2, patches);
|
|
71
|
-
};
|
|
72
|
-
}
|
|
73
|
-
const mutableStore = internal.toMutableRaw(state);
|
|
74
|
-
if (!mutableStore) {
|
|
75
|
-
throw new Error("Pinia store instance is not found");
|
|
76
|
-
}
|
|
77
|
-
const stopWatch = mutableStore.$subscribe((...args) => {
|
|
78
|
-
store._subscriptions.forEach((callback) => callback(...args));
|
|
79
|
-
});
|
|
80
|
-
const destroy = () => {
|
|
81
|
-
instancesMap.delete(state);
|
|
82
|
-
stopWatch();
|
|
83
|
-
};
|
|
84
|
-
store._destroyers.add(destroy);
|
|
136
|
+
/**
|
|
137
|
+
* Adapt a store type to Pinia
|
|
138
|
+
*/
|
|
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;
|
|
85
148
|
};
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
descriptors[key] = {
|
|
97
|
-
get() {
|
|
98
|
-
return getter.call(this, this);
|
|
99
|
-
}
|
|
100
|
-
};
|
|
101
|
-
}
|
|
102
|
-
const rawState = Object.defineProperties(
|
|
103
|
-
{
|
|
104
|
-
...options.state?.(),
|
|
105
|
-
...options.actions
|
|
106
|
-
},
|
|
107
|
-
descriptors
|
|
108
|
-
);
|
|
109
|
-
const pinia = (0, import_pinia.createPinia)();
|
|
110
|
-
(0, import_pinia.setActivePinia)(pinia);
|
|
111
|
-
return {
|
|
112
|
-
copyState: options,
|
|
113
|
-
key: "actions",
|
|
114
|
-
bind: (state) => {
|
|
115
|
-
rawState.name = state.$id;
|
|
116
|
-
instancesMap.set(rawState, state);
|
|
117
|
-
return rawState;
|
|
118
|
-
}
|
|
119
|
-
};
|
|
120
|
-
})
|
|
121
|
-
});
|
|
122
|
-
var adapt = (store) => store;
|
|
123
|
-
|
|
124
|
-
// index.ts
|
|
125
|
-
__reExport(index_exports, src_exports, module.exports);
|
|
126
|
-
// Annotate the CommonJS export names for ESM import in node:
|
|
127
|
-
0 && (module.exports = {
|
|
128
|
-
adapt,
|
|
129
|
-
bindPinia
|
|
149
|
+
//#endregion
|
|
150
|
+
exports.adapt = adapt;
|
|
151
|
+
exports.bindPinia = bindPinia;
|
|
152
|
+
Object.keys(pinia).forEach(function(k) {
|
|
153
|
+
if (k !== "default" && !Object.prototype.hasOwnProperty.call(exports, k)) Object.defineProperty(exports, k, {
|
|
154
|
+
enumerable: true,
|
|
155
|
+
get: function() {
|
|
156
|
+
return pinia[k];
|
|
157
|
+
}
|
|
158
|
+
});
|
|
130
159
|
});
|
package/dist/index.mjs
CHANGED
|
@@ -1,127 +1,150 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
1
|
+
import { applyMutableAdapterPatches, createBinder, isEqualMutableAdapterSnapshot, onStoreReady, replaceExternalStoreState, replaceMutableAdapterState, sanitizeInitialStateValue, snapshotMutableAdapterPureState } from "coaction";
|
|
2
|
+
import { createPinia } from "pinia";
|
|
3
|
+
export * from "pinia";
|
|
4
|
+
//#endregion
|
|
5
|
+
//#region packages/coaction-pinia/src/index.ts
|
|
6
|
+
const instancesMap = /* @__PURE__ */ new WeakMap();
|
|
7
|
+
const handleStore = (store, state, _, internal) => {
|
|
8
|
+
const rawState = state;
|
|
9
|
+
let isApplyingCoactionState = false;
|
|
10
|
+
let lastSnapshot;
|
|
11
|
+
const restoreClientState = (snapshot) => {
|
|
12
|
+
const mutableStore = internal.toMutableRaw(rawState);
|
|
13
|
+
if (!mutableStore) return;
|
|
14
|
+
isApplyingCoactionState = true;
|
|
15
|
+
try {
|
|
16
|
+
replaceMutableAdapterState(internal.rootState ?? rawState, mutableStore, store.getState(), snapshot);
|
|
17
|
+
} finally {
|
|
18
|
+
lastSnapshot = snapshotMutableAdapterPureState(store);
|
|
19
|
+
isApplyingCoactionState = false;
|
|
20
|
+
}
|
|
21
|
+
};
|
|
22
|
+
const syncSharedExternalChange = () => {
|
|
23
|
+
const currentSnapshot = snapshotMutableAdapterPureState(store);
|
|
24
|
+
if (isApplyingCoactionState) {
|
|
25
|
+
lastSnapshot = currentSnapshot;
|
|
26
|
+
return "handled";
|
|
27
|
+
}
|
|
28
|
+
if (store.share === "client" && lastSnapshot) {
|
|
29
|
+
if (!isEqualMutableAdapterSnapshot(currentSnapshot, lastSnapshot)) restoreClientState(lastSnapshot);
|
|
30
|
+
return "ignored";
|
|
31
|
+
}
|
|
32
|
+
if (store.share === "main" && lastSnapshot) {
|
|
33
|
+
const rootState = internal.rootState;
|
|
34
|
+
internal.rootState = lastSnapshot;
|
|
35
|
+
try {
|
|
36
|
+
replaceExternalStoreState(store, internal, currentSnapshot, { syncImmutable: false });
|
|
37
|
+
} finally {
|
|
38
|
+
internal.rootState = rootState;
|
|
39
|
+
}
|
|
40
|
+
lastSnapshot = currentSnapshot;
|
|
41
|
+
return "handled";
|
|
42
|
+
}
|
|
43
|
+
lastSnapshot = currentSnapshot;
|
|
44
|
+
return "external";
|
|
45
|
+
};
|
|
46
|
+
if (!internal.toMutableRaw) {
|
|
47
|
+
internal.toMutableRaw = (key) => instancesMap.get(key);
|
|
48
|
+
Object.assign(store, { subscribe: (callback) => {
|
|
49
|
+
internal.assertAlive?.("subscribe");
|
|
50
|
+
store._subscriptions.add(callback);
|
|
51
|
+
return () => {
|
|
52
|
+
store._subscriptions?.delete(callback);
|
|
53
|
+
};
|
|
54
|
+
} });
|
|
55
|
+
store._subscriptions = /* @__PURE__ */ new Set();
|
|
56
|
+
store._destroyers = /* @__PURE__ */ new Set();
|
|
57
|
+
const baseDestroy = store.destroy;
|
|
58
|
+
let destroyed = false;
|
|
59
|
+
store.destroy = () => {
|
|
60
|
+
if (destroyed) return;
|
|
61
|
+
destroyed = true;
|
|
62
|
+
store._subscriptions?.clear();
|
|
63
|
+
store._subscriptions = void 0;
|
|
64
|
+
store._destroyers?.forEach((destroy) => destroy());
|
|
65
|
+
store._destroyers?.clear();
|
|
66
|
+
store._destroyers = void 0;
|
|
67
|
+
baseDestroy();
|
|
68
|
+
};
|
|
69
|
+
store.apply = (nextState = store.getPureState(), patches) => {
|
|
70
|
+
internal.assertAlive?.("apply");
|
|
71
|
+
isApplyingCoactionState = true;
|
|
72
|
+
try {
|
|
73
|
+
if (!patches) {
|
|
74
|
+
if (nextState === store.getState()) return;
|
|
75
|
+
replaceMutableAdapterState(internal.rootState ?? rawState, internal.toMutableRaw(rawState), store.getState(), nextState);
|
|
76
|
+
return;
|
|
77
|
+
}
|
|
78
|
+
applyMutableAdapterPatches(nextState, patches, internal.rootState ?? rawState, internal.toMutableRaw(rawState), store.getState());
|
|
79
|
+
} finally {
|
|
80
|
+
lastSnapshot = snapshotMutableAdapterPureState(store);
|
|
81
|
+
isApplyingCoactionState = false;
|
|
82
|
+
internal.notifyStateChange?.();
|
|
83
|
+
}
|
|
84
|
+
};
|
|
85
|
+
}
|
|
86
|
+
const mutableStore = internal.toMutableRaw(state);
|
|
87
|
+
if (!mutableStore) throw new Error("Pinia store instance is not found");
|
|
88
|
+
let stopWatch;
|
|
89
|
+
const cancelReadySubscription = onStoreReady(store, () => {
|
|
90
|
+
replaceMutableAdapterState(internal.rootState ?? rawState, mutableStore, store.getState(), sanitizeInitialStateValue(snapshotMutableAdapterPureState(store)));
|
|
91
|
+
lastSnapshot = snapshotMutableAdapterPureState(store);
|
|
92
|
+
stopWatch = mutableStore.$subscribe((...args) => {
|
|
93
|
+
const change = syncSharedExternalChange();
|
|
94
|
+
if (change === "ignored") return;
|
|
95
|
+
if (change === "external") internal.notifyStateChange?.();
|
|
96
|
+
store._subscriptions.forEach((callback) => callback(...args));
|
|
97
|
+
});
|
|
98
|
+
});
|
|
99
|
+
const destroy = () => {
|
|
100
|
+
cancelReadySubscription();
|
|
101
|
+
instancesMap.delete(state);
|
|
102
|
+
stopWatch?.();
|
|
103
|
+
};
|
|
104
|
+
store._destroyers.add(destroy);
|
|
8
105
|
};
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
};
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
}
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
var handleStore = (store, state, _, internal) => {
|
|
39
|
-
if (!internal.toMutableRaw) {
|
|
40
|
-
internal.toMutableRaw = (key) => instancesMap.get(key);
|
|
41
|
-
Object.assign(store, {
|
|
42
|
-
subscribe: (callback) => {
|
|
43
|
-
store._subscriptions.add(callback);
|
|
44
|
-
return () => store._subscriptions.delete(callback);
|
|
45
|
-
}
|
|
46
|
-
});
|
|
47
|
-
store._subscriptions = /* @__PURE__ */ new Set();
|
|
48
|
-
store._destroyers = /* @__PURE__ */ new Set();
|
|
49
|
-
const baseDestroy = store.destroy;
|
|
50
|
-
let destroyed = false;
|
|
51
|
-
store.destroy = () => {
|
|
52
|
-
if (destroyed) {
|
|
53
|
-
return;
|
|
54
|
-
}
|
|
55
|
-
destroyed = true;
|
|
56
|
-
baseDestroy();
|
|
57
|
-
store._subscriptions.clear();
|
|
58
|
-
store._subscriptions = void 0;
|
|
59
|
-
store._destroyers.forEach((destroy2) => destroy2());
|
|
60
|
-
store._destroyers = void 0;
|
|
61
|
-
};
|
|
62
|
-
store.apply = (state2 = store.getState(), patches) => {
|
|
63
|
-
if (!patches) {
|
|
64
|
-
if (state2 === store.getState()) return;
|
|
65
|
-
Object.assign(store.getState(), state2);
|
|
66
|
-
return;
|
|
67
|
-
}
|
|
68
|
-
apply(state2, patches);
|
|
69
|
-
};
|
|
70
|
-
}
|
|
71
|
-
const mutableStore = internal.toMutableRaw(state);
|
|
72
|
-
if (!mutableStore) {
|
|
73
|
-
throw new Error("Pinia store instance is not found");
|
|
74
|
-
}
|
|
75
|
-
const stopWatch = mutableStore.$subscribe((...args) => {
|
|
76
|
-
store._subscriptions.forEach((callback) => callback(...args));
|
|
77
|
-
});
|
|
78
|
-
const destroy = () => {
|
|
79
|
-
instancesMap.delete(state);
|
|
80
|
-
stopWatch();
|
|
81
|
-
};
|
|
82
|
-
store._destroyers.add(destroy);
|
|
83
|
-
};
|
|
84
|
-
var bindPinia = createBinder({
|
|
85
|
-
handleStore,
|
|
86
|
-
handleState: ((options) => {
|
|
87
|
-
const descriptors = {};
|
|
88
|
-
options.getters = options.getters ?? {};
|
|
89
|
-
for (const key of Object.keys(options.getters)) {
|
|
90
|
-
const getter = options.getters[key];
|
|
91
|
-
if (typeof getter !== "function") {
|
|
92
|
-
continue;
|
|
93
|
-
}
|
|
94
|
-
descriptors[key] = {
|
|
95
|
-
get() {
|
|
96
|
-
return getter.call(this, this);
|
|
97
|
-
}
|
|
98
|
-
};
|
|
99
|
-
}
|
|
100
|
-
const rawState = Object.defineProperties(
|
|
101
|
-
{
|
|
102
|
-
...options.state?.(),
|
|
103
|
-
...options.actions
|
|
104
|
-
},
|
|
105
|
-
descriptors
|
|
106
|
-
);
|
|
107
|
-
const pinia = createPinia();
|
|
108
|
-
setActivePinia(pinia);
|
|
109
|
-
return {
|
|
110
|
-
copyState: options,
|
|
111
|
-
key: "actions",
|
|
112
|
-
bind: (state) => {
|
|
113
|
-
rawState.name = state.$id;
|
|
114
|
-
instancesMap.set(rawState, state);
|
|
115
|
-
return rawState;
|
|
116
|
-
}
|
|
117
|
-
};
|
|
118
|
-
})
|
|
106
|
+
/**
|
|
107
|
+
* Bind a store to Pinia
|
|
108
|
+
*/
|
|
109
|
+
const bindPinia = createBinder({
|
|
110
|
+
handleStore,
|
|
111
|
+
handleState: ((options) => {
|
|
112
|
+
const descriptors = {};
|
|
113
|
+
options.getters = options.getters ?? {};
|
|
114
|
+
options.actions = options.actions ?? {};
|
|
115
|
+
for (const key of Object.keys(options.getters)) {
|
|
116
|
+
const getter = options.getters[key];
|
|
117
|
+
if (typeof getter !== "function") continue;
|
|
118
|
+
descriptors[key] = { get() {
|
|
119
|
+
return getter.call(this, this);
|
|
120
|
+
} };
|
|
121
|
+
}
|
|
122
|
+
const rawState = Object.defineProperties({
|
|
123
|
+
...options.state?.(),
|
|
124
|
+
...options.actions
|
|
125
|
+
}, descriptors);
|
|
126
|
+
return {
|
|
127
|
+
copyState: options,
|
|
128
|
+
key: "actions",
|
|
129
|
+
bind: (state) => {
|
|
130
|
+
instancesMap.set(rawState, state);
|
|
131
|
+
return rawState;
|
|
132
|
+
}
|
|
133
|
+
};
|
|
134
|
+
})
|
|
119
135
|
});
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
136
|
+
/**
|
|
137
|
+
* Adapt a store type to Pinia
|
|
138
|
+
*/
|
|
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;
|
|
127
148
|
};
|
|
149
|
+
//#endregion
|
|
150
|
+
export { adapt, bindPinia };
|
package/package.json
CHANGED
|
@@ -1,75 +1,68 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@coaction/pinia",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "2.1.0",
|
|
4
4
|
"description": "A Coaction integration tool for Pinia",
|
|
5
5
|
"keywords": [
|
|
6
|
-
"state",
|
|
7
6
|
"coaction",
|
|
8
|
-
"pinia"
|
|
7
|
+
"pinia",
|
|
8
|
+
"state"
|
|
9
9
|
],
|
|
10
|
-
"
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
10
|
+
"homepage": "https://github.com/coactionjs/coaction/tree/main/packages/coaction-pinia#readme",
|
|
11
|
+
"bugs": {
|
|
12
|
+
"url": "https://github.com/coactionjs/coaction/issues"
|
|
13
|
+
},
|
|
14
14
|
"license": "MIT",
|
|
15
|
+
"author": "unadlib",
|
|
16
|
+
"repository": {
|
|
17
|
+
"type": "git",
|
|
18
|
+
"url": "https://github.com/coactionjs/coaction.git",
|
|
19
|
+
"directory": "packages/coaction-pinia"
|
|
20
|
+
},
|
|
21
|
+
"files": [
|
|
22
|
+
"dist"
|
|
23
|
+
],
|
|
24
|
+
"sideEffects": false,
|
|
15
25
|
"main": "dist/index.js",
|
|
16
26
|
"module": "dist/index.mjs",
|
|
27
|
+
"types": "dist/index.d.ts",
|
|
17
28
|
"exports": {
|
|
18
29
|
".": {
|
|
19
|
-
"
|
|
20
|
-
|
|
21
|
-
|
|
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
|
+
},
|
|
22
38
|
"default": "./dist/index.mjs"
|
|
23
39
|
},
|
|
24
40
|
"./package.json": "./package.json"
|
|
25
41
|
},
|
|
26
|
-
"
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
"dist"
|
|
30
|
-
],
|
|
31
|
-
"repository": {
|
|
32
|
-
"type": "git",
|
|
33
|
-
"url": "https://github.com/unadlib/coaction.git",
|
|
34
|
-
"directory": "packages/coaction-pinia"
|
|
35
|
-
},
|
|
36
|
-
"bugs": {
|
|
37
|
-
"url": "https://github.com/unadlib/coaction/issues"
|
|
38
|
-
},
|
|
39
|
-
"peerDependencies": {
|
|
40
|
-
"coaction": "^1.5.0",
|
|
41
|
-
"mutative": "^1.1.0",
|
|
42
|
-
"pinia": "^2.0 || ^3.0.0"
|
|
42
|
+
"publishConfig": {
|
|
43
|
+
"access": "public",
|
|
44
|
+
"provenance": true
|
|
43
45
|
},
|
|
44
|
-
"
|
|
45
|
-
"
|
|
46
|
-
"optional": true
|
|
47
|
-
},
|
|
48
|
-
"mutative": {
|
|
49
|
-
"optional": true
|
|
50
|
-
},
|
|
51
|
-
"pinia": {
|
|
52
|
-
"optional": true
|
|
53
|
-
}
|
|
46
|
+
"dependencies": {
|
|
47
|
+
"mutability": "^1.2.1"
|
|
54
48
|
},
|
|
55
49
|
"devDependencies": {
|
|
56
50
|
"data-transport": "^5.0.3",
|
|
57
51
|
"mutative": "^1.3.0",
|
|
58
52
|
"pinia": "^3.0.4",
|
|
59
|
-
"coaction": "1.
|
|
53
|
+
"coaction": "2.1.0"
|
|
60
54
|
},
|
|
61
|
-
"
|
|
62
|
-
"
|
|
63
|
-
|
|
64
|
-
"publishConfig": {
|
|
65
|
-
"access": "public",
|
|
66
|
-
"provenance": true
|
|
55
|
+
"peerDependencies": {
|
|
56
|
+
"coaction": ">=2.1.0 <3",
|
|
57
|
+
"pinia": "^2.0 || ^3.0.0"
|
|
67
58
|
},
|
|
68
|
-
"
|
|
59
|
+
"authors": [
|
|
60
|
+
"Michael Lin <unadlib@gmail.com> (https://github.com/unadlib)"
|
|
61
|
+
],
|
|
69
62
|
"scripts": {
|
|
70
63
|
"clean": "rm -rf dist",
|
|
71
64
|
"test": "vitest run test",
|
|
72
|
-
"build": "
|
|
73
|
-
"dev": "
|
|
65
|
+
"build": "node ../../scripts/build-package.mjs",
|
|
66
|
+
"dev": "node ../../scripts/build-package.mjs --watch"
|
|
74
67
|
}
|
|
75
68
|
}
|