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.
- package/jest.config.js +50 -0
- package/package.json +52 -2
- package/GlobalStore.cjs +0 -525
- package/GlobalStore.d.ts +0 -160
- package/GlobalStore.debugProps.cjs +0 -28
- package/GlobalStore.debugProps.d.ts +0 -5
- package/GlobalStore.debugProps.js +0 -28
- package/GlobalStore.debugProps.mjs +0 -7
- package/GlobalStore.js +0 -525
- package/GlobalStore.mjs +0 -497
- package/actions.cjs +0 -61
- package/actions.d.ts +0 -32
- package/actions.js +0 -61
- package/actions.mjs +0 -30
- package/bundle.cjs +0 -40
- package/bundle.js +0 -40
- package/bundle.mjs +0 -19
- package/createContext.cjs +0 -253
- package/createContext.d.ts +0 -15
- package/createContext.js +0 -253
- package/createContext.mjs +0 -235
- package/createGlobalState.cjs +0 -29
- package/createGlobalState.d.ts +0 -7
- package/createGlobalState.js +0 -29
- package/createGlobalState.mjs +0 -8
- package/index.d.ts +0 -9
- package/isRecord.cjs +0 -46
- package/isRecord.d.ts +0 -2
- package/isRecord.js +0 -46
- package/isRecord.mjs +0 -15
- package/shallowCompare.cjs +0 -115
- package/shallowCompare.d.ts +0 -52
- package/shallowCompare.js +0 -115
- package/shallowCompare.mjs +0 -84
- package/throwWrongKeyOnActionCollectionConfig.cjs +0 -41
- package/throwWrongKeyOnActionCollectionConfig.d.ts +0 -2
- package/throwWrongKeyOnActionCollectionConfig.js +0 -41
- package/throwWrongKeyOnActionCollectionConfig.mjs +0 -20
- package/types.cjs +0 -18
- package/types.d.ts +0 -1250
- package/types.js +0 -18
- package/types.mjs +0 -0
- package/uniqueId.cjs +0 -72
- package/uniqueId.d.ts +0 -7
- package/uniqueId.js +0 -72
- package/uniqueId.mjs +0 -51
package/bundle.cjs
DELETED
|
@@ -1,40 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
var __defProp = Object.defineProperty;
|
|
3
|
-
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
-
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
-
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
-
var __export = (target, all) => {
|
|
7
|
-
for (var name in all)
|
|
8
|
-
__defProp(target, name, { get: all[name], enumerable: true });
|
|
9
|
-
};
|
|
10
|
-
var __copyProps = (to, from, except, desc) => {
|
|
11
|
-
if (from && typeof from === "object" || typeof from === "function") {
|
|
12
|
-
for (let key of __getOwnPropNames(from))
|
|
13
|
-
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
14
|
-
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
15
|
-
}
|
|
16
|
-
return to;
|
|
17
|
-
};
|
|
18
|
-
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
19
|
-
|
|
20
|
-
// src/index.ts
|
|
21
|
-
var src_exports = {};
|
|
22
|
-
__export(src_exports, {
|
|
23
|
-
GlobalStore: () => import_GlobalStore.GlobalStore,
|
|
24
|
-
actions: () => import_actions.actions,
|
|
25
|
-
createContext: () => import_createContext.createContext,
|
|
26
|
-
createGlobalState: () => import_createGlobalState.createGlobalState,
|
|
27
|
-
isRecord: () => import_isRecord.isRecord,
|
|
28
|
-
shallowCompare: () => import_shallowCompare.shallowCompare,
|
|
29
|
-
throwWrongKeyOnActionCollectionConfig: () => import_throwWrongKeyOnActionCollectionConfig.throwWrongKeyOnActionCollectionConfig,
|
|
30
|
-
uniqueId: () => import_uniqueId.uniqueId
|
|
31
|
-
});
|
|
32
|
-
module.exports = __toCommonJS(src_exports);
|
|
33
|
-
var import_GlobalStore = require("./GlobalStore.cjs");
|
|
34
|
-
var import_createGlobalState = require("./createGlobalState.cjs");
|
|
35
|
-
var import_shallowCompare = require("./shallowCompare.cjs");
|
|
36
|
-
var import_uniqueId = require("./uniqueId.cjs");
|
|
37
|
-
var import_throwWrongKeyOnActionCollectionConfig = require("./throwWrongKeyOnActionCollectionConfig.cjs");
|
|
38
|
-
var import_isRecord = require("./isRecord.cjs");
|
|
39
|
-
var import_actions = require("./actions.cjs");
|
|
40
|
-
var import_createContext = require("./createContext.cjs");
|
package/bundle.js
DELETED
|
@@ -1,40 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
var __defProp = Object.defineProperty;
|
|
3
|
-
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
-
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
-
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
-
var __export = (target, all) => {
|
|
7
|
-
for (var name in all)
|
|
8
|
-
__defProp(target, name, { get: all[name], enumerable: true });
|
|
9
|
-
};
|
|
10
|
-
var __copyProps = (to, from, except, desc) => {
|
|
11
|
-
if (from && typeof from === "object" || typeof from === "function") {
|
|
12
|
-
for (let key of __getOwnPropNames(from))
|
|
13
|
-
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
14
|
-
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
15
|
-
}
|
|
16
|
-
return to;
|
|
17
|
-
};
|
|
18
|
-
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
19
|
-
|
|
20
|
-
// src/index.ts
|
|
21
|
-
var src_exports = {};
|
|
22
|
-
__export(src_exports, {
|
|
23
|
-
GlobalStore: () => import_GlobalStore.GlobalStore,
|
|
24
|
-
actions: () => import_actions.actions,
|
|
25
|
-
createContext: () => import_createContext.createContext,
|
|
26
|
-
createGlobalState: () => import_createGlobalState.createGlobalState,
|
|
27
|
-
isRecord: () => import_isRecord.isRecord,
|
|
28
|
-
shallowCompare: () => import_shallowCompare.shallowCompare,
|
|
29
|
-
throwWrongKeyOnActionCollectionConfig: () => import_throwWrongKeyOnActionCollectionConfig.throwWrongKeyOnActionCollectionConfig,
|
|
30
|
-
uniqueId: () => import_uniqueId.uniqueId
|
|
31
|
-
});
|
|
32
|
-
module.exports = __toCommonJS(src_exports);
|
|
33
|
-
var import_GlobalStore = require("./GlobalStore.js");
|
|
34
|
-
var import_createGlobalState = require("./createGlobalState.js");
|
|
35
|
-
var import_shallowCompare = require("./shallowCompare.js");
|
|
36
|
-
var import_uniqueId = require("./uniqueId.js");
|
|
37
|
-
var import_throwWrongKeyOnActionCollectionConfig = require("./throwWrongKeyOnActionCollectionConfig.js");
|
|
38
|
-
var import_isRecord = require("./isRecord.js");
|
|
39
|
-
var import_actions = require("./actions.js");
|
|
40
|
-
var import_createContext = require("./createContext.js");
|
package/bundle.mjs
DELETED
|
@@ -1,19 +0,0 @@
|
|
|
1
|
-
// src/index.ts
|
|
2
|
-
import { GlobalStore } from "./GlobalStore.mjs";
|
|
3
|
-
import { createGlobalState } from "./createGlobalState.mjs";
|
|
4
|
-
import { shallowCompare } from "./shallowCompare.mjs";
|
|
5
|
-
import { uniqueId } from "./uniqueId.mjs";
|
|
6
|
-
import { throwWrongKeyOnActionCollectionConfig } from "./throwWrongKeyOnActionCollectionConfig.mjs";
|
|
7
|
-
import { isRecord } from "./isRecord.mjs";
|
|
8
|
-
import { actions } from "./actions.mjs";
|
|
9
|
-
import { createContext } from "./createContext.mjs";
|
|
10
|
-
export {
|
|
11
|
-
GlobalStore,
|
|
12
|
-
actions,
|
|
13
|
-
createContext,
|
|
14
|
-
createGlobalState,
|
|
15
|
-
isRecord,
|
|
16
|
-
shallowCompare,
|
|
17
|
-
throwWrongKeyOnActionCollectionConfig,
|
|
18
|
-
uniqueId
|
|
19
|
-
};
|
package/createContext.cjs
DELETED
|
@@ -1,253 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
var __create = Object.create;
|
|
3
|
-
var __defProp = Object.defineProperty;
|
|
4
|
-
var __defProps = Object.defineProperties;
|
|
5
|
-
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
6
|
-
var __getOwnPropDescs = Object.getOwnPropertyDescriptors;
|
|
7
|
-
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
8
|
-
var __getOwnPropSymbols = Object.getOwnPropertySymbols;
|
|
9
|
-
var __getProtoOf = Object.getPrototypeOf;
|
|
10
|
-
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
11
|
-
var __propIsEnum = Object.prototype.propertyIsEnumerable;
|
|
12
|
-
var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
|
|
13
|
-
var __spreadValues = (a, b) => {
|
|
14
|
-
for (var prop in b || (b = {}))
|
|
15
|
-
if (__hasOwnProp.call(b, prop))
|
|
16
|
-
__defNormalProp(a, prop, b[prop]);
|
|
17
|
-
if (__getOwnPropSymbols)
|
|
18
|
-
for (var prop of __getOwnPropSymbols(b)) {
|
|
19
|
-
if (__propIsEnum.call(b, prop))
|
|
20
|
-
__defNormalProp(a, prop, b[prop]);
|
|
21
|
-
}
|
|
22
|
-
return a;
|
|
23
|
-
};
|
|
24
|
-
var __spreadProps = (a, b) => __defProps(a, __getOwnPropDescs(b));
|
|
25
|
-
var __objRest = (source, exclude) => {
|
|
26
|
-
var target = {};
|
|
27
|
-
for (var prop in source)
|
|
28
|
-
if (__hasOwnProp.call(source, prop) && exclude.indexOf(prop) < 0)
|
|
29
|
-
target[prop] = source[prop];
|
|
30
|
-
if (source != null && __getOwnPropSymbols)
|
|
31
|
-
for (var prop of __getOwnPropSymbols(source)) {
|
|
32
|
-
if (exclude.indexOf(prop) < 0 && __propIsEnum.call(source, prop))
|
|
33
|
-
target[prop] = source[prop];
|
|
34
|
-
}
|
|
35
|
-
return target;
|
|
36
|
-
};
|
|
37
|
-
var __export = (target, all) => {
|
|
38
|
-
for (var name in all)
|
|
39
|
-
__defProp(target, name, { get: all[name], enumerable: true });
|
|
40
|
-
};
|
|
41
|
-
var __copyProps = (to, from, except, desc) => {
|
|
42
|
-
if (from && typeof from === "object" || typeof from === "function") {
|
|
43
|
-
for (let key of __getOwnPropNames(from))
|
|
44
|
-
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
45
|
-
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
46
|
-
}
|
|
47
|
-
return to;
|
|
48
|
-
};
|
|
49
|
-
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
|
|
50
|
-
// If the importer is in node compatibility mode or this is not an ESM
|
|
51
|
-
// file that has been converted to a CommonJS file using a Babel-
|
|
52
|
-
// compatible transform (i.e. "__esModule" has not been set), then set
|
|
53
|
-
// "default" to the CommonJS "module.exports" for node compatibility.
|
|
54
|
-
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
|
|
55
|
-
mod
|
|
56
|
-
));
|
|
57
|
-
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
58
|
-
|
|
59
|
-
// src/createContext.ts
|
|
60
|
-
var createContext_exports = {};
|
|
61
|
-
__export(createContext_exports, {
|
|
62
|
-
createContext: () => createContext,
|
|
63
|
-
default: () => createContext_default
|
|
64
|
-
});
|
|
65
|
-
module.exports = __toCommonJS(createContext_exports);
|
|
66
|
-
var import_react = require("react");
|
|
67
|
-
var import_GlobalStore = require("./GlobalStore.cjs");
|
|
68
|
-
var import_isFunction = __toESM(require("json-storage-formatter/isFunction"));
|
|
69
|
-
var import_uniqueId = __toESM(require("./uniqueId.cjs"));
|
|
70
|
-
var createContext = (valueArg, contextArgs = {}) => {
|
|
71
|
-
var _a;
|
|
72
|
-
const Context = (0, import_react.createContext)(null);
|
|
73
|
-
const contextIdentifier = (_a = contextArgs.name) != null ? _a : (0, import_uniqueId.default)("ctx:");
|
|
74
|
-
const getInheritedState = () => (0, import_isFunction.default)(valueArg) ? valueArg() : valueArg;
|
|
75
|
-
const hasValueProp = (props) => {
|
|
76
|
-
return Object.prototype.hasOwnProperty.call(props, "value");
|
|
77
|
-
};
|
|
78
|
-
const useProviderValue = (props) => {
|
|
79
|
-
var _a2;
|
|
80
|
-
(0, import_react.useDebugValue)(`${contextIdentifier}:Provider`);
|
|
81
|
-
const [store] = (0, import_react.useState)(() => {
|
|
82
|
-
var _a3, _b, _c, _d;
|
|
83
|
-
const initialValue = (() => {
|
|
84
|
-
if (hasValueProp(props))
|
|
85
|
-
return (0, import_isFunction.default)(props.value) ? props.value(getInheritedState()) : props.value;
|
|
86
|
-
return getInheritedState();
|
|
87
|
-
})();
|
|
88
|
-
const store2 = new import_GlobalStore.GlobalStore(initialValue, __spreadProps(__spreadValues({}, contextArgs), {
|
|
89
|
-
metadata: (_a3 = (0, import_isFunction.default)(contextArgs.metadata) ? contextArgs.metadata() : contextArgs.metadata) != null ? _a3 : {}
|
|
90
|
-
}));
|
|
91
|
-
const storeToolsExtensions = {
|
|
92
|
-
use
|
|
93
|
-
};
|
|
94
|
-
Object.assign(store2.storeTools, storeToolsExtensions);
|
|
95
|
-
(_c = (_b = contextArgs.callbacks) == null ? void 0 : _b.onCreated) == null ? void 0 : _c.call(
|
|
96
|
-
_b,
|
|
97
|
-
store2.storeTools,
|
|
98
|
-
store2
|
|
99
|
-
);
|
|
100
|
-
(_d = props.onCreated) == null ? void 0 : _d.call(props, store2.storeTools, store2);
|
|
101
|
-
return store2;
|
|
102
|
-
});
|
|
103
|
-
(0, import_react.useLayoutEffect)(() => {
|
|
104
|
-
const shouldUpdateState = hasValueProp(props) && !(0, import_isFunction.default)(props.value);
|
|
105
|
-
if (!shouldUpdateState) return;
|
|
106
|
-
store.setState(props.value);
|
|
107
|
-
}, [store, props.value]);
|
|
108
|
-
(0, import_react.useEffect)(() => {
|
|
109
|
-
var _a3, _b, _c;
|
|
110
|
-
const unsubscribes = [
|
|
111
|
-
(_b = (_a3 = contextArgs.callbacks) == null ? void 0 : _a3.onMounted) == null ? void 0 : _b.call(
|
|
112
|
-
_a3,
|
|
113
|
-
store.storeTools,
|
|
114
|
-
store
|
|
115
|
-
),
|
|
116
|
-
(_c = props.onMounted) == null ? void 0 : _c.call(props, store.storeTools, store)
|
|
117
|
-
].filter(Boolean);
|
|
118
|
-
return () => {
|
|
119
|
-
var _a4, _b2, _c2;
|
|
120
|
-
(_b2 = (_a4 = store.callbacks) == null ? void 0 : _a4.onUnMount) == null ? void 0 : _b2.call(_a4, store);
|
|
121
|
-
unsubscribes.forEach((unsubscribe) => unsubscribe == null ? void 0 : unsubscribe());
|
|
122
|
-
(_c2 = store.__onUnMountContext) == null ? void 0 : _c2.call(
|
|
123
|
-
store,
|
|
124
|
-
store
|
|
125
|
-
);
|
|
126
|
-
};
|
|
127
|
-
}, [store]);
|
|
128
|
-
(_a2 = props.onRender) == null ? void 0 : _a2.call(props, store.storeTools, store);
|
|
129
|
-
return store;
|
|
130
|
-
};
|
|
131
|
-
const Provider = (args) => {
|
|
132
|
-
const store = useProviderValue(args);
|
|
133
|
-
return (0, import_react.createElement)(Context.Provider, { value: store.use }, args.children);
|
|
134
|
-
};
|
|
135
|
-
Provider.displayName = `${contextIdentifier}:Provider`;
|
|
136
|
-
Context.displayName = `${contextIdentifier}`;
|
|
137
|
-
const providerExtensions = {
|
|
138
|
-
makeProviderWrapper: (parentOptions) => {
|
|
139
|
-
const context = {
|
|
140
|
-
current: void 0,
|
|
141
|
-
instance: void 0
|
|
142
|
-
};
|
|
143
|
-
const wrapper = (_a2) => {
|
|
144
|
-
var _b = _a2, { children } = _b, options = __objRest(_b, ["children"]);
|
|
145
|
-
return (0, import_react.createElement)(
|
|
146
|
-
Provider,
|
|
147
|
-
__spreadProps(__spreadValues(__spreadValues({}, parentOptions), options), {
|
|
148
|
-
onCreated: (ctx, store) => {
|
|
149
|
-
var _a3;
|
|
150
|
-
context.current = ctx;
|
|
151
|
-
context.instance = store;
|
|
152
|
-
(_a3 = parentOptions == null ? void 0 : parentOptions.onCreated) == null ? void 0 : _a3.call(parentOptions, ctx, store);
|
|
153
|
-
}
|
|
154
|
-
}),
|
|
155
|
-
children
|
|
156
|
-
);
|
|
157
|
-
};
|
|
158
|
-
return { wrapper, context };
|
|
159
|
-
}
|
|
160
|
-
};
|
|
161
|
-
const use = (...args) => {
|
|
162
|
-
(0, import_react.useDebugValue)(`${contextIdentifier}:use`);
|
|
163
|
-
const hook = (0, import_react.useContext)(Context);
|
|
164
|
-
if (!hook) throw new Error("use hook must be used within a ContextProvider");
|
|
165
|
-
return hook(...args);
|
|
166
|
-
};
|
|
167
|
-
const api = () => {
|
|
168
|
-
(0, import_react.useDebugValue)(`${contextIdentifier}:api`);
|
|
169
|
-
const hook = (0, import_react.useContext)(Context);
|
|
170
|
-
if (!hook) throw new Error("api hook must be used within a ContextProvider");
|
|
171
|
-
return hook;
|
|
172
|
-
};
|
|
173
|
-
const observable = (...args) => {
|
|
174
|
-
(0, import_react.useDebugValue)(`${contextIdentifier}:observable`);
|
|
175
|
-
const context = api();
|
|
176
|
-
const props = (0, import_react.useRef)(args);
|
|
177
|
-
props.current = args;
|
|
178
|
-
return (0, import_react.useMemo)(() => {
|
|
179
|
-
var _a2, _b, _c;
|
|
180
|
-
return context.createObservable(
|
|
181
|
-
(state) => {
|
|
182
|
-
const selector = props.current[0];
|
|
183
|
-
return selector(state);
|
|
184
|
-
},
|
|
185
|
-
{
|
|
186
|
-
// we call the equality in this way to execute always the latest version of the functions
|
|
187
|
-
// check if the root state changed
|
|
188
|
-
isEqualRoot: !((_a2 = props.current[1]) == null ? void 0 : _a2.isEqualRoot) ? void 0 : (current, next) => {
|
|
189
|
-
const isEqualRoot = props.current[1].isEqualRoot;
|
|
190
|
-
return Boolean(isEqualRoot(current, next));
|
|
191
|
-
},
|
|
192
|
-
// check if the selection changed
|
|
193
|
-
isEqual: !((_b = props.current[1]) == null ? void 0 : _b.isEqual) ? void 0 : (current, next) => {
|
|
194
|
-
const isEqual = props.current[1].isEqual;
|
|
195
|
-
return Boolean(isEqual(current, next));
|
|
196
|
-
},
|
|
197
|
-
name: (_c = props.current[1]) == null ? void 0 : _c.name
|
|
198
|
-
}
|
|
199
|
-
);
|
|
200
|
-
}, [context]);
|
|
201
|
-
};
|
|
202
|
-
const useExtensions = {
|
|
203
|
-
displayName: Context.displayName,
|
|
204
|
-
createSelectorHook: createSelectorHook.bind(use),
|
|
205
|
-
api,
|
|
206
|
-
select: (...args) => use(...args)[0],
|
|
207
|
-
observable,
|
|
208
|
-
actions: () => {
|
|
209
|
-
return api().actions;
|
|
210
|
-
}
|
|
211
|
-
};
|
|
212
|
-
Object.assign(Provider, providerExtensions);
|
|
213
|
-
Object.assign(use, useExtensions);
|
|
214
|
-
return {
|
|
215
|
-
use,
|
|
216
|
-
Provider,
|
|
217
|
-
Context
|
|
218
|
-
};
|
|
219
|
-
};
|
|
220
|
-
function createSelectorHook(selector, hookConfig) {
|
|
221
|
-
var _a;
|
|
222
|
-
const selectorIdentifier = (_a = hookConfig == null ? void 0 : hookConfig.name) != null ? _a : (0, import_uniqueId.default)("sh:");
|
|
223
|
-
const use = (...selectorArgs) => {
|
|
224
|
-
const context = this.api();
|
|
225
|
-
(0, import_react.useDebugValue)(`${this.displayName}:selector`);
|
|
226
|
-
(0, import_react.useDebugValue)(`${selectorIdentifier}:use`);
|
|
227
|
-
return (0, import_react.useMemo)(() => {
|
|
228
|
-
return context.createSelectorHook(selector, hookConfig);
|
|
229
|
-
}, [context])(...selectorArgs);
|
|
230
|
-
};
|
|
231
|
-
const api = () => {
|
|
232
|
-
(0, import_react.useDebugValue)(`${this.displayName}:selector`);
|
|
233
|
-
(0, import_react.useDebugValue)(`${selectorIdentifier}:api`);
|
|
234
|
-
const context = this.api();
|
|
235
|
-
return (0, import_react.useMemo)(() => {
|
|
236
|
-
const observable = context.createObservable(selector, hookConfig);
|
|
237
|
-
return __spreadProps(__spreadValues({}, context), {
|
|
238
|
-
getState: () => observable.getState(),
|
|
239
|
-
subscribe: observable.subscribe.bind(observable),
|
|
240
|
-
createSelectorHook: import_GlobalStore.createSelectorHook.bind(observable),
|
|
241
|
-
createObservable: import_GlobalStore.createObservable.bind(observable)
|
|
242
|
-
});
|
|
243
|
-
}, [context]);
|
|
244
|
-
};
|
|
245
|
-
const publicExtensions = {
|
|
246
|
-
displayName: this.displayName,
|
|
247
|
-
createSelectorHook: createSelectorHook.bind(use),
|
|
248
|
-
api
|
|
249
|
-
};
|
|
250
|
-
Object.assign(use, publicExtensions);
|
|
251
|
-
return use;
|
|
252
|
-
}
|
|
253
|
-
var createContext_default = createContext;
|
package/createContext.d.ts
DELETED
|
@@ -1,15 +0,0 @@
|
|
|
1
|
-
import type { CreateContext } from './types';
|
|
2
|
-
/**
|
|
3
|
-
* @description Creates a highly granular React context with its associated provider and state hook.
|
|
4
|
-
* Unlike the native `React.createContext`, this version provides fine-grained reactivity and supports
|
|
5
|
-
* state selection, metadata handling, and optional custom actions for controlled mutations.
|
|
6
|
-
*
|
|
7
|
-
* Components using the generated hook only re-render when the selected part of the state changes,
|
|
8
|
-
* making it efficient for large or deeply nested state trees.
|
|
9
|
-
*/
|
|
10
|
-
export declare const createContext: CreateContext;
|
|
11
|
-
export default createContext;
|
|
12
|
-
/**
|
|
13
|
-
* Temporary re-export types for backwards compatibility
|
|
14
|
-
*/
|
|
15
|
-
export type { GlobalStoreContextCallbacks, GlobalStoreCallbacks, ContextActionCollectionResult, ContextActionCollectionConfig, ContextStoreToolsExtensions, ContextStoreTools, ContextProviderExtensions, ContextProvider, ReadonlyContextHook, ContextHook, ContextPublicApi, ReadonlyContextPublicApi, CreateContext, InferContextApi, InferStateApi, } from './types';
|
package/createContext.js
DELETED
|
@@ -1,253 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
var __create = Object.create;
|
|
3
|
-
var __defProp = Object.defineProperty;
|
|
4
|
-
var __defProps = Object.defineProperties;
|
|
5
|
-
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
6
|
-
var __getOwnPropDescs = Object.getOwnPropertyDescriptors;
|
|
7
|
-
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
8
|
-
var __getOwnPropSymbols = Object.getOwnPropertySymbols;
|
|
9
|
-
var __getProtoOf = Object.getPrototypeOf;
|
|
10
|
-
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
11
|
-
var __propIsEnum = Object.prototype.propertyIsEnumerable;
|
|
12
|
-
var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
|
|
13
|
-
var __spreadValues = (a, b) => {
|
|
14
|
-
for (var prop in b || (b = {}))
|
|
15
|
-
if (__hasOwnProp.call(b, prop))
|
|
16
|
-
__defNormalProp(a, prop, b[prop]);
|
|
17
|
-
if (__getOwnPropSymbols)
|
|
18
|
-
for (var prop of __getOwnPropSymbols(b)) {
|
|
19
|
-
if (__propIsEnum.call(b, prop))
|
|
20
|
-
__defNormalProp(a, prop, b[prop]);
|
|
21
|
-
}
|
|
22
|
-
return a;
|
|
23
|
-
};
|
|
24
|
-
var __spreadProps = (a, b) => __defProps(a, __getOwnPropDescs(b));
|
|
25
|
-
var __objRest = (source, exclude) => {
|
|
26
|
-
var target = {};
|
|
27
|
-
for (var prop in source)
|
|
28
|
-
if (__hasOwnProp.call(source, prop) && exclude.indexOf(prop) < 0)
|
|
29
|
-
target[prop] = source[prop];
|
|
30
|
-
if (source != null && __getOwnPropSymbols)
|
|
31
|
-
for (var prop of __getOwnPropSymbols(source)) {
|
|
32
|
-
if (exclude.indexOf(prop) < 0 && __propIsEnum.call(source, prop))
|
|
33
|
-
target[prop] = source[prop];
|
|
34
|
-
}
|
|
35
|
-
return target;
|
|
36
|
-
};
|
|
37
|
-
var __export = (target, all) => {
|
|
38
|
-
for (var name in all)
|
|
39
|
-
__defProp(target, name, { get: all[name], enumerable: true });
|
|
40
|
-
};
|
|
41
|
-
var __copyProps = (to, from, except, desc) => {
|
|
42
|
-
if (from && typeof from === "object" || typeof from === "function") {
|
|
43
|
-
for (let key of __getOwnPropNames(from))
|
|
44
|
-
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
45
|
-
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
46
|
-
}
|
|
47
|
-
return to;
|
|
48
|
-
};
|
|
49
|
-
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
|
|
50
|
-
// If the importer is in node compatibility mode or this is not an ESM
|
|
51
|
-
// file that has been converted to a CommonJS file using a Babel-
|
|
52
|
-
// compatible transform (i.e. "__esModule" has not been set), then set
|
|
53
|
-
// "default" to the CommonJS "module.exports" for node compatibility.
|
|
54
|
-
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
|
|
55
|
-
mod
|
|
56
|
-
));
|
|
57
|
-
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
58
|
-
|
|
59
|
-
// src/createContext.ts
|
|
60
|
-
var createContext_exports = {};
|
|
61
|
-
__export(createContext_exports, {
|
|
62
|
-
createContext: () => createContext,
|
|
63
|
-
default: () => createContext_default
|
|
64
|
-
});
|
|
65
|
-
module.exports = __toCommonJS(createContext_exports);
|
|
66
|
-
var import_react = require("react");
|
|
67
|
-
var import_GlobalStore = require("./GlobalStore.js");
|
|
68
|
-
var import_isFunction = __toESM(require("json-storage-formatter/isFunction"));
|
|
69
|
-
var import_uniqueId = __toESM(require("./uniqueId.js"));
|
|
70
|
-
var createContext = (valueArg, contextArgs = {}) => {
|
|
71
|
-
var _a;
|
|
72
|
-
const Context = (0, import_react.createContext)(null);
|
|
73
|
-
const contextIdentifier = (_a = contextArgs.name) != null ? _a : (0, import_uniqueId.default)("ctx:");
|
|
74
|
-
const getInheritedState = () => (0, import_isFunction.default)(valueArg) ? valueArg() : valueArg;
|
|
75
|
-
const hasValueProp = (props) => {
|
|
76
|
-
return Object.prototype.hasOwnProperty.call(props, "value");
|
|
77
|
-
};
|
|
78
|
-
const useProviderValue = (props) => {
|
|
79
|
-
var _a2;
|
|
80
|
-
(0, import_react.useDebugValue)(`${contextIdentifier}:Provider`);
|
|
81
|
-
const [store] = (0, import_react.useState)(() => {
|
|
82
|
-
var _a3, _b, _c, _d;
|
|
83
|
-
const initialValue = (() => {
|
|
84
|
-
if (hasValueProp(props))
|
|
85
|
-
return (0, import_isFunction.default)(props.value) ? props.value(getInheritedState()) : props.value;
|
|
86
|
-
return getInheritedState();
|
|
87
|
-
})();
|
|
88
|
-
const store2 = new import_GlobalStore.GlobalStore(initialValue, __spreadProps(__spreadValues({}, contextArgs), {
|
|
89
|
-
metadata: (_a3 = (0, import_isFunction.default)(contextArgs.metadata) ? contextArgs.metadata() : contextArgs.metadata) != null ? _a3 : {}
|
|
90
|
-
}));
|
|
91
|
-
const storeToolsExtensions = {
|
|
92
|
-
use
|
|
93
|
-
};
|
|
94
|
-
Object.assign(store2.storeTools, storeToolsExtensions);
|
|
95
|
-
(_c = (_b = contextArgs.callbacks) == null ? void 0 : _b.onCreated) == null ? void 0 : _c.call(
|
|
96
|
-
_b,
|
|
97
|
-
store2.storeTools,
|
|
98
|
-
store2
|
|
99
|
-
);
|
|
100
|
-
(_d = props.onCreated) == null ? void 0 : _d.call(props, store2.storeTools, store2);
|
|
101
|
-
return store2;
|
|
102
|
-
});
|
|
103
|
-
(0, import_react.useLayoutEffect)(() => {
|
|
104
|
-
const shouldUpdateState = hasValueProp(props) && !(0, import_isFunction.default)(props.value);
|
|
105
|
-
if (!shouldUpdateState) return;
|
|
106
|
-
store.setState(props.value);
|
|
107
|
-
}, [store, props.value]);
|
|
108
|
-
(0, import_react.useEffect)(() => {
|
|
109
|
-
var _a3, _b, _c;
|
|
110
|
-
const unsubscribes = [
|
|
111
|
-
(_b = (_a3 = contextArgs.callbacks) == null ? void 0 : _a3.onMounted) == null ? void 0 : _b.call(
|
|
112
|
-
_a3,
|
|
113
|
-
store.storeTools,
|
|
114
|
-
store
|
|
115
|
-
),
|
|
116
|
-
(_c = props.onMounted) == null ? void 0 : _c.call(props, store.storeTools, store)
|
|
117
|
-
].filter(Boolean);
|
|
118
|
-
return () => {
|
|
119
|
-
var _a4, _b2, _c2;
|
|
120
|
-
(_b2 = (_a4 = store.callbacks) == null ? void 0 : _a4.onUnMount) == null ? void 0 : _b2.call(_a4, store);
|
|
121
|
-
unsubscribes.forEach((unsubscribe) => unsubscribe == null ? void 0 : unsubscribe());
|
|
122
|
-
(_c2 = store.__onUnMountContext) == null ? void 0 : _c2.call(
|
|
123
|
-
store,
|
|
124
|
-
store
|
|
125
|
-
);
|
|
126
|
-
};
|
|
127
|
-
}, [store]);
|
|
128
|
-
(_a2 = props.onRender) == null ? void 0 : _a2.call(props, store.storeTools, store);
|
|
129
|
-
return store;
|
|
130
|
-
};
|
|
131
|
-
const Provider = (args) => {
|
|
132
|
-
const store = useProviderValue(args);
|
|
133
|
-
return (0, import_react.createElement)(Context.Provider, { value: store.use }, args.children);
|
|
134
|
-
};
|
|
135
|
-
Provider.displayName = `${contextIdentifier}:Provider`;
|
|
136
|
-
Context.displayName = `${contextIdentifier}`;
|
|
137
|
-
const providerExtensions = {
|
|
138
|
-
makeProviderWrapper: (parentOptions) => {
|
|
139
|
-
const context = {
|
|
140
|
-
current: void 0,
|
|
141
|
-
instance: void 0
|
|
142
|
-
};
|
|
143
|
-
const wrapper = (_a2) => {
|
|
144
|
-
var _b = _a2, { children } = _b, options = __objRest(_b, ["children"]);
|
|
145
|
-
return (0, import_react.createElement)(
|
|
146
|
-
Provider,
|
|
147
|
-
__spreadProps(__spreadValues(__spreadValues({}, parentOptions), options), {
|
|
148
|
-
onCreated: (ctx, store) => {
|
|
149
|
-
var _a3;
|
|
150
|
-
context.current = ctx;
|
|
151
|
-
context.instance = store;
|
|
152
|
-
(_a3 = parentOptions == null ? void 0 : parentOptions.onCreated) == null ? void 0 : _a3.call(parentOptions, ctx, store);
|
|
153
|
-
}
|
|
154
|
-
}),
|
|
155
|
-
children
|
|
156
|
-
);
|
|
157
|
-
};
|
|
158
|
-
return { wrapper, context };
|
|
159
|
-
}
|
|
160
|
-
};
|
|
161
|
-
const use = (...args) => {
|
|
162
|
-
(0, import_react.useDebugValue)(`${contextIdentifier}:use`);
|
|
163
|
-
const hook = (0, import_react.useContext)(Context);
|
|
164
|
-
if (!hook) throw new Error("use hook must be used within a ContextProvider");
|
|
165
|
-
return hook(...args);
|
|
166
|
-
};
|
|
167
|
-
const api = () => {
|
|
168
|
-
(0, import_react.useDebugValue)(`${contextIdentifier}:api`);
|
|
169
|
-
const hook = (0, import_react.useContext)(Context);
|
|
170
|
-
if (!hook) throw new Error("api hook must be used within a ContextProvider");
|
|
171
|
-
return hook;
|
|
172
|
-
};
|
|
173
|
-
const observable = (...args) => {
|
|
174
|
-
(0, import_react.useDebugValue)(`${contextIdentifier}:observable`);
|
|
175
|
-
const context = api();
|
|
176
|
-
const props = (0, import_react.useRef)(args);
|
|
177
|
-
props.current = args;
|
|
178
|
-
return (0, import_react.useMemo)(() => {
|
|
179
|
-
var _a2, _b, _c;
|
|
180
|
-
return context.createObservable(
|
|
181
|
-
(state) => {
|
|
182
|
-
const selector = props.current[0];
|
|
183
|
-
return selector(state);
|
|
184
|
-
},
|
|
185
|
-
{
|
|
186
|
-
// we call the equality in this way to execute always the latest version of the functions
|
|
187
|
-
// check if the root state changed
|
|
188
|
-
isEqualRoot: !((_a2 = props.current[1]) == null ? void 0 : _a2.isEqualRoot) ? void 0 : (current, next) => {
|
|
189
|
-
const isEqualRoot = props.current[1].isEqualRoot;
|
|
190
|
-
return Boolean(isEqualRoot(current, next));
|
|
191
|
-
},
|
|
192
|
-
// check if the selection changed
|
|
193
|
-
isEqual: !((_b = props.current[1]) == null ? void 0 : _b.isEqual) ? void 0 : (current, next) => {
|
|
194
|
-
const isEqual = props.current[1].isEqual;
|
|
195
|
-
return Boolean(isEqual(current, next));
|
|
196
|
-
},
|
|
197
|
-
name: (_c = props.current[1]) == null ? void 0 : _c.name
|
|
198
|
-
}
|
|
199
|
-
);
|
|
200
|
-
}, [context]);
|
|
201
|
-
};
|
|
202
|
-
const useExtensions = {
|
|
203
|
-
displayName: Context.displayName,
|
|
204
|
-
createSelectorHook: createSelectorHook.bind(use),
|
|
205
|
-
api,
|
|
206
|
-
select: (...args) => use(...args)[0],
|
|
207
|
-
observable,
|
|
208
|
-
actions: () => {
|
|
209
|
-
return api().actions;
|
|
210
|
-
}
|
|
211
|
-
};
|
|
212
|
-
Object.assign(Provider, providerExtensions);
|
|
213
|
-
Object.assign(use, useExtensions);
|
|
214
|
-
return {
|
|
215
|
-
use,
|
|
216
|
-
Provider,
|
|
217
|
-
Context
|
|
218
|
-
};
|
|
219
|
-
};
|
|
220
|
-
function createSelectorHook(selector, hookConfig) {
|
|
221
|
-
var _a;
|
|
222
|
-
const selectorIdentifier = (_a = hookConfig == null ? void 0 : hookConfig.name) != null ? _a : (0, import_uniqueId.default)("sh:");
|
|
223
|
-
const use = (...selectorArgs) => {
|
|
224
|
-
const context = this.api();
|
|
225
|
-
(0, import_react.useDebugValue)(`${this.displayName}:selector`);
|
|
226
|
-
(0, import_react.useDebugValue)(`${selectorIdentifier}:use`);
|
|
227
|
-
return (0, import_react.useMemo)(() => {
|
|
228
|
-
return context.createSelectorHook(selector, hookConfig);
|
|
229
|
-
}, [context])(...selectorArgs);
|
|
230
|
-
};
|
|
231
|
-
const api = () => {
|
|
232
|
-
(0, import_react.useDebugValue)(`${this.displayName}:selector`);
|
|
233
|
-
(0, import_react.useDebugValue)(`${selectorIdentifier}:api`);
|
|
234
|
-
const context = this.api();
|
|
235
|
-
return (0, import_react.useMemo)(() => {
|
|
236
|
-
const observable = context.createObservable(selector, hookConfig);
|
|
237
|
-
return __spreadProps(__spreadValues({}, context), {
|
|
238
|
-
getState: () => observable.getState(),
|
|
239
|
-
subscribe: observable.subscribe.bind(observable),
|
|
240
|
-
createSelectorHook: import_GlobalStore.createSelectorHook.bind(observable),
|
|
241
|
-
createObservable: import_GlobalStore.createObservable.bind(observable)
|
|
242
|
-
});
|
|
243
|
-
}, [context]);
|
|
244
|
-
};
|
|
245
|
-
const publicExtensions = {
|
|
246
|
-
displayName: this.displayName,
|
|
247
|
-
createSelectorHook: createSelectorHook.bind(use),
|
|
248
|
-
api
|
|
249
|
-
};
|
|
250
|
-
Object.assign(use, publicExtensions);
|
|
251
|
-
return use;
|
|
252
|
-
}
|
|
253
|
-
var createContext_default = createContext;
|