@coaction/pinia 0.1.5 → 1.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/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2024 Michael Lin
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md CHANGED
@@ -39,6 +39,11 @@ const useStore = create(() =>
39
39
  );
40
40
  ```
41
41
 
42
+ ## Limitations
43
+
44
+ - `@coaction/pinia` only supports binding a whole Pinia store.
45
+ - Coaction `Slices` mode is not supported in this adapter.
46
+
42
47
  ## Documentation
43
48
 
44
49
  You can find the documentation [here](https://github.com/unadlib/coaction).
@@ -0,0 +1,33 @@
1
+ import { StoreDefinition, StateTree, _GettersTree, DefineStoreOptions } from 'pinia';
2
+ export * from 'pinia';
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
+ ];
23
+ /**
24
+ * Bind a store to Pinia
25
+ */
26
+ declare const bindPinia: <Id extends string, S extends StateTree = {}, G extends _GettersTree<S> = {}, A = {}>(options: Omit<DefineStoreOptions<Id, S, G, A>, "id">) => Omit<DefineStoreOptions<Id, S, G, A>, "id">;
27
+ /**
28
+ * Adapt a store type to Pinia
29
+ */
30
+ declare const adapt: <T extends object>(store: StoreDefinition<IStore<T>[0], IStore<T>[1], IStore<T>[2], IStore<T>[3]>) => T;
31
+ 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 };
@@ -1,5 +1,6 @@
1
- import type { _GettersTree, DefineStoreOptions, StateTree, StoreDefinition } from 'pinia';
1
+ import { StoreDefinition, StateTree, _GettersTree, DefineStoreOptions } from 'pinia';
2
2
  export * from 'pinia';
3
+
3
4
  type FunctionKeys<T> = {
4
5
  [K in keyof T]: T[K] extends (...args: any[]) => any ? K : never;
5
6
  }[keyof T];
@@ -11,7 +12,7 @@ type ReadonlyKeys<T> = {
11
12
  [P in K]: T[P];
12
13
  }>> extends true ? K : never;
13
14
  }[keyof T];
14
- export type IStore<T extends object> = [
15
+ type IStore<T extends object> = [
15
16
  string,
16
17
  Pick<T, Exclude<keyof T, ReadonlyKeys<T> | FunctionKeys<T>>>,
17
18
  {
@@ -22,9 +23,11 @@ export type IStore<T extends object> = [
22
23
  /**
23
24
  * Bind a store to Pinia
24
25
  */
25
- export declare const bindPinia: <Id extends string, S extends StateTree = {}, G extends _GettersTree<S> = {}, A = {}>(options: Omit<DefineStoreOptions<Id, S, G, A>, "id">) => Omit<DefineStoreOptions<Id, S, G, A>, "id">;
26
+ declare const bindPinia: <Id extends string, S extends StateTree = {}, G extends _GettersTree<S> = {}, A = {}>(options: Omit<DefineStoreOptions<Id, S, G, A>, "id">) => Omit<DefineStoreOptions<Id, S, G, A>, "id">;
26
27
  /**
27
28
  * Adapt a store type to Pinia
28
29
  */
29
- export declare const adapt: <T extends object>(store: StoreDefinition<IStore<T>[0], IStore<T>[1], IStore<T>[2], IStore<T>[3]>) => T;
30
- export type PiniaStore<T extends object> = StoreDefinition<IStore<T>[0], IStore<T>[1], IStore<T>[2], IStore<T>[3]>;
30
+ declare const adapt: <T extends object>(store: StoreDefinition<IStore<T>[0], IStore<T>[1], IStore<T>[2], IStore<T>[3]>) => T;
31
+ 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 };
package/dist/index.js ADDED
@@ -0,0 +1,121 @@
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 });
9
+ };
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
34
+ });
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
+ store.destroy = () => {
53
+ baseDestroy();
54
+ store._subscriptions.clear();
55
+ store._subscriptions = void 0;
56
+ store._destroyers.forEach((destroy2) => destroy2());
57
+ store._destroyers = void 0;
58
+ };
59
+ store.apply = (state2 = store.getState(), patches) => {
60
+ if (!patches) {
61
+ if (state2 === store.getState()) return;
62
+ Object.assign(store.getState(), state2);
63
+ return;
64
+ }
65
+ (0, import_mutability.apply)(state2, patches);
66
+ };
67
+ }
68
+ const mutableStore = internal.toMutableRaw(state);
69
+ if (!mutableStore) {
70
+ throw new Error("Pinia store instance is not found");
71
+ }
72
+ const stopWatch = mutableStore.$subscribe((...args) => {
73
+ store._subscriptions.forEach((callback) => callback(...args));
74
+ });
75
+ const destroy = () => {
76
+ instancesMap.delete(state);
77
+ stopWatch();
78
+ };
79
+ store._destroyers.add(destroy);
80
+ };
81
+ var bindPinia = (0, import_coaction.createBinder)({
82
+ handleStore,
83
+ handleState: ((options) => {
84
+ const descriptors = {};
85
+ options.getters = options.getters ?? {};
86
+ for (const key in options.getters) {
87
+ descriptors[key] = {
88
+ get() {
89
+ return options.getters[key].call(this, this);
90
+ }
91
+ };
92
+ }
93
+ const rawState = Object.defineProperties(
94
+ {
95
+ ...options.state?.(),
96
+ ...options.actions
97
+ },
98
+ descriptors
99
+ );
100
+ const pinia = (0, import_pinia.createPinia)();
101
+ (0, import_pinia.setActivePinia)(pinia);
102
+ return {
103
+ copyState: options,
104
+ key: "actions",
105
+ bind: (state) => {
106
+ rawState.name = state.$id;
107
+ instancesMap.set(rawState, state);
108
+ return rawState;
109
+ }
110
+ };
111
+ })
112
+ });
113
+ var adapt = (store) => store;
114
+
115
+ // index.ts
116
+ __reExport(index_exports, src_exports, module.exports);
117
+ // Annotate the CommonJS export names for ESM import in node:
118
+ 0 && (module.exports = {
119
+ adapt,
120
+ bindPinia
121
+ });
package/dist/index.mjs ADDED
@@ -0,0 +1,118 @@
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 });
8
+ };
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
+ store.destroy = () => {
51
+ baseDestroy();
52
+ store._subscriptions.clear();
53
+ store._subscriptions = void 0;
54
+ store._destroyers.forEach((destroy2) => destroy2());
55
+ store._destroyers = void 0;
56
+ };
57
+ store.apply = (state2 = store.getState(), patches) => {
58
+ if (!patches) {
59
+ if (state2 === store.getState()) return;
60
+ Object.assign(store.getState(), state2);
61
+ return;
62
+ }
63
+ apply(state2, patches);
64
+ };
65
+ }
66
+ const mutableStore = internal.toMutableRaw(state);
67
+ if (!mutableStore) {
68
+ throw new Error("Pinia store instance is not found");
69
+ }
70
+ const stopWatch = mutableStore.$subscribe((...args) => {
71
+ store._subscriptions.forEach((callback) => callback(...args));
72
+ });
73
+ const destroy = () => {
74
+ instancesMap.delete(state);
75
+ stopWatch();
76
+ };
77
+ store._destroyers.add(destroy);
78
+ };
79
+ var bindPinia = createBinder({
80
+ handleStore,
81
+ handleState: ((options) => {
82
+ const descriptors = {};
83
+ options.getters = options.getters ?? {};
84
+ for (const key in options.getters) {
85
+ descriptors[key] = {
86
+ get() {
87
+ return options.getters[key].call(this, this);
88
+ }
89
+ };
90
+ }
91
+ const rawState = Object.defineProperties(
92
+ {
93
+ ...options.state?.(),
94
+ ...options.actions
95
+ },
96
+ descriptors
97
+ );
98
+ const pinia = createPinia();
99
+ setActivePinia(pinia);
100
+ return {
101
+ copyState: options,
102
+ key: "actions",
103
+ bind: (state) => {
104
+ rawState.name = state.$id;
105
+ instancesMap.set(rawState, state);
106
+ return rawState;
107
+ }
108
+ };
109
+ })
110
+ });
111
+ var adapt = (store) => store;
112
+
113
+ // index.ts
114
+ __reExport(index_exports, src_exports);
115
+ export {
116
+ adapt,
117
+ bindPinia
118
+ };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@coaction/pinia",
3
- "version": "0.1.5",
3
+ "version": "1.0.0",
4
4
  "description": "A Coaction integration tool for Pinia",
5
5
  "keywords": [
6
6
  "state",
@@ -12,44 +12,34 @@
12
12
  ],
13
13
  "homepage": "https://github.com/unadlib/coaction/tree/main/packages/coaction-pinia#readme",
14
14
  "license": "MIT",
15
- "main": "dist/coaction-pinia.cjs.js",
16
- "module": "dist/coaction-pinia.esm.js",
17
- "umd:main": "dist/coaction-pinia.umd.min.js",
15
+ "main": "dist/index.js",
16
+ "module": "dist/index.mjs",
18
17
  "exports": {
19
18
  ".": {
20
- "types": {
21
- "import": "./dist/coaction-pinia.cjs.mjs",
22
- "default": "./dist/coaction-pinia.cjs.js"
23
- },
24
- "module": "./dist/coaction-pinia.esm.js",
25
- "import": "./dist/coaction-pinia.cjs.mjs",
26
- "default": "./dist/coaction-pinia.cjs.js"
19
+ "types": "./dist/index.d.ts",
20
+ "import": "./dist/index.mjs",
21
+ "require": "./dist/index.js",
22
+ "default": "./dist/index.mjs"
27
23
  },
28
24
  "./package.json": "./package.json"
29
25
  },
30
- "types": "dist/coaction-pinia.cjs.d.ts",
26
+ "types": "dist/index.d.ts",
31
27
  "sideEffects": false,
32
28
  "files": [
33
29
  "dist"
34
30
  ],
35
31
  "repository": {
36
32
  "type": "git",
37
- "url": "git+ssh://git@github.com/unadlib/coaction.git"
33
+ "url": "https://github.com/unadlib/coaction.git",
34
+ "directory": "packages/coaction-pinia"
38
35
  },
39
- "scripts": {},
40
36
  "bugs": {
41
37
  "url": "https://github.com/unadlib/coaction/issues"
42
38
  },
43
- "preconstruct": {
44
- "umdName": "CoactionPinia",
45
- "entrypoints": [
46
- "./index.ts"
47
- ]
48
- },
49
39
  "peerDependencies": {
50
- "coaction": "^0.1.5",
40
+ "coaction": "^0.2.0",
51
41
  "mutative": "^1.1.0",
52
- "pinia": "^2.0"
42
+ "pinia": "^2.0 || ^3.0.0"
53
43
  },
54
44
  "peerDependenciesMeta": {
55
45
  "coaction": {
@@ -63,11 +53,23 @@
63
53
  }
64
54
  },
65
55
  "devDependencies": {
66
- "coaction": "^0.1.5",
67
- "mutative": "^1.1.0",
68
- "pinia": "^2.2.4"
56
+ "coaction": "^0.2.0",
57
+ "data-transport": "^5.0.3",
58
+ "mutative": "^1.3.0",
59
+ "pinia": "^3.0.4"
69
60
  },
70
61
  "dependencies": {
71
- "mutability": "^1.1.1"
62
+ "mutability": "^1.2.1"
63
+ },
64
+ "publishConfig": {
65
+ "access": "public",
66
+ "provenance": true
67
+ },
68
+ "author": "unadlib",
69
+ "scripts": {
70
+ "clean": "rm -rf dist",
71
+ "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"
72
74
  }
73
- }
75
+ }
@@ -1,2 +0,0 @@
1
- export * from "./declarations/src/index.js";
2
- //# sourceMappingURL=data:application/json;charset=utf-8;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoiY29hY3Rpb24tcGluaWEuY2pzLmQubXRzIiwic291cmNlUm9vdCI6IiIsInNvdXJjZXMiOlsiLi9kZWNsYXJhdGlvbnMvc3JjL2luZGV4LmQudHMiXSwibmFtZXMiOltdLCJtYXBwaW5ncyI6IkFBQUEifQ==
@@ -1,2 +0,0 @@
1
- export * from "./declarations/src/index";
2
- //# sourceMappingURL=data:application/json;charset=utf-8;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoiY29hY3Rpb24tcGluaWEuY2pzLmQudHMiLCJzb3VyY2VSb290IjoiIiwic291cmNlcyI6WyIuL2RlY2xhcmF0aW9ucy9zcmMvaW5kZXguZC50cyJdLCJuYW1lcyI6W10sIm1hcHBpbmdzIjoiQUFBQSJ9
@@ -1,105 +0,0 @@
1
- 'use strict';
2
-
3
- Object.defineProperty(exports, '__esModule', { value: true });
4
-
5
- var mutability = require('mutability');
6
- var coaction = require('coaction');
7
- var pinia = require('pinia');
8
-
9
- const instancesMap = new WeakMap();
10
- const handleStore = (store, state, _, internal) => {
11
- if (!internal.toMutableRaw) {
12
- internal.toMutableRaw = key => instancesMap.get(key);
13
- Object.assign(store, {
14
- subscribe: callback => {
15
- store._subscriptions.add(callback);
16
- return () => store._subscriptions.delete(callback);
17
- }
18
- });
19
- store._subscriptions = new Set();
20
- store._destroyers = new Set();
21
- const baseDestroy = store.destroy;
22
- store.destroy = () => {
23
- baseDestroy();
24
- store._subscriptions.clear();
25
- store._subscriptions = undefined;
26
- store._destroyers.forEach(destroy => destroy());
27
- store._destroyers = undefined;
28
- };
29
- store.apply = (state = store.getState(), patches) => {
30
- if (!patches) {
31
- if (state === store.getState()) return;
32
- if (store.isSliceStore) {
33
- if (typeof state === 'object' && state !== null) {
34
- for (const key in state) {
35
- const _key = key;
36
- const _state = state[_key];
37
- if (typeof _state === 'object' && _state !== null) {
38
- Object.assign(store.getState()[_key], _state);
39
- }
40
- }
41
- }
42
- } else {
43
- Object.assign(store.getState(), state);
44
- }
45
- return;
46
- }
47
- mutability.apply(state, patches);
48
- };
49
- }
50
- const stopWatch = internal.toMutableRaw(state).$subscribe((...args) => {
51
- store._subscriptions.forEach(callback => callback(...args));
52
- });
53
- const destroy = () => {
54
- instancesMap.delete(state);
55
- stopWatch();
56
- };
57
- store._destroyers.add(destroy);
58
- };
59
-
60
- /**
61
- * Bind a store to Pinia
62
- */
63
- const bindPinia = coaction.createBinder({
64
- handleStore,
65
- handleState: options => {
66
- const descriptors = {};
67
- options.getters = options.getters ?? {};
68
- for (const key in options.getters) {
69
- descriptors[key] = {
70
- get() {
71
- return options.getters[key].call(this, this);
72
- }
73
- };
74
- }
75
- const rawState = Object.defineProperties({
76
- ...options.state?.(),
77
- ...options.actions
78
- }, descriptors);
79
- const pinia$1 = pinia.createPinia();
80
- pinia.setActivePinia(pinia$1);
81
- return {
82
- copyState: options,
83
- key: 'actions',
84
- bind: state => {
85
- rawState.name = state.$id;
86
- instancesMap.set(rawState, state);
87
- return rawState;
88
- }
89
- };
90
- }
91
- });
92
-
93
- /**
94
- * Adapt a store type to Pinia
95
- */
96
- const adapt = store => store;
97
-
98
- exports.adapt = adapt;
99
- exports.bindPinia = bindPinia;
100
- Object.keys(pinia).forEach(function (k) {
101
- if (k !== 'default' && !exports.hasOwnProperty(k)) Object.defineProperty(exports, k, {
102
- enumerable: true,
103
- get: function () { return pinia[k]; }
104
- });
105
- });
@@ -1 +0,0 @@
1
- export * from "./coaction-pinia.cjs.js";
@@ -1,95 +0,0 @@
1
- import { apply } from 'mutability';
2
- import { createBinder } from 'coaction';
3
- import { createPinia, setActivePinia } from 'pinia';
4
- export * from 'pinia';
5
-
6
- const instancesMap = new WeakMap();
7
- const handleStore = (store, state, _, internal) => {
8
- if (!internal.toMutableRaw) {
9
- internal.toMutableRaw = key => instancesMap.get(key);
10
- Object.assign(store, {
11
- subscribe: callback => {
12
- store._subscriptions.add(callback);
13
- return () => store._subscriptions.delete(callback);
14
- }
15
- });
16
- store._subscriptions = new Set();
17
- store._destroyers = new Set();
18
- const baseDestroy = store.destroy;
19
- store.destroy = () => {
20
- baseDestroy();
21
- store._subscriptions.clear();
22
- store._subscriptions = undefined;
23
- store._destroyers.forEach(destroy => destroy());
24
- store._destroyers = undefined;
25
- };
26
- store.apply = (state = store.getState(), patches) => {
27
- if (!patches) {
28
- if (state === store.getState()) return;
29
- if (store.isSliceStore) {
30
- if (typeof state === 'object' && state !== null) {
31
- for (const key in state) {
32
- const _key = key;
33
- const _state = state[_key];
34
- if (typeof _state === 'object' && _state !== null) {
35
- Object.assign(store.getState()[_key], _state);
36
- }
37
- }
38
- }
39
- } else {
40
- Object.assign(store.getState(), state);
41
- }
42
- return;
43
- }
44
- apply(state, patches);
45
- };
46
- }
47
- const stopWatch = internal.toMutableRaw(state).$subscribe((...args) => {
48
- store._subscriptions.forEach(callback => callback(...args));
49
- });
50
- const destroy = () => {
51
- instancesMap.delete(state);
52
- stopWatch();
53
- };
54
- store._destroyers.add(destroy);
55
- };
56
-
57
- /**
58
- * Bind a store to Pinia
59
- */
60
- const bindPinia = createBinder({
61
- handleStore,
62
- handleState: options => {
63
- const descriptors = {};
64
- options.getters = options.getters ?? {};
65
- for (const key in options.getters) {
66
- descriptors[key] = {
67
- get() {
68
- return options.getters[key].call(this, this);
69
- }
70
- };
71
- }
72
- const rawState = Object.defineProperties({
73
- ...options.state?.(),
74
- ...options.actions
75
- }, descriptors);
76
- const pinia = createPinia();
77
- setActivePinia(pinia);
78
- return {
79
- copyState: options,
80
- key: 'actions',
81
- bind: state => {
82
- rawState.name = state.$id;
83
- instancesMap.set(rawState, state);
84
- return rawState;
85
- }
86
- };
87
- }
88
- });
89
-
90
- /**
91
- * Adapt a store type to Pinia
92
- */
93
- const adapt = store => store;
94
-
95
- export { adapt, bindPinia };
@@ -1,2 +0,0 @@
1
- !function(e,t){"object"==typeof exports&&"undefined"!=typeof module?t(exports,require("mutative"),require("coaction"),require("pinia")):"function"==typeof define&&define.amd?define(["exports","mutative","coaction","pinia"],t):t((e="undefined"!=typeof globalThis?globalThis:e||self).CoactionPinia={},e.Mutative,e.Coaction,e.Pinia)}(this,(function(e,t,r,n){"use strict";const o="replace";function a(e){return Array.isArray(e)?1:e instanceof Map?2:e instanceof Set?3:0}function s(e,t){return 2===a(e)?e.get(t):e[t]}function i(e){if("object"!=typeof e||null===e)return e;if(Array.isArray(e))return e.map(i);if(e instanceof Map)return new Map(Array.from(e.entries()).map((([e,t])=>[e,i(t)])));if(e instanceof Set)return new Set(Array.from(e).map(i));const t=Object.create(Object.getPrototypeOf(e));for(const r in e)t[r]=i(e[r]);return t}const c=new WeakMap,u=r.createBinder({handleStore:(e,t,r,n)=>{if(!n.toMutableRaw){n.toMutableRaw=e=>c.get(e),Object.assign(e,{subscribe:t=>(e._subscriptions.add(t),()=>e._subscriptions.delete(t))}),e._subscriptions=new Set,e._destroyers=new Set;const t=e.destroy;e.destroy=()=>{t(),e._subscriptions.clear(),e._subscriptions=void 0,e._destroyers.forEach((e=>e())),e._destroyers=void 0},e.apply=(t=e.getState(),r)=>{if(r)!function(e,t){let r;for(r=t.length-1;r>=0;r-=1){const{value:n,op:a,path:s}=t[r];if(!s.length&&a===o||""===s&&"add"===a){e=n;break}}r>-1&&(t=t.slice(r+1)),t.forEach((t=>{const{path:r,op:n}=t,c=function(e){return Array.isArray(e)?e:e.split("/").map((e=>e.replace(/~1/g,"/").replace(/~0/g,"~"))).slice(1)}(r);let u=e;for(let e=0;e<c.length-1;e+=1){const t=a(u);let r=c[e];if("string"!=typeof r&&"number"!=typeof r&&(r=String(r)),(0===t||1===t)&&("__proto__"===r||"constructor"===r)||"function"==typeof u&&"prototype"===r)throw new Error("Patching reserved attributes like __proto__ and constructor is not allowed.");if(u=s(3===a(u)?Array.from(u):u,r),"object"!=typeof u)throw new Error(`Cannot apply patch at '${c.join("/")}'.`)}const f=a(u),p=i(t.value),l=c[c.length-1];switch(n){case o:switch(f){case 2:return u.set(l,p);case 3:throw new Error("Cannot apply replace patch to set.");default:return u[l]=p}case"add":switch(f){case 1:return"-"===l?u.push(p):u.splice(l,0,p);case 2:return u.set(l,p);case 3:return u.add(p);default:return u[l]=p}case"remove":switch(f){case 1:return u.splice(l,1);case 2:return u.delete(l);case 3:return u.delete(t.value);default:return delete u[l]}default:throw new Error(`Unsupported patch operation: ${n}.`)}}))}(t,r);else{if(t===e.getState())return;if(e.isSliceStore){if("object"==typeof t&&null!==t)for(const r in t){const n=r,o=t[n];"object"==typeof o&&null!==o&&Object.assign(e.getState()[n],o)}}else Object.assign(e.getState(),t)}}}const u=n.toMutableRaw(t).$subscribe(((...t)=>{e._subscriptions.forEach((e=>e(...t)))}));e._destroyers.add((()=>{c.delete(t),u()}))},handleState:e=>{const t={};e.getters=e.getters??{};for(const r in e.getters)t[r]={get(){return e.getters[r].call(this,this)}};const r=Object.defineProperties({...e.state?.(),...e.actions},t),o=n.createPinia();return n.setActivePinia(o),{copyState:e,key:"actions",bind:e=>(r.name=e.$id,c.set(r,e),r)}}});e.adapt=e=>e,e.bindPinia=u,Object.keys(n).forEach((function(t){"default"===t||e.hasOwnProperty(t)||Object.defineProperty(e,t,{enumerable:!0,get:function(){return n[t]}})})),Object.defineProperty(e,"__esModule",{value:!0})}));
2
- //# sourceMappingURL=coaction-pinia.umd.min.js.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"coaction-pinia.umd.min.js","sources":["../../../node_modules/mutability/dist/index.esm.js","../src/index.ts"],"sourcesContent":["import{create as e}from\"mutative\";const t=\"remove\",r=\"replace\",n=\"add\";function o(e){return Array.isArray(e)?1:e instanceof Map?2:e instanceof Set?3:0}function a(e,t){return 2===o(e)?e.get(t):e[t]}function c(e){if(\"object\"!=typeof e||null===e)return e;if(Array.isArray(e))return e.map(c);if(e instanceof Map)return new Map(Array.from(e.entries()).map((([e,t])=>[e,c(t)])));if(e instanceof Set)return new Set(Array.from(e).map(c));const t=Object.create(Object.getPrototypeOf(e));for(const r in e)t[r]=c(e[r]);return t}function s(e,s){let i;for(i=s.length-1;i>=0;i-=1){const{value:t,op:o,path:a}=s[i];if(!a.length&&o===r||\"\"===a&&o===n){e=t;break}}i>-1&&(s=s.slice(i+1)),s.forEach((s=>{const{path:i,op:p}=s,u=function(e){return Array.isArray(e)?e:e.split(\"/\").map((e=>e.replace(/~1/g,\"/\").replace(/~0/g,\"~\"))).slice(1)}(i);let f=e;for(let e=0;e<u.length-1;e+=1){const t=o(f);let r=u[e];if(\"string\"!=typeof r&&\"number\"!=typeof r&&(r=String(r)),(0===t||1===t)&&(\"__proto__\"===r||\"constructor\"===r)||\"function\"==typeof f&&\"prototype\"===r)throw new Error(\"Patching reserved attributes like __proto__ and constructor is not allowed.\");if(f=a(3===o(f)?Array.from(f):f,r),\"object\"!=typeof f)throw new Error(`Cannot apply patch at '${u.join(\"/\")}'.`)}const l=o(f),h=c(s.value),y=u[u.length-1];switch(p){case r:switch(l){case 2:return f.set(y,h);case 3:throw new Error(\"Cannot apply replace patch to set.\");default:return f[y]=h}case n:switch(l){case 1:return\"-\"===y?f.push(h):f.splice(y,0,h);case 2:return f.set(y,h);case 3:return f.add(h);default:return f[y]=h}case t:switch(l){case 1:return f.splice(y,1);case 2:return f.delete(y);case 3:return f.delete(s.value);default:return delete f[y]}default:throw new Error(`Unsupported patch operation: ${p}.`)}}))}const i=(t,r)=>{const[,n,o]=e(t,r,{enablePatches:!0});return s(t,n),{inversePatches:o,patches:n}};export{s as apply,i as mutate};\n//# sourceMappingURL=index.esm.js.map\n","import { apply } from 'mutability';\nimport { createBinder, type Store } from 'coaction';\nimport { createPinia, setActivePinia } from 'pinia';\nimport type {\n _GettersTree,\n DefineStoreOptions,\n StateTree,\n StoreDefinition\n} from 'pinia';\n\nexport * from 'pinia';\n\nconst instancesMap = new WeakMap<object, unknown>();\n\ntype StoreWithSubscriptions = Store<object> & {\n // TODO: fix type\n _subscriptions?: Set<(...args: any) => void>;\n _destroyers?: Set<() => void>;\n};\n\ntype FunctionKeys<T> = {\n [K in keyof T]: T[K] extends (...args: any[]) => any ? K : never;\n}[keyof T];\n\ntype Equal<X, Y> =\n (<T>() => T extends X ? 1 : 2) extends <T>() => T extends Y ? 1 : 2\n ? true\n : false;\n\ntype ReadonlyKeys<T> = {\n [K in keyof T]: Equal<\n { [P in K]: T[P] },\n Readonly<{ [P in K]: T[P] }>\n > extends true\n ? K\n : never;\n}[keyof T];\n\nexport type IStore<T extends object> = [\n string,\n Pick<T, Exclude<keyof T, ReadonlyKeys<T> | FunctionKeys<T>>>,\n {\n [K in ReadonlyKeys<T>]: (\n state: Pick<T, Exclude<keyof T, ReadonlyKeys<T> | FunctionKeys<T>>>\n ) => T[K];\n },\n Pick<T, FunctionKeys<T>>\n];\n\nconst handleStore = (\n store: StoreWithSubscriptions,\n state: object,\n _: object,\n internal: any\n) => {\n if (!internal.toMutableRaw) {\n internal.toMutableRaw = (key: any) => instancesMap.get(key);\n Object.assign(store, {\n subscribe: (callback: any) => {\n store._subscriptions!.add(callback);\n return () => store._subscriptions!.delete(callback);\n }\n });\n store._subscriptions = new Set<() => void>();\n store._destroyers = new Set<() => void>();\n const baseDestroy = store.destroy;\n store.destroy = () => {\n baseDestroy();\n store._subscriptions!.clear();\n store._subscriptions = undefined;\n store._destroyers!.forEach((destroy) => destroy());\n store._destroyers = undefined;\n };\n store.apply = (state = store.getState(), patches) => {\n if (!patches) {\n if (state === store.getState()) return;\n if (store.isSliceStore) {\n if (typeof state === 'object' && state !== null) {\n for (const key in state) {\n const _key = key as keyof typeof state;\n const _state = state[_key];\n if (typeof _state === 'object' && _state !== null) {\n Object.assign(store.getState()[_key], _state);\n }\n }\n }\n } else {\n Object.assign(store.getState(), state);\n }\n return;\n }\n apply(state, patches);\n };\n }\n const stopWatch = internal\n .toMutableRaw(state)\n .$subscribe((...args: unknown[]) => {\n store._subscriptions!.forEach((callback) => callback(...args));\n });\n const destroy = () => {\n instancesMap.delete(state);\n stopWatch();\n };\n store._destroyers!.add(destroy);\n};\n\n/**\n * Bind a store to Pinia\n */\nexport const bindPinia = createBinder({\n handleStore,\n handleState: ((options: DefineStoreOptions<any, any, any, any>) => {\n const descriptors: Record<string, PropertyDescriptor> = {};\n options.getters = options.getters ?? {};\n for (const key in options.getters) {\n descriptors[key] = {\n get() {\n return options.getters[key].call(this, this);\n }\n };\n }\n const rawState = Object.defineProperties(\n {\n ...options.state?.(),\n ...options.actions\n },\n descriptors\n );\n const pinia = createPinia();\n setActivePinia(pinia);\n return {\n copyState: options as any,\n key: 'actions',\n bind: (state: any) => {\n rawState.name = state.$id;\n instancesMap.set(rawState, state);\n return rawState;\n }\n };\n }) as any\n}) as <\n Id extends string,\n S extends StateTree = {},\n G extends _GettersTree<S> = {},\n A = {}\n>(\n options: Omit<DefineStoreOptions<Id, S, G, A>, 'id'>\n) => Omit<DefineStoreOptions<Id, S, G, A>, 'id'>;\n\n/**\n * Adapt a store type to Pinia\n */\nexport const adapt = <T extends object>(\n store: StoreDefinition<IStore<T>[0], IStore<T>[1], IStore<T>[2], IStore<T>[3]>\n) => store as any as T;\n\nexport type PiniaStore<T extends object> = StoreDefinition<\n IStore<T>[0],\n IStore<T>[1],\n IStore<T>[2],\n IStore<T>[3]\n>;\n"],"names":["r","o","e","Array","isArray","Map","Set","a","t","get","c","map","from","entries","Object","create","getPrototypeOf","instancesMap","WeakMap","bindPinia","createBinder","handleStore","store","state","_","internal","toMutableRaw","key","assign","subscribe","callback","_subscriptions","add","delete","_destroyers","baseDestroy","destroy","clear","undefined","forEach","apply","getState","patches","s","i","length","value","op","path","slice","p","u","split","replace","f","String","Error","join","l","h","y","set","push","splice","isSliceStore","_key","_state","stopWatch","$subscribe","args","handleState","options","descriptors","getters","call","this","rawState","defineProperties","actions","pinia","createPinia","setActivePinia","copyState","bind","name","$id"],"mappings":"gXAAkC,MAAiBA,EAAE,UAAkB,SAASC,EAAEC,GAAG,OAAOC,MAAMC,QAAQF,GAAG,EAAEA,aAAaG,IAAI,EAAEH,aAAaI,IAAI,EAAE,CAAC,CAAC,SAASC,EAAEL,EAAEM,GAAG,OAAO,IAAIP,EAAEC,GAAGA,EAAEO,IAAID,GAAGN,EAAEM,EAAE,CAAC,SAASE,EAAER,GA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