@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 CHANGED
@@ -1,40 +1,40 @@
1
1
  # @coaction/pinia
2
2
 
3
- ![Node CI](https://github.com/unadlib/coaction/workflows/Node%20CI/badge.svg)
4
- [![npm](https://img.shields.io/npm/v/@coaction/pinia.svg)](https://www.npmjs.com/package/@coaction/pinia)
5
- ![license](https://img.shields.io/npm/l/@coaction/pinia)
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)
6
4
 
7
5
  A Coaction integration tool for Pinia
8
6
 
9
7
  ## Installation
10
8
 
11
- You can install it via npm, yarn or pnpm.
9
+ Install it with pnpm:
12
10
 
13
11
  ```sh
14
- npm install coaction @coaction/pinia
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
- defineStore(
26
- 'test',
27
- bindPinia({
28
- state: () => ({ count: 0 }),
29
- getters: {
30
- double: (state) => state.count * 2
31
- },
32
- actions: {
33
- increment(state) {
34
- state.count += 1;
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/unadlib/coaction).
55
+ You can find the documentation [here](https://github.com/coactionjs/coaction).
package/dist/index.d.mts CHANGED
@@ -1,25 +1,13 @@
1
- import { StoreDefinition, StateTree, _GettersTree, DefineStoreOptions } from 'pinia';
2
- export * from 'pinia';
1
+ import { DefineStoreOptions, StateTree, StoreDefinition, _GettersTree } from "pinia";
2
+ export * from "pinia";
3
3
 
4
- type FunctionKeys<T> = {
5
- [K in keyof T]: T[K] extends (...args: any[]) => any ? K : never;
6
- }[keyof T];
7
- type Equal<X, Y> = (<T>() => T extends X ? 1 : 2) extends <T>() => T extends Y ? 1 : 2 ? true : false;
8
- type ReadonlyKeys<T> = {
9
- [K in keyof T]: Equal<{
10
- [P in K]: T[P];
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 { type IStore, type PiniaStore, adapt, bindPinia };
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 { StoreDefinition, StateTree, _GettersTree, DefineStoreOptions } from 'pinia';
2
- export * from 'pinia';
1
+ import { DefineStoreOptions, StateTree, StoreDefinition, _GettersTree } from "pinia";
2
+ export * from "pinia";
3
3
 
4
- type FunctionKeys<T> = {
5
- [K in keyof T]: T[K] extends (...args: any[]) => any ? K : never;
6
- }[keyof T];
7
- type Equal<X, Y> = (<T>() => T extends X ? 1 : 2) extends <T>() => T extends Y ? 1 : 2 ? true : false;
8
- type ReadonlyKeys<T> = {
9
- [K in keyof T]: Equal<{
10
- [P in K]: T[P];
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 { type IStore, type PiniaStore, adapt, bindPinia };
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
- "use strict";
2
- var __defProp = Object.defineProperty;
3
- var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
- var __getOwnPropNames = Object.getOwnPropertyNames;
5
- var __hasOwnProp = Object.prototype.hasOwnProperty;
6
- var __export = (target, all) => {
7
- for (var name in all)
8
- __defProp(target, name, { get: all[name], enumerable: true });
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
- var __copyProps = (to, from, except, desc) => {
11
- if (from && typeof from === "object" || typeof from === "function") {
12
- for (let key of __getOwnPropNames(from))
13
- if (!__hasOwnProp.call(to, key) && key !== except)
14
- __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
15
- }
16
- return to;
17
- };
18
- var __reExport = (target, mod, secondTarget) => (__copyProps(target, mod, "default"), secondTarget && __copyProps(secondTarget, mod, "default"));
19
- var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
20
-
21
- // index.ts
22
- var index_exports = {};
23
- __export(index_exports, {
24
- adapt: () => adapt,
25
- bindPinia: () => bindPinia
26
- });
27
- module.exports = __toCommonJS(index_exports);
28
-
29
- // src/index.ts
30
- var src_exports = {};
31
- __export(src_exports, {
32
- adapt: () => adapt,
33
- bindPinia: () => bindPinia
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
- var import_mutability = require("mutability");
36
- var import_coaction = require("coaction");
37
- var import_pinia = require("pinia");
38
- __reExport(src_exports, require("pinia"));
39
- var instancesMap = /* @__PURE__ */ new WeakMap();
40
- var handleStore = (store, state, _, internal) => {
41
- if (!internal.toMutableRaw) {
42
- internal.toMutableRaw = (key) => instancesMap.get(key);
43
- Object.assign(store, {
44
- subscribe: (callback) => {
45
- store._subscriptions.add(callback);
46
- return () => store._subscriptions.delete(callback);
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
- var bindPinia = (0, import_coaction.createBinder)({
87
- handleStore,
88
- handleState: ((options) => {
89
- const descriptors = {};
90
- options.getters = options.getters ?? {};
91
- for (const key of Object.keys(options.getters)) {
92
- const getter = options.getters[key];
93
- if (typeof getter !== "function") {
94
- continue;
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
- var __defProp = Object.defineProperty;
2
- var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
3
- var __getOwnPropNames = Object.getOwnPropertyNames;
4
- var __hasOwnProp = Object.prototype.hasOwnProperty;
5
- var __export = (target, all) => {
6
- for (var name in all)
7
- __defProp(target, name, { get: all[name], enumerable: true });
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
- var __copyProps = (to, from, except, desc) => {
10
- if (from && typeof from === "object" || typeof from === "function") {
11
- for (let key of __getOwnPropNames(from))
12
- if (!__hasOwnProp.call(to, key) && key !== except)
13
- __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
14
- }
15
- return to;
16
- };
17
- var __reExport = (target, mod, secondTarget) => (__copyProps(target, mod, "default"), secondTarget && __copyProps(secondTarget, mod, "default"));
18
-
19
- // index.ts
20
- var index_exports = {};
21
- __export(index_exports, {
22
- adapt: () => adapt,
23
- bindPinia: () => bindPinia
24
- });
25
-
26
- // src/index.ts
27
- var src_exports = {};
28
- __export(src_exports, {
29
- adapt: () => adapt,
30
- bindPinia: () => bindPinia
31
- });
32
- __reExport(src_exports, pinia_star);
33
- import { apply } from "mutability";
34
- import { createBinder } from "coaction";
35
- import { createPinia, setActivePinia } from "pinia";
36
- import * as pinia_star from "pinia";
37
- var instancesMap = /* @__PURE__ */ new WeakMap();
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
- var adapt = (store) => store;
121
-
122
- // index.ts
123
- __reExport(index_exports, src_exports);
124
- export {
125
- adapt,
126
- bindPinia
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.5.0",
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
- "authors": [
11
- "Michael Lin <unadlib@gmail.com> (https://github.com/unadlib)"
12
- ],
13
- "homepage": "https://github.com/unadlib/coaction/tree/main/packages/coaction-pinia#readme",
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
- "types": "./dist/index.d.ts",
20
- "import": "./dist/index.mjs",
21
- "require": "./dist/index.js",
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
- "types": "dist/index.d.ts",
27
- "sideEffects": false,
28
- "files": [
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
- "peerDependenciesMeta": {
45
- "coaction": {
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.5.0"
53
+ "coaction": "2.1.0"
60
54
  },
61
- "dependencies": {
62
- "mutability": "^1.2.1"
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
- "author": "unadlib",
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": "tsup index.ts --format cjs,esm --dts --clean --out-dir dist",
73
- "dev": "tsup index.ts --format cjs,esm --dts --watch --out-dir dist"
65
+ "build": "node ../../scripts/build-package.mjs",
66
+ "dev": "node ../../scripts/build-package.mjs --watch"
74
67
  }
75
68
  }