beestate 0.1.4 → 0.1.6
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 +1 -0
- package/dist/@types/InternalStore.d.ts +15 -54
- package/dist/@types/StoreProvider.d.ts +2 -2
- package/dist/@types/createStore.d.ts +5 -6
- package/dist/@types/createStoreController.d.ts +6 -0
- package/dist/@types/createStoreRegistry.d.ts +8 -5
- package/dist/@types/createUseDispatcher.d.ts +7 -7
- package/dist/@types/createUseLatest.d.ts +11 -0
- package/dist/@types/createUseProp.d.ts +3 -6
- package/dist/@types/createUsePropAll.d.ts +3 -6
- package/dist/@types/createUseStore.d.ts +11 -11
- package/dist/@types/index.d.ts +12 -4
- package/dist/@types/pipe/Error.d.ts +6 -0
- package/dist/@types/pipe/check.d.ts +37 -0
- package/dist/@types/pipe/createPipeKit.d.ts +24 -0
- package/dist/@types/pipe/debug/createDebugger.d.ts +2 -0
- package/dist/@types/pipe/debug/debugInstruction.d.ts +1 -0
- package/dist/@types/pipe/debug/extendedDebugInstruction.d.ts +5 -0
- package/dist/@types/pipe/debug/index.d.ts +4 -0
- package/dist/@types/pipe/debug/log.d.ts +2 -0
- package/dist/@types/pipe/deepCopy.env-detect.dev.d.ts +1 -0
- package/dist/@types/pipe/deepCopy.env-detect.main.d.ts +4 -0
- package/dist/@types/pipe/entities/Adjunct.d.ts +3 -0
- package/dist/@types/pipe/entities/Connect.d.ts +9 -0
- package/dist/@types/pipe/entities/DataBarrel.d.ts +24 -0
- package/dist/@types/pipe/entities/Fill.d.ts +25 -0
- package/dist/@types/pipe/entities/Instruction.d.ts +81 -0
- package/dist/@types/pipe/entities/ParentPipe.d.ts +9 -0
- package/dist/@types/pipe/entities/Pipe.d.ts +57 -0
- package/dist/@types/pipe/entities/Stream.d.ts +13 -0
- package/dist/@types/pipe/entities/StreamGroup.d.ts +25 -0
- package/dist/@types/pipe/entities/index.d.ts +9 -0
- package/dist/@types/pipe/index.core.d.ts +26 -0
- package/dist/@types/pipe/index.d.ts +4 -0
- package/dist/@types/pipe/index.debug.d.ts +14 -0
- package/dist/@types/pipe/instruction.d.ts +9 -0
- package/dist/@types/requestState.d.ts +19 -0
- package/dist/@types/requestStateDefault.d.ts +7 -0
- package/dist/@types/requestStatus.d.ts +12 -0
- package/dist/@types/types/Dispatcher.d.ts +5 -7
- package/dist/@types/types/InitInstance.d.ts +2 -2
- package/dist/@types/types/InitPayload.d.ts +3 -4
- package/dist/@types/types/InstanceKey.d.ts +1 -1
- package/dist/@types/types/InternalStore/Dispatch.d.ts +7 -0
- package/dist/@types/types/InternalStore/GetState.d.ts +5 -0
- package/dist/@types/types/InternalStore/GetStateAll.d.ts +5 -0
- package/dist/@types/types/InternalStore/InitInstance.d.ts +8 -0
- package/dist/@types/types/InternalStore/Listener.d.ts +7 -0
- package/dist/@types/types/InternalStore/ListenerAll.d.ts +7 -0
- package/dist/@types/types/InternalStore/ResetInstance.d.ts +6 -0
- package/dist/@types/types/InternalStore/Subscribe.d.ts +7 -0
- package/dist/@types/types/InternalStore/SubscribeAll.d.ts +6 -0
- package/dist/@types/types/InternalStore/Unsubscribe.d.ts +3 -0
- package/dist/@types/types/InternalStore/UnsubscribeAll.d.ts +3 -0
- package/dist/@types/types/InternalStoreProps.d.ts +6 -6
- package/dist/@types/types/Reducer.d.ts +2 -2
- package/dist/@types/types/ReducerLink.d.ts +8 -6
- package/dist/@types/types/ReducerMap.d.ts +2 -2
- package/dist/@types/types/ReducerPayload.d.ts +3 -4
- package/dist/@types/types/RegisterDefaultInstance.d.ts +2 -2
- package/dist/@types/types/RegisterInstance.d.ts +3 -3
- package/dist/@types/types/RequestState/RequestAbortedState.d.ts +12 -0
- package/dist/@types/types/RequestState/RequestFulfilledState.d.ts +12 -0
- package/dist/@types/types/RequestState/RequestIdleState.d.ts +12 -0
- package/dist/@types/types/RequestState/RequestPendingState.d.ts +12 -0
- package/dist/@types/types/RequestState/RequestRejectedState.d.ts +12 -0
- package/dist/@types/types/RequestState.d.ts +7 -0
- package/dist/@types/types/RequestStateMeta.d.ts +1 -0
- package/dist/@types/types/RequestStateStoreKey.d.ts +5 -0
- package/dist/@types/types/ResetInstance.d.ts +1 -1
- package/dist/@types/types/ResetPayload.d.ts +3 -4
- package/dist/@types/types/StateLink.d.ts +5 -5
- package/dist/@types/types/Store.d.ts +8 -8
- package/dist/@types/types/StoreController.d.ts +7 -6
- package/dist/@types/types/StoreInstanceController.d.ts +7 -0
- package/dist/@types/types/StoreRegistry.d.ts +1 -1
- package/dist/@types/types/UseProp.d.ts +7 -0
- package/dist/@types/types/UsePropAll.d.ts +7 -0
- package/dist/cjs/index.cjs.js +229 -112
- package/dist/esm/index.esm.js +229 -114
- package/package.json +2 -1
- package/dist/@types/types/StoreControllerInstance.d.ts +0 -7
- package/dist/@types/types/index.d.ts +0 -20
package/dist/esm/index.esm.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import React, { useContext, useMemo,
|
|
1
|
+
import React, { useContext, useMemo, useState, useRef, useCallback, useEffect } from 'react';
|
|
2
2
|
import { useSyncExternalStoreWithSelector } from 'use-sync-external-store/with-selector';
|
|
3
3
|
import { jsx } from 'react/jsx-runtime';
|
|
4
4
|
|
|
@@ -41,30 +41,112 @@ function createUseDispatcher(storeRegistry) {
|
|
|
41
41
|
};
|
|
42
42
|
}
|
|
43
43
|
|
|
44
|
+
function createUseLatest(storeRegistry) {
|
|
45
|
+
return function useLatest(saga, reducerLinks, instanceKey) {
|
|
46
|
+
return;
|
|
47
|
+
};
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
class InternalStore {
|
|
51
|
+
constructor(name) {
|
|
52
|
+
this.getState = (instanceKey) => {
|
|
53
|
+
return this.state[instanceKey];
|
|
54
|
+
};
|
|
55
|
+
this.getStateAll = () => {
|
|
56
|
+
return this.state;
|
|
57
|
+
};
|
|
58
|
+
this.initInstance = (instanceKey, initialState, initPayload, initReducer) => {
|
|
59
|
+
// TODO От множественной инициализации было решено отказаться. Надо кинуть предупреждение.
|
|
60
|
+
this.state = { ...this.state, [instanceKey]: this.state[instanceKey] ?? initialState };
|
|
61
|
+
// TODO От множественной инициализации было решено отказаться. Надо кинуть предупреждение.
|
|
62
|
+
this.listeners[instanceKey] = this.listeners[instanceKey] ?? new Set();
|
|
63
|
+
this.dispatch(instanceKey, initPayload, initReducer);
|
|
64
|
+
return (resetPayload, resetReducer) => {
|
|
65
|
+
this.dispatch(instanceKey, resetPayload, resetReducer);
|
|
66
|
+
// TODO Тут место для уничтожения состояния экземпляра хранилища через `delete this.state[instanceKey];`
|
|
67
|
+
};
|
|
68
|
+
};
|
|
69
|
+
this.subscribe = (instanceKey, listener) => {
|
|
70
|
+
this.listeners[instanceKey].add(listener);
|
|
71
|
+
return () => {
|
|
72
|
+
this.listeners[instanceKey].delete(listener);
|
|
73
|
+
if (this.listeners[instanceKey].size === 0) {
|
|
74
|
+
delete this.listeners[instanceKey];
|
|
75
|
+
}
|
|
76
|
+
};
|
|
77
|
+
};
|
|
78
|
+
this.subscribeAll = (listener) => {
|
|
79
|
+
this.listenersAll.add(listener);
|
|
80
|
+
return () => {
|
|
81
|
+
this.listenersAll.delete(listener);
|
|
82
|
+
};
|
|
83
|
+
};
|
|
84
|
+
this.dispatch = (instanceKey, payload, reducer) => {
|
|
85
|
+
const instanceState = this.state[instanceKey];
|
|
86
|
+
const instanceNextState = reducer(instanceState, payload);
|
|
87
|
+
this.state = { ...this.state, [instanceKey]: instanceNextState };
|
|
88
|
+
this.listeners[instanceKey].forEach((listener) => listener(instanceKey, payload, reducer));
|
|
89
|
+
this.listenersAll.forEach((listener) => listener(instanceKey, payload, reducer));
|
|
90
|
+
// TODO Убрать в логгер
|
|
91
|
+
console.log(`dispatch [store: ${this.name}, instanceKey: ${String(instanceKey)}, action: ${reducer.name}]:`);
|
|
92
|
+
// TODO Убрать в логгер
|
|
93
|
+
console.log({ payload, [`prev state`]: instanceState, [`next state`]: instanceNextState });
|
|
94
|
+
};
|
|
95
|
+
this.listeners = {};
|
|
96
|
+
this.listenersAll = new Set();
|
|
97
|
+
this.name = name;
|
|
98
|
+
this.state = {};
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
function createStoreController(storeRegistry, internalStoreProps) {
|
|
103
|
+
let storeController = storeRegistry[internalStoreProps.uniqKey];
|
|
104
|
+
if (!storeController) {
|
|
105
|
+
const internalStore = new InternalStore(internalStoreProps.name);
|
|
106
|
+
storeController = { earlySubscribers: {}, instances: {}, internalStore };
|
|
107
|
+
storeRegistry[internalStoreProps.uniqKey] = storeController;
|
|
108
|
+
}
|
|
109
|
+
return storeController;
|
|
110
|
+
}
|
|
111
|
+
|
|
44
112
|
function createUseProp(storeRegistry) {
|
|
45
113
|
return function useProp(stateLink, instanceKey) {
|
|
46
114
|
const internalStoreProps = stateLink[INTERNAL_STORE_PROPS_ACCESSOR];
|
|
47
115
|
const contextInstanceKey = useContext(internalStoreProps.context);
|
|
48
116
|
const actualInstanceKey = instanceKey ?? contextInstanceKey ?? DEFAULT_INSTANCE_KEY;
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
117
|
+
const [, setUninitializedHack] = useState(0);
|
|
118
|
+
const earlySubscriberRef = useRef({ earlySubscriber: null });
|
|
119
|
+
const storeController = createStoreController(storeRegistry, internalStoreProps);
|
|
120
|
+
const initialized = actualInstanceKey in storeController.instances;
|
|
121
|
+
const { getSnapshot, subscribe } = useMemo(() => {
|
|
122
|
+
let subscribe;
|
|
123
|
+
let getSnapshot;
|
|
124
|
+
if (initialized) {
|
|
125
|
+
subscribe = (listener) => {
|
|
126
|
+
return storeController.internalStore.subscribe(actualInstanceKey, listener);
|
|
127
|
+
};
|
|
128
|
+
getSnapshot = () => {
|
|
129
|
+
return storeController.internalStore.getState(actualInstanceKey);
|
|
130
|
+
};
|
|
131
|
+
}
|
|
132
|
+
else {
|
|
133
|
+
subscribe = () => () => undefined;
|
|
134
|
+
getSnapshot = () => internalStoreProps.initialState;
|
|
135
|
+
if (!storeController.earlySubscribers[actualInstanceKey]) {
|
|
136
|
+
storeController.earlySubscribers[actualInstanceKey] = [];
|
|
137
|
+
}
|
|
138
|
+
else if (earlySubscriberRef.current.earlySubscriber) {
|
|
139
|
+
storeController.earlySubscribers[actualInstanceKey].splice(storeController.earlySubscribers[actualInstanceKey].indexOf(earlySubscriberRef.current.earlySubscriber), 1);
|
|
140
|
+
}
|
|
141
|
+
const earlySubscriber = earlySubscriberRef.current.earlySubscriber = () => setUninitializedHack(state => state + 1);
|
|
142
|
+
storeController.earlySubscribers[actualInstanceKey].push(earlySubscriber);
|
|
143
|
+
}
|
|
144
|
+
return { getSnapshot, subscribe };
|
|
145
|
+
}, [actualInstanceKey, initialized, internalStoreProps, storeController]);
|
|
64
146
|
const selector = useCallback((state) => {
|
|
65
147
|
return state[stateLink.stateName];
|
|
66
148
|
}, [stateLink]);
|
|
67
|
-
return useSyncExternalStoreWithSelector(subscribe, getSnapshot,
|
|
149
|
+
return useSyncExternalStoreWithSelector(subscribe, getSnapshot, null, selector);
|
|
68
150
|
};
|
|
69
151
|
}
|
|
70
152
|
|
|
@@ -80,7 +162,7 @@ function createUsePropAll(storeRegistry) {
|
|
|
80
162
|
return internalStore.subscribeAll(listener);
|
|
81
163
|
}, [internalStore]);
|
|
82
164
|
const getSnapshot = useCallback(() => {
|
|
83
|
-
return internalStore.
|
|
165
|
+
return internalStore.getStateAll();
|
|
84
166
|
}, [internalStore]);
|
|
85
167
|
const selector = useCallback((state) => {
|
|
86
168
|
let stateAll;
|
|
@@ -112,7 +194,7 @@ function createUsePropAll(storeRegistry) {
|
|
|
112
194
|
return prevStateAllRef.current.prevStateAll;
|
|
113
195
|
}
|
|
114
196
|
}, [instanceKeys, stateLink]);
|
|
115
|
-
return useSyncExternalStoreWithSelector(subscribe, getSnapshot,
|
|
197
|
+
return useSyncExternalStoreWithSelector(subscribe, getSnapshot, null, selector);
|
|
116
198
|
};
|
|
117
199
|
}
|
|
118
200
|
function isEqual(prevStateAll, stateAll) {
|
|
@@ -129,119 +211,65 @@ function isEqual(prevStateAll, stateAll) {
|
|
|
129
211
|
}
|
|
130
212
|
}
|
|
131
213
|
|
|
132
|
-
class InternalStore {
|
|
133
|
-
constructor(name) {
|
|
134
|
-
this.getStateAll = () => {
|
|
135
|
-
return this.state;
|
|
136
|
-
};
|
|
137
|
-
this.getState = (instanceKey) => {
|
|
138
|
-
return this.state[instanceKey];
|
|
139
|
-
};
|
|
140
|
-
this.initInstance = (instanceKey, initialState, initPayload, initReducer) => {
|
|
141
|
-
const initState = initReducer(initialState, initPayload);
|
|
142
|
-
this.state = {
|
|
143
|
-
...this.state,
|
|
144
|
-
[instanceKey]: initState,
|
|
145
|
-
};
|
|
146
|
-
// TODO Убрать в логгер
|
|
147
|
-
console.log(`initInstance [store: ${this.name}, instance: ${String(instanceKey)}]:`);
|
|
148
|
-
console.dir(this.state);
|
|
149
|
-
return (resetPayload, resetReducer) => {
|
|
150
|
-
const resetState = resetReducer(initState, resetPayload);
|
|
151
|
-
// TODO Тут место для уничтожения состояния экземпляра хранилища через `delete this.state[instanceKey];`
|
|
152
|
-
this.state = { ...this.state, [instanceKey]: resetState };
|
|
153
|
-
// TODO Убрать в логгер
|
|
154
|
-
console.log(`resetInstance [store: ${this.name}, instance: ${String(instanceKey)}]:`);
|
|
155
|
-
console.dir(this.state);
|
|
156
|
-
};
|
|
157
|
-
};
|
|
158
|
-
this.subscribe = (instanceKey, listener) => {
|
|
159
|
-
if (!this.listeners[instanceKey]) {
|
|
160
|
-
this.listeners[instanceKey] = new Set();
|
|
161
|
-
}
|
|
162
|
-
this.listeners[instanceKey].add(listener);
|
|
163
|
-
return () => {
|
|
164
|
-
this.listeners[instanceKey].delete(listener);
|
|
165
|
-
if (this.listeners[instanceKey].size === 0) {
|
|
166
|
-
delete this.listeners[instanceKey];
|
|
167
|
-
}
|
|
168
|
-
};
|
|
169
|
-
};
|
|
170
|
-
this.subscribeAll = (listener) => {
|
|
171
|
-
this.listenersAll.add(listener);
|
|
172
|
-
return () => {
|
|
173
|
-
this.listenersAll.delete(listener);
|
|
174
|
-
};
|
|
175
|
-
};
|
|
176
|
-
this.getSnapshot = (instanceKey) => {
|
|
177
|
-
return this.state[instanceKey];
|
|
178
|
-
};
|
|
179
|
-
this.getSnapshotAll = () => {
|
|
180
|
-
return this.state;
|
|
181
|
-
};
|
|
182
|
-
this.dispatch = (instanceKey, payload, reducer) => {
|
|
183
|
-
const instanceState = this.state[instanceKey];
|
|
184
|
-
const changes = reducer(instanceState, payload);
|
|
185
|
-
this.update(instanceKey, changes);
|
|
186
|
-
// TODO Убрать в логгер
|
|
187
|
-
console.log(`dispatch [store: ${this.name}, instance: ${String(instanceKey)}, action: ${reducer.name}]:`);
|
|
188
|
-
console.dir({ payload, [`prev state`]: instanceState, [`next state`]: changes });
|
|
189
|
-
};
|
|
190
|
-
this.update = (instanceKey, changes) => {
|
|
191
|
-
this.state = { ...this.state, [instanceKey]: changes };
|
|
192
|
-
this.listeners[instanceKey].forEach((listener) => listener());
|
|
193
|
-
this.listenersAll.forEach((listener) => listener());
|
|
194
|
-
};
|
|
195
|
-
this.listeners = {};
|
|
196
|
-
this.listenersAll = new Set();
|
|
197
|
-
this.name = name;
|
|
198
|
-
this.state = {};
|
|
199
|
-
}
|
|
200
|
-
}
|
|
201
|
-
|
|
202
214
|
function createUseStore(storeRegistry) {
|
|
203
215
|
function useStore(store, ...args) {
|
|
204
216
|
let instanceKeys;
|
|
205
217
|
let registerInstance;
|
|
206
218
|
if (typeof args[0] === `function`) {
|
|
207
|
-
instanceKeys = [];
|
|
219
|
+
instanceKeys = [DEFAULT_INSTANCE_KEY];
|
|
208
220
|
registerInstance = args[0];
|
|
209
221
|
}
|
|
210
222
|
else {
|
|
211
223
|
instanceKeys = args[0];
|
|
212
224
|
registerInstance = args[1];
|
|
213
225
|
}
|
|
214
|
-
|
|
226
|
+
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
227
|
+
const actualInstanceKeys = useMemo(() => instanceKeys, instanceKeys);
|
|
215
228
|
const storeRef = useRef({ store });
|
|
216
229
|
const ownerKeyRef = useRef({ ownerKey: Symbol(`ownerKey`) });
|
|
217
230
|
const initInstancesRef = useRef({ initInstances: {} });
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
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
|
+
useEffect(() => {
|
|
237
|
+
const { store } = storeRef.current;
|
|
238
|
+
const { ownerKey } = ownerKeyRef.current;
|
|
239
|
+
const { initInstances } = initInstancesRef.current;
|
|
221
240
|
for (const instanceKey of actualInstanceKeys) {
|
|
222
241
|
if (!initInstances[instanceKey]) {
|
|
223
|
-
const ownerKey = ownerKeyRef.current.ownerKey;
|
|
224
242
|
const initInstance = (initPayload) => {
|
|
225
243
|
const internalStoreProps = store[INTERNAL_STORE_PROPS_ACCESSOR];
|
|
226
|
-
|
|
227
|
-
if (!storeController) {
|
|
228
|
-
const internalStore = new InternalStore(internalStoreProps.name);
|
|
229
|
-
storeController = { internalStore, instances: {} };
|
|
230
|
-
storeRegistry[internalStoreProps.uniqKey] = storeController;
|
|
231
|
-
// TODO Убрать в логгер
|
|
232
|
-
console.log(`registerStore [store: ${storeController.internalStore.name}]:`);
|
|
233
|
-
console.dir(storeRegistry);
|
|
234
|
-
}
|
|
244
|
+
const storeController = createStoreController(storeRegistry, internalStoreProps);
|
|
235
245
|
if (!storeController.instances[instanceKey]) {
|
|
236
|
-
|
|
246
|
+
// TODO Убрать в логгер
|
|
247
|
+
console.log(`Store instance initialization [store: ${storeController.internalStore.name}, instanceKey: ${instanceKey.toString()}]`);
|
|
248
|
+
// TODO Убрать в логгер
|
|
249
|
+
console.log({ storeRegistry });
|
|
250
|
+
const resetState = storeController.internalStore.initInstance(instanceKey, internalStoreProps.initialState, initPayload, internalStoreProps.$$init);
|
|
237
251
|
storeController.instances[instanceKey] = { owners: [], resetState };
|
|
252
|
+
if (storeController.earlySubscribers[instanceKey]) {
|
|
253
|
+
for (const subscriber of storeController.earlySubscribers[instanceKey]) {
|
|
254
|
+
subscriber();
|
|
255
|
+
}
|
|
256
|
+
storeController.earlySubscribers[instanceKey].splice(0);
|
|
257
|
+
}
|
|
258
|
+
}
|
|
259
|
+
else {
|
|
260
|
+
// TODO Множественная инициализация хранилища
|
|
261
|
+
// Архитектура позволяет несколько раз инициализировать одно и то же хранилище с
|
|
262
|
+
// одним и тем же ключом, однако возможно от этого стоит отказаться. Возможно
|
|
263
|
+
// стоит просить разработчиков проверять инициализирована ли стора и
|
|
264
|
+
// инициализировать ее при необходимости.
|
|
265
|
+
console.warn(`Duplicate store initialization for key "${instanceKey.toString()}"`);
|
|
238
266
|
}
|
|
239
267
|
storeController.instances[instanceKey].owners.push(ownerKey);
|
|
240
268
|
return (resetPayload) => {
|
|
241
269
|
storeController.instances[instanceKey].owners.splice(storeController.instances[instanceKey].owners.indexOf(ownerKey), 1);
|
|
242
270
|
if (storeController.instances[instanceKey].owners.length === 0) {
|
|
243
271
|
// TODO Надо ли оповещать слушателей если один экземпляр хранилища удалился
|
|
244
|
-
storeController.instances[instanceKey].resetState(resetPayload, internalStoreProps
|
|
272
|
+
storeController.instances[instanceKey].resetState(resetPayload, internalStoreProps.$$reset);
|
|
245
273
|
delete storeController.instances[instanceKey];
|
|
246
274
|
}
|
|
247
275
|
delete initInstances[instanceKey];
|
|
@@ -262,7 +290,7 @@ function createUseStore(storeRegistry) {
|
|
|
262
290
|
}
|
|
263
291
|
},
|
|
264
292
|
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
265
|
-
actualInstanceKeys);
|
|
293
|
+
[actualInstanceKeys, /*registerInstance*/]);
|
|
266
294
|
useEffect(() => {
|
|
267
295
|
const initInstances = initInstancesRef.current.initInstances;
|
|
268
296
|
return () => {
|
|
@@ -277,23 +305,26 @@ function createUseStore(storeRegistry) {
|
|
|
277
305
|
|
|
278
306
|
function createStoreRegistry() {
|
|
279
307
|
const storeRegistry = {};
|
|
280
|
-
const
|
|
308
|
+
const useStore = createUseStore(storeRegistry);
|
|
281
309
|
const useProp = createUseProp(storeRegistry);
|
|
282
310
|
const usePropAll = createUsePropAll(storeRegistry);
|
|
283
|
-
const
|
|
311
|
+
const useDispatcher = createUseDispatcher(storeRegistry);
|
|
312
|
+
const useLatest = createUseLatest();
|
|
284
313
|
return {
|
|
285
|
-
|
|
314
|
+
storeRegistry,
|
|
315
|
+
useStore,
|
|
286
316
|
useProp,
|
|
287
317
|
usePropAll,
|
|
288
|
-
|
|
318
|
+
useDispatcher,
|
|
319
|
+
useLatest,
|
|
289
320
|
};
|
|
290
321
|
}
|
|
291
322
|
|
|
292
323
|
function createStore(name, initialState, reducers) {
|
|
293
324
|
const context = React.createContext(null);
|
|
294
325
|
const internalStoreProps = {
|
|
295
|
-
|
|
296
|
-
|
|
326
|
+
$$init: reducers.$$init,
|
|
327
|
+
$$reset: reducers.$$reset,
|
|
297
328
|
context,
|
|
298
329
|
initialState,
|
|
299
330
|
name,
|
|
@@ -322,6 +353,90 @@ function createStore(name, initialState, reducers) {
|
|
|
322
353
|
return store;
|
|
323
354
|
}
|
|
324
355
|
|
|
356
|
+
const requestStateDefault = {
|
|
357
|
+
isAborted: false,
|
|
358
|
+
isFulfilled: false,
|
|
359
|
+
isIdle: false,
|
|
360
|
+
isPending: false,
|
|
361
|
+
isRejected: false,
|
|
362
|
+
};
|
|
363
|
+
|
|
364
|
+
const requestStatus = {
|
|
365
|
+
IDLE: 'IDLE',
|
|
366
|
+
PENDING: 'PENDING',
|
|
367
|
+
FULFILLED: 'FULFILLED',
|
|
368
|
+
REJECTED: 'REJECTED',
|
|
369
|
+
ABORTED: 'ABORTED',
|
|
370
|
+
FORCE_IDLE: 'IDLE',
|
|
371
|
+
FORCE_PENDING: 'PENDING',
|
|
372
|
+
FORCE_FULFILLED: 'FULFILLED',
|
|
373
|
+
FORCE_REJECTED: 'REJECTED',
|
|
374
|
+
FORCE_ABORTED: 'ABORTED',
|
|
375
|
+
};
|
|
376
|
+
|
|
377
|
+
const requestState = {
|
|
378
|
+
idle: (meta) => {
|
|
379
|
+
return {
|
|
380
|
+
...requestStateDefault,
|
|
381
|
+
isIdle: true,
|
|
382
|
+
status: requestStatus.IDLE,
|
|
383
|
+
error: null,
|
|
384
|
+
meta: meta ?? null,
|
|
385
|
+
};
|
|
386
|
+
},
|
|
387
|
+
pending: (meta) => {
|
|
388
|
+
return {
|
|
389
|
+
...requestStateDefault,
|
|
390
|
+
isPending: true,
|
|
391
|
+
status: requestStatus.PENDING,
|
|
392
|
+
error: null,
|
|
393
|
+
meta: meta ?? null,
|
|
394
|
+
};
|
|
395
|
+
},
|
|
396
|
+
fulfilled: (meta) => {
|
|
397
|
+
return {
|
|
398
|
+
...requestStateDefault,
|
|
399
|
+
isFulfilled: true,
|
|
400
|
+
status: requestStatus.FULFILLED,
|
|
401
|
+
meta: meta ?? null,
|
|
402
|
+
error: null,
|
|
403
|
+
};
|
|
404
|
+
},
|
|
405
|
+
rejected: (error, meta) => {
|
|
406
|
+
return {
|
|
407
|
+
...requestStateDefault,
|
|
408
|
+
isRejected: true,
|
|
409
|
+
status: requestStatus.REJECTED,
|
|
410
|
+
error: error ?? null,
|
|
411
|
+
meta: meta ?? null,
|
|
412
|
+
};
|
|
413
|
+
},
|
|
414
|
+
aborted: (meta) => {
|
|
415
|
+
return {
|
|
416
|
+
...requestStateDefault,
|
|
417
|
+
isAborted: true,
|
|
418
|
+
status: requestStatus.ABORTED,
|
|
419
|
+
meta: meta ?? null,
|
|
420
|
+
error: null,
|
|
421
|
+
};
|
|
422
|
+
},
|
|
423
|
+
forceIdle: (meta) => {
|
|
424
|
+
return requestState.idle(meta);
|
|
425
|
+
},
|
|
426
|
+
forcePending: (meta) => {
|
|
427
|
+
return requestState.pending(meta);
|
|
428
|
+
},
|
|
429
|
+
forceFulfilled: (meta) => {
|
|
430
|
+
return requestState.fulfilled(meta);
|
|
431
|
+
},
|
|
432
|
+
forceRejected: (error, meta) => {
|
|
433
|
+
return requestState.rejected(error, meta);
|
|
434
|
+
},
|
|
435
|
+
forceAborted: (meta) => {
|
|
436
|
+
return requestState.aborted(meta);
|
|
437
|
+
},
|
|
438
|
+
};
|
|
439
|
+
|
|
325
440
|
function StoreProvider(props) {
|
|
326
441
|
const Context = props.store[INTERNAL_STORE_PROPS_ACCESSOR].context;
|
|
327
442
|
return (jsx(Context.Provider, { value: props.instanceKey, children: props.children }));
|
|
@@ -329,4 +444,4 @@ function StoreProvider(props) {
|
|
|
329
444
|
|
|
330
445
|
const { useDispatcher, useProp, usePropAll, useStore, } = createStoreRegistry();
|
|
331
446
|
|
|
332
|
-
export { DESTROY, InternalStore, StoreProvider, createStore, createStoreRegistry, useDispatcher, useProp, usePropAll, useStore };
|
|
447
|
+
export { DESTROY, InternalStore, StoreProvider, createStore, createStoreRegistry, requestState, requestStatus, useDispatcher, useProp, usePropAll, useStore };
|
package/package.json
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
{
|
|
2
|
+
"author": "Yakov Dalinchuk <yakov.dalinchuk@gmail.com>",
|
|
2
3
|
"dependencies": {
|
|
3
4
|
"use-sync-external-store": "^1.5.0"
|
|
4
5
|
},
|
|
@@ -19,5 +20,5 @@
|
|
|
19
20
|
"prepublishOnly": "npm run build"
|
|
20
21
|
},
|
|
21
22
|
"types": "dist/@types",
|
|
22
|
-
"version": "0.1.
|
|
23
|
+
"version": "0.1.6"
|
|
23
24
|
}
|
|
@@ -1,7 +0,0 @@
|
|
|
1
|
-
import type { StoreState } from './';
|
|
2
|
-
import { type Payload } from './';
|
|
3
|
-
import { type Reducer } from './';
|
|
4
|
-
export type StoreControllerInstance<TStoreState extends StoreState = StoreState, TResetPayload extends void | Payload = void | Payload> = {
|
|
5
|
-
owners: symbol[];
|
|
6
|
-
resetState: (resetPayload: TResetPayload, resetReducer: Reducer<TStoreState, TResetPayload>) => void;
|
|
7
|
-
};
|
|
@@ -1,20 +0,0 @@
|
|
|
1
|
-
export * from './Dispatcher.tsx';
|
|
2
|
-
export * from './InitInstance.tsx';
|
|
3
|
-
export * from './InstanceKey.tsx';
|
|
4
|
-
export * from './InitPayload.tsx';
|
|
5
|
-
export * from './InternalStoreProps.tsx';
|
|
6
|
-
export * from './Payload.tsx';
|
|
7
|
-
export * from './Reducer.tsx';
|
|
8
|
-
export * from './ReducerLink.tsx';
|
|
9
|
-
export * from './ReducerMap.tsx';
|
|
10
|
-
export * from './ReducerPayload.tsx';
|
|
11
|
-
export * from './RegisterDefaultInstance.tsx';
|
|
12
|
-
export * from './RegisterInstance.tsx';
|
|
13
|
-
export * from './ResetInstance.tsx';
|
|
14
|
-
export * from './ResetPayload.tsx';
|
|
15
|
-
export * from './StateLink.tsx';
|
|
16
|
-
export * from './Store.tsx';
|
|
17
|
-
export * from './StoreController.tsx';
|
|
18
|
-
export * from './StoreControllerInstance.tsx';
|
|
19
|
-
export * from './StoreRegistry.tsx';
|
|
20
|
-
export * from './StoreState.tsx';
|