@dr.pogodin/react-global-state 0.9.3 → 0.10.0-alpha.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.
- package/README.md +2 -1
- package/package.json +45 -20
- package/tsconfig.json +9 -0
- package/build/module/GlobalState.js +0 -191
- package/build/module/GlobalState.js.map +0 -1
- package/build/module/GlobalStateProvider.js +0 -81
- package/build/module/GlobalStateProvider.js.map +0 -1
- package/build/module/index.js +0 -12
- package/build/module/index.js.map +0 -1
- package/build/module/useAsyncCollection.js +0 -58
- package/build/module/useAsyncCollection.js.map +0 -1
- package/build/module/useAsyncData.js +0 -206
- package/build/module/useAsyncData.js.map +0 -1
- package/build/module/useGlobalState.js +0 -109
- package/build/module/useGlobalState.js.map +0 -1
- package/build/module/utils.js +0 -23
- package/build/module/utils.js.map +0 -1
- package/build/node/GlobalState.js +0 -174
- package/build/node/GlobalState.js.map +0 -1
- package/build/node/GlobalStateProvider.js +0 -88
- package/build/node/GlobalStateProvider.js.map +0 -1
- package/build/node/index.js +0 -56
- package/build/node/index.js.map +0 -1
- package/build/node/useAsyncCollection.js +0 -63
- package/build/node/useAsyncCollection.js.map +0 -1
- package/build/node/useAsyncData.js +0 -211
- package/build/node/useAsyncData.js.map +0 -1
- package/build/node/useGlobalState.js +0 -114
- package/build/node/useGlobalState.js.map +0 -1
- package/build/node/utils.js +0 -31
- package/build/node/utils.js.map +0 -1
- package/index.d.ts +0 -70
package/README.md
CHANGED
|
@@ -4,6 +4,7 @@
|
|
|
4
4
|
[](https://www.npmjs.com/package/@dr.pogodin/react-global-state)
|
|
5
5
|
[](https://app.circleci.com/pipelines/github/birdofpreyru/react-global-state)
|
|
6
6
|
[](https://github.com/birdofpreyru/react-global-state)
|
|
7
|
+
[](https://dr.pogodin.studio/docs/react-global-state)
|
|
7
8
|
|
|
8
9
|
State of the art approach to the global state and asynchronous data management
|
|
9
10
|
in React applications, powered by hooks and Context API. Simple, efficient, with
|
|
@@ -12,7 +13,7 @@ full server-side rendering (SSR) support.
|
|
|
12
13
|
[Library Reference](https://dr.pogodin.studio/docs/react-global-state/index.html) •
|
|
13
14
|
[Blog Article](https://dr.pogodin.studio/dev-blog/the-global-state-in-react-designed-right)
|
|
14
15
|
|
|
15
|
-
[](https://github.com/sponsors/birdofpreyru)
|
|
16
17
|
|
|
17
18
|
### Motivation
|
|
18
19
|
|
package/package.json
CHANGED
|
@@ -1,8 +1,10 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@dr.pogodin/react-global-state",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.10.0-alpha.1",
|
|
4
4
|
"description": "Hook-based global state for React",
|
|
5
5
|
"main": "./build/node/index.js",
|
|
6
|
+
"react-native": "src/index.ts",
|
|
7
|
+
"source": "src/index.ts",
|
|
6
8
|
"exports": {
|
|
7
9
|
"module": "./build/module/index.js",
|
|
8
10
|
"node": "./build/node/index.js",
|
|
@@ -13,9 +15,12 @@
|
|
|
13
15
|
"build": "rimraf build && npm run build:module && npm run build:node",
|
|
14
16
|
"build:module": "rimraf build/web && babel src --out-dir build/module --source-maps --config-file ./babel.module.config.js",
|
|
15
17
|
"build:node": "rimraf build/node && babel src --out-dir build/node --source-maps",
|
|
16
|
-
"jest": "jest
|
|
17
|
-
"
|
|
18
|
-
"
|
|
18
|
+
"jest": "npm run jest:types && npm run jest:logic",
|
|
19
|
+
"jest:types": "jest --config jest/config-types.json",
|
|
20
|
+
"jest:logic": "jest --config jest/config.json -w 1",
|
|
21
|
+
"lint": "eslint --ext .js,.jsx,.ts,.tsx .",
|
|
22
|
+
"test": "npm run lint && npm run typecheck && npm run jest",
|
|
23
|
+
"typecheck": "tsc --noEmit && tsc --project __tests__/tsconfig.typecheck.json"
|
|
19
24
|
},
|
|
20
25
|
"repository": "github:birdofpreyru/react-global-state",
|
|
21
26
|
"keywords": [
|
|
@@ -24,7 +29,8 @@
|
|
|
24
29
|
"state",
|
|
25
30
|
"hooks",
|
|
26
31
|
"hook",
|
|
27
|
-
"global"
|
|
32
|
+
"global",
|
|
33
|
+
"typescript"
|
|
28
34
|
],
|
|
29
35
|
"author": "Dr. Sergey Pogodin <doc@pogodin.studio> (https://dr.pogodin.studio)",
|
|
30
36
|
"license": "MIT",
|
|
@@ -32,35 +38,54 @@
|
|
|
32
38
|
"url": "https://github.com/birdofpreyru/react-global-state.git/issues"
|
|
33
39
|
},
|
|
34
40
|
"homepage": "https://dr.pogodin.studio/docs/react-global-state/index.html",
|
|
41
|
+
"engines": {
|
|
42
|
+
"node": ">=16"
|
|
43
|
+
},
|
|
35
44
|
"dependencies": {
|
|
36
|
-
"@babel/runtime": "^7.
|
|
45
|
+
"@babel/runtime": "^7.22.6",
|
|
46
|
+
"@dr.pogodin/js-utils": "^0.0.6",
|
|
37
47
|
"lodash": "^4.17.21",
|
|
38
48
|
"uuid": "^9.0.0"
|
|
39
49
|
},
|
|
40
50
|
"devDependencies": {
|
|
41
|
-
"@babel/cli": "^7.
|
|
42
|
-
"@babel/core": "^7.
|
|
43
|
-
"@babel/eslint-parser": "^7.
|
|
44
|
-
"@babel/eslint-plugin": "^7.
|
|
45
|
-
"@babel/node": "^7.
|
|
46
|
-
"@babel/plugin-transform-runtime": "^7.
|
|
47
|
-
"@babel/preset-env": "^7.
|
|
48
|
-
"@babel/preset-react": "^7.
|
|
49
|
-
"babel-
|
|
51
|
+
"@babel/cli": "^7.22.6",
|
|
52
|
+
"@babel/core": "^7.22.8",
|
|
53
|
+
"@babel/eslint-parser": "^7.22.7",
|
|
54
|
+
"@babel/eslint-plugin": "^7.22.5",
|
|
55
|
+
"@babel/node": "^7.22.6",
|
|
56
|
+
"@babel/plugin-transform-runtime": "^7.22.7",
|
|
57
|
+
"@babel/preset-env": "^7.22.7",
|
|
58
|
+
"@babel/preset-react": "^7.22.5",
|
|
59
|
+
"@babel/preset-typescript": "^7.22.5",
|
|
60
|
+
"@tsconfig/recommended": "^1.0.2",
|
|
61
|
+
"@tsd/typescript": "^5.1.6",
|
|
62
|
+
"@types/jest": "^29.5.3",
|
|
63
|
+
"@types/lodash": "^4.14.195",
|
|
64
|
+
"@types/pretty": "^2.0.1",
|
|
65
|
+
"@types/react": "^18.2.14",
|
|
66
|
+
"@types/react-dom": "^18.2.6",
|
|
67
|
+
"@types/uuid": "^9.0.2",
|
|
68
|
+
"@typescript-eslint/eslint-plugin": "^5.61.0",
|
|
69
|
+
"@typescript-eslint/parser": "^5.61.0",
|
|
70
|
+
"babel-jest": "^29.6.1",
|
|
50
71
|
"babel-plugin-module-resolver": "^5.0.0",
|
|
51
|
-
"eslint": "^8.
|
|
72
|
+
"eslint": "^8.44.0",
|
|
52
73
|
"eslint-config-airbnb": "^19.0.4",
|
|
74
|
+
"eslint-config-airbnb-typescript": "^17.0.0",
|
|
53
75
|
"eslint-import-resolver-babel-module": "^5.3.2",
|
|
54
76
|
"eslint-plugin-import": "^2.27.5",
|
|
55
|
-
"eslint-plugin-jest": "^27.2.
|
|
77
|
+
"eslint-plugin-jest": "^27.2.2",
|
|
56
78
|
"eslint-plugin-jsx-a11y": "^6.7.1",
|
|
57
79
|
"eslint-plugin-react": "^7.32.2",
|
|
58
80
|
"eslint-plugin-react-hooks": "^4.6.0",
|
|
59
|
-
"jest": "^29.
|
|
60
|
-
"jest-environment-jsdom": "^29.
|
|
81
|
+
"jest": "^29.6.1",
|
|
82
|
+
"jest-environment-jsdom": "^29.6.1",
|
|
83
|
+
"jest-runner-tsd": "^6.0.0",
|
|
61
84
|
"mockdate": "^3.0.5",
|
|
62
85
|
"pretty": "^2.0.0",
|
|
63
|
-
"rimraf": "^
|
|
86
|
+
"rimraf": "^5.0.1",
|
|
87
|
+
"tsd-lite": "^0.8.0",
|
|
88
|
+
"typescript": "^5.1.6"
|
|
64
89
|
},
|
|
65
90
|
"peerDependencies": {
|
|
66
91
|
"react": "^18.2.0",
|
package/tsconfig.json
ADDED
|
@@ -1,191 +0,0 @@
|
|
|
1
|
-
import _classPrivateFieldGet from "@babel/runtime/helpers/classPrivateFieldGet";
|
|
2
|
-
import _classPrivateFieldSet from "@babel/runtime/helpers/classPrivateFieldSet";
|
|
3
|
-
function _classPrivateFieldInitSpec(obj, privateMap, value) { _checkPrivateRedeclaration(obj, privateMap); privateMap.set(obj, value); }
|
|
4
|
-
function _checkPrivateRedeclaration(obj, privateCollection) { if (privateCollection.has(obj)) { throw new TypeError("Cannot initialize the same private elements twice on an object"); } }
|
|
5
|
-
import { cloneDeep, get, isFunction, isObject, isNil, set, toPath } from 'lodash';
|
|
6
|
-
import { isDebugMode } from "./utils";
|
|
7
|
-
const ERR_NO_SSR_WATCH = 'GlobalState must not be watched at server side';
|
|
8
|
-
var _initialState = /*#__PURE__*/new WeakMap();
|
|
9
|
-
var _nextNotifierId = /*#__PURE__*/new WeakMap();
|
|
10
|
-
var _ssrContext = /*#__PURE__*/new WeakMap();
|
|
11
|
-
var _currentState = /*#__PURE__*/new WeakMap();
|
|
12
|
-
var _watchers = /*#__PURE__*/new WeakMap();
|
|
13
|
-
export default class GlobalState {
|
|
14
|
-
/**
|
|
15
|
-
* Creates a new global state object.
|
|
16
|
-
* @param {any} [initialState] Intial global state content.
|
|
17
|
-
* @param {SsrContext} [ssrContext] Server-side rendering context.
|
|
18
|
-
*/
|
|
19
|
-
constructor(initialState, ssrContext) {
|
|
20
|
-
_classPrivateFieldInitSpec(this, _initialState, {
|
|
21
|
-
writable: true,
|
|
22
|
-
value: void 0
|
|
23
|
-
});
|
|
24
|
-
_classPrivateFieldInitSpec(this, _nextNotifierId, {
|
|
25
|
-
writable: true,
|
|
26
|
-
value: null
|
|
27
|
-
});
|
|
28
|
-
_classPrivateFieldInitSpec(this, _ssrContext, {
|
|
29
|
-
writable: true,
|
|
30
|
-
value: void 0
|
|
31
|
-
});
|
|
32
|
-
_classPrivateFieldInitSpec(this, _currentState, {
|
|
33
|
-
writable: true,
|
|
34
|
-
value: void 0
|
|
35
|
-
});
|
|
36
|
-
_classPrivateFieldInitSpec(this, _watchers, {
|
|
37
|
-
writable: true,
|
|
38
|
-
value: []
|
|
39
|
-
});
|
|
40
|
-
_classPrivateFieldSet(this, _currentState, initialState);
|
|
41
|
-
_classPrivateFieldSet(this, _initialState, initialState);
|
|
42
|
-
if (ssrContext) {
|
|
43
|
-
/* eslint-disable no-param-reassign */
|
|
44
|
-
ssrContext.dirty = false;
|
|
45
|
-
ssrContext.pending = [];
|
|
46
|
-
ssrContext.state = _classPrivateFieldGet(this, _currentState);
|
|
47
|
-
/* eslint-enable no-param-reassign */
|
|
48
|
-
|
|
49
|
-
_classPrivateFieldSet(this, _ssrContext, ssrContext);
|
|
50
|
-
}
|
|
51
|
-
if (process.env.NODE_ENV !== 'production' && isDebugMode()) {
|
|
52
|
-
/* eslint-disable no-console */
|
|
53
|
-
let msg = 'New ReactGlobalState created';
|
|
54
|
-
if (ssrContext) msg += ' (SSR mode)';
|
|
55
|
-
console.groupCollapsed(msg);
|
|
56
|
-
console.log('Initial state:', cloneDeep(initialState));
|
|
57
|
-
console.groupEnd();
|
|
58
|
-
/* eslint-enable no-console */
|
|
59
|
-
}
|
|
60
|
-
}
|
|
61
|
-
|
|
62
|
-
/**
|
|
63
|
-
* Gets current or initial value at the specified "path" of the global state.
|
|
64
|
-
* Allows to get the entire global state, and automatically set default value
|
|
65
|
-
* at the "path".
|
|
66
|
-
* @param {string} [path] Dot-delimitered state path. Pass it "null",
|
|
67
|
-
* or "undefined" to refer the entire global state.
|
|
68
|
-
* @param {object} [options={}] Additional options.
|
|
69
|
-
* @param {boolean} [options.initialState] If "true" the value will be read
|
|
70
|
-
* from the initial state instead of the current one.
|
|
71
|
-
* @param {any} [options.initialValue] If the value read from the "path" is
|
|
72
|
-
* "undefined", this "initialValue" will be returned instead. In such case
|
|
73
|
-
* "initialValue" will also be written to the "path" of the current global
|
|
74
|
-
* state (no matter "initialState" flag), if "undefined" is stored there.
|
|
75
|
-
* @return {any} Retrieved value.
|
|
76
|
-
*/
|
|
77
|
-
get(path) {
|
|
78
|
-
let {
|
|
79
|
-
initialState,
|
|
80
|
-
initialValue
|
|
81
|
-
} = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
|
|
82
|
-
const state = initialState ? _classPrivateFieldGet(this, _initialState) : _classPrivateFieldGet(this, _currentState);
|
|
83
|
-
let value = isNil(path) ? state : get(state, path);
|
|
84
|
-
if (value === undefined && initialValue !== undefined) {
|
|
85
|
-
value = isFunction(initialValue) ? initialValue() : initialValue;
|
|
86
|
-
if (!initialState || this.get(path) === undefined) this.set(path, value);
|
|
87
|
-
}
|
|
88
|
-
return value;
|
|
89
|
-
}
|
|
90
|
-
|
|
91
|
-
/**
|
|
92
|
-
* Writes the `value` to given global state `path`.
|
|
93
|
-
* @param {string} [path] Dot-delimitered state path. If not given, entire
|
|
94
|
-
* global state content is replaced by the `value`.
|
|
95
|
-
* @param {any} value The value.
|
|
96
|
-
* @return {any} Given `value` itself.
|
|
97
|
-
*/
|
|
98
|
-
set(path, value) {
|
|
99
|
-
if (value !== this.get(path)) {
|
|
100
|
-
if (process.env.NODE_ENV !== 'production' && isDebugMode()) {
|
|
101
|
-
/* eslint-disable no-console */
|
|
102
|
-
console.groupCollapsed(`ReactGlobalState update. Path: "${path || ''}"`);
|
|
103
|
-
console.log('New value:', cloneDeep(value));
|
|
104
|
-
/* eslint-enable no-console */
|
|
105
|
-
}
|
|
106
|
-
|
|
107
|
-
if (isNil(path)) _classPrivateFieldSet(this, _currentState, value);else {
|
|
108
|
-
const root = {
|
|
109
|
-
state: _classPrivateFieldGet(this, _currentState)
|
|
110
|
-
};
|
|
111
|
-
let segIdx = 0;
|
|
112
|
-
let pos = root;
|
|
113
|
-
const pathSegments = toPath(`state.${path}`);
|
|
114
|
-
for (; segIdx < pathSegments.length - 1; segIdx += 1) {
|
|
115
|
-
const seg = pathSegments[segIdx];
|
|
116
|
-
const next = pos[seg];
|
|
117
|
-
if (Array.isArray(next)) pos[seg] = [...next];else if (isObject(next)) pos[seg] = {
|
|
118
|
-
...next
|
|
119
|
-
};else {
|
|
120
|
-
// We arrived to a state sub-segment, where the remaining part of
|
|
121
|
-
// the update path does not exist yet. We rely on lodash's set()
|
|
122
|
-
// function to create the remaining path, and set the value.
|
|
123
|
-
set(pos, pathSegments.slice(segIdx), value);
|
|
124
|
-
break;
|
|
125
|
-
}
|
|
126
|
-
pos = pos[seg];
|
|
127
|
-
}
|
|
128
|
-
if (segIdx === pathSegments.length - 1) {
|
|
129
|
-
pos[pathSegments[segIdx]] = value;
|
|
130
|
-
}
|
|
131
|
-
_classPrivateFieldSet(this, _currentState, root.state);
|
|
132
|
-
}
|
|
133
|
-
if (_classPrivateFieldGet(this, _ssrContext)) {
|
|
134
|
-
_classPrivateFieldGet(this, _ssrContext).dirty = true;
|
|
135
|
-
_classPrivateFieldGet(this, _ssrContext).state = _classPrivateFieldGet(this, _currentState);
|
|
136
|
-
} else if (!_classPrivateFieldGet(this, _nextNotifierId)) {
|
|
137
|
-
_classPrivateFieldSet(this, _nextNotifierId, setTimeout(() => {
|
|
138
|
-
_classPrivateFieldSet(this, _nextNotifierId, null);
|
|
139
|
-
[..._classPrivateFieldGet(this, _watchers)].forEach(w => w());
|
|
140
|
-
}));
|
|
141
|
-
}
|
|
142
|
-
if (process.env.NODE_ENV !== 'production' && isDebugMode()) {
|
|
143
|
-
/* eslint-disable no-console */
|
|
144
|
-
console.log('New state:', cloneDeep(_classPrivateFieldGet(this, _currentState)));
|
|
145
|
-
console.groupEnd();
|
|
146
|
-
/* eslint-enable no-console */
|
|
147
|
-
}
|
|
148
|
-
}
|
|
149
|
-
|
|
150
|
-
return value;
|
|
151
|
-
}
|
|
152
|
-
|
|
153
|
-
/**
|
|
154
|
-
* Unsubscribes `callback` from watching state updates; no operation if
|
|
155
|
-
* `callback` is not subscribed to the state updates.
|
|
156
|
-
* @param {function} callback
|
|
157
|
-
* @throws if {@link SsrContext} is attached to the state instance: the state
|
|
158
|
-
* watching functionality is intended for client-side (non-SSR) only.
|
|
159
|
-
*/
|
|
160
|
-
unWatch(callback) {
|
|
161
|
-
if (_classPrivateFieldGet(this, _ssrContext)) throw new Error(ERR_NO_SSR_WATCH);
|
|
162
|
-
const watchers = _classPrivateFieldGet(this, _watchers);
|
|
163
|
-
const pos = watchers.indexOf(callback);
|
|
164
|
-
if (pos >= 0) {
|
|
165
|
-
watchers[pos] = watchers[watchers.length - 1];
|
|
166
|
-
watchers.pop();
|
|
167
|
-
}
|
|
168
|
-
}
|
|
169
|
-
get ssrContext() {
|
|
170
|
-
return _classPrivateFieldGet(this, _ssrContext);
|
|
171
|
-
}
|
|
172
|
-
|
|
173
|
-
/**
|
|
174
|
-
* Subscribes `callback` to watch state updates; no operation if
|
|
175
|
-
* `callback` is already subscribed to this state instance.
|
|
176
|
-
* @param {function} callback It will be called without any arguments every
|
|
177
|
-
* time the state content changes (note, howhever, separate state updates can
|
|
178
|
-
* be applied to the state at once, and watching callbacks will be called once
|
|
179
|
-
* after such bulk update).
|
|
180
|
-
* @throws if {@link SsrContext} is attached to the state instance: the state
|
|
181
|
-
* watching functionality is intended for client-side (non-SSR) only.
|
|
182
|
-
*/
|
|
183
|
-
watch(callback) {
|
|
184
|
-
if (_classPrivateFieldGet(this, _ssrContext)) throw new Error(ERR_NO_SSR_WATCH);
|
|
185
|
-
const watchers = _classPrivateFieldGet(this, _watchers);
|
|
186
|
-
if (watchers.indexOf(callback) < 0) {
|
|
187
|
-
watchers.push(callback);
|
|
188
|
-
}
|
|
189
|
-
}
|
|
190
|
-
}
|
|
191
|
-
//# sourceMappingURL=GlobalState.js.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"GlobalState.js","names":["cloneDeep","get","isFunction","isObject","isNil","set","toPath","isDebugMode","ERR_NO_SSR_WATCH","_initialState","WeakMap","_nextNotifierId","_ssrContext","_currentState","_watchers","GlobalState","constructor","initialState","ssrContext","_classPrivateFieldInitSpec","writable","value","_classPrivateFieldSet","dirty","pending","state","_classPrivateFieldGet","process","env","NODE_ENV","msg","console","groupCollapsed","log","groupEnd","path","initialValue","arguments","length","undefined","root","segIdx","pos","pathSegments","seg","next","Array","isArray","slice","setTimeout","forEach","w","unWatch","callback","Error","watchers","indexOf","pop","watch","push"],"sources":["../../src/GlobalState.js"],"sourcesContent":["import {\n cloneDeep,\n get,\n isFunction,\n isObject,\n isNil,\n set,\n toPath,\n} from 'lodash';\n\nimport { isDebugMode } from './utils';\n\nconst ERR_NO_SSR_WATCH = 'GlobalState must not be watched at server side';\n\nexport default class GlobalState {\n #initialState;\n\n #nextNotifierId = null;\n\n #ssrContext;\n\n #currentState;\n\n #watchers = [];\n\n /**\n * Creates a new global state object.\n * @param {any} [initialState] Intial global state content.\n * @param {SsrContext} [ssrContext] Server-side rendering context.\n */\n constructor(initialState, ssrContext) {\n this.#currentState = initialState;\n this.#initialState = initialState;\n\n if (ssrContext) {\n /* eslint-disable no-param-reassign */\n ssrContext.dirty = false;\n ssrContext.pending = [];\n ssrContext.state = this.#currentState;\n /* eslint-enable no-param-reassign */\n\n this.#ssrContext = ssrContext;\n }\n\n if (process.env.NODE_ENV !== 'production' && isDebugMode()) {\n /* eslint-disable no-console */\n let msg = 'New ReactGlobalState created';\n if (ssrContext) msg += ' (SSR mode)';\n console.groupCollapsed(msg);\n console.log('Initial state:', cloneDeep(initialState));\n console.groupEnd();\n /* eslint-enable no-console */\n }\n }\n\n /**\n * Gets current or initial value at the specified \"path\" of the global state.\n * Allows to get the entire global state, and automatically set default value\n * at the \"path\".\n * @param {string} [path] Dot-delimitered state path. Pass it \"null\",\n * or \"undefined\" to refer the entire global state.\n * @param {object} [options={}] Additional options.\n * @param {boolean} [options.initialState] If \"true\" the value will be read\n * from the initial state instead of the current one.\n * @param {any} [options.initialValue] If the value read from the \"path\" is\n * \"undefined\", this \"initialValue\" will be returned instead. In such case\n * \"initialValue\" will also be written to the \"path\" of the current global\n * state (no matter \"initialState\" flag), if \"undefined\" is stored there.\n * @return {any} Retrieved value.\n */\n get(path, { initialState, initialValue } = {}) {\n const state = initialState ? this.#initialState : this.#currentState;\n let value = isNil(path) ? state : get(state, path);\n if (value === undefined && initialValue !== undefined) {\n value = isFunction(initialValue) ? initialValue() : initialValue;\n if (!initialState || this.get(path) === undefined) this.set(path, value);\n }\n return value;\n }\n\n /**\n * Writes the `value` to given global state `path`.\n * @param {string} [path] Dot-delimitered state path. If not given, entire\n * global state content is replaced by the `value`.\n * @param {any} value The value.\n * @return {any} Given `value` itself.\n */\n set(path, value) {\n if (value !== this.get(path)) {\n if (process.env.NODE_ENV !== 'production' && isDebugMode()) {\n /* eslint-disable no-console */\n console.groupCollapsed(\n `ReactGlobalState update. Path: \"${path || ''}\"`,\n );\n console.log('New value:', cloneDeep(value));\n /* eslint-enable no-console */\n }\n\n if (isNil(path)) this.#currentState = value;\n else {\n const root = { state: this.#currentState };\n let segIdx = 0;\n let pos = root;\n const pathSegments = toPath(`state.${path}`);\n for (; segIdx < pathSegments.length - 1; segIdx += 1) {\n const seg = pathSegments[segIdx];\n const next = pos[seg];\n if (Array.isArray(next)) pos[seg] = [...next];\n else if (isObject(next)) pos[seg] = { ...next };\n else {\n // We arrived to a state sub-segment, where the remaining part of\n // the update path does not exist yet. We rely on lodash's set()\n // function to create the remaining path, and set the value.\n set(pos, pathSegments.slice(segIdx), value);\n break;\n }\n pos = pos[seg];\n }\n\n if (segIdx === pathSegments.length - 1) {\n pos[pathSegments[segIdx]] = value;\n }\n\n this.#currentState = root.state;\n }\n\n if (this.#ssrContext) {\n this.#ssrContext.dirty = true;\n this.#ssrContext.state = this.#currentState;\n } else if (!this.#nextNotifierId) {\n this.#nextNotifierId = setTimeout(() => {\n this.#nextNotifierId = null;\n [...this.#watchers].forEach((w) => w());\n });\n }\n if (process.env.NODE_ENV !== 'production' && isDebugMode()) {\n /* eslint-disable no-console */\n console.log('New state:', cloneDeep(this.#currentState));\n console.groupEnd();\n /* eslint-enable no-console */\n }\n }\n return value;\n }\n\n /**\n * Unsubscribes `callback` from watching state updates; no operation if\n * `callback` is not subscribed to the state updates.\n * @param {function} callback\n * @throws if {@link SsrContext} is attached to the state instance: the state\n * watching functionality is intended for client-side (non-SSR) only.\n */\n unWatch(callback) {\n if (this.#ssrContext) throw new Error(ERR_NO_SSR_WATCH);\n\n const watchers = this.#watchers;\n const pos = watchers.indexOf(callback);\n if (pos >= 0) {\n watchers[pos] = watchers[watchers.length - 1];\n watchers.pop();\n }\n }\n\n get ssrContext() { return this.#ssrContext; }\n\n /**\n * Subscribes `callback` to watch state updates; no operation if\n * `callback` is already subscribed to this state instance.\n * @param {function} callback It will be called without any arguments every\n * time the state content changes (note, howhever, separate state updates can\n * be applied to the state at once, and watching callbacks will be called once\n * after such bulk update).\n * @throws if {@link SsrContext} is attached to the state instance: the state\n * watching functionality is intended for client-side (non-SSR) only.\n */\n watch(callback) {\n if (this.#ssrContext) throw new Error(ERR_NO_SSR_WATCH);\n\n const watchers = this.#watchers;\n if (watchers.indexOf(callback) < 0) {\n watchers.push(callback);\n }\n }\n}\n"],"mappings":";;;;AAAA,SACEA,SAAS,EACTC,GAAG,EACHC,UAAU,EACVC,QAAQ,EACRC,KAAK,EACLC,GAAG,EACHC,MAAM,QACD,QAAQ;AAEf,SAASC,WAAW;AAEpB,MAAMC,gBAAgB,GAAG,gDAAgD;AAAC,IAAAC,aAAA,oBAAAC,OAAA;AAAA,IAAAC,eAAA,oBAAAD,OAAA;AAAA,IAAAE,WAAA,oBAAAF,OAAA;AAAA,IAAAG,aAAA,oBAAAH,OAAA;AAAA,IAAAI,SAAA,oBAAAJ,OAAA;AAE1E,eAAe,MAAMK,WAAW,CAAC;EAW/B;AACF;AACA;AACA;AACA;EACEC,WAAWA,CAACC,YAAY,EAAEC,UAAU,EAAE;IAAAC,0BAAA,OAAAV,aAAA;MAAAW,QAAA;MAAAC,KAAA;IAAA;IAAAF,0BAAA,OAAAR,eAAA;MAAAS,QAAA;MAAAC,KAAA,EAbpB;IAAI;IAAAF,0BAAA,OAAAP,WAAA;MAAAQ,QAAA;MAAAC,KAAA;IAAA;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
|
|
2
|
-
|
|
3
|
-
import { createContext, useContext, useRef } from 'react';
|
|
4
|
-
import GlobalState from "./GlobalState";
|
|
5
|
-
import { jsx as _jsx } from "react/jsx-runtime";
|
|
6
|
-
const context = /*#__PURE__*/createContext();
|
|
7
|
-
|
|
8
|
-
/**
|
|
9
|
-
* Gets {@link GlobalState} instance from the context. In most cases
|
|
10
|
-
* you should use {@link useGlobalState}, and other hooks to interact with
|
|
11
|
-
* the global state, instead of accessing it directly.
|
|
12
|
-
* @return {GlobalState}
|
|
13
|
-
*/
|
|
14
|
-
export function getGlobalState() {
|
|
15
|
-
// Here Rules of Hooks are violated because "getGlobalState()" does not follow
|
|
16
|
-
// convention that hook names should start with use... This is intentional in
|
|
17
|
-
// our case, as getGlobalState() hook is intended for advance scenarious,
|
|
18
|
-
// while the normal interaction with the global state should happen via
|
|
19
|
-
// another hook, useGlobalState().
|
|
20
|
-
/* eslint-disable react-hooks/rules-of-hooks */
|
|
21
|
-
const globalState = useContext(context);
|
|
22
|
-
/* eslint-enable react-hooks/rules-of-hooks */
|
|
23
|
-
if (!globalState) throw new Error('Missing GlobalStateProvider');
|
|
24
|
-
return globalState;
|
|
25
|
-
}
|
|
26
|
-
|
|
27
|
-
/**
|
|
28
|
-
* @category Hooks
|
|
29
|
-
* @desc Gets SSR context.
|
|
30
|
-
* @param {boolean} [throwWithoutSsrContext=true] If `true` (default),
|
|
31
|
-
* this hook will throw if no SSR context is attached to the global state;
|
|
32
|
-
* set `false` to not throw in such case. In either case the hook will throw
|
|
33
|
-
* if the {@link <GlobalStateProvider>} (hence the state) is missing.
|
|
34
|
-
* @returns {SsrContext} SSR context.
|
|
35
|
-
* @throws
|
|
36
|
-
* - If current component has no parent {@link <GlobalStateProvider>}
|
|
37
|
-
* in the rendered React tree.
|
|
38
|
-
* - If `throwWithoutSsrContext` is `true`, and there is no SSR context attached
|
|
39
|
-
* to the global state provided by {@link <GlobalStateProvider>}.
|
|
40
|
-
*/
|
|
41
|
-
export function getSsrContext() {
|
|
42
|
-
let throwWithoutSsrContext = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : true;
|
|
43
|
-
const {
|
|
44
|
-
ssrContext
|
|
45
|
-
} = getGlobalState();
|
|
46
|
-
if (!ssrContext && throwWithoutSsrContext) {
|
|
47
|
-
throw new Error('No SSR context found');
|
|
48
|
-
}
|
|
49
|
-
return ssrContext;
|
|
50
|
-
}
|
|
51
|
-
|
|
52
|
-
/**
|
|
53
|
-
* Provides global state to its children.
|
|
54
|
-
* @prop {ReactNode} [children] Component children, which will be provided with
|
|
55
|
-
* the global state, and rendered in place of the provider.
|
|
56
|
-
* @prop {any} [initialState] Initial content of the global state.
|
|
57
|
-
* @prop {SsrContext} [ssrContext] Server-side rendering (SSR) context.
|
|
58
|
-
* @prop {boolean|GlobalState} [stateProxy] This option is useful for code
|
|
59
|
-
* splitting and SSR implementation:
|
|
60
|
-
* - If `true`, this provider instance will fetch and reuse the global state
|
|
61
|
-
* from a parent provider.
|
|
62
|
-
* - If `GlobalState` instance, it will be used by this provider.
|
|
63
|
-
* - If not given, a new `GlobalState` instance will be created and used.
|
|
64
|
-
*/
|
|
65
|
-
export default function GlobalStateProvider(_ref) {
|
|
66
|
-
let {
|
|
67
|
-
children,
|
|
68
|
-
initialState,
|
|
69
|
-
ssrContext,
|
|
70
|
-
stateProxy
|
|
71
|
-
} = _ref;
|
|
72
|
-
const state = useRef();
|
|
73
|
-
if (!state.current) {
|
|
74
|
-
if (stateProxy instanceof GlobalState) state.current = stateProxy;else if (stateProxy) state.current = getGlobalState();else state.current = new GlobalState(initialState, ssrContext);
|
|
75
|
-
}
|
|
76
|
-
return /*#__PURE__*/_jsx(context.Provider, {
|
|
77
|
-
value: state.current,
|
|
78
|
-
children: children
|
|
79
|
-
});
|
|
80
|
-
}
|
|
81
|
-
//# sourceMappingURL=GlobalStateProvider.js.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"GlobalStateProvider.js","names":["createContext","useContext","useRef","GlobalState","jsx","_jsx","context","getGlobalState","globalState","Error","getSsrContext","throwWithoutSsrContext","arguments","length","undefined","ssrContext","GlobalStateProvider","_ref","children","initialState","stateProxy","state","current","Provider","value"],"sources":["../../src/GlobalStateProvider.jsx"],"sourcesContent":["/* eslint-disable react/prop-types */\n\nimport { createContext, useContext, useRef } from 'react';\n\nimport GlobalState from './GlobalState';\n\nconst context = createContext();\n\n/**\n * Gets {@link GlobalState} instance from the context. In most cases\n * you should use {@link useGlobalState}, and other hooks to interact with\n * the global state, instead of accessing it directly.\n * @return {GlobalState}\n */\nexport function getGlobalState() {\n // Here Rules of Hooks are violated because \"getGlobalState()\" does not follow\n // convention that hook names should start with use... This is intentional in\n // our case, as getGlobalState() hook is intended for advance scenarious,\n // while the normal interaction with the global state should happen via\n // another hook, useGlobalState().\n /* eslint-disable react-hooks/rules-of-hooks */\n const globalState = useContext(context);\n /* eslint-enable react-hooks/rules-of-hooks */\n if (!globalState) throw new Error('Missing GlobalStateProvider');\n return globalState;\n}\n\n/**\n * @category Hooks\n * @desc Gets SSR context.\n * @param {boolean} [throwWithoutSsrContext=true] If `true` (default),\n * this hook will throw if no SSR context is attached to the global state;\n * set `false` to not throw in such case. In either case the hook will throw\n * if the {@link <GlobalStateProvider>} (hence the state) is missing.\n * @returns {SsrContext} SSR context.\n * @throws\n * - If current component has no parent {@link <GlobalStateProvider>}\n * in the rendered React tree.\n * - If `throwWithoutSsrContext` is `true`, and there is no SSR context attached\n * to the global state provided by {@link <GlobalStateProvider>}.\n */\nexport function getSsrContext(throwWithoutSsrContext = true) {\n const { ssrContext } = getGlobalState();\n if (!ssrContext && throwWithoutSsrContext) {\n throw new Error('No SSR context found');\n }\n return ssrContext;\n}\n\n/**\n * Provides global state to its children.\n * @prop {ReactNode} [children] Component children, which will be provided with\n * the global state, and rendered in place of the provider.\n * @prop {any} [initialState] Initial content of the global state.\n * @prop {SsrContext} [ssrContext] Server-side rendering (SSR) context.\n * @prop {boolean|GlobalState} [stateProxy] This option is useful for code\n * splitting and SSR implementation:\n * - If `true`, this provider instance will fetch and reuse the global state\n * from a parent provider.\n * - If `GlobalState` instance, it will be used by this provider.\n * - If not given, a new `GlobalState` instance will be created and used.\n */\nexport default function GlobalStateProvider({\n children,\n initialState,\n ssrContext,\n stateProxy,\n}) {\n const state = useRef();\n if (!state.current) {\n if (stateProxy instanceof GlobalState) state.current = stateProxy;\n else if (stateProxy) state.current = getGlobalState();\n else state.current = new GlobalState(initialState, ssrContext);\n }\n return (\n <context.Provider value={state.current}>\n {children}\n </context.Provider>\n );\n}\n"],"mappings":"AAAA;;AAEA,SAASA,aAAa,EAAEC,UAAU,EAAEC,MAAM,QAAQ,OAAO;AAEzD,OAAOC,WAAW;AAAsB,SAAAC,GAAA,IAAAC,IAAA;AAExC,MAAMC,OAAO,gBAAGN,aAAa,EAAE;;AAE/B;AACA;AACA;AACA;AACA;AACA;AACA,OAAO,SAASO,cAAcA,CAAA,EAAG;EAC/B;EACA;EACA;EACA;EACA;EACA;EACA,MAAMC,WAAW,GAAGP,UAAU,CAACK,OAAO,CAAC;EACvC;EACA,IAAI,CAACE,WAAW,EAAE,MAAM,IAAIC,KAAK,CAAC,6BAA6B,CAAC;EAChE,OAAOD,WAAW;AACpB;;AAEA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA,OAAO,SAASE,aAAaA,CAAA,EAAgC;EAAA,IAA/BC,sBAAsB,GAAAC,SAAA,CAAAC,MAAA,QAAAD,SAAA,QAAAE,SAAA,GAAAF,SAAA,MAAG,IAAI;EACzD,MAAM;IAAEG;EAAW,CAAC,GAAGR,cAAc,EAAE;EACvC,IAAI,CAACQ,UAAU,IAAIJ,sBAAsB,EAAE;IACzC,MAAM,IAAIF,KAAK,CAAC,sBAAsB,CAAC;EACzC;EACA,OAAOM,UAAU;AACnB;;AAEA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA,eAAe,SAASC,mBAAmBA,CAAAC,IAAA,EAKxC;EAAA,IALyC;IAC1CC,QAAQ;IACRC,YAAY;IACZJ,UAAU;IACVK;EACF,CAAC,GAAAH,IAAA;EACC,MAAMI,KAAK,GAAGnB,MAAM,EAAE;EACtB,IAAI,CAACmB,KAAK,CAACC,OAAO,EAAE;IAClB,IAAIF,UAAU,YAAYjB,WAAW,EAAEkB,KAAK,CAACC,OAAO,GAAGF,UAAU,CAAC,KAC7D,IAAIA,UAAU,EAAEC,KAAK,CAACC,OAAO,GAAGf,cAAc,EAAE,CAAC,KACjDc,KAAK,CAACC,OAAO,GAAG,IAAInB,WAAW,CAACgB,YAAY,EAAEJ,UAAU,CAAC;EAChE;EACA,oBACEV,IAAA,CAACC,OAAO,CAACiB,QAAQ;IAACC,KAAK,EAAEH,KAAK,CAACC,OAAQ;IAAAJ,QAAA,EACpCA;EAAQ,EACQ;AAEvB"}
|
package/build/module/index.js
DELETED
|
@@ -1,12 +0,0 @@
|
|
|
1
|
-
// TODO: This is a temporary polyfill for `Promise.allSettled(..)` method,
|
|
2
|
-
// which is supported natively by NodeJS >= v12.9.0. As earlier NodeJS version
|
|
3
|
-
// are still in a wide use, this polyfill is added here, and it is to be dropped
|
|
4
|
-
// some time later.
|
|
5
|
-
if (!Promise.allSettled) {
|
|
6
|
-
Promise.allSettled = promises => Promise.all(promises.map(p => p instanceof Promise ? p.finally(() => null) : p));
|
|
7
|
-
}
|
|
8
|
-
export { default as GlobalStateProvider, getGlobalState, getSsrContext } from "./GlobalStateProvider";
|
|
9
|
-
export { default as useAsyncCollection } from "./useAsyncCollection";
|
|
10
|
-
export { default as useAsyncData } from "./useAsyncData";
|
|
11
|
-
export { default as useGlobalState } from "./useGlobalState";
|
|
12
|
-
//# sourceMappingURL=index.js.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","names":["Promise","allSettled","promises","all","map","p","finally","default","GlobalStateProvider","getGlobalState","getSsrContext","useAsyncCollection","useAsyncData","useGlobalState"],"sources":["../../src/index.js"],"sourcesContent":["// TODO: This is a temporary polyfill for `Promise.allSettled(..)` method,\n// which is supported natively by NodeJS >= v12.9.0. As earlier NodeJS version\n// are still in a wide use, this polyfill is added here, and it is to be dropped\n// some time later.\nif (!Promise.allSettled) {\n Promise.allSettled = (promises) => Promise.all(\n promises.map((p) => (p instanceof Promise ? p.finally(() => null) : p)),\n );\n}\n\nexport {\n default as GlobalStateProvider,\n getGlobalState,\n getSsrContext,\n} from './GlobalStateProvider';\n\nexport { default as useAsyncCollection } from './useAsyncCollection';\nexport { default as useAsyncData } from './useAsyncData';\nexport { default as useGlobalState } from './useGlobalState';\n"],"mappings":"AAAA;AACA;AACA;AACA;AACA,IAAI,CAACA,OAAO,CAACC,UAAU,EAAE;EACvBD,OAAO,CAACC,UAAU,GAAIC,QAAQ,IAAKF,OAAO,CAACG,GAAG,CAC5CD,QAAQ,CAACE,GAAG,CAAEC,CAAC,IAAMA,CAAC,YAAYL,OAAO,GAAGK,CAAC,CAACC,OAAO,CAAC,MAAM,IAAI,CAAC,GAAGD,CAAE,CAAC,CACxE;AACH;AAEA,SACEE,OAAO,IAAIC,mBAAmB,EAC9BC,cAAc,EACdC,aAAa;AAGf,SAASH,OAAO,IAAII,kBAAkB;AACtC,SAASJ,OAAO,IAAIK,YAAY;AAChC,SAASL,OAAO,IAAIM,cAAc"}
|
|
@@ -1,58 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Loads and uses an item in an async collection.
|
|
3
|
-
*/
|
|
4
|
-
|
|
5
|
-
import useAsyncData from "./useAsyncData";
|
|
6
|
-
|
|
7
|
-
/**
|
|
8
|
-
* Resolves and stores at the given `path` of global state elements of
|
|
9
|
-
* an asynchronous data collection. In other words, it is an auxiliar wrapper
|
|
10
|
-
* around {@link useAsyncData}, which uses a loader which resolves to different
|
|
11
|
-
* data, based on ID argument passed in, and stores data fetched for different
|
|
12
|
-
* IDs in the state.
|
|
13
|
-
* @param {string} id ID of the collection item to load & use.
|
|
14
|
-
* @param {string} path The global state path where entire collection should be
|
|
15
|
-
* stored.
|
|
16
|
-
* @param {AsyncCollectionLoader} loader A loader function, which takes an
|
|
17
|
-
* ID of data to load, and resolves to the corresponding data.
|
|
18
|
-
* @param {object} [options] Additional options.
|
|
19
|
-
* @param {any[]} [options.deps=[]] An array of dependencies, which trigger
|
|
20
|
-
* data reload when changed. Given dependency changes are watched shallowly
|
|
21
|
-
* (similarly to the standard React's
|
|
22
|
-
* [useEffect()](https://reactjs.org/docs/hooks-reference.html#useeffect)).
|
|
23
|
-
* @param {boolean} [options.noSSR] If `true`, this hook won't load data during
|
|
24
|
-
* server-side rendering.
|
|
25
|
-
* @param {number} [options.garbageCollectAge=maxage] The maximum age of data
|
|
26
|
-
* (in milliseconds), after which they are dropped from the state when the last
|
|
27
|
-
* component referencing them via `useAsyncData()` hook unmounts. Defaults to
|
|
28
|
-
* `maxage` option value.
|
|
29
|
-
* @param {number} [options.maxage=5 x 60 x 1000] The maximum age of
|
|
30
|
-
* data (in milliseconds) acceptable to the hook's caller. If loaded data are
|
|
31
|
-
* older than this value, `null` is returned instead. Defaults to 5 minutes.
|
|
32
|
-
* @param {number} [options.refreshAge=maxage] The maximum age of data
|
|
33
|
-
* (in milliseconds), after which their refreshment will be triggered when
|
|
34
|
-
* any component referencing them via `useAsyncData()` hook (re-)renders.
|
|
35
|
-
* Defaults to `maxage` value.
|
|
36
|
-
* @return {{
|
|
37
|
-
* data: any,
|
|
38
|
-
* loading: boolean,
|
|
39
|
-
* timestamp: number
|
|
40
|
-
* }} Returns an object with three fields: `data` holds the actual result of
|
|
41
|
-
* last `loader` invokation, if any, and if satisfies `maxage` limit; `loading`
|
|
42
|
-
* is a boolean flag, which is `true` if data are being loaded (the hook is
|
|
43
|
-
* waiting for `loader` function resolution); `timestamp` (in milliseconds)
|
|
44
|
-
* is Unix timestamp of related data currently loaded into the global state.
|
|
45
|
-
*
|
|
46
|
-
* Note that loaded data, if any, are stored at the given `path` of global state
|
|
47
|
-
* along with related meta-information, using slightly different state segment
|
|
48
|
-
* structure (see {@link AsyncDataEnvelope}). That segment of the global state
|
|
49
|
-
* can be accessed, and even modified using other hooks,
|
|
50
|
-
* _e.g._ {@link useGlobalState}, but doing so you may interfere with related
|
|
51
|
-
* `useAsyncData()` hooks logic.
|
|
52
|
-
*/
|
|
53
|
-
export default function useAsyncCollection(id, path, loader) {
|
|
54
|
-
let options = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : {};
|
|
55
|
-
const itemPath = path ? `${path}.${id}` : id;
|
|
56
|
-
return useAsyncData(itemPath, oldData => loader(id, oldData), options);
|
|
57
|
-
}
|
|
58
|
-
//# sourceMappingURL=useAsyncCollection.js.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"useAsyncCollection.js","names":["useAsyncData","useAsyncCollection","id","path","loader","options","arguments","length","undefined","itemPath","oldData"],"sources":["../../src/useAsyncCollection.js"],"sourcesContent":["/**\n * Loads and uses an item in an async collection.\n */\n\nimport useAsyncData from './useAsyncData';\n\n/**\n * Resolves and stores at the given `path` of global state elements of\n * an asynchronous data collection. In other words, it is an auxiliar wrapper\n * around {@link useAsyncData}, which uses a loader which resolves to different\n * data, based on ID argument passed in, and stores data fetched for different\n * IDs in the state.\n * @param {string} id ID of the collection item to load & use.\n * @param {string} path The global state path where entire collection should be\n * stored.\n * @param {AsyncCollectionLoader} loader A loader function, which takes an\n * ID of data to load, and resolves to the corresponding data.\n * @param {object} [options] Additional options.\n * @param {any[]} [options.deps=[]] An array of dependencies, which trigger\n * data reload when changed. Given dependency changes are watched shallowly\n * (similarly to the standard React's\n * [useEffect()](https://reactjs.org/docs/hooks-reference.html#useeffect)).\n * @param {boolean} [options.noSSR] If `true`, this hook won't load data during\n * server-side rendering.\n * @param {number} [options.garbageCollectAge=maxage] The maximum age of data\n * (in milliseconds), after which they are dropped from the state when the last\n * component referencing them via `useAsyncData()` hook unmounts. Defaults to\n * `maxage` option value.\n * @param {number} [options.maxage=5 x 60 x 1000] The maximum age of\n * data (in milliseconds) acceptable to the hook's caller. If loaded data are\n * older than this value, `null` is returned instead. Defaults to 5 minutes.\n * @param {number} [options.refreshAge=maxage] The maximum age of data\n * (in milliseconds), after which their refreshment will be triggered when\n * any component referencing them via `useAsyncData()` hook (re-)renders.\n * Defaults to `maxage` value.\n * @return {{\n * data: any,\n * loading: boolean,\n * timestamp: number\n * }} Returns an object with three fields: `data` holds the actual result of\n * last `loader` invokation, if any, and if satisfies `maxage` limit; `loading`\n * is a boolean flag, which is `true` if data are being loaded (the hook is\n * waiting for `loader` function resolution); `timestamp` (in milliseconds)\n * is Unix timestamp of related data currently loaded into the global state.\n *\n * Note that loaded data, if any, are stored at the given `path` of global state\n * along with related meta-information, using slightly different state segment\n * structure (see {@link AsyncDataEnvelope}). That segment of the global state\n * can be accessed, and even modified using other hooks,\n * _e.g._ {@link useGlobalState}, but doing so you may interfere with related\n * `useAsyncData()` hooks logic.\n */\nexport default function useAsyncCollection(\n id,\n path,\n loader,\n options = {},\n) {\n const itemPath = path ? `${path}.${id}` : id;\n return useAsyncData(itemPath, (oldData) => loader(id, oldData), options);\n}\n"],"mappings":"AAAA;AACA;AACA;;AAEA,OAAOA,YAAY;;AAEnB;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA,eAAe,SAASC,kBAAkBA,CACxCC,EAAE,EACFC,IAAI,EACJC,MAAM,EAEN;EAAA,IADAC,OAAO,GAAAC,SAAA,CAAAC,MAAA,QAAAD,SAAA,QAAAE,SAAA,GAAAF,SAAA,MAAG,CAAC,CAAC;EAEZ,MAAMG,QAAQ,GAAGN,IAAI,GAAI,GAAEA,IAAK,IAAGD,EAAG,EAAC,GAAGA,EAAE;EAC5C,OAAOF,YAAY,CAACS,QAAQ,EAAGC,OAAO,IAAKN,MAAM,CAACF,EAAE,EAAEQ,OAAO,CAAC,EAAEL,OAAO,CAAC;AAC1E"}
|