beestate 0.1.6 → 0.1.7

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.
@@ -1,8 +1,12 @@
1
1
  import React from 'react';
2
2
  import type { InstanceKey } from './types/InstanceKey.tsx';
3
- import type { Store } from './types/Store.tsx';
3
+ import { INTERNAL_STORE_PROPS_ACCESSOR } from './constants.tsx';
4
4
  export type StoreProviderProps = React.PropsWithChildren<{
5
- store: Store;
5
+ store: {
6
+ [INTERNAL_STORE_PROPS_ACCESSOR]: {
7
+ context: React.Context<null | InstanceKey>;
8
+ };
9
+ };
6
10
  instanceKey: InstanceKey;
7
11
  }>;
8
12
  export declare function StoreProvider(props: StoreProviderProps): import("react/jsx-runtime").JSX.Element;
@@ -3,8 +3,8 @@ import type { InstanceKey } from './InstanceKey.tsx';
3
3
  import type { ReducerMap } from './ReducerMap.tsx';
4
4
  import type { StoreState } from './StoreState.tsx';
5
5
  export type InternalStoreProps<TStoreName extends string = string, TStoreState extends StoreState = StoreState, TReducerMap extends ReducerMap<TStoreState> = ReducerMap<TStoreState>> = {
6
- [`$$init`]: TReducerMap[`$$init`];
7
- [`$$reset`]: TReducerMap[`$$reset`];
6
+ $$init: TReducerMap[`$$init`];
7
+ $$reset: TReducerMap[`$$reset`];
8
8
  context: React.Context<null | InstanceKey>;
9
9
  initialState: TStoreState;
10
10
  name: TStoreName;
@@ -1,6 +1,6 @@
1
1
  import type { Reducer } from './Reducer.tsx';
2
2
  import type { StoreState } from './StoreState.tsx';
3
3
  export type ReducerMap<TStoreState extends StoreState = StoreState> = Record<string, Reducer<TStoreState, void | any>> & {
4
- [`$$init`]: Reducer<TStoreState, void | any>;
5
- [`$$reset`]: Reducer<TStoreState, void | any>;
4
+ $$init: Reducer<TStoreState, void | any>;
5
+ $$reset: Reducer<TStoreState, void | any>;
6
6
  };
@@ -6,9 +6,8 @@ import type { StateLink } from './StateLink.tsx';
6
6
  import type { StoreState } from './StoreState.tsx';
7
7
  export type Store<TStoreName extends string = string, TStoreState extends StoreState = StoreState, TReducerMap extends ReducerMap<TStoreState> = ReducerMap<TStoreState>> = {
8
8
  [INTERNAL_STORE_PROPS_ACCESSOR]: InternalStoreProps<TStoreName, TStoreState, TReducerMap>;
9
- } & {
10
- [`$$init`]: void;
11
- [`$$reset`]: void;
9
+ $$init: void;
10
+ $$reset: void;
12
11
  } & {
13
12
  [TKey in keyof TStoreState]: TKey extends string ? StateLink<TStoreName, TStoreState, TReducerMap, TKey> : never;
14
13
  } & {
@@ -215,10 +215,18 @@ function isEqual(prevStateAll, stateAll) {
215
215
 
216
216
  function createUseStore(storeRegistry) {
217
217
  function useStore(store, ...args) {
218
+ const storeRef = React.useRef({ store });
219
+ const storeMismatchWarnedRef = React.useRef({ warned: false });
220
+ if (storeRef.current.store !== store) {
221
+ console.warn(`Store reference mismatch. You must pass the same store instance on every render.`);
222
+ storeMismatchWarnedRef.current.warned = true;
223
+ }
218
224
  let instanceKeys;
219
225
  let registerInstance;
226
+ const internalStoreProps = storeRef.current.store[INTERNAL_STORE_PROPS_ACCESSOR];
227
+ const contextInstanceKey = React.useContext(internalStoreProps.context);
220
228
  if (typeof args[0] === `function`) {
221
- instanceKeys = [DEFAULT_INSTANCE_KEY];
229
+ instanceKeys = [contextInstanceKey ?? DEFAULT_INSTANCE_KEY];
222
230
  registerInstance = args[0];
223
231
  }
224
232
  else {
@@ -227,22 +235,14 @@ function createUseStore(storeRegistry) {
227
235
  }
228
236
  // eslint-disable-next-line react-hooks/exhaustive-deps
229
237
  const actualInstanceKeys = React.useMemo(() => instanceKeys, instanceKeys);
230
- const storeRef = React.useRef({ store });
231
238
  const ownerKeyRef = React.useRef({ ownerKey: Symbol(`ownerKey`) });
232
239
  const initInstancesRef = React.useRef({ initInstances: {} });
233
- const storeMismatchWarnedRef = React.useRef({ warned: false });
234
- if (storeRef.current.store !== store) {
235
- console.warn(`Store reference mismatch. You must pass the same store instance on every render.`);
236
- storeMismatchWarnedRef.current.warned = true;
237
- }
238
240
  React.useEffect(() => {
239
- const { store } = storeRef.current;
240
241
  const { ownerKey } = ownerKeyRef.current;
241
242
  const { initInstances } = initInstancesRef.current;
242
243
  for (const instanceKey of actualInstanceKeys) {
243
244
  if (!initInstances[instanceKey]) {
244
245
  const initInstance = (initPayload) => {
245
- const internalStoreProps = store[INTERNAL_STORE_PROPS_ACCESSOR];
246
246
  const storeController = createStoreController(storeRegistry, internalStoreProps);
247
247
  if (!storeController.instances[instanceKey]) {
248
248
  // TODO Убрать в логгер
@@ -292,7 +292,7 @@ function createUseStore(storeRegistry) {
292
292
  }
293
293
  },
294
294
  // eslint-disable-next-line react-hooks/exhaustive-deps
295
- [actualInstanceKeys, /*registerInstance*/]);
295
+ [actualInstanceKeys, internalStoreProps, /*registerInstance*/]);
296
296
  React.useEffect(() => {
297
297
  const initInstances = initInstancesRef.current.initInstances;
298
298
  return () => {
@@ -213,10 +213,18 @@ function isEqual(prevStateAll, stateAll) {
213
213
 
214
214
  function createUseStore(storeRegistry) {
215
215
  function useStore(store, ...args) {
216
+ const storeRef = useRef({ store });
217
+ const storeMismatchWarnedRef = useRef({ warned: false });
218
+ if (storeRef.current.store !== store) {
219
+ console.warn(`Store reference mismatch. You must pass the same store instance on every render.`);
220
+ storeMismatchWarnedRef.current.warned = true;
221
+ }
216
222
  let instanceKeys;
217
223
  let registerInstance;
224
+ const internalStoreProps = storeRef.current.store[INTERNAL_STORE_PROPS_ACCESSOR];
225
+ const contextInstanceKey = useContext(internalStoreProps.context);
218
226
  if (typeof args[0] === `function`) {
219
- instanceKeys = [DEFAULT_INSTANCE_KEY];
227
+ instanceKeys = [contextInstanceKey ?? DEFAULT_INSTANCE_KEY];
220
228
  registerInstance = args[0];
221
229
  }
222
230
  else {
@@ -225,22 +233,14 @@ function createUseStore(storeRegistry) {
225
233
  }
226
234
  // eslint-disable-next-line react-hooks/exhaustive-deps
227
235
  const actualInstanceKeys = useMemo(() => instanceKeys, instanceKeys);
228
- const storeRef = useRef({ store });
229
236
  const ownerKeyRef = useRef({ ownerKey: Symbol(`ownerKey`) });
230
237
  const initInstancesRef = useRef({ initInstances: {} });
231
- const storeMismatchWarnedRef = useRef({ warned: false });
232
- if (storeRef.current.store !== store) {
233
- console.warn(`Store reference mismatch. You must pass the same store instance on every render.`);
234
- storeMismatchWarnedRef.current.warned = true;
235
- }
236
238
  useEffect(() => {
237
- const { store } = storeRef.current;
238
239
  const { ownerKey } = ownerKeyRef.current;
239
240
  const { initInstances } = initInstancesRef.current;
240
241
  for (const instanceKey of actualInstanceKeys) {
241
242
  if (!initInstances[instanceKey]) {
242
243
  const initInstance = (initPayload) => {
243
- const internalStoreProps = store[INTERNAL_STORE_PROPS_ACCESSOR];
244
244
  const storeController = createStoreController(storeRegistry, internalStoreProps);
245
245
  if (!storeController.instances[instanceKey]) {
246
246
  // TODO Убрать в логгер
@@ -290,7 +290,7 @@ function createUseStore(storeRegistry) {
290
290
  }
291
291
  },
292
292
  // eslint-disable-next-line react-hooks/exhaustive-deps
293
- [actualInstanceKeys, /*registerInstance*/]);
293
+ [actualInstanceKeys, internalStoreProps, /*registerInstance*/]);
294
294
  useEffect(() => {
295
295
  const initInstances = initInstancesRef.current.initInstances;
296
296
  return () => {
package/package.json CHANGED
@@ -20,5 +20,5 @@
20
20
  "prepublishOnly": "npm run build"
21
21
  },
22
22
  "types": "dist/@types",
23
- "version": "0.1.6"
23
+ "version": "0.1.7"
24
24
  }