state-decorator 7.0.0-beta.9-dev → 7.0.0-rc2

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 (59) hide show
  1. package/README.md +55 -24
  2. package/development.d.ts +10 -0
  3. package/development.js +2 -0
  4. package/development.js.map +1 -0
  5. package/graph.d.ts +8 -0
  6. package/helpers.js +1 -174
  7. package/helpers.js.map +1 -1
  8. package/impl.d.ts +20 -32
  9. package/impl.js +1 -996
  10. package/impl.js.map +1 -1
  11. package/index.d.ts +10 -11
  12. package/index.js +1 -1044
  13. package/index.js.map +1 -1
  14. package/middlewares.d.ts +0 -23
  15. package/middlewares.js +1 -526
  16. package/middlewares.js.map +1 -1
  17. package/package.json +2 -2
  18. package/shallow.d.ts +0 -14
  19. package/test.d.ts +7 -21
  20. package/test.js +1 -358
  21. package/test.js.map +1 -1
  22. package/types.d.ts +73 -72
  23. package/v5.d.ts +5 -18
  24. package/v5.js +1 -322
  25. package/v5.js.map +1 -1
  26. package/v5_test.d.ts +4 -17
  27. package/v5_test.js +1 -416
  28. package/v5_test.js.map +1 -1
  29. package/v5_types.d.ts +15 -29
  30. package/v6.d.ts +7 -3
  31. package/v6.js +1 -120
  32. package/v6.js.map +1 -1
  33. package/es/helpers.d.ts +0 -66
  34. package/es/helpers.js +0 -163
  35. package/es/helpers.js.map +0 -1
  36. package/es/impl.d.ts +0 -209
  37. package/es/impl.js +0 -964
  38. package/es/impl.js.map +0 -1
  39. package/es/index.d.ts +0 -164
  40. package/es/index.js +0 -1014
  41. package/es/index.js.map +0 -1
  42. package/es/middlewares.d.ts +0 -26
  43. package/es/middlewares.js +0 -520
  44. package/es/middlewares.js.map +0 -1
  45. package/es/shallow.d.ts +0 -15
  46. package/es/test.d.ts +0 -136
  47. package/es/test.js +0 -350
  48. package/es/test.js.map +0 -1
  49. package/es/types.d.ts +0 -381
  50. package/es/v5.d.ts +0 -58
  51. package/es/v5.js +0 -302
  52. package/es/v5.js.map +0 -1
  53. package/es/v5_test.d.ts +0 -70
  54. package/es/v5_test.js +0 -393
  55. package/es/v5_test.js.map +0 -1
  56. package/es/v5_types.d.ts +0 -227
  57. package/es/v6.d.ts +0 -17
  58. package/es/v6.js +0 -97
  59. package/es/v6.js.map +0 -1
package/es/index.js DELETED
@@ -1,1014 +0,0 @@
1
- import e, { useMemo, useContext, useReducer, useRef, useLayoutEffect, useEffect } from 'react';
2
- import { createRef, isSimpleSyncAction, decorateSimpleSyncAction, isAsyncAction, decorateAsyncAction, computeAsyncActionInput, isSyncAction, decorateSyncAction, globalConfig, onPropChange, DEFAULT_PROMISE_ID, computeDerivedValues, buildOnMountInvocationContext, buildOnUnMountInvocationContext, isLoadingImpl, runMiddlewares, buildLoadingMap, buildErrorMap } from './impl';
3
- export { EffectError, ParallelActionError, setGlobalConfig } from './impl';
4
-
5
- /*! *****************************************************************************
6
- Copyright (c) Microsoft Corporation.
7
-
8
- Permission to use, copy, modify, and/or distribute this software for any
9
- purpose with or without fee is hereby granted.
10
-
11
- THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
12
- REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
13
- AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
14
- INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
15
- LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
16
- OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
17
- PERFORMANCE OF THIS SOFTWARE.
18
- ***************************************************************************** */
19
-
20
- var __assign = function() {
21
- __assign = Object.assign || function __assign(t) {
22
- for (var s, i = 1, n = arguments.length; i < n; i++) {
23
- s = arguments[i];
24
- for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p)) t[p] = s[p];
25
- }
26
- return t;
27
- };
28
- return __assign.apply(this, arguments);
29
- };
30
-
31
- function __rest(s, e) {
32
- var t = {};
33
- for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0)
34
- t[p] = s[p];
35
- if (s != null && typeof Object.getOwnPropertySymbols === "function")
36
- for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {
37
- if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i]))
38
- t[p[i]] = s[p[i]];
39
- }
40
- return t;
41
- }
42
-
43
- function __spreadArray(to, from, pack) {
44
- if (pack || arguments.length === 2) for (var i = 0, l = from.length, ar; i < l; i++) {
45
- if (ar || !(i in from)) {
46
- if (!ar) ar = Array.prototype.slice.call(from, 0, i);
47
- ar[i] = from[i];
48
- }
49
- }
50
- return to.concat(ar || Array.prototype.slice.call(from));
51
- }
52
-
53
- function createCommonjsModule(fn) {
54
- var module = { exports: {} };
55
- return fn(module, module.exports), module.exports;
56
- }
57
-
58
- /**
59
- * @license React
60
- * use-sync-external-store-shim.production.min.js
61
- *
62
- * Copyright (c) Facebook, Inc. and its affiliates.
63
- *
64
- * This source code is licensed under the MIT license found in the
65
- * LICENSE file in the root directory of this source tree.
66
- */
67
- function h(a,b){return a===b&&(0!==a||1/a===1/b)||a!==a&&b!==b}var k="function"===typeof Object.is?Object.is:h,l=e.useState,m=e.useEffect,n=e.useLayoutEffect,p$1=e.useDebugValue;function q$1(a,b){var d=b(),f=l({inst:{value:d,getSnapshot:b}}),c=f[0].inst,g=f[1];n(function(){c.value=d;c.getSnapshot=b;r$1(c)&&g({inst:c});},[a,d,b]);m(function(){r$1(c)&&g({inst:c});return a(function(){r$1(c)&&g({inst:c});})},[a]);p$1(d);return d}
68
- function r$1(a){var b=a.getSnapshot;a=a.value;try{var d=b();return !k(a,d)}catch(f){return !0}}function t$1(a,b){return b()}var u$1="undefined"===typeof window||"undefined"===typeof window.document||"undefined"===typeof window.document.createElement?t$1:q$1;var useSyncExternalStore=void 0!==e.useSyncExternalStore?e.useSyncExternalStore:u$1;
69
-
70
- var useSyncExternalStoreShim_production_min = {
71
- useSyncExternalStore: useSyncExternalStore
72
- };
73
-
74
- /**
75
- * @license React
76
- * use-sync-external-store-shim.development.js
77
- *
78
- * Copyright (c) Facebook, Inc. and its affiliates.
79
- *
80
- * This source code is licensed under the MIT license found in the
81
- * LICENSE file in the root directory of this source tree.
82
- */
83
-
84
- var useSyncExternalStoreShim_development = createCommonjsModule(function (module, exports) {
85
-
86
- if (process.env.NODE_ENV !== "production") {
87
- (function() {
88
-
89
- /* global __REACT_DEVTOOLS_GLOBAL_HOOK__ */
90
- if (
91
- typeof __REACT_DEVTOOLS_GLOBAL_HOOK__ !== 'undefined' &&
92
- typeof __REACT_DEVTOOLS_GLOBAL_HOOK__.registerInternalModuleStart ===
93
- 'function'
94
- ) {
95
- __REACT_DEVTOOLS_GLOBAL_HOOK__.registerInternalModuleStart(new Error());
96
- }
97
- var React = e;
98
-
99
- var ReactSharedInternals = React.__SECRET_INTERNALS_DO_NOT_USE_OR_YOU_WILL_BE_FIRED;
100
-
101
- function error(format) {
102
- {
103
- {
104
- for (var _len2 = arguments.length, args = new Array(_len2 > 1 ? _len2 - 1 : 0), _key2 = 1; _key2 < _len2; _key2++) {
105
- args[_key2 - 1] = arguments[_key2];
106
- }
107
-
108
- printWarning('error', format, args);
109
- }
110
- }
111
- }
112
-
113
- function printWarning(level, format, args) {
114
- // When changing this logic, you might want to also
115
- // update consoleWithStackDev.www.js as well.
116
- {
117
- var ReactDebugCurrentFrame = ReactSharedInternals.ReactDebugCurrentFrame;
118
- var stack = ReactDebugCurrentFrame.getStackAddendum();
119
-
120
- if (stack !== '') {
121
- format += '%s';
122
- args = args.concat([stack]);
123
- } // eslint-disable-next-line react-internal/safe-string-coercion
124
-
125
-
126
- var argsWithFormat = args.map(function (item) {
127
- return String(item);
128
- }); // Careful: RN currently depends on this prefix
129
-
130
- argsWithFormat.unshift('Warning: ' + format); // We intentionally don't use spread (or .apply) directly because it
131
- // breaks IE9: https://github.com/facebook/react/issues/13610
132
- // eslint-disable-next-line react-internal/no-production-logging
133
-
134
- Function.prototype.apply.call(console[level], console, argsWithFormat);
135
- }
136
- }
137
-
138
- /**
139
- * inlined Object.is polyfill to avoid requiring consumers ship their own
140
- * https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/is
141
- */
142
- function is(x, y) {
143
- return x === y && (x !== 0 || 1 / x === 1 / y) || x !== x && y !== y // eslint-disable-line no-self-compare
144
- ;
145
- }
146
-
147
- var objectIs = typeof Object.is === 'function' ? Object.is : is;
148
-
149
- // dispatch for CommonJS interop named imports.
150
-
151
- var useState = React.useState,
152
- useEffect = React.useEffect,
153
- useLayoutEffect = React.useLayoutEffect,
154
- useDebugValue = React.useDebugValue;
155
- var didWarnOld18Alpha = false;
156
- var didWarnUncachedGetSnapshot = false; // Disclaimer: This shim breaks many of the rules of React, and only works
157
- // because of a very particular set of implementation details and assumptions
158
- // -- change any one of them and it will break. The most important assumption
159
- // is that updates are always synchronous, because concurrent rendering is
160
- // only available in versions of React that also have a built-in
161
- // useSyncExternalStore API. And we only use this shim when the built-in API
162
- // does not exist.
163
- //
164
- // Do not assume that the clever hacks used by this hook also work in general.
165
- // The point of this shim is to replace the need for hacks by other libraries.
166
-
167
- function useSyncExternalStore(subscribe, getSnapshot, // Note: The shim does not use getServerSnapshot, because pre-18 versions of
168
- // React do not expose a way to check if we're hydrating. So users of the shim
169
- // will need to track that themselves and return the correct value
170
- // from `getSnapshot`.
171
- getServerSnapshot) {
172
- {
173
- if (!didWarnOld18Alpha) {
174
- if (React.startTransition !== undefined) {
175
- didWarnOld18Alpha = true;
176
-
177
- error('You are using an outdated, pre-release alpha of React 18 that ' + 'does not support useSyncExternalStore. The ' + 'use-sync-external-store shim will not work correctly. Upgrade ' + 'to a newer pre-release.');
178
- }
179
- }
180
- } // Read the current snapshot from the store on every render. Again, this
181
- // breaks the rules of React, and only works here because of specific
182
- // implementation details, most importantly that updates are
183
- // always synchronous.
184
-
185
-
186
- var value = getSnapshot();
187
-
188
- {
189
- if (!didWarnUncachedGetSnapshot) {
190
- var cachedValue = getSnapshot();
191
-
192
- if (!objectIs(value, cachedValue)) {
193
- error('The result of getSnapshot should be cached to avoid an infinite loop');
194
-
195
- didWarnUncachedGetSnapshot = true;
196
- }
197
- }
198
- } // Because updates are synchronous, we don't queue them. Instead we force a
199
- // re-render whenever the subscribed state changes by updating an some
200
- // arbitrary useState hook. Then, during render, we call getSnapshot to read
201
- // the current value.
202
- //
203
- // Because we don't actually use the state returned by the useState hook, we
204
- // can save a bit of memory by storing other stuff in that slot.
205
- //
206
- // To implement the early bailout, we need to track some things on a mutable
207
- // object. Usually, we would put that in a useRef hook, but we can stash it in
208
- // our useState hook instead.
209
- //
210
- // To force a re-render, we call forceUpdate({inst}). That works because the
211
- // new object always fails an equality check.
212
-
213
-
214
- var _useState = useState({
215
- inst: {
216
- value: value,
217
- getSnapshot: getSnapshot
218
- }
219
- }),
220
- inst = _useState[0].inst,
221
- forceUpdate = _useState[1]; // Track the latest getSnapshot function with a ref. This needs to be updated
222
- // in the layout phase so we can access it during the tearing check that
223
- // happens on subscribe.
224
-
225
-
226
- useLayoutEffect(function () {
227
- inst.value = value;
228
- inst.getSnapshot = getSnapshot; // Whenever getSnapshot or subscribe changes, we need to check in the
229
- // commit phase if there was an interleaved mutation. In concurrent mode
230
- // this can happen all the time, but even in synchronous mode, an earlier
231
- // effect may have mutated the store.
232
-
233
- if (checkIfSnapshotChanged(inst)) {
234
- // Force a re-render.
235
- forceUpdate({
236
- inst: inst
237
- });
238
- }
239
- }, [subscribe, value, getSnapshot]);
240
- useEffect(function () {
241
- // Check for changes right before subscribing. Subsequent changes will be
242
- // detected in the subscription handler.
243
- if (checkIfSnapshotChanged(inst)) {
244
- // Force a re-render.
245
- forceUpdate({
246
- inst: inst
247
- });
248
- }
249
-
250
- var handleStoreChange = function () {
251
- // TODO: Because there is no cross-renderer API for batching updates, it's
252
- // up to the consumer of this library to wrap their subscription event
253
- // with unstable_batchedUpdates. Should we try to detect when this isn't
254
- // the case and print a warning in development?
255
- // The store changed. Check if the snapshot changed since the last time we
256
- // read from the store.
257
- if (checkIfSnapshotChanged(inst)) {
258
- // Force a re-render.
259
- forceUpdate({
260
- inst: inst
261
- });
262
- }
263
- }; // Subscribe to the store and return a clean-up function.
264
-
265
-
266
- return subscribe(handleStoreChange);
267
- }, [subscribe]);
268
- useDebugValue(value);
269
- return value;
270
- }
271
-
272
- function checkIfSnapshotChanged(inst) {
273
- var latestGetSnapshot = inst.getSnapshot;
274
- var prevValue = inst.value;
275
-
276
- try {
277
- var nextValue = latestGetSnapshot();
278
- return !objectIs(prevValue, nextValue);
279
- } catch (error) {
280
- return true;
281
- }
282
- }
283
-
284
- function useSyncExternalStore$1(subscribe, getSnapshot, getServerSnapshot) {
285
- // Note: The shim does not use getServerSnapshot, because pre-18 versions of
286
- // React do not expose a way to check if we're hydrating. So users of the shim
287
- // will need to track that themselves and return the correct value
288
- // from `getSnapshot`.
289
- return getSnapshot();
290
- }
291
-
292
- var canUseDOM = !!(typeof window !== 'undefined' && typeof window.document !== 'undefined' && typeof window.document.createElement !== 'undefined');
293
-
294
- var isServerEnvironment = !canUseDOM;
295
-
296
- var shim = isServerEnvironment ? useSyncExternalStore$1 : useSyncExternalStore;
297
- var useSyncExternalStore$2 = React.useSyncExternalStore !== undefined ? React.useSyncExternalStore : shim;
298
-
299
- exports.useSyncExternalStore = useSyncExternalStore$2;
300
- /* global __REACT_DEVTOOLS_GLOBAL_HOOK__ */
301
- if (
302
- typeof __REACT_DEVTOOLS_GLOBAL_HOOK__ !== 'undefined' &&
303
- typeof __REACT_DEVTOOLS_GLOBAL_HOOK__.registerInternalModuleStop ===
304
- 'function'
305
- ) {
306
- __REACT_DEVTOOLS_GLOBAL_HOOK__.registerInternalModuleStop(new Error());
307
- }
308
-
309
- })();
310
- }
311
- });
312
-
313
- var shim = createCommonjsModule(function (module) {
314
-
315
- if (process.env.NODE_ENV === 'production') {
316
- module.exports = useSyncExternalStoreShim_production_min;
317
- } else {
318
- module.exports = useSyncExternalStoreShim_development;
319
- }
320
- });
321
-
322
- /**
323
- * @license React
324
- * use-sync-external-store-shim/with-selector.production.min.js
325
- *
326
- * Copyright (c) Facebook, Inc. and its affiliates.
327
- *
328
- * This source code is licensed under the MIT license found in the
329
- * LICENSE file in the root directory of this source tree.
330
- */
331
- function p(a,b){return a===b&&(0!==a||1/a===1/b)||a!==a&&b!==b}var q="function"===typeof Object.is?Object.is:p,r=shim.useSyncExternalStore,t=e.useRef,u=e.useEffect,v=e.useMemo,w=e.useDebugValue;
332
- var useSyncExternalStoreWithSelector$1=function(a,b,e,l,g){var c=t(null);if(null===c.current){var f={hasValue:!1,value:null};c.current=f;}else f=c.current;c=v(function(){function a(a){if(!c){c=!0;d=a;a=l(a);if(void 0!==g&&f.hasValue){var b=f.value;if(g(b,a))return k=b}return k=a}b=k;if(q(d,a))return b;var e=l(a);if(void 0!==g&&g(b,e))return b;d=a;return k=e}var c=!1,d,k,m=void 0===e?null:e;return [function(){return a(b())},null===m?void 0:function(){return a(m())}]},[b,e,l,g]);var d=r(a,c[0],c[1]);
333
- u(function(){f.hasValue=!0;f.value=d;},[d]);w(d);return d};
334
-
335
- var withSelector_production_min = {
336
- useSyncExternalStoreWithSelector: useSyncExternalStoreWithSelector$1
337
- };
338
-
339
- /**
340
- * @license React
341
- * use-sync-external-store-shim/with-selector.development.js
342
- *
343
- * Copyright (c) Facebook, Inc. and its affiliates.
344
- *
345
- * This source code is licensed under the MIT license found in the
346
- * LICENSE file in the root directory of this source tree.
347
- */
348
-
349
- var withSelector_development = createCommonjsModule(function (module, exports) {
350
-
351
- if (process.env.NODE_ENV !== "production") {
352
- (function() {
353
-
354
- /* global __REACT_DEVTOOLS_GLOBAL_HOOK__ */
355
- if (
356
- typeof __REACT_DEVTOOLS_GLOBAL_HOOK__ !== 'undefined' &&
357
- typeof __REACT_DEVTOOLS_GLOBAL_HOOK__.registerInternalModuleStart ===
358
- 'function'
359
- ) {
360
- __REACT_DEVTOOLS_GLOBAL_HOOK__.registerInternalModuleStart(new Error());
361
- }
362
- var React = e;
363
- var shim$1 = shim;
364
-
365
- /**
366
- * inlined Object.is polyfill to avoid requiring consumers ship their own
367
- * https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/is
368
- */
369
- function is(x, y) {
370
- return x === y && (x !== 0 || 1 / x === 1 / y) || x !== x && y !== y // eslint-disable-line no-self-compare
371
- ;
372
- }
373
-
374
- var objectIs = typeof Object.is === 'function' ? Object.is : is;
375
-
376
- var useSyncExternalStore = shim$1.useSyncExternalStore;
377
-
378
- // for CommonJS interop.
379
-
380
- var useRef = React.useRef,
381
- useEffect = React.useEffect,
382
- useMemo = React.useMemo,
383
- useDebugValue = React.useDebugValue; // Same as useSyncExternalStore, but supports selector and isEqual arguments.
384
-
385
- function useSyncExternalStoreWithSelector(subscribe, getSnapshot, getServerSnapshot, selector, isEqual) {
386
- // Use this to track the rendered snapshot.
387
- var instRef = useRef(null);
388
- var inst;
389
-
390
- if (instRef.current === null) {
391
- inst = {
392
- hasValue: false,
393
- value: null
394
- };
395
- instRef.current = inst;
396
- } else {
397
- inst = instRef.current;
398
- }
399
-
400
- var _useMemo = useMemo(function () {
401
- // Track the memoized state using closure variables that are local to this
402
- // memoized instance of a getSnapshot function. Intentionally not using a
403
- // useRef hook, because that state would be shared across all concurrent
404
- // copies of the hook/component.
405
- var hasMemo = false;
406
- var memoizedSnapshot;
407
- var memoizedSelection;
408
-
409
- var memoizedSelector = function (nextSnapshot) {
410
- if (!hasMemo) {
411
- // The first time the hook is called, there is no memoized result.
412
- hasMemo = true;
413
- memoizedSnapshot = nextSnapshot;
414
-
415
- var _nextSelection = selector(nextSnapshot);
416
-
417
- if (isEqual !== undefined) {
418
- // Even if the selector has changed, the currently rendered selection
419
- // may be equal to the new selection. We should attempt to reuse the
420
- // current value if possible, to preserve downstream memoizations.
421
- if (inst.hasValue) {
422
- var currentSelection = inst.value;
423
-
424
- if (isEqual(currentSelection, _nextSelection)) {
425
- memoizedSelection = currentSelection;
426
- return currentSelection;
427
- }
428
- }
429
- }
430
-
431
- memoizedSelection = _nextSelection;
432
- return _nextSelection;
433
- } // We may be able to reuse the previous invocation's result.
434
-
435
-
436
- // We may be able to reuse the previous invocation's result.
437
- var prevSnapshot = memoizedSnapshot;
438
- var prevSelection = memoizedSelection;
439
-
440
- if (objectIs(prevSnapshot, nextSnapshot)) {
441
- // The snapshot is the same as last time. Reuse the previous selection.
442
- return prevSelection;
443
- } // The snapshot has changed, so we need to compute a new selection.
444
-
445
-
446
- // The snapshot has changed, so we need to compute a new selection.
447
- var nextSelection = selector(nextSnapshot); // If a custom isEqual function is provided, use that to check if the data
448
- // has changed. If it hasn't, return the previous selection. That signals
449
- // to React that the selections are conceptually equal, and we can bail
450
- // out of rendering.
451
-
452
- // If a custom isEqual function is provided, use that to check if the data
453
- // has changed. If it hasn't, return the previous selection. That signals
454
- // to React that the selections are conceptually equal, and we can bail
455
- // out of rendering.
456
- if (isEqual !== undefined && isEqual(prevSelection, nextSelection)) {
457
- return prevSelection;
458
- }
459
-
460
- memoizedSnapshot = nextSnapshot;
461
- memoizedSelection = nextSelection;
462
- return nextSelection;
463
- }; // Assigning this to a constant so that Flow knows it can't change.
464
-
465
-
466
- // Assigning this to a constant so that Flow knows it can't change.
467
- var maybeGetServerSnapshot = getServerSnapshot === undefined ? null : getServerSnapshot;
468
-
469
- var getSnapshotWithSelector = function () {
470
- return memoizedSelector(getSnapshot());
471
- };
472
-
473
- var getServerSnapshotWithSelector = maybeGetServerSnapshot === null ? undefined : function () {
474
- return memoizedSelector(maybeGetServerSnapshot());
475
- };
476
- return [getSnapshotWithSelector, getServerSnapshotWithSelector];
477
- }, [getSnapshot, getServerSnapshot, selector, isEqual]),
478
- getSelection = _useMemo[0],
479
- getServerSelection = _useMemo[1];
480
-
481
- var value = useSyncExternalStore(subscribe, getSelection, getServerSelection);
482
- useEffect(function () {
483
- inst.hasValue = true;
484
- inst.value = value;
485
- }, [value]);
486
- useDebugValue(value);
487
- return value;
488
- }
489
-
490
- exports.useSyncExternalStoreWithSelector = useSyncExternalStoreWithSelector;
491
- /* global __REACT_DEVTOOLS_GLOBAL_HOOK__ */
492
- if (
493
- typeof __REACT_DEVTOOLS_GLOBAL_HOOK__ !== 'undefined' &&
494
- typeof __REACT_DEVTOOLS_GLOBAL_HOOK__.registerInternalModuleStop ===
495
- 'function'
496
- ) {
497
- __REACT_DEVTOOLS_GLOBAL_HOOK__.registerInternalModuleStop(new Error());
498
- }
499
-
500
- })();
501
- }
502
- });
503
-
504
- var withSelector = createCommonjsModule(function (module) {
505
-
506
- if (process.env.NODE_ENV === 'production') {
507
- module.exports = withSelector_production_min;
508
- } else {
509
- module.exports = withSelector_development;
510
- }
511
- });
512
-
513
- /*! *****************************************************************************
514
- Copyright (c) GlobalSport SAS.
515
-
516
- Permission to use, copy, modify, and/or distribute this software for any
517
- purpose with or without fee is hereby granted.
518
-
519
- THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
520
- REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
521
- AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
522
- INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
523
- LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
524
- OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
525
- PERFORMANCE OF THIS SOFTWARE.
526
- ***************************************************************************** */
527
- /**
528
- * The State decorator can manage only one asynchronous action of same name at a time.
529
- * This enum is enumertaing all the policies in such case.
530
- */
531
- var ConflictPolicy;
532
- (function (ConflictPolicy) {
533
- /**
534
- * Reject the promise.
535
- */
536
- ConflictPolicy["REJECT"] = "reject";
537
- /**
538
- * Return a resolved promise with no result.
539
- */
540
- ConflictPolicy["IGNORE"] = "ignore";
541
- /**
542
- * Execute only the last call that conflicts after the one ongoing.
543
- */
544
- ConflictPolicy["KEEP_LAST"] = "keepLast";
545
- /**
546
- * Execute all calls one after another.
547
- */
548
- ConflictPolicy["KEEP_ALL"] = "keepAll";
549
- /**
550
- * Execute all calls one after another.
551
- * @see getPromiseId
552
- */
553
- ConflictPolicy["PARALLEL"] = "parallel";
554
- /**
555
- * Reuse the ongoing promise if any and if possible (same arguments). Only for GET requests!
556
- */
557
- ConflictPolicy["REUSE"] = "reuse";
558
- })(ConflictPolicy || (ConflictPolicy = {}));
559
-
560
- var useSyncExternalStoreWithSelector = withSelector.useSyncExternalStoreWithSelector;
561
- // =============================
562
- //
563
- // STORE
564
- //
565
- // =============================
566
- /**
567
- * Create a new store.
568
- * @param config Store configuration
569
- * @returns a store.
570
- */
571
- function createStore(config) {
572
- var stateRef = createRef();
573
- var derivedStateRef = createRef();
574
- var propsRef = createRef();
575
- var actionsRef = createRef();
576
- var timeoutRef = createRef();
577
- var loadingParallelMapRef = createRef();
578
- var loadingMapRef = createRef();
579
- var loadingRef = createRef();
580
- var errorMapRef = createRef();
581
- var promisesRef = createRef();
582
- var conflictActionsRef = createRef();
583
- var initializedRef = createRef(false);
584
- var snapshotRef = createRef();
585
- var middlewaresRef = createRef([]);
586
- var getInitialState = config.getInitialState, actionsImpl = config.actions, _a = config.middlewares, middlewares = _a === void 0 ? [] : _a, options = __rest(config, ["getInitialState", "actions", "middlewares"]);
587
- var stateListeners = {};
588
- var stateListenerCount = 0;
589
- function addStateListener(listener) {
590
- var id = "".concat(stateListenerCount++);
591
- stateListeners[id] = listener;
592
- return function () {
593
- delete stateListeners[id];
594
- };
595
- }
596
- function notifyStateListeners() {
597
- updateSnapshot();
598
- Object.keys(stateListeners).forEach(function (listenerId) {
599
- stateListeners[listenerId]();
600
- });
601
- }
602
- function setProps(p) {
603
- var oldProps = propsRef.current;
604
- propsRef.current = p;
605
- if (!initializedRef.current) {
606
- init(p);
607
- }
608
- else {
609
- onPropChange(stateRef, derivedStateRef, propsRef, oldProps, actionsRef, options, getInitialState, setState, false, false);
610
- }
611
- }
612
- /** Middleware only: sets the internal state, recompute derived state and notify listeners */
613
- function setInternalState(s) {
614
- stateRef.current = s;
615
- computeDerivedValues(stateRef, propsRef, derivedStateRef, options);
616
- notifyStateListeners();
617
- }
618
- function setInternalLoadingMap(map) {
619
- loadingParallelMapRef.current = map;
620
- loadingMapRef.current = getLoadingMap();
621
- loadingRef.current = isAnyLoading();
622
- }
623
- function init(p) {
624
- var _a;
625
- if (!initializedRef.current) {
626
- propsRef.current = p;
627
- initializedRef.current = true;
628
- stateRef.current = typeof getInitialState === 'function' ? getInitialState(p) : getInitialState;
629
- var initialActionSet_1 = new Set(options === null || options === void 0 ? void 0 : options.initialActionsMarkedLoading);
630
- var loadingMap = Object.keys(actionsImpl).reduce(function (acc, name) {
631
- var _a;
632
- acc[name] = initialActionSet_1.has(name) ? (_a = {}, _a[DEFAULT_PROMISE_ID] = true, _a) : {};
633
- return acc;
634
- }, {});
635
- setInternalLoadingMap(loadingMap);
636
- errorMapRef.current = Object.keys(actionsImpl).reduce(function (acc, name) {
637
- acc[name] = {};
638
- return acc;
639
- }, {});
640
- timeoutRef.current = {};
641
- promisesRef.current = {};
642
- conflictActionsRef.current = {};
643
- derivedStateRef.current = {
644
- state: null,
645
- deps: {},
646
- };
647
- // init middlewares
648
- middlewaresRef.current = [];
649
- var ctx_1 = {
650
- options: options,
651
- get state() {
652
- return stateRef.current;
653
- },
654
- actions: actionsImpl,
655
- setState: setInternalState,
656
- };
657
- var mapper = function (m) {
658
- var _a;
659
- var middleware = m();
660
- middlewaresRef.current.push(middleware);
661
- (_a = middleware.init) === null || _a === void 0 ? void 0 : _a.call(middleware, ctx_1);
662
- };
663
- (_a = globalConfig.defaultMiddlewares) === null || _a === void 0 ? void 0 : _a.forEach(mapper);
664
- middlewares === null || middlewares === void 0 ? void 0 : middlewares.forEach(mapper);
665
- // end init
666
- computeDerivedValues(stateRef, propsRef, derivedStateRef, options);
667
- // manage PropsChange with onMount
668
- onPropChange(stateRef, derivedStateRef, propsRef, null, actionsRef, options, getInitialState, setState, true, false);
669
- if (options === null || options === void 0 ? void 0 : options.onMount) {
670
- options.onMount(buildOnMountInvocationContext(stateRef, derivedStateRef, propsRef, actionsRef));
671
- }
672
- notifyStateListeners();
673
- }
674
- }
675
- function clearError(actionName, promiseId) {
676
- if (promiseId === void 0) { promiseId = DEFAULT_PROMISE_ID; }
677
- var map = errorMapRef.current[actionName];
678
- if (map === null || map === void 0 ? void 0 : map[promiseId]) {
679
- delete map[promiseId];
680
- }
681
- notifyStateListeners();
682
- }
683
- function invokeOnMountDeferred() {
684
- // manage PropsChange with onMount
685
- onPropChange(stateRef, derivedStateRef, propsRef, null, actionsRef, options, getInitialState, setState, true, true);
686
- if (options === null || options === void 0 ? void 0 : options.onMountDeferred) {
687
- options.onMountDeferred(buildOnMountInvocationContext(stateRef, derivedStateRef, propsRef, actionsRef));
688
- }
689
- }
690
- function abortActionsOnDestroy() {
691
- var abortedActions = Object.keys(actionsImpl).reduce(function (acc, actionName) {
692
- var action = actionsImpl[actionName];
693
- if (isAsyncAction(action) && action.abortable) {
694
- var map_1 = loadingParallelMapRef.current[actionName];
695
- var aborted = Object.keys(map_1).reduce(function (acc, promiseId) {
696
- if (map_1[promiseId]) {
697
- // ongoing action
698
- abortAction(actionName, promiseId);
699
- acc.push(promiseId);
700
- }
701
- return acc;
702
- }, []);
703
- if (aborted.length > 0) {
704
- acc[actionName] = aborted;
705
- }
706
- }
707
- return acc;
708
- }, {});
709
- return abortedActions;
710
- }
711
- function destroy() {
712
- var _a;
713
- if (initializedRef.current) {
714
- var abortedActions = abortActionsOnDestroy();
715
- (_a = options === null || options === void 0 ? void 0 : options.onUnmount) === null || _a === void 0 ? void 0 : _a.call(options, buildOnUnMountInvocationContext(stateRef, derivedStateRef, propsRef, abortedActions));
716
- propsRef.current = null;
717
- stateRef.current = null;
718
- loadingParallelMapRef.current = null;
719
- loadingMapRef.current = null;
720
- loadingRef.current = null;
721
- errorMapRef.current = null;
722
- promisesRef.current = null;
723
- conflictActionsRef.current = null;
724
- derivedStateRef.current = null;
725
- snapshotRef.current = null;
726
- Object.keys(timeoutRef.current).forEach(function (name) {
727
- clearTimeout(timeoutRef.current[name]);
728
- });
729
- timeoutRef.current = null;
730
- stateListeners = {};
731
- initializedRef.current = false;
732
- middlewaresRef.current.forEach(function (m) {
733
- var _a;
734
- (_a = m.destroy) === null || _a === void 0 ? void 0 : _a.call(m);
735
- });
736
- middlewaresRef.current = [];
737
- }
738
- }
739
- function setState(newStateIn, newLoadingMapIn, actionName, actionType, isAsync, actionCtx, propsChanged, init) {
740
- if (propsChanged === void 0) { propsChanged = false; }
741
- if (init === void 0) { init = false; }
742
- var hasChanged = false;
743
- if (!initializedRef.current) {
744
- return;
745
- }
746
- var newLoadingMap = newLoadingMapIn;
747
- var oldLoading = isAsync
748
- ? isLoadingImpl(newLoadingMap || loadingParallelMapRef.current, [actionName, actionCtx === null || actionCtx === void 0 ? void 0 : actionCtx.promiseId])
749
- : false;
750
- var _a = runMiddlewares(middlewaresRef.current, stateRef.current, newStateIn, oldLoading, actionName, actionType, isAsync, actionCtx), newState = _a.newState, newLoading = _a.newLoading;
751
- if (newState != null) {
752
- hasChanged = true;
753
- stateRef.current = newState;
754
- }
755
- if (oldLoading !== newLoading) {
756
- newLoadingMap = buildLoadingMap(newLoadingMap, actionName, (actionCtx === null || actionCtx === void 0 ? void 0 : actionCtx.promiseId) || DEFAULT_PROMISE_ID, newLoading);
757
- if (Object.keys(newLoadingMap).length === 0) {
758
- newLoadingMap = undefined;
759
- }
760
- }
761
- if (newLoadingMap != null) {
762
- hasChanged = true;
763
- setInternalLoadingMap(newLoadingMap);
764
- }
765
- if (hasChanged) {
766
- computeDerivedValues(stateRef, propsRef, derivedStateRef, options);
767
- if (!init) {
768
- stateRef.current = newState || stateRef.current;
769
- notifyStateListeners();
770
- }
771
- }
772
- else if (propsChanged) {
773
- var derivedChanged = computeDerivedValues(stateRef, propsRef, derivedStateRef, options);
774
- if (derivedChanged && !init) {
775
- notifyStateListeners();
776
- }
777
- }
778
- }
779
- var keys = Object.keys(actionsImpl);
780
- actionsRef.current = keys.reduce(function (acc, actionName) {
781
- var action = actionsImpl[actionName];
782
- if (isSimpleSyncAction(action)) {
783
- acc[actionName] = decorateSimpleSyncAction(actionName, action, stateRef, derivedStateRef, propsRef, initializedRef, setState);
784
- }
785
- else if (isAsyncAction(action)) {
786
- acc[actionName] = decorateAsyncAction({
787
- actionName: actionName,
788
- stateRef: stateRef,
789
- derivedStateRef: derivedStateRef,
790
- propsRef: propsRef,
791
- loadingParallelMapRef: loadingParallelMapRef,
792
- errorMapRef: errorMapRef,
793
- actionsRef: actionsRef,
794
- promisesRef: promisesRef,
795
- conflictActionsRef: conflictActionsRef,
796
- initializedRef: initializedRef,
797
- options: options,
798
- setState: setState,
799
- clearError: clearError,
800
- action: computeAsyncActionInput(action),
801
- });
802
- }
803
- else if (isSyncAction(action)) {
804
- acc[actionName] = decorateSyncAction(actionName, action, stateRef, derivedStateRef, propsRef, actionsRef, initializedRef, timeoutRef, options, setState, clearError);
805
- }
806
- return acc;
807
- }, {});
808
- function isLoading() {
809
- var props = [];
810
- for (var _i = 0; _i < arguments.length; _i++) {
811
- props[_i] = arguments[_i];
812
- }
813
- return isLoadingImpl.apply(void 0, __spreadArray([loadingParallelMapRef.current], props, false));
814
- }
815
- function abortAction(actionName, promiseId) {
816
- var _a, _b;
817
- if (promiseId === void 0) { promiseId = DEFAULT_PROMISE_ID; }
818
- var abortController = (_b = (_a = promisesRef.current[actionName]) === null || _a === void 0 ? void 0 : _a[promiseId]) === null || _b === void 0 ? void 0 : _b.abortController;
819
- var res = false;
820
- if (abortController) {
821
- abortController.abort();
822
- res = true;
823
- }
824
- return res;
825
- }
826
- function isAnyLoading() {
827
- if (initializedRef.current) {
828
- var loadingMap_1 = loadingParallelMapRef.current;
829
- return (Object.keys(loadingMap_1).find(function (actionName) {
830
- var promises = loadingMap_1[actionName];
831
- return Object.keys(promises).find(function (pId) { return !!promises[pId]; });
832
- }) != null);
833
- }
834
- return null;
835
- }
836
- function getLoadingMap() {
837
- if (initializedRef.current) {
838
- var map_2 = loadingParallelMapRef.current;
839
- var keys_1 = Object.keys(map_2);
840
- return keys_1.reduce(function (acc, actionName) {
841
- var subMap = map_2[actionName];
842
- acc[actionName] = Object.keys(subMap).find(function (pId) { return !!subMap[pId]; }) != null;
843
- return acc;
844
- }, {});
845
- }
846
- return null;
847
- }
848
- function getErrorMap() {
849
- if (initializedRef.current) {
850
- return buildErrorMap(errorMapRef.current);
851
- }
852
- return null;
853
- }
854
- function getConfig() {
855
- return __assign({}, config);
856
- }
857
- function updateSnapshot() {
858
- snapshotRef.current = __assign(__assign(__assign(__assign({}, actionsRef.current), stateRef.current), derivedStateRef.current.state), { isLoading: isLoading, abortAction: abortAction, loading: loadingRef.current, loadingMap: loadingMapRef.current, loadingParallelMap: loadingParallelMapRef.current, errorMap: getErrorMap(), errorParallelMap: errorMapRef.current });
859
- }
860
- function getSnapshot() {
861
- return snapshotRef.current;
862
- }
863
- return {
864
- getConfig: getConfig,
865
- setProps: setProps,
866
- addStateListener: addStateListener,
867
- init: init,
868
- destroy: destroy,
869
- isLoading: isLoading,
870
- abortAction: abortAction,
871
- clearError: clearError,
872
- getSnapshot: getSnapshot,
873
- invokeOnMountDeferred: invokeOnMountDeferred,
874
- get actions() {
875
- return actionsRef.current;
876
- },
877
- get state() {
878
- if (initializedRef.current) {
879
- return __assign(__assign({}, stateRef.current), derivedStateRef.current.state);
880
- }
881
- return null;
882
- },
883
- get loading() {
884
- return loadingRef.current;
885
- },
886
- get loadingMap() {
887
- return loadingMapRef.current;
888
- },
889
- get errorMap() {
890
- return getErrorMap();
891
- },
892
- get loadingParallelMap() {
893
- if (initializedRef.current) {
894
- return loadingParallelMapRef.current;
895
- }
896
- return null;
897
- },
898
- get errorParallelMap() {
899
- if (initializedRef.current) {
900
- return errorMapRef.current;
901
- }
902
- return null;
903
- },
904
- };
905
- }
906
- function useStoreSlice(store, funcOrArr) {
907
- var slicerFunc = useMemo(function () { return (funcOrArr instanceof Function ? funcOrArr : function (ctx) { return pick(ctx, funcOrArr); }); }, [funcOrArr]);
908
- return useSyncExternalStoreWithSelector(store.addStateListener, store.getSnapshot, store.getSnapshot, slicerFunc, globalConfig.comparator);
909
- }
910
- function useStoreContextSlice(storeContext, funcOrArr) {
911
- var store = useContext(storeContext);
912
- return useStoreSlice(store, funcOrArr);
913
- }
914
- /**
915
- * Binds the store to this component.
916
- * Store will NOT be destroyed when component is unmouted.
917
- * Props passed to actions will come from this components.
918
- * If store is configured to react of props changes, it will used passed props.
919
- * The component will be refreshed for every change in the store.
920
- *
921
- * @param store The store to listen to.
922
- * @param props The parent component props
923
- * @param refreshOnUpdate Whether refresh the component if store state changes.
924
- * @returns The store.
925
- */
926
- function useStore(store, props, refreshOnUpdate) {
927
- if (props === void 0) { props = null; }
928
- if (refreshOnUpdate === void 0) { refreshOnUpdate = true; }
929
- var _a = useReducer(function (s) { return (s > 100 ? 0 : s + 1); }, 0), forceRefresh = _a[1];
930
- var storeRef = useRef(store);
931
- store.setProps(props);
932
- useLayoutEffect(function () {
933
- // In development mode, React is calling in order:
934
- // - render
935
- // - useLayoutEffect
936
- // - useEffect
937
- // - useLayoutEffect => delete callback
938
- // - useEffect => delete callback
939
- // - useLayoutEffect
940
- // - useEffect
941
- //
942
- // Here we enforce a initialization of the store after a destroy in the useEffect delete callback
943
- store.init(props);
944
- }, []);
945
- useLayoutEffect(function () {
946
- if (refreshOnUpdate) {
947
- return storeRef.current.addStateListener(function () {
948
- forceRefresh();
949
- });
950
- }
951
- return undefined;
952
- }, [refreshOnUpdate]);
953
- return store;
954
- }
955
- /**
956
- * Creates and manages a local store.
957
- * The component will be refreshed for every change in the store.
958
- * Store will NOT be destroyed when component is unmouted.
959
- *
960
- * @param getInitialState Function to compute initial state from props.
961
- * @param actionImpl Actions implementation.
962
- * @param props Owner component props to update state or react on prop changes
963
- * @param options The store options.
964
- * @param refreshOnUpdate Whether refresh the component if store state changes.
965
- * @returns The state, actions and isLoading function.
966
- */
967
- function useLocalStore(config, props, refreshOnUpdate) {
968
- if (refreshOnUpdate === void 0) { refreshOnUpdate = true; }
969
- var storeRef = useRef();
970
- if (storeRef.current == null) {
971
- storeRef.current = createStore(config);
972
- }
973
- useEffect(function () {
974
- storeRef.current.invokeOnMountDeferred();
975
- return function () {
976
- var _a;
977
- (_a = storeRef.current) === null || _a === void 0 ? void 0 : _a.destroy();
978
- };
979
- }, []);
980
- return useStore(storeRef.current, props, refreshOnUpdate);
981
- }
982
- // ------------- UTILS
983
- /**
984
- * Creates a new object by picking properties to the object passed a first parameter
985
- * @param obj Object to pick properties from
986
- * @param props Property names
987
- * @returns a new object containing only picked properties
988
- */
989
- function pick(obj, props) {
990
- return props.reduce(function (acc, p) {
991
- if (obj.hasOwnProperty(p)) {
992
- acc[p] = obj[p];
993
- }
994
- return acc;
995
- }, {});
996
- }
997
- /**
998
- * Returns a function that creates a new object by picking properties to the object passed as first parameter.
999
- * To be used in conjunction of <code>useStoreSlice</code> like in example:
1000
- * const s = useStoreSlice(myStore, slice('list', 'addItem'));
1001
- * @param props Property names
1002
- * @returns a function to pick property from an passed object.
1003
- */
1004
- function slice() {
1005
- var props = [];
1006
- for (var _i = 0; _i < arguments.length; _i++) {
1007
- props[_i] = arguments[_i];
1008
- }
1009
- return function (store) { return pick(store, props); };
1010
- }
1011
-
1012
- export default useLocalStore;
1013
- export { ConflictPolicy, createStore, pick, slice, useLocalStore, useStore, useStoreContextSlice, useStoreSlice };
1014
- //# sourceMappingURL=index.js.map