pinia-react 1.1.1 → 1.2.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/dist/index.d.ts CHANGED
@@ -40,6 +40,10 @@ interface StoreDefinition<Id extends string = string, S extends StateTree = Stat
40
40
  * Id of the store. Used by map helpers.
41
41
  */
42
42
  $id: Id;
43
+ /**
44
+ * Return to store for use within non-functional components
45
+ */
46
+ $getStore: () => Store<Id, S, G, A>;
43
47
  }
44
48
  type PiniaPluginContext<Id extends string = string, S extends StateTree = StateTree, G = _GettersTree<S>, A = _ActionsTree> = {
45
49
  options: DefineStoreOptions<Id, S, G, A>;
package/dist/index.js CHANGED
@@ -1,5 +1,5 @@
1
1
  import { ReactiveEffect, activeEffect, computed, isReactive, isRef, markRaw, reactive, toRefs, watch } from "@maoism/runtime-core";
2
- import React, { useCallback, useId, useRef } from "react";
2
+ import { useCallback, useId, useRef, useSyncExternalStore } from "react";
3
3
  import { isFunction } from "savage-types";
4
4
  import "savage-utils";
5
5
 
@@ -93,12 +93,9 @@ function defineStore(id, options) {
93
93
  }
94
94
  };
95
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) => {
96
+ const store = reactive(Object.assign(baseStore, toRefs($state), Object.keys(actions ?? []).reduce((x, y) => Object.assign(x, { [y]: function(...args) {
97
+ return actions[y].call(store, ...args);
98
+ } }), {}), Object.keys(getters || {}).reduce((computedGetters, name) => {
102
99
  computedGetters[name] = markRaw(computed(() => {
103
100
  return getters?.[name].call(store, store);
104
101
  }));
@@ -122,7 +119,8 @@ function defineStore(id, options) {
122
119
  const store = pinia._store.get(id);
123
120
  isSyncListening = true;
124
121
  const _id = useRef([useId()]);
125
- const storeSnapshotRef = React.useRef({ ...store });
122
+ const storeSnapshotRef = useRef({ ...store });
123
+ const isCollectDep = useRef(false);
126
124
  const subscribe = useCallback((onStoreChange) => {
127
125
  subscribeMap.set(_id.current, onStoreChange);
128
126
  return () => {
@@ -132,24 +130,33 @@ function defineStore(id, options) {
132
130
  effectMap.delete(_id.current);
133
131
  };
134
132
  }, []);
135
- React.useSyncExternalStore(subscribe, () => storeSnapshotRef.current, () => ({ ...store }));
133
+ useSyncExternalStore(subscribe, () => storeSnapshotRef.current, () => ({ ...store }));
136
134
  let effect = effectMap.get(_id.current);
137
135
  if (!effect) {
138
136
  const fn = () => {
139
137
  const onStoreChange = subscribeMap.get(_id.current);
140
- onStoreChange?.();
138
+ if (!isCollectDep.current) {
139
+ storeSnapshotRef.current = { ...store };
140
+ onStoreChange?.();
141
+ }
141
142
  };
142
143
  effect = new ReactiveEffect(fn, noop, () => {
143
- storeSnapshotRef.current = { ...store };
144
144
  if (effect?.dirty) effect.run();
145
145
  });
146
146
  activeEffect.value = effect;
147
+ isCollectDep.current = true;
147
148
  effect.run();
148
149
  effectMap.set(_id.current, effect);
150
+ isCollectDep.current = false;
149
151
  }
150
152
  return store;
151
153
  }
152
154
  useStore.$id = id;
155
+ useStore.$getStore = () => {
156
+ if (!pinia._store.has(id)) createStore();
157
+ const store = pinia._store.get(id);
158
+ return store;
159
+ };
153
160
  return useStore;
154
161
  }
155
162
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "pinia-react",
3
- "version": "1.1.1",
3
+ "version": "1.2.0",
4
4
  "type": "module",
5
5
  "homepage": "https://github.com/savageKarl/pinia-react#readme",
6
6
  "bugs": {
@@ -30,7 +30,8 @@
30
30
  "dev": "tsdown --watch",
31
31
  "playground": "vite --config playground/vite.config.ts",
32
32
  "test": "vitest",
33
- "semantic-release": "semantic-release"
33
+ "semantic-release": "semantic-release",
34
+ "format": "pnpm exec biome check --write"
34
35
  },
35
36
  "keywords": [
36
37
  "pinia",