pinia-react 1.0.0 → 1.1.1

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) 2025 karl
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
Binary file
@@ -0,0 +1,64 @@
1
+ //#region src/types.d.ts
2
+
3
+ type StateTree = Record<string | number | symbol, unknown>;
4
+ type _StoreWithGetters<G> = { readonly [k in keyof G]: G[k] extends ((...args: any[]) => infer R) ? R : G[k] };
5
+ type _ActionsTree = Record<string | number | symbol, (...args: any[]) => any>;
6
+ type PiniaCustomStateProperties<S extends StateTree = StateTree> = {};
7
+ type _GettersTree<S extends StateTree> = Record<string, (state: S & PiniaCustomStateProperties<S>) => any>;
8
+ /**
9
+ * Interface to be extended by the user when they add properties through plugins.
10
+ */
11
+ type PiniaCustomProperties<Id extends string = string, S extends StateTree = StateTree, G = _GettersTree<S>, A = _ActionsTree> = {};
12
+ type _DeepPartial<T> = { [K in keyof T]?: _DeepPartial<T[K]> };
13
+ interface _StoreWithState<Id extends string, S extends StateTree, G, A> {
14
+ $id: Id;
15
+ $state: S & PiniaCustomStateProperties<S>;
16
+ $patch(partialState: _DeepPartial<S>): void;
17
+ $patch<F extends (state: S) => any>(stateMutator: ReturnType<F> extends Promise<any> ? never : F): void;
18
+ $reset(): void;
19
+ $subscribe(callback: (newValue: S) => any, options?: {
20
+ detached: boolean;
21
+ }): any;
22
+ }
23
+ type Store<Id extends string, S extends StateTree, G, A> = _StoreWithState<Id, S, G, A> & S & _StoreWithGetters<G> & (_ActionsTree extends A ? {} : A) & PiniaCustomProperties<Id, S, G, A> & PiniaCustomStateProperties<S>;
24
+ type StoreGeneric = Store<string, StateTree, _GettersTree<StateTree>, _ActionsTree>;
25
+ type DefineStoreOptionsBase<S extends StateTree, Store> = {};
26
+ interface DefineStoreOptions<Id extends string, S extends StateTree, G, A> extends DefineStoreOptionsBase<S, Store<Id, S, G, A>> {
27
+ state?: () => S;
28
+ getters?: G & ThisType<S & _StoreWithGetters<G> & PiniaCustomProperties>;
29
+ actions?: A & ThisType<A & S & _StoreWithState<Id, S, G, A> & _StoreWithGetters<G> & PiniaCustomProperties>;
30
+ }
31
+ /**
32
+ * Return type of `defineStore()`. Function that allows instantiating a store.
33
+ */
34
+ interface StoreDefinition<Id extends string = string, S extends StateTree = StateTree, G = _GettersTree<S>, A = _ActionsTree> {
35
+ /**
36
+ * Returns a store, creates it if necessary.
37
+ */
38
+ (): Store<Id, S, G, A>;
39
+ /**
40
+ * Id of the store. Used by map helpers.
41
+ */
42
+ $id: Id;
43
+ }
44
+ type PiniaPluginContext<Id extends string = string, S extends StateTree = StateTree, G = _GettersTree<S>, A = _ActionsTree> = {
45
+ options: DefineStoreOptions<Id, S, G, A>;
46
+ store: Store<Id, S, G, A>;
47
+ };
48
+ type PiniaPlugin = (context: PiniaPluginContext) => Partial<PiniaCustomProperties & PiniaCustomStateProperties> | undefined;
49
+ //#endregion
50
+ //#region src/defineStore.d.ts
51
+ declare function defineStore<Id extends string, S extends StateTree, G extends _GettersTree<S> = {}, A extends _ActionsTree = {}>(id: Id, options: DefineStoreOptions<Id, S, G, A>): StoreDefinition<Id, S, G, A>;
52
+ //#endregion
53
+ //#region src/pinia.d.ts
54
+ interface Pinia {
55
+ _store: Map<string, StoreGeneric>;
56
+ _state: Map<string, StateTree>;
57
+ _plugins: Set<PiniaPlugin>;
58
+ use(plugin: PiniaPlugin): this;
59
+ }
60
+ declare let pinia: Pinia;
61
+ declare function createPinia(): Pinia;
62
+ declare function setActivePinia(_pinia: Pinia): void;
63
+ //#endregion
64
+ export { type DefineStoreOptionsBase, type PiniaCustomProperties, type PiniaCustomStateProperties, type PiniaPlugin, type PiniaPluginContext, type StateTree, type Store, createPinia, defineStore, pinia, setActivePinia };
package/dist/index.js ADDED
@@ -0,0 +1,157 @@
1
+ import { ReactiveEffect, activeEffect, computed, isReactive, isRef, markRaw, reactive, toRefs, watch } from "@maoism/runtime-core";
2
+ import React, { useCallback, useId, useRef } from "react";
3
+ import { isFunction } from "savage-types";
4
+ import "savage-utils";
5
+
6
+ //#region src/pinia.ts
7
+ let pinia;
8
+ function createPinia() {
9
+ return {
10
+ _store: /* @__PURE__ */ new Map(),
11
+ _state: /* @__PURE__ */ new Map(),
12
+ _plugins: /* @__PURE__ */ new Set(),
13
+ use(p) {
14
+ this._plugins.add(p);
15
+ return this;
16
+ }
17
+ };
18
+ }
19
+ function setActivePinia(_pinia) {
20
+ pinia = _pinia;
21
+ }
22
+ setActivePinia(createPinia());
23
+
24
+ //#endregion
25
+ //#region src/utils.ts
26
+ function noop() {
27
+ return {};
28
+ }
29
+ function isPlainObject(o) {
30
+ return o && typeof o === "object" && Object.prototype.toString.call(o) === "[object Object]" && typeof o.toJSON !== "function";
31
+ }
32
+ function mergeReactiveObjects(target, patchToApply) {
33
+ if (target instanceof Map && patchToApply instanceof Map) patchToApply.forEach((value, key) => target.set(key, value));
34
+ if (target instanceof Set && patchToApply instanceof Set) patchToApply.forEach(target.add, target);
35
+ for (const key in patchToApply) {
36
+ if (!Object.hasOwn(patchToApply, key)) continue;
37
+ const subPatch = patchToApply[key];
38
+ const targetValue = target[key];
39
+ if (isPlainObject(targetValue) && isPlainObject(subPatch) && target.hasOwnProperty(key) && !isRef(subPatch) && !isReactive(subPatch)) target[key] = mergeReactiveObjects(targetValue, subPatch);
40
+ else target[key] = subPatch;
41
+ }
42
+ return target;
43
+ }
44
+
45
+ //#endregion
46
+ //#region src/subscription.ts
47
+ const subscriptions = /* @__PURE__ */ new Set();
48
+ function addSubscriptions(callback, onCleanup = noop) {
49
+ subscriptions.add(callback);
50
+ const remove = () => {
51
+ subscriptions.delete(callback);
52
+ onCleanup();
53
+ };
54
+ return remove;
55
+ }
56
+ function triggerSubscription(state) {
57
+ subscriptions.forEach((callback) => callback(state));
58
+ }
59
+
60
+ //#endregion
61
+ //#region src/defineStore.ts
62
+ let isLoadingPlugin = false;
63
+ function defineStore(id, options) {
64
+ let isSyncListening = false;
65
+ function createStore() {
66
+ const { state, actions, getters } = options;
67
+ const $state = reactive(state ? state() : {});
68
+ const initState = state ? state() : {};
69
+ const baseStore = {
70
+ $id: id,
71
+ $state,
72
+ $patch(val) {
73
+ isSyncListening = false;
74
+ if (isFunction(val)) val($state);
75
+ else mergeReactiveObjects($state, val);
76
+ isSyncListening = true;
77
+ triggerSubscription($state);
78
+ },
79
+ $reset() {
80
+ this.$patch((v) => {
81
+ Object.assign(v, initState);
82
+ });
83
+ },
84
+ $subscribe(cb) {
85
+ const remove = addSubscriptions(cb, () => unwatch());
86
+ const unwatch = watch($state, (state$1) => {
87
+ if (isSyncListening) cb(state$1);
88
+ }, {
89
+ deep: true,
90
+ flush: "sync"
91
+ });
92
+ return remove;
93
+ }
94
+ };
95
+ pinia._state.set(id, $state);
96
+ const store = reactive(Object.assign(baseStore, toRefs($state), Object.keys(actions ?? []).reduce((x, y) => {
97
+ const key = y;
98
+ return Object.assign(x, { [key]: function(...args) {
99
+ return actions[key].call(this, ...args);
100
+ } });
101
+ }, {}), Object.keys(getters || {}).reduce((computedGetters, name) => {
102
+ computedGetters[name] = markRaw(computed(() => {
103
+ return getters?.[name].call(store, store);
104
+ }));
105
+ return computedGetters;
106
+ }, {})));
107
+ const lastLoadingPlugin = isLoadingPlugin;
108
+ isLoadingPlugin = true;
109
+ pinia._plugins.forEach((p) => {
110
+ Object.assign(store, p({
111
+ store,
112
+ options
113
+ }) || {});
114
+ });
115
+ isLoadingPlugin = lastLoadingPlugin;
116
+ pinia._store.set(id, store);
117
+ }
118
+ const effectMap = /* @__PURE__ */ new WeakMap();
119
+ const subscribeMap = /* @__PURE__ */ new WeakMap();
120
+ function useStore() {
121
+ if (!pinia._store.has(id)) createStore();
122
+ const store = pinia._store.get(id);
123
+ isSyncListening = true;
124
+ const _id = useRef([useId()]);
125
+ const storeSnapshotRef = React.useRef({ ...store });
126
+ const subscribe = useCallback((onStoreChange) => {
127
+ subscribeMap.set(_id.current, onStoreChange);
128
+ return () => {
129
+ const effect$1 = effectMap.get(_id.current);
130
+ if (effect$1) effect$1.stop();
131
+ subscribeMap.delete(_id.current);
132
+ effectMap.delete(_id.current);
133
+ };
134
+ }, []);
135
+ React.useSyncExternalStore(subscribe, () => storeSnapshotRef.current, () => ({ ...store }));
136
+ let effect = effectMap.get(_id.current);
137
+ if (!effect) {
138
+ const fn = () => {
139
+ const onStoreChange = subscribeMap.get(_id.current);
140
+ onStoreChange?.();
141
+ };
142
+ effect = new ReactiveEffect(fn, noop, () => {
143
+ storeSnapshotRef.current = { ...store };
144
+ if (effect?.dirty) effect.run();
145
+ });
146
+ activeEffect.value = effect;
147
+ effect.run();
148
+ effectMap.set(_id.current, effect);
149
+ }
150
+ return store;
151
+ }
152
+ useStore.$id = id;
153
+ return useStore;
154
+ }
155
+
156
+ //#endregion
157
+ export { createPinia, defineStore, pinia, setActivePinia };
package/package.json CHANGED
@@ -1,12 +1,75 @@
1
1
  {
2
2
  "name": "pinia-react",
3
- "version": "1.0.0",
4
- "main": "index.js",
3
+ "version": "1.1.1",
4
+ "type": "module",
5
+ "homepage": "https://github.com/savageKarl/pinia-react#readme",
6
+ "bugs": {
7
+ "url": "https://github.com/savageKarl/pinia-react/issues"
8
+ },
9
+ "repository": {
10
+ "type": "git",
11
+ "url": "git+https://github.com/savageKarl/pinia-react.git"
12
+ },
13
+ "author": "savageKarl <shan.revolt@gmail.com>",
14
+ "files": [
15
+ "dist"
16
+ ],
17
+ "main": "./dist/index.js",
18
+ "module": "./dist/index.js",
19
+ "types": "./dist/index.d.ts",
20
+ "exports": {
21
+ ".": "./dist/index.js",
22
+ "./package.json": "./package.json"
23
+ },
24
+ "publishConfig": {
25
+ "access": "public"
26
+ },
5
27
  "scripts": {
6
- "test": "echo \"Error: no test specified\" && exit 1"
28
+ "prepare": "npx simple-git-hooks",
29
+ "build": "tsdown",
30
+ "dev": "tsdown --watch",
31
+ "playground": "vite --config playground/vite.config.ts",
32
+ "test": "vitest",
33
+ "semantic-release": "semantic-release"
34
+ },
35
+ "keywords": [
36
+ "pinia",
37
+ "react-pinia",
38
+ "react-store"
39
+ ],
40
+ "license": "MIT",
41
+ "description": "",
42
+ "simple-git-hooks": {
43
+ "commit-msg": "npx --no-install commitlint --edit \"$1\""
44
+ },
45
+ "peerDependencies": {
46
+ "react": "^18.0.0 || ^19.0.0",
47
+ "react-dom": "^18.0.0 || ^19.0.0"
48
+ },
49
+ "devDependencies": {
50
+ "@biomejs/biome": "2.1.3",
51
+ "@commitlint/cli": "^19.8.1",
52
+ "@commitlint/config-conventional": "^19.8.1",
53
+ "@semantic-release/changelog": "^6.0.3",
54
+ "@semantic-release/git": "^10.0.1",
55
+ "@testing-library/jest-dom": "^6.6.4",
56
+ "@testing-library/react": "^16.3.0",
57
+ "@testing-library/user-event": "^14.6.1",
58
+ "@types/node": "^22.15.17",
59
+ "@types/react": "^19.1.3",
60
+ "@types/react-dom": "^19.1.4",
61
+ "@vitejs/plugin-react": "^5.0.0",
62
+ "jsdom": "^26.1.0",
63
+ "semantic-release": "^24.2.7",
64
+ "simple-git-hooks": "^2.13.1",
65
+ "tsdown": "^0.13.3",
66
+ "typescript": "^5.8.3",
67
+ "vite": "npm:rolldown-vite@latest",
68
+ "vitest": "^3.2.4"
7
69
  },
8
- "keywords": [],
9
- "author": "",
10
- "license": "ISC",
11
- "description": ""
70
+ "dependencies": {
71
+ "@maoism/runtime-core": "^3.4.13",
72
+ "savage-types": "^1.0.20",
73
+ "savage-utils": "^1.0.65"
74
+ }
12
75
  }