react-hooks-global-states 16.0.0 → 16.0.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.
Files changed (46) hide show
  1. package/jest.config.js +50 -0
  2. package/package.json +52 -2
  3. package/GlobalStore.cjs +0 -525
  4. package/GlobalStore.d.ts +0 -160
  5. package/GlobalStore.debugProps.cjs +0 -28
  6. package/GlobalStore.debugProps.d.ts +0 -5
  7. package/GlobalStore.debugProps.js +0 -28
  8. package/GlobalStore.debugProps.mjs +0 -7
  9. package/GlobalStore.js +0 -525
  10. package/GlobalStore.mjs +0 -497
  11. package/actions.cjs +0 -61
  12. package/actions.d.ts +0 -32
  13. package/actions.js +0 -61
  14. package/actions.mjs +0 -30
  15. package/bundle.cjs +0 -40
  16. package/bundle.js +0 -40
  17. package/bundle.mjs +0 -19
  18. package/createContext.cjs +0 -253
  19. package/createContext.d.ts +0 -15
  20. package/createContext.js +0 -253
  21. package/createContext.mjs +0 -235
  22. package/createGlobalState.cjs +0 -29
  23. package/createGlobalState.d.ts +0 -7
  24. package/createGlobalState.js +0 -29
  25. package/createGlobalState.mjs +0 -8
  26. package/index.d.ts +0 -9
  27. package/isRecord.cjs +0 -46
  28. package/isRecord.d.ts +0 -2
  29. package/isRecord.js +0 -46
  30. package/isRecord.mjs +0 -15
  31. package/shallowCompare.cjs +0 -115
  32. package/shallowCompare.d.ts +0 -52
  33. package/shallowCompare.js +0 -115
  34. package/shallowCompare.mjs +0 -84
  35. package/throwWrongKeyOnActionCollectionConfig.cjs +0 -41
  36. package/throwWrongKeyOnActionCollectionConfig.d.ts +0 -2
  37. package/throwWrongKeyOnActionCollectionConfig.js +0 -41
  38. package/throwWrongKeyOnActionCollectionConfig.mjs +0 -20
  39. package/types.cjs +0 -18
  40. package/types.d.ts +0 -1250
  41. package/types.js +0 -18
  42. package/types.mjs +0 -0
  43. package/uniqueId.cjs +0 -72
  44. package/uniqueId.d.ts +0 -7
  45. package/uniqueId.js +0 -72
  46. package/uniqueId.mjs +0 -51
package/GlobalStore.mjs DELETED
@@ -1,497 +0,0 @@
1
- var __defProp = Object.defineProperty;
2
- var __defProps = Object.defineProperties;
3
- var __getOwnPropDescs = Object.getOwnPropertyDescriptors;
4
- var __getOwnPropSymbols = Object.getOwnPropertySymbols;
5
- var __hasOwnProp = Object.prototype.hasOwnProperty;
6
- var __propIsEnum = Object.prototype.propertyIsEnumerable;
7
- var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
8
- var __spreadValues = (a, b) => {
9
- for (var prop in b || (b = {}))
10
- if (__hasOwnProp.call(b, prop))
11
- __defNormalProp(a, prop, b[prop]);
12
- if (__getOwnPropSymbols)
13
- for (var prop of __getOwnPropSymbols(b)) {
14
- if (__propIsEnum.call(b, prop))
15
- __defNormalProp(a, prop, b[prop]);
16
- }
17
- return a;
18
- };
19
- var __spreadProps = (a, b) => __defProps(a, __getOwnPropDescs(b));
20
-
21
- // src/GlobalStore.ts
22
- import { useDebugValue, useMemo, useRef, useSyncExternalStore } from "react";
23
- import isFunction from "json-storage-formatter/isFunction";
24
- import isRecord from "./isRecord.mjs";
25
- import shallowCompare, { isArray } from "./shallowCompare.mjs";
26
- import throwWrongKeyOnActionCollectionConfig from "./throwWrongKeyOnActionCollectionConfig.mjs";
27
- import uniqueId from "./uniqueId.mjs";
28
- import debugProps from "./GlobalStore.debugProps.mjs";
29
- var GlobalStore = class _GlobalStore {
30
- constructor(state, args = { metadata: {} }) {
31
- this.cleanupFunctions = [];
32
- this.actionsConfig = null;
33
- this.callbacks = null;
34
- /**
35
- * @description If the actionsConfig is defined, this will be a map of actions that can be used to modify or interact with the state
36
- * */
37
- this.actions = null;
38
- /**
39
- * @deprecated
40
- * @description Set of subscribers that are listening to state changes
41
- * Useful for debugging purposes... You'll probably not need to use this in your application
42
- */
43
- this.subscribers = /* @__PURE__ */ new Set();
44
- this.createSelectorHook = createSelectorHook;
45
- this.createObservable = createObservable;
46
- var _a, _b, _c;
47
- const { metadata, callbacks, actions, name: storeName } = args;
48
- if (isFunction(state)) this.stateCallback = state;
49
- if (isFunction(metadata)) this.metadataCallback = metadata;
50
- this._name = storeName != null ? storeName : uniqueId("gs:");
51
- this.state = this.stateCallback ? this.stateCallback() : state;
52
- this.metadata = this.metadataCallback ? this.metadataCallback() : metadata != null ? metadata : {};
53
- this.callbacks = callbacks != null ? callbacks : null;
54
- this.actionsConfig = actions != null ? actions : null;
55
- if (debugProps.isDevToolsPresent) {
56
- const hookStack = (_a = new Error().stack) != null ? _a : "";
57
- (_c = (_b = debugProps).REACT_GLOBAL_STATE_HOOK_DEBUG) == null ? void 0 : _c.call(_b, this, args, hookStack);
58
- }
59
- const isExtensionClass = this.constructor !== _GlobalStore;
60
- if (isExtensionClass) return;
61
- this.initialize();
62
- }
63
- /**
64
- * @description
65
- * Initializes the global store, setting up the main hook and actions map if applicable,
66
- */
67
- async initialize() {
68
- var _a, _b, _c, _d, _e, _f, _g, _h;
69
- const storeAndActions = (_b = (_a = this.__devtools_initialize_getStoreActionsMapWrapped) == null ? void 0 : _a.call(this)) != null ? _b : this.getStoreActionsMap();
70
- const { actions, storeTools } = storeAndActions;
71
- this.actions = actions;
72
- this.storeTools = storeTools;
73
- this.use = this.getMainHook();
74
- const extensionCleanup = (_d = (_c = this.onInit) == null ? void 0 : _c.call(this)) != null ? _d : null;
75
- if (isFunction(extensionCleanup)) this.cleanupFunctions.push(extensionCleanup);
76
- const { onInit: onInitFromConfig } = (_e = this.callbacks) != null ? _e : {};
77
- if (!onInitFromConfig) return;
78
- const onInitFromConfigStoreTools = (_g = (_f = this.__devtools_getLifeCycleStoreToolsWrapper) == null ? void 0 : _f.call(this, "config/onInit/")) != null ? _g : storeTools;
79
- const configCleanup = (_h = onInitFromConfig == null ? void 0 : onInitFromConfig(onInitFromConfigStoreTools)) != null ? _h : null;
80
- if (isFunction(configCleanup)) this.cleanupFunctions.push(configCleanup);
81
- }
82
- /**
83
- * set the state for a single subscriber
84
- * validate if the state should be updated by comparing the previous state and the new state
85
- */
86
- executeSetStateForSubscriber(subscription, args) {
87
- const {
88
- selector,
89
- onStoreChange: callback,
90
- currentState: currentChildState,
91
- isEqualRoot = (a, b) => a === b,
92
- isEqual = (a, b) => a === b
93
- } = subscription;
94
- if (!args.forceUpdate && isEqualRoot(args.currentState, args.newState)) {
95
- return { didUpdate: false };
96
- }
97
- const newChildState = selector ? selector(args.newState) : args.newState;
98
- if (!args.forceUpdate && isEqual(currentChildState, newChildState)) {
99
- return { didUpdate: false };
100
- }
101
- this.partialUpdateSubscription(subscription, {
102
- currentState: newChildState
103
- });
104
- callback(
105
- {
106
- state: newChildState
107
- },
108
- {
109
- identifier: args.identifier
110
- }
111
- );
112
- return { didUpdate: true };
113
- }
114
- /**
115
- * set the state and update all the subscribers
116
- * @param {State} newState - The new state to set
117
- * @param {object} options - The options for setting the state
118
- * @param {boolean} [options.forceUpdate] - Whether to force the update even if the state is the same
119
- * @param {string} [options.identifier] - An optional identifier for the state change
120
- * */
121
- setActualStateWithoutValidations(newState, { forceUpdate, identifier }) {
122
- const currentState = this.state;
123
- const shouldUpdate = forceUpdate || currentState !== newState;
124
- if (!shouldUpdate) return;
125
- this.state = newState;
126
- const subscribers = this.subscribers.values();
127
- const args = {
128
- forceUpdate,
129
- newState,
130
- currentState,
131
- identifier
132
- };
133
- for (const subscription of subscribers) {
134
- this.executeSetStateForSubscriber(subscription, args);
135
- }
136
- }
137
- /**
138
- * Set the value of the metadata property, this is no reactive and will not trigger a re-render
139
- * @param {MetadataSetter<Metadata>} setter - The setter function or the value to set
140
- * */
141
- setMetadata(setter) {
142
- const metadata = isFunction(setter) ? setter(this.metadata) : setter;
143
- this.metadata = metadata;
144
- }
145
- /**
146
- * Returns the metadata [non-reactive additional information associated with the global state]
147
- */
148
- getMetadata() {
149
- return this.metadata;
150
- }
151
- /**
152
- * Get the current value of the state
153
- */
154
- getState() {
155
- return this.state;
156
- }
157
- /**
158
- * Subscribe an individual callback to state changes
159
- */
160
- subscribe(...[param1, param2, param3]) {
161
- var _a;
162
- const hasExplicitSelector = isFunction(param2);
163
- const selector = hasExplicitSelector ? param1 : void 0;
164
- const callback = hasExplicitSelector ? param2 : param1;
165
- const options = (_a = hasExplicitSelector ? param3 : param2) != null ? _a : void 0;
166
- const initialState = selector ? selector(this.state) : this.state;
167
- if (!(options == null ? void 0 : options.skipFirst)) {
168
- callback(initialState);
169
- }
170
- const subscription = __spreadValues({
171
- selector,
172
- currentState: initialState,
173
- onStoreChange: ({ state }) => callback(state)
174
- }, options);
175
- this.subscribeCallback(subscription);
176
- return () => {
177
- this.subscribers.delete(subscription);
178
- };
179
- }
180
- /**
181
- * Adds a subscription object to the subscribers set and returns the unsubscribe function
182
- */
183
- subscribeCallback(subscription) {
184
- var _a, _b;
185
- (_b = (_a = this.callbacks) == null ? void 0 : _a.onSubscribed) == null ? void 0 : _b.call(_a, this.storeTools, subscription);
186
- this.subscribers.add(subscription);
187
- return () => {
188
- this.subscribers.delete(subscription);
189
- };
190
- }
191
- partialUpdateSubscription(subscription, values) {
192
- Object.assign(subscription, values);
193
- }
194
- /**
195
- * Returns a custom hook that allows to handle a global state
196
- * @returns {[State, StateMutator, Metadata]} - The state, the state setter or the actions map, the metadata
197
- * */
198
- getMainHook() {
199
- const identifier = `globalHook:${this._name}`;
200
- const use = (selector, args = []) => {
201
- useDebugValue(identifier);
202
- const options = isArray(args) ? { dependencies: args } : args != null ? args : {};
203
- const subscriptionRef = useRef(null);
204
- const selectorWrapper = (state) => {
205
- return isFunction(selector) ? selector(state) : state;
206
- };
207
- subscriptionRef.current = (() => {
208
- if (subscriptionRef.current) return subscriptionRef.current;
209
- return __spreadValues({
210
- selector: selectorWrapper,
211
- currentState: selectorWrapper(this.state),
212
- onStoreChange: () => {
213
- throw new Error("Callback not set");
214
- }
215
- }, options);
216
- })();
217
- const currentDependencies = subscriptionRef.current.dependencies;
218
- const newDependencies = options == null ? void 0 : options.dependencies;
219
- const extensions = __spreadProps(__spreadValues({}, options), {
220
- selector: selectorWrapper,
221
- dependencies: newDependencies
222
- });
223
- this.partialUpdateSubscription(subscriptionRef.current, extensions);
224
- const { subscribe, getSnapshot, getServerSnapshot } = useMemo(() => {
225
- const subscribe2 = (onStoreChange) => {
226
- subscriptionRef.current.onStoreChange = onStoreChange;
227
- return this.subscribeCallback(subscriptionRef.current);
228
- };
229
- const getSnapshot2 = () => {
230
- return subscriptionRef.current.currentState;
231
- };
232
- const getServerSnapshot2 = getSnapshot2;
233
- return { subscribe: subscribe2, getSnapshot: getSnapshot2, getServerSnapshot: getServerSnapshot2 };
234
- }, []);
235
- this.reselectIfDependenciesChanged({
236
- subscription: subscriptionRef.current,
237
- newDependencies,
238
- currentDependencies
239
- });
240
- return [
241
- useSyncExternalStore(subscribe, getSnapshot, getServerSnapshot),
242
- this.getStateOrchestrator(),
243
- this.metadata
244
- ];
245
- };
246
- const apiAsReadOnly = this;
247
- const useExtensions = {
248
- actions: this.actions,
249
- createObservable: this.createObservable.bind(apiAsReadOnly),
250
- createSelectorHook: this.createSelectorHook.bind(apiAsReadOnly),
251
- dispose: this.dispose.bind(this),
252
- getMetadata: this.getMetadata.bind(this),
253
- getState: this.getState.bind(this),
254
- reset: this.reset.bind(this),
255
- setMetadata: this.setMetadata.bind(this),
256
- setState: this.setState.bind(this),
257
- subscribe: this.subscribe.bind(this),
258
- // useful for debugging purposes
259
- subscribers: this.subscribers,
260
- // sugar syntax
261
- use,
262
- select: (...args) => use(...args)[0]
263
- };
264
- Object.assign(use, useExtensions);
265
- return use;
266
- }
267
- reselectIfDependenciesChanged({
268
- subscription,
269
- newDependencies,
270
- currentDependencies
271
- }) {
272
- if (!subscription.selector) return;
273
- if (currentDependencies === newDependencies) return;
274
- const isLengthEqual = (currentDependencies == null ? void 0 : currentDependencies.length) === (newDependencies == null ? void 0 : newDependencies.length);
275
- const isSameValues = isLengthEqual && shallowCompare(currentDependencies, newDependencies);
276
- if (isSameValues) return;
277
- this.partialUpdateSubscription(subscription, {
278
- currentState: subscription.selector(this.state)
279
- });
280
- }
281
- /**
282
- * Returns the state setter or the actions map
283
- * @returns {StateMutator} - The state setter or the actions map
284
- * */
285
- getStateOrchestrator() {
286
- return (() => {
287
- if (this.actions) {
288
- return this.actions;
289
- }
290
- return this.setState.bind(this);
291
- })();
292
- }
293
- /**
294
- * This is the only setState function that should be exposed outside the class
295
- * This is responsible for defining whenever or not the state change should be allowed or prevented
296
- * the function also execute the functions:
297
- * - onStateChanged (if defined) - this function is executed after the state change
298
- * - computePreventStateChange (if defined) - this function is executed before the state change and it should return a boolean value that will be used to determine if the state change should be prevented or not
299
- */
300
- setState(setter, {
301
- forceUpdate,
302
- identifier
303
- } = {}) {
304
- var _a, _b, _c, _d, _e, _f;
305
- const previousState = this.state;
306
- const newState = isFunction(setter) ? setter(previousState) : setter;
307
- if (!forceUpdate && this.state === newState) return;
308
- const setState = this.setActualStateWithoutValidations;
309
- const stateChanges = {
310
- previousState,
311
- state: newState,
312
- identifier
313
- };
314
- const storeTools = __spreadProps(__spreadValues(__spreadValues({}, this.storeTools), stateChanges), {
315
- setState
316
- });
317
- if ((_a = this.callbacks) == null ? void 0 : _a.computePreventStateChange) {
318
- const shouldPreventStateChange = (_c = (_b = this.callbacks) == null ? void 0 : _b.computePreventStateChange) == null ? void 0 : _c.call(_b, storeTools);
319
- if (shouldPreventStateChange) return;
320
- }
321
- this.setActualStateWithoutValidations(newState, { forceUpdate, identifier });
322
- (_d = this.onStateChanged) == null ? void 0 : _d.call(this, storeTools);
323
- (_f = (_e = this.callbacks) == null ? void 0 : _e.onStateChanged) == null ? void 0 : _f.call(_e, storeTools);
324
- }
325
- /**
326
- * This creates storeTools and actions map if applicable
327
- * */
328
- getStoreActionsMap() {
329
- const storeTools = {
330
- setMetadata: this.setMetadata.bind(this),
331
- getMetadata: this.getMetadata.bind(this),
332
- getState: this.getState.bind(this),
333
- setState: this.setState.bind(this),
334
- subscribe: this.subscribe.bind(this),
335
- actions: null
336
- };
337
- if (!isRecord(this.actionsConfig)) {
338
- return {
339
- actions: null,
340
- storeTools
341
- };
342
- }
343
- const actionsConfig = this.actionsConfig;
344
- const actions = {};
345
- storeTools.actions = actions;
346
- const actionsKeys = Object.keys(actionsConfig);
347
- for (const action_key of actionsKeys) {
348
- Object.assign(actions, {
349
- [action_key](...parameters) {
350
- const actionConfig = actionsConfig[action_key];
351
- const action = actionConfig.apply(actions, parameters);
352
- const actionIsNotAFunction = typeof action !== "function";
353
- if (actionIsNotAFunction) {
354
- throwWrongKeyOnActionCollectionConfig(action_key);
355
- }
356
- const result = action.call(actions, storeTools);
357
- return result;
358
- }
359
- });
360
- }
361
- return {
362
- actions,
363
- storeTools
364
- };
365
- }
366
- removeSubscriptions() {
367
- this.subscribers.clear();
368
- }
369
- executeCleanupTasks() {
370
- this.cleanupFunctions.forEach((cleanup) => {
371
- cleanup == null ? void 0 : cleanup();
372
- });
373
- this.cleanupFunctions.length = 0;
374
- }
375
- dispose() {
376
- this.removeSubscriptions();
377
- this.executeCleanupTasks();
378
- }
379
- /**
380
- * @description Resets the store to a new initial state and re-runs initialization.
381
- *
382
- * This method is reserved for advanced use cases and testing scenarios, use with caution.
383
- */
384
- reset(...args) {
385
- var _a, _b, _c, _d, _e, _f;
386
- this.executeCleanupTasks();
387
- const hasArgs = args.length > 0;
388
- const state = (() => {
389
- if (hasArgs) return args[0];
390
- if (this.stateCallback) return this.stateCallback();
391
- return this.state;
392
- })();
393
- const metadata = (() => {
394
- if (hasArgs) return args[1];
395
- if (this.metadataCallback) return this.metadataCallback();
396
- return this.metadata;
397
- })();
398
- this.setActualStateWithoutValidations(state, { forceUpdate: true });
399
- this.setMetadata(metadata);
400
- const extensionCleanup = (_b = (_a = this.onInit) == null ? void 0 : _a.call(this)) != null ? _b : null;
401
- if (isFunction(extensionCleanup)) this.cleanupFunctions.push(extensionCleanup);
402
- const { onInit: onInitFromConfig } = (_c = this.callbacks) != null ? _c : {};
403
- if (!onInitFromConfig) return;
404
- const onInitFromConfigStoreTools = (_e = (_d = this.__devtools_getLifeCycleStoreToolsWrapper) == null ? void 0 : _d.call(this, "config/onInit/")) != null ? _e : this.storeTools;
405
- const configCleanup = (_f = onInitFromConfig == null ? void 0 : onInitFromConfig(onInitFromConfigStoreTools)) != null ? _f : null;
406
- if (isFunction(configCleanup)) this.cleanupFunctions.push(configCleanup);
407
- }
408
- //#endregion DevTools extensions
409
- };
410
- function createObservable(selector, options) {
411
- const selectorName = options == null ? void 0 : options.name;
412
- const mainIsEqualRoot = options == null ? void 0 : options.isEqualRoot;
413
- const mainIsEqual = options == null ? void 0 : options.isEqual;
414
- let rootState = this.getState();
415
- let selectedState = (selector != null ? selector : (s) => s)(rootState);
416
- const childStore = new GlobalStore(selectedState, {
417
- name: selectorName != null ? selectorName : uniqueId("sh:")
418
- });
419
- const unsubscribeFromRootState = this.subscribe(
420
- (newRoot) => {
421
- const isRootEqual = (mainIsEqualRoot != null ? mainIsEqualRoot : Object.is)(rootState, newRoot);
422
- if (isRootEqual) return;
423
- rootState = newRoot;
424
- const selectedState$ = selector(newRoot);
425
- const isSelectedValueEqual = (mainIsEqual != null ? mainIsEqual : Object.is)(selectedState, selectedState$);
426
- if (isSelectedValueEqual) return;
427
- selectedState = selectedState$;
428
- childStore.setState(selectedState$);
429
- },
430
- { skipFirst: true }
431
- );
432
- const observable = childStore.subscribe.bind(childStore);
433
- const extensions = {
434
- getState: childStore.getState.bind(childStore),
435
- subscribe: childStore.subscribe.bind(childStore),
436
- createSelectorHook: createSelectorHook.bind(observable),
437
- createObservable: createObservable.bind(observable),
438
- dispose: () => {
439
- unsubscribeFromRootState();
440
- childStore.dispose();
441
- },
442
- subscribers: childStore.subscribers
443
- };
444
- Object.assign(observable, extensions);
445
- return observable;
446
- }
447
- function createSelectorHook(selector, options) {
448
- const selectorName = options == null ? void 0 : options.name;
449
- const mainIsEqualRoot = options == null ? void 0 : options.isEqualRoot;
450
- const mainIsEqual = options == null ? void 0 : options.isEqual;
451
- let rootState = this.getState();
452
- let selectedState = (selector != null ? selector : (s) => s)(rootState);
453
- const derivedStore = new GlobalStore(selectedState, {
454
- name: selectorName != null ? selectorName : uniqueId("sh:")
455
- });
456
- const unsubscribeFromRootState = this.subscribe(
457
- (newRoot) => {
458
- const isRootEqual = (mainIsEqualRoot != null ? mainIsEqualRoot : Object.is)(rootState, newRoot);
459
- if (isRootEqual) return;
460
- rootState = newRoot;
461
- const selectedState$ = selector(newRoot);
462
- const isSelectedValueEqual = (mainIsEqual != null ? mainIsEqual : Object.is)(selectedState, selectedState$);
463
- if (isSelectedValueEqual) return;
464
- selectedState = selectedState$;
465
- derivedStore.setState(selectedState$);
466
- },
467
- { skipFirst: true }
468
- );
469
- const selectorHook = (...args) => {
470
- const [selection] = derivedStore.use(...args);
471
- return selection;
472
- };
473
- const extensions = {
474
- getState: () => derivedStore.getState(),
475
- subscribe: derivedStore.subscribe.bind(derivedStore),
476
- createSelectorHook: createSelectorHook.bind(
477
- selectorHook
478
- ),
479
- createObservable: createObservable.bind(
480
- selectorHook
481
- ),
482
- dispose: () => {
483
- unsubscribeFromRootState();
484
- derivedStore.dispose();
485
- },
486
- subscribers: derivedStore.subscribers
487
- };
488
- Object.assign(selectorHook, extensions);
489
- return selectorHook;
490
- }
491
- var GlobalStore_default = GlobalStore;
492
- export {
493
- GlobalStore,
494
- createObservable,
495
- createSelectorHook,
496
- GlobalStore_default as default
497
- };
package/actions.cjs DELETED
@@ -1,61 +0,0 @@
1
- "use strict";
2
- var __create = Object.create;
3
- var __defProp = Object.defineProperty;
4
- var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
5
- var __getOwnPropNames = Object.getOwnPropertyNames;
6
- var __getProtoOf = Object.getPrototypeOf;
7
- var __hasOwnProp = Object.prototype.hasOwnProperty;
8
- var __export = (target, all) => {
9
- for (var name in all)
10
- __defProp(target, name, { get: all[name], enumerable: true });
11
- };
12
- var __copyProps = (to, from, except, desc) => {
13
- if (from && typeof from === "object" || typeof from === "function") {
14
- for (let key of __getOwnPropNames(from))
15
- if (!__hasOwnProp.call(to, key) && key !== except)
16
- __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
17
- }
18
- return to;
19
- };
20
- var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
21
- // If the importer is in node compatibility mode or this is not an ESM
22
- // file that has been converted to a CommonJS file using a Babel-
23
- // compatible transform (i.e. "__esModule" has not been set), then set
24
- // "default" to the CommonJS "module.exports" for node compatibility.
25
- isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
26
- mod
27
- ));
28
- var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
29
-
30
- // src/actions.ts
31
- var actions_exports = {};
32
- __export(actions_exports, {
33
- actions: () => actions,
34
- default: () => actions_default
35
- });
36
- module.exports = __toCommonJS(actions_exports);
37
- var import_GlobalStore = __toESM(require("./GlobalStore.cjs"));
38
- function actions(api, actions2) {
39
- const getStoreActionsMap = (api$, actions$) => {
40
- const newApi = {
41
- // prevents overriding api context
42
- setMetadata: (...args) => api$.setMetadata(...args),
43
- getMetadata: () => api$.getMetadata(),
44
- getState: () => api$.getState(),
45
- setState: (...args) => api$.setState(...args),
46
- subscribe: (...args) => api$.subscribe(...args),
47
- actionsConfig: actions$,
48
- actions: null
49
- };
50
- const { storeTools, actions: newActions } = import_GlobalStore.default.prototype.getStoreActionsMap.call(newApi);
51
- storeTools.actions = api$.actions;
52
- return newActions;
53
- };
54
- if (api && actions2) {
55
- return getStoreActionsMap(api, actions2);
56
- }
57
- return (actions$) => {
58
- return (api$) => getStoreActionsMap(api$, actions$);
59
- };
60
- }
61
- var actions_default = actions;
package/actions.d.ts DELETED
@@ -1,32 +0,0 @@
1
- import type { DerivedActionsBuilder, DerivedActionsConfig, StoreTools } from './types';
2
- /**
3
- * Creates a reusable action template builder.
4
- *
5
- * @example
6
- * const makeUserActions = actions<API>()({
7
- * syncUser(id) {
8
- * return (store) => {...}
9
- * }
10
- * });
11
- *
12
- * const { syncUser } = makeUserActions(api);
13
- */
14
- export declare function actions<Api extends StoreTools<any, any, any>>(): DerivedActionsBuilder<Api>;
15
- /**
16
- * Creates and action group from a config and binds it the provided store
17
- *
18
- * @example
19
- * const userActions = actions(api, {
20
- * syncUser(id) {
21
- * return (store) => {...}
22
- * }
23
- * });
24
- *
25
- * const { syncUser } = userActions;
26
- */
27
- export declare function actions<Api extends StoreTools<any, any, any>, Actions extends DerivedActionsConfig<Api>>(api: Api, actions: Actions): {
28
- [key in keyof Actions]: {
29
- (...params: Parameters<Actions[key]>): ReturnType<ReturnType<Actions[key]>>;
30
- };
31
- };
32
- export default actions;
package/actions.js DELETED
@@ -1,61 +0,0 @@
1
- "use strict";
2
- var __create = Object.create;
3
- var __defProp = Object.defineProperty;
4
- var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
5
- var __getOwnPropNames = Object.getOwnPropertyNames;
6
- var __getProtoOf = Object.getPrototypeOf;
7
- var __hasOwnProp = Object.prototype.hasOwnProperty;
8
- var __export = (target, all) => {
9
- for (var name in all)
10
- __defProp(target, name, { get: all[name], enumerable: true });
11
- };
12
- var __copyProps = (to, from, except, desc) => {
13
- if (from && typeof from === "object" || typeof from === "function") {
14
- for (let key of __getOwnPropNames(from))
15
- if (!__hasOwnProp.call(to, key) && key !== except)
16
- __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
17
- }
18
- return to;
19
- };
20
- var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
21
- // If the importer is in node compatibility mode or this is not an ESM
22
- // file that has been converted to a CommonJS file using a Babel-
23
- // compatible transform (i.e. "__esModule" has not been set), then set
24
- // "default" to the CommonJS "module.exports" for node compatibility.
25
- isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
26
- mod
27
- ));
28
- var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
29
-
30
- // src/actions.ts
31
- var actions_exports = {};
32
- __export(actions_exports, {
33
- actions: () => actions,
34
- default: () => actions_default
35
- });
36
- module.exports = __toCommonJS(actions_exports);
37
- var import_GlobalStore = __toESM(require("./GlobalStore.js"));
38
- function actions(api, actions2) {
39
- const getStoreActionsMap = (api$, actions$) => {
40
- const newApi = {
41
- // prevents overriding api context
42
- setMetadata: (...args) => api$.setMetadata(...args),
43
- getMetadata: () => api$.getMetadata(),
44
- getState: () => api$.getState(),
45
- setState: (...args) => api$.setState(...args),
46
- subscribe: (...args) => api$.subscribe(...args),
47
- actionsConfig: actions$,
48
- actions: null
49
- };
50
- const { storeTools, actions: newActions } = import_GlobalStore.default.prototype.getStoreActionsMap.call(newApi);
51
- storeTools.actions = api$.actions;
52
- return newActions;
53
- };
54
- if (api && actions2) {
55
- return getStoreActionsMap(api, actions2);
56
- }
57
- return (actions$) => {
58
- return (api$) => getStoreActionsMap(api$, actions$);
59
- };
60
- }
61
- var actions_default = actions;
package/actions.mjs DELETED
@@ -1,30 +0,0 @@
1
- // src/actions.ts
2
- import GlobalStore from "./GlobalStore.mjs";
3
- function actions(api, actions2) {
4
- const getStoreActionsMap = (api$, actions$) => {
5
- const newApi = {
6
- // prevents overriding api context
7
- setMetadata: (...args) => api$.setMetadata(...args),
8
- getMetadata: () => api$.getMetadata(),
9
- getState: () => api$.getState(),
10
- setState: (...args) => api$.setState(...args),
11
- subscribe: (...args) => api$.subscribe(...args),
12
- actionsConfig: actions$,
13
- actions: null
14
- };
15
- const { storeTools, actions: newActions } = GlobalStore.prototype.getStoreActionsMap.call(newApi);
16
- storeTools.actions = api$.actions;
17
- return newActions;
18
- };
19
- if (api && actions2) {
20
- return getStoreActionsMap(api, actions2);
21
- }
22
- return (actions$) => {
23
- return (api$) => getStoreActionsMap(api$, actions$);
24
- };
25
- }
26
- var actions_default = actions;
27
- export {
28
- actions,
29
- actions_default as default
30
- };