@pixotope/react-context-store 0.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/.turbo/turbo-build.log +20 -0
- package/.turbo/turbo-check-types.log +4 -0
- package/.turbo/turbo-test.log +14 -0
- package/dist/index.cjs +404 -0
- package/dist/index.cjs.map +1 -0
- package/dist/index.d.cts +54 -0
- package/dist/index.d.ts +54 -0
- package/dist/index.js +402 -0
- package/dist/index.js.map +1 -0
- package/package.json +33 -0
- package/src/index.ts +1 -0
- package/src/store.test.tsx +238 -0
- package/src/store.tsx +327 -0
- package/src/vitest-setup.ts +1 -0
- package/tsconfig.json +28 -0
- package/vitest.config.mts +21 -0
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
|
|
2
|
+
> @pixotope/react-context-store@0.0.1 build /home/circleci/project/packages/react-context-store
|
|
3
|
+
> tsup src/index.ts --format cjs,esm --dts --sourcemap
|
|
4
|
+
|
|
5
|
+
[34mCLI[39m Building entry: src/index.ts
|
|
6
|
+
[34mCLI[39m Using tsconfig: tsconfig.json
|
|
7
|
+
[34mCLI[39m tsup v8.5.0
|
|
8
|
+
[34mCLI[39m Target: es6
|
|
9
|
+
[34mCJS[39m Build start
|
|
10
|
+
[34mESM[39m Build start
|
|
11
|
+
[32mCJS[39m [1mdist/index.cjs [22m[32m15.37 KB[39m
|
|
12
|
+
[32mCJS[39m [1mdist/index.cjs.map [22m[32m25.31 KB[39m
|
|
13
|
+
[32mCJS[39m ⚡️ Build success in 29ms
|
|
14
|
+
[32mESM[39m [1mdist/index.js [22m[32m15.11 KB[39m
|
|
15
|
+
[32mESM[39m [1mdist/index.js.map [22m[32m25.09 KB[39m
|
|
16
|
+
[32mESM[39m ⚡️ Build success in 29ms
|
|
17
|
+
[34mDTS[39m Build start
|
|
18
|
+
[32mDTS[39m ⚡️ Build success in 1514ms
|
|
19
|
+
[32mDTS[39m [1mdist/index.d.cts [22m[32m2.21 KB[39m
|
|
20
|
+
[32mDTS[39m [1mdist/index.d.ts [22m[32m2.21 KB[39m
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
|
|
2
|
+
> @pixotope/react-context-store@0.0.1 test /home/circleci/project/packages/react-context-store
|
|
3
|
+
> vitest
|
|
4
|
+
|
|
5
|
+
|
|
6
|
+
[1m[46m RUN [49m[22m [36mv3.2.4 [39m[90m/home/circleci/project/packages/react-context-store[39m
|
|
7
|
+
|
|
8
|
+
[32m✓[39m src/store.test.tsx [2m([22m[2m12 tests[22m[2m)[22m[32m 45[2mms[22m[39m
|
|
9
|
+
|
|
10
|
+
[2m Test Files [22m [1m[32m1 passed[39m[22m[90m (1)[39m
|
|
11
|
+
[2m Tests [22m [1m[32m12 passed[39m[22m[90m (12)[39m
|
|
12
|
+
[2m Start at [22m 08:21:39
|
|
13
|
+
[2m Duration [22m 2.67s[2m (transform 88ms, setup 110ms, collect 276ms, tests 45ms, environment 486ms, prepare 405ms)[22m
|
|
14
|
+
|
package/dist/index.cjs
ADDED
|
@@ -0,0 +1,404 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __create = Object.create;
|
|
3
|
+
var __defProp = Object.defineProperty;
|
|
4
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
5
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
6
|
+
var __getProtoOf = Object.getPrototypeOf;
|
|
7
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
8
|
+
var __commonJS = (cb, mod) => function __require() {
|
|
9
|
+
return mod || (0, cb[__getOwnPropNames(cb)[0]])((mod = { exports: {} }).exports, mod), mod.exports;
|
|
10
|
+
};
|
|
11
|
+
var __export = (target, all) => {
|
|
12
|
+
for (var name in all)
|
|
13
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
14
|
+
};
|
|
15
|
+
var __copyProps = (to, from, except, desc) => {
|
|
16
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
17
|
+
for (let key of __getOwnPropNames(from))
|
|
18
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
19
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
20
|
+
}
|
|
21
|
+
return to;
|
|
22
|
+
};
|
|
23
|
+
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
|
|
24
|
+
// If the importer is in node compatibility mode or this is not an ESM
|
|
25
|
+
// file that has been converted to a CommonJS file using a Babel-
|
|
26
|
+
// compatible transform (i.e. "__esModule" has not been set), then set
|
|
27
|
+
// "default" to the CommonJS "module.exports" for node compatibility.
|
|
28
|
+
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
|
|
29
|
+
mod
|
|
30
|
+
));
|
|
31
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
32
|
+
|
|
33
|
+
// ../../node_modules/.pnpm/use-sync-external-store@1.5.0_react@19.1.0/node_modules/use-sync-external-store/cjs/use-sync-external-store-shim.production.js
|
|
34
|
+
var require_use_sync_external_store_shim_production = __commonJS({
|
|
35
|
+
"../../node_modules/.pnpm/use-sync-external-store@1.5.0_react@19.1.0/node_modules/use-sync-external-store/cjs/use-sync-external-store-shim.production.js"(exports2) {
|
|
36
|
+
"use strict";
|
|
37
|
+
var React2 = require("react");
|
|
38
|
+
function is(x, y) {
|
|
39
|
+
return x === y && (0 !== x || 1 / x === 1 / y) || x !== x && y !== y;
|
|
40
|
+
}
|
|
41
|
+
var objectIs = "function" === typeof Object.is ? Object.is : is;
|
|
42
|
+
var useState = React2.useState;
|
|
43
|
+
var useEffect = React2.useEffect;
|
|
44
|
+
var useLayoutEffect = React2.useLayoutEffect;
|
|
45
|
+
var useDebugValue = React2.useDebugValue;
|
|
46
|
+
function useSyncExternalStore$2(subscribe, getSnapshot) {
|
|
47
|
+
var value = getSnapshot(), _useState = useState({ inst: { value, getSnapshot } }), inst = _useState[0].inst, forceUpdate = _useState[1];
|
|
48
|
+
useLayoutEffect(
|
|
49
|
+
function() {
|
|
50
|
+
inst.value = value;
|
|
51
|
+
inst.getSnapshot = getSnapshot;
|
|
52
|
+
checkIfSnapshotChanged(inst) && forceUpdate({ inst });
|
|
53
|
+
},
|
|
54
|
+
[subscribe, value, getSnapshot]
|
|
55
|
+
);
|
|
56
|
+
useEffect(
|
|
57
|
+
function() {
|
|
58
|
+
checkIfSnapshotChanged(inst) && forceUpdate({ inst });
|
|
59
|
+
return subscribe(function() {
|
|
60
|
+
checkIfSnapshotChanged(inst) && forceUpdate({ inst });
|
|
61
|
+
});
|
|
62
|
+
},
|
|
63
|
+
[subscribe]
|
|
64
|
+
);
|
|
65
|
+
useDebugValue(value);
|
|
66
|
+
return value;
|
|
67
|
+
}
|
|
68
|
+
function checkIfSnapshotChanged(inst) {
|
|
69
|
+
var latestGetSnapshot = inst.getSnapshot;
|
|
70
|
+
inst = inst.value;
|
|
71
|
+
try {
|
|
72
|
+
var nextValue = latestGetSnapshot();
|
|
73
|
+
return !objectIs(inst, nextValue);
|
|
74
|
+
} catch (error) {
|
|
75
|
+
return true;
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
function useSyncExternalStore$1(subscribe, getSnapshot) {
|
|
79
|
+
return getSnapshot();
|
|
80
|
+
}
|
|
81
|
+
var shim = "undefined" === typeof window || "undefined" === typeof window.document || "undefined" === typeof window.document.createElement ? useSyncExternalStore$1 : useSyncExternalStore$2;
|
|
82
|
+
exports2.useSyncExternalStore = void 0 !== React2.useSyncExternalStore ? React2.useSyncExternalStore : shim;
|
|
83
|
+
}
|
|
84
|
+
});
|
|
85
|
+
|
|
86
|
+
// ../../node_modules/.pnpm/use-sync-external-store@1.5.0_react@19.1.0/node_modules/use-sync-external-store/cjs/use-sync-external-store-shim.development.js
|
|
87
|
+
var require_use_sync_external_store_shim_development = __commonJS({
|
|
88
|
+
"../../node_modules/.pnpm/use-sync-external-store@1.5.0_react@19.1.0/node_modules/use-sync-external-store/cjs/use-sync-external-store-shim.development.js"(exports2) {
|
|
89
|
+
"use strict";
|
|
90
|
+
"production" !== process.env.NODE_ENV && function() {
|
|
91
|
+
function is(x, y) {
|
|
92
|
+
return x === y && (0 !== x || 1 / x === 1 / y) || x !== x && y !== y;
|
|
93
|
+
}
|
|
94
|
+
function useSyncExternalStore$2(subscribe, getSnapshot) {
|
|
95
|
+
didWarnOld18Alpha || void 0 === React2.startTransition || (didWarnOld18Alpha = true, console.error(
|
|
96
|
+
"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."
|
|
97
|
+
));
|
|
98
|
+
var value = getSnapshot();
|
|
99
|
+
if (!didWarnUncachedGetSnapshot) {
|
|
100
|
+
var cachedValue = getSnapshot();
|
|
101
|
+
objectIs(value, cachedValue) || (console.error(
|
|
102
|
+
"The result of getSnapshot should be cached to avoid an infinite loop"
|
|
103
|
+
), didWarnUncachedGetSnapshot = true);
|
|
104
|
+
}
|
|
105
|
+
cachedValue = useState({
|
|
106
|
+
inst: { value, getSnapshot }
|
|
107
|
+
});
|
|
108
|
+
var inst = cachedValue[0].inst, forceUpdate = cachedValue[1];
|
|
109
|
+
useLayoutEffect(
|
|
110
|
+
function() {
|
|
111
|
+
inst.value = value;
|
|
112
|
+
inst.getSnapshot = getSnapshot;
|
|
113
|
+
checkIfSnapshotChanged(inst) && forceUpdate({ inst });
|
|
114
|
+
},
|
|
115
|
+
[subscribe, value, getSnapshot]
|
|
116
|
+
);
|
|
117
|
+
useEffect(
|
|
118
|
+
function() {
|
|
119
|
+
checkIfSnapshotChanged(inst) && forceUpdate({ inst });
|
|
120
|
+
return subscribe(function() {
|
|
121
|
+
checkIfSnapshotChanged(inst) && forceUpdate({ inst });
|
|
122
|
+
});
|
|
123
|
+
},
|
|
124
|
+
[subscribe]
|
|
125
|
+
);
|
|
126
|
+
useDebugValue(value);
|
|
127
|
+
return value;
|
|
128
|
+
}
|
|
129
|
+
function checkIfSnapshotChanged(inst) {
|
|
130
|
+
var latestGetSnapshot = inst.getSnapshot;
|
|
131
|
+
inst = inst.value;
|
|
132
|
+
try {
|
|
133
|
+
var nextValue = latestGetSnapshot();
|
|
134
|
+
return !objectIs(inst, nextValue);
|
|
135
|
+
} catch (error) {
|
|
136
|
+
return true;
|
|
137
|
+
}
|
|
138
|
+
}
|
|
139
|
+
function useSyncExternalStore$1(subscribe, getSnapshot) {
|
|
140
|
+
return getSnapshot();
|
|
141
|
+
}
|
|
142
|
+
"undefined" !== typeof __REACT_DEVTOOLS_GLOBAL_HOOK__ && "function" === typeof __REACT_DEVTOOLS_GLOBAL_HOOK__.registerInternalModuleStart && __REACT_DEVTOOLS_GLOBAL_HOOK__.registerInternalModuleStart(Error());
|
|
143
|
+
var React2 = require("react"), objectIs = "function" === typeof Object.is ? Object.is : is, useState = React2.useState, useEffect = React2.useEffect, useLayoutEffect = React2.useLayoutEffect, useDebugValue = React2.useDebugValue, didWarnOld18Alpha = false, didWarnUncachedGetSnapshot = false, shim = "undefined" === typeof window || "undefined" === typeof window.document || "undefined" === typeof window.document.createElement ? useSyncExternalStore$1 : useSyncExternalStore$2;
|
|
144
|
+
exports2.useSyncExternalStore = void 0 !== React2.useSyncExternalStore ? React2.useSyncExternalStore : shim;
|
|
145
|
+
"undefined" !== typeof __REACT_DEVTOOLS_GLOBAL_HOOK__ && "function" === typeof __REACT_DEVTOOLS_GLOBAL_HOOK__.registerInternalModuleStop && __REACT_DEVTOOLS_GLOBAL_HOOK__.registerInternalModuleStop(Error());
|
|
146
|
+
}();
|
|
147
|
+
}
|
|
148
|
+
});
|
|
149
|
+
|
|
150
|
+
// ../../node_modules/.pnpm/use-sync-external-store@1.5.0_react@19.1.0/node_modules/use-sync-external-store/shim/index.js
|
|
151
|
+
var require_shim = __commonJS({
|
|
152
|
+
"../../node_modules/.pnpm/use-sync-external-store@1.5.0_react@19.1.0/node_modules/use-sync-external-store/shim/index.js"(exports2, module2) {
|
|
153
|
+
"use strict";
|
|
154
|
+
if (process.env.NODE_ENV === "production") {
|
|
155
|
+
module2.exports = require_use_sync_external_store_shim_production();
|
|
156
|
+
} else {
|
|
157
|
+
module2.exports = require_use_sync_external_store_shim_development();
|
|
158
|
+
}
|
|
159
|
+
}
|
|
160
|
+
});
|
|
161
|
+
|
|
162
|
+
// ../utils/dist/comparison/index.js
|
|
163
|
+
var require_comparison = __commonJS({
|
|
164
|
+
"../utils/dist/comparison/index.js"(exports2, module2) {
|
|
165
|
+
"use strict";
|
|
166
|
+
var __defProp2 = Object.defineProperty;
|
|
167
|
+
var __getOwnPropDesc2 = Object.getOwnPropertyDescriptor;
|
|
168
|
+
var __getOwnPropNames2 = Object.getOwnPropertyNames;
|
|
169
|
+
var __hasOwnProp2 = Object.prototype.hasOwnProperty;
|
|
170
|
+
var __export2 = (target, all) => {
|
|
171
|
+
for (var name in all)
|
|
172
|
+
__defProp2(target, name, { get: all[name], enumerable: true });
|
|
173
|
+
};
|
|
174
|
+
var __copyProps2 = (to, from, except, desc) => {
|
|
175
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
176
|
+
for (let key of __getOwnPropNames2(from))
|
|
177
|
+
if (!__hasOwnProp2.call(to, key) && key !== except)
|
|
178
|
+
__defProp2(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc2(from, key)) || desc.enumerable });
|
|
179
|
+
}
|
|
180
|
+
return to;
|
|
181
|
+
};
|
|
182
|
+
var __toCommonJS2 = (mod) => __copyProps2(__defProp2({}, "__esModule", { value: true }), mod);
|
|
183
|
+
var comparison_exports = {};
|
|
184
|
+
__export2(comparison_exports, {
|
|
185
|
+
shallowEqual: () => shallowEqual2
|
|
186
|
+
});
|
|
187
|
+
module2.exports = __toCommonJS2(comparison_exports);
|
|
188
|
+
function shallowEqual2(objA, objB) {
|
|
189
|
+
if (Object.is(objA, objB)) {
|
|
190
|
+
return true;
|
|
191
|
+
}
|
|
192
|
+
if (typeof objA !== "object" || objA === null || typeof objB !== "object" || objB === null) {
|
|
193
|
+
return false;
|
|
194
|
+
}
|
|
195
|
+
if (objA instanceof Map && objB instanceof Map) {
|
|
196
|
+
if (objA.size !== objB.size) return false;
|
|
197
|
+
for (const [key, value] of objA) {
|
|
198
|
+
if (!Object.is(value, objB.get(key))) {
|
|
199
|
+
return false;
|
|
200
|
+
}
|
|
201
|
+
}
|
|
202
|
+
return true;
|
|
203
|
+
}
|
|
204
|
+
if (objA instanceof Set && objB instanceof Set) {
|
|
205
|
+
if (objA.size !== objB.size) return false;
|
|
206
|
+
for (const value of objA) {
|
|
207
|
+
if (!objB.has(value)) {
|
|
208
|
+
return false;
|
|
209
|
+
}
|
|
210
|
+
}
|
|
211
|
+
return true;
|
|
212
|
+
}
|
|
213
|
+
const keysA = Object.keys(objA);
|
|
214
|
+
if (keysA.length !== Object.keys(objB).length) {
|
|
215
|
+
return false;
|
|
216
|
+
}
|
|
217
|
+
for (let i = 0; i < keysA.length; i++) {
|
|
218
|
+
if (!Object.prototype.hasOwnProperty.call(objB, keysA[i]) || !Object.is(objA[keysA[i]], objB[keysA[i]])) {
|
|
219
|
+
return false;
|
|
220
|
+
}
|
|
221
|
+
}
|
|
222
|
+
return true;
|
|
223
|
+
}
|
|
224
|
+
}
|
|
225
|
+
});
|
|
226
|
+
|
|
227
|
+
// src/index.ts
|
|
228
|
+
var index_exports = {};
|
|
229
|
+
__export(index_exports, {
|
|
230
|
+
createContextStore: () => createContextStore
|
|
231
|
+
});
|
|
232
|
+
module.exports = __toCommonJS(index_exports);
|
|
233
|
+
|
|
234
|
+
// src/store.tsx
|
|
235
|
+
var import_react = __toESM(require("react"), 1);
|
|
236
|
+
var import_shim = __toESM(require_shim(), 1);
|
|
237
|
+
var import_comparison = __toESM(require_comparison(), 1);
|
|
238
|
+
var LIB_NAME = "@pixotope/react-context-store";
|
|
239
|
+
function isFunction(value) {
|
|
240
|
+
return typeof value === "function";
|
|
241
|
+
}
|
|
242
|
+
function createContextStore(initialState, actions = {}, options = {}) {
|
|
243
|
+
let globalStore = options.global ? initialState : void 0;
|
|
244
|
+
function useStoreData({
|
|
245
|
+
defaultSubscriber = []
|
|
246
|
+
} = {}) {
|
|
247
|
+
const store = (0, import_react.useRef)(globalStore != null ? globalStore : initialState);
|
|
248
|
+
const get = (0, import_react.useCallback)(() => store.current, []);
|
|
249
|
+
const subscribers = (0, import_react.useRef)(
|
|
250
|
+
new Set(defaultSubscriber)
|
|
251
|
+
);
|
|
252
|
+
const set = (0, import_react.useCallback)((value) => {
|
|
253
|
+
store.current = isFunction(value) ? value(store.current) : value;
|
|
254
|
+
if (options.global) {
|
|
255
|
+
globalStore = store.current;
|
|
256
|
+
}
|
|
257
|
+
subscribers.current.forEach((callback) => callback(store.current));
|
|
258
|
+
}, []);
|
|
259
|
+
const subscribe = (0, import_react.useCallback)((callback) => {
|
|
260
|
+
subscribers.current.add(callback);
|
|
261
|
+
return () => subscribers.current.delete(callback);
|
|
262
|
+
}, []);
|
|
263
|
+
return {
|
|
264
|
+
get,
|
|
265
|
+
set,
|
|
266
|
+
subscribe
|
|
267
|
+
};
|
|
268
|
+
}
|
|
269
|
+
const StoreContext = (0, import_react.createContext)(null);
|
|
270
|
+
function Provider({ children }) {
|
|
271
|
+
return /* @__PURE__ */ import_react.default.createElement(
|
|
272
|
+
StoreContext.Provider,
|
|
273
|
+
{
|
|
274
|
+
value: useStoreData({ defaultSubscriber: [observable.broadcast] })
|
|
275
|
+
},
|
|
276
|
+
children
|
|
277
|
+
);
|
|
278
|
+
}
|
|
279
|
+
const observers = /* @__PURE__ */ new Set();
|
|
280
|
+
function subscribeExternal(selector = (store) => store, callback, compare = (first, second) => first === second) {
|
|
281
|
+
let lastSelectedState;
|
|
282
|
+
const shouldSendUpdates = (newSelectedState) => {
|
|
283
|
+
if (lastSelectedState === void 0 || !compare(lastSelectedState, newSelectedState)) {
|
|
284
|
+
lastSelectedState = newSelectedState;
|
|
285
|
+
return true;
|
|
286
|
+
}
|
|
287
|
+
return false;
|
|
288
|
+
};
|
|
289
|
+
const selectedCb = (store) => {
|
|
290
|
+
const selected = selector(store);
|
|
291
|
+
const shouldSend = shouldSendUpdates(selected);
|
|
292
|
+
if (shouldSend) {
|
|
293
|
+
callback(selector(store));
|
|
294
|
+
}
|
|
295
|
+
};
|
|
296
|
+
observers.add(selectedCb);
|
|
297
|
+
return () => observers.delete(selectedCb);
|
|
298
|
+
}
|
|
299
|
+
const observable = {
|
|
300
|
+
subscribe: subscribeExternal,
|
|
301
|
+
unsubscribe: (callback) => {
|
|
302
|
+
observers.delete(callback);
|
|
303
|
+
},
|
|
304
|
+
broadcast: (state) => {
|
|
305
|
+
observers.forEach((callback) => callback(state));
|
|
306
|
+
}
|
|
307
|
+
};
|
|
308
|
+
function useStore(selector = (store) => store, compare = ((_a) => (_a = options.compare) != null ? _a : import_comparison.shallowEqual)()) {
|
|
309
|
+
const store = (0, import_react.useContext)(StoreContext);
|
|
310
|
+
const lastSelectedState = (0, import_react.useRef)(void 0);
|
|
311
|
+
if (!store) {
|
|
312
|
+
throw new Error(
|
|
313
|
+
`[${LIB_NAME}] Store not found. Make sure the component is wrapped in a ${Provider} component.`
|
|
314
|
+
);
|
|
315
|
+
}
|
|
316
|
+
const state = (0, import_shim.useSyncExternalStore)(
|
|
317
|
+
store.subscribe,
|
|
318
|
+
() => {
|
|
319
|
+
const selectedState = selector(store.get());
|
|
320
|
+
if (lastSelectedState.current === void 0 || !compare(lastSelectedState.current, selectedState)) {
|
|
321
|
+
lastSelectedState.current = selectedState;
|
|
322
|
+
}
|
|
323
|
+
return lastSelectedState.current;
|
|
324
|
+
},
|
|
325
|
+
() => selector(initialState)
|
|
326
|
+
);
|
|
327
|
+
return {
|
|
328
|
+
get: () => state,
|
|
329
|
+
set: store.set,
|
|
330
|
+
selector: () => store.get()
|
|
331
|
+
};
|
|
332
|
+
}
|
|
333
|
+
function useActions() {
|
|
334
|
+
const store = (0, import_react.useContext)(StoreContext);
|
|
335
|
+
if (!store) {
|
|
336
|
+
throw new Error(
|
|
337
|
+
`[${LIB_NAME}] Store not found. Make sure the component is wrapped in a ${Provider} component.`
|
|
338
|
+
);
|
|
339
|
+
}
|
|
340
|
+
const actionProxy = (0, import_react.useMemo)(
|
|
341
|
+
() => new Proxy(actions, {
|
|
342
|
+
get: (target, prop) => {
|
|
343
|
+
const action = target[prop];
|
|
344
|
+
if (action) {
|
|
345
|
+
return (args) => {
|
|
346
|
+
action(
|
|
347
|
+
{
|
|
348
|
+
set: store.set,
|
|
349
|
+
get: store.get
|
|
350
|
+
},
|
|
351
|
+
{
|
|
352
|
+
payload: args
|
|
353
|
+
}
|
|
354
|
+
);
|
|
355
|
+
};
|
|
356
|
+
}
|
|
357
|
+
},
|
|
358
|
+
set: () => {
|
|
359
|
+
throw new Error(`[${LIB_NAME}] Actions cannot be updated`);
|
|
360
|
+
}
|
|
361
|
+
}),
|
|
362
|
+
[]
|
|
363
|
+
);
|
|
364
|
+
return actionProxy;
|
|
365
|
+
}
|
|
366
|
+
const useSetStore = () => useStore(() => false).set;
|
|
367
|
+
return {
|
|
368
|
+
Provider,
|
|
369
|
+
useStore,
|
|
370
|
+
useSetStore,
|
|
371
|
+
useActions,
|
|
372
|
+
subscribe: observable.subscribe,
|
|
373
|
+
unsubscribe: observable.unsubscribe
|
|
374
|
+
};
|
|
375
|
+
}
|
|
376
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
377
|
+
0 && (module.exports = {
|
|
378
|
+
createContextStore
|
|
379
|
+
});
|
|
380
|
+
/*! Bundled license information:
|
|
381
|
+
|
|
382
|
+
use-sync-external-store/cjs/use-sync-external-store-shim.production.js:
|
|
383
|
+
(**
|
|
384
|
+
* @license React
|
|
385
|
+
* use-sync-external-store-shim.production.js
|
|
386
|
+
*
|
|
387
|
+
* Copyright (c) Meta Platforms, Inc. and affiliates.
|
|
388
|
+
*
|
|
389
|
+
* This source code is licensed under the MIT license found in the
|
|
390
|
+
* LICENSE file in the root directory of this source tree.
|
|
391
|
+
*)
|
|
392
|
+
|
|
393
|
+
use-sync-external-store/cjs/use-sync-external-store-shim.development.js:
|
|
394
|
+
(**
|
|
395
|
+
* @license React
|
|
396
|
+
* use-sync-external-store-shim.development.js
|
|
397
|
+
*
|
|
398
|
+
* Copyright (c) Meta Platforms, Inc. and affiliates.
|
|
399
|
+
*
|
|
400
|
+
* This source code is licensed under the MIT license found in the
|
|
401
|
+
* LICENSE file in the root directory of this source tree.
|
|
402
|
+
*)
|
|
403
|
+
*/
|
|
404
|
+
//# sourceMappingURL=index.cjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../../../node_modules/.pnpm/use-sync-external-store@1.5.0_react@19.1.0/node_modules/use-sync-external-store/cjs/use-sync-external-store-shim.production.js","../../../node_modules/.pnpm/use-sync-external-store@1.5.0_react@19.1.0/node_modules/use-sync-external-store/cjs/use-sync-external-store-shim.development.js","../../../node_modules/.pnpm/use-sync-external-store@1.5.0_react@19.1.0/node_modules/use-sync-external-store/shim/index.js","../../utils/dist/comparison/index.js","../src/index.ts","../src/store.tsx"],"sourcesContent":["/**\n * @license React\n * use-sync-external-store-shim.production.js\n *\n * Copyright (c) Meta Platforms, Inc. and affiliates.\n *\n * This source code is licensed under the MIT license found in the\n * LICENSE file in the root directory of this source tree.\n */\n\n\"use strict\";\nvar React = require(\"react\");\nfunction is(x, y) {\n return (x === y && (0 !== x || 1 / x === 1 / y)) || (x !== x && y !== y);\n}\nvar objectIs = \"function\" === typeof Object.is ? Object.is : is,\n useState = React.useState,\n useEffect = React.useEffect,\n useLayoutEffect = React.useLayoutEffect,\n useDebugValue = React.useDebugValue;\nfunction useSyncExternalStore$2(subscribe, getSnapshot) {\n var value = getSnapshot(),\n _useState = useState({ inst: { value: value, getSnapshot: getSnapshot } }),\n inst = _useState[0].inst,\n forceUpdate = _useState[1];\n useLayoutEffect(\n function () {\n inst.value = value;\n inst.getSnapshot = getSnapshot;\n checkIfSnapshotChanged(inst) && forceUpdate({ inst: inst });\n },\n [subscribe, value, getSnapshot]\n );\n useEffect(\n function () {\n checkIfSnapshotChanged(inst) && forceUpdate({ inst: inst });\n return subscribe(function () {\n checkIfSnapshotChanged(inst) && forceUpdate({ inst: inst });\n });\n },\n [subscribe]\n );\n useDebugValue(value);\n return value;\n}\nfunction checkIfSnapshotChanged(inst) {\n var latestGetSnapshot = inst.getSnapshot;\n inst = inst.value;\n try {\n var nextValue = latestGetSnapshot();\n return !objectIs(inst, nextValue);\n } catch (error) {\n return !0;\n }\n}\nfunction useSyncExternalStore$1(subscribe, getSnapshot) {\n return getSnapshot();\n}\nvar shim =\n \"undefined\" === typeof window ||\n \"undefined\" === typeof window.document ||\n \"undefined\" === typeof window.document.createElement\n ? useSyncExternalStore$1\n : useSyncExternalStore$2;\nexports.useSyncExternalStore =\n void 0 !== React.useSyncExternalStore ? React.useSyncExternalStore : shim;\n","/**\n * @license React\n * use-sync-external-store-shim.development.js\n *\n * Copyright (c) Meta Platforms, Inc. and affiliates.\n *\n * This source code is licensed under the MIT license found in the\n * LICENSE file in the root directory of this source tree.\n */\n\n\"use strict\";\n\"production\" !== process.env.NODE_ENV &&\n (function () {\n function is(x, y) {\n return (x === y && (0 !== x || 1 / x === 1 / y)) || (x !== x && y !== y);\n }\n function useSyncExternalStore$2(subscribe, getSnapshot) {\n didWarnOld18Alpha ||\n void 0 === React.startTransition ||\n ((didWarnOld18Alpha = !0),\n console.error(\n \"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.\"\n ));\n var value = getSnapshot();\n if (!didWarnUncachedGetSnapshot) {\n var cachedValue = getSnapshot();\n objectIs(value, cachedValue) ||\n (console.error(\n \"The result of getSnapshot should be cached to avoid an infinite loop\"\n ),\n (didWarnUncachedGetSnapshot = !0));\n }\n cachedValue = useState({\n inst: { value: value, getSnapshot: getSnapshot }\n });\n var inst = cachedValue[0].inst,\n forceUpdate = cachedValue[1];\n useLayoutEffect(\n function () {\n inst.value = value;\n inst.getSnapshot = getSnapshot;\n checkIfSnapshotChanged(inst) && forceUpdate({ inst: inst });\n },\n [subscribe, value, getSnapshot]\n );\n useEffect(\n function () {\n checkIfSnapshotChanged(inst) && forceUpdate({ inst: inst });\n return subscribe(function () {\n checkIfSnapshotChanged(inst) && forceUpdate({ inst: inst });\n });\n },\n [subscribe]\n );\n useDebugValue(value);\n return value;\n }\n function checkIfSnapshotChanged(inst) {\n var latestGetSnapshot = inst.getSnapshot;\n inst = inst.value;\n try {\n var nextValue = latestGetSnapshot();\n return !objectIs(inst, nextValue);\n } catch (error) {\n return !0;\n }\n }\n function useSyncExternalStore$1(subscribe, getSnapshot) {\n return getSnapshot();\n }\n \"undefined\" !== typeof __REACT_DEVTOOLS_GLOBAL_HOOK__ &&\n \"function\" ===\n typeof __REACT_DEVTOOLS_GLOBAL_HOOK__.registerInternalModuleStart &&\n __REACT_DEVTOOLS_GLOBAL_HOOK__.registerInternalModuleStart(Error());\n var React = require(\"react\"),\n objectIs = \"function\" === typeof Object.is ? Object.is : is,\n useState = React.useState,\n useEffect = React.useEffect,\n useLayoutEffect = React.useLayoutEffect,\n useDebugValue = React.useDebugValue,\n didWarnOld18Alpha = !1,\n didWarnUncachedGetSnapshot = !1,\n shim =\n \"undefined\" === typeof window ||\n \"undefined\" === typeof window.document ||\n \"undefined\" === typeof window.document.createElement\n ? useSyncExternalStore$1\n : useSyncExternalStore$2;\n exports.useSyncExternalStore =\n void 0 !== React.useSyncExternalStore ? React.useSyncExternalStore : shim;\n \"undefined\" !== typeof __REACT_DEVTOOLS_GLOBAL_HOOK__ &&\n \"function\" ===\n typeof __REACT_DEVTOOLS_GLOBAL_HOOK__.registerInternalModuleStop &&\n __REACT_DEVTOOLS_GLOBAL_HOOK__.registerInternalModuleStop(Error());\n })();\n","'use strict';\n\nif (process.env.NODE_ENV === 'production') {\n module.exports = require('../cjs/use-sync-external-store-shim.production.js');\n} else {\n module.exports = require('../cjs/use-sync-external-store-shim.development.js');\n}\n","\"use strict\";\nvar __defProp = Object.defineProperty;\nvar __getOwnPropDesc = Object.getOwnPropertyDescriptor;\nvar __getOwnPropNames = Object.getOwnPropertyNames;\nvar __hasOwnProp = Object.prototype.hasOwnProperty;\nvar __export = (target, all) => {\n for (var name in all)\n __defProp(target, name, { get: all[name], enumerable: true });\n};\nvar __copyProps = (to, from, except, desc) => {\n if (from && typeof from === \"object\" || typeof from === \"function\") {\n for (let key of __getOwnPropNames(from))\n if (!__hasOwnProp.call(to, key) && key !== except)\n __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });\n }\n return to;\n};\nvar __toCommonJS = (mod) => __copyProps(__defProp({}, \"__esModule\", { value: true }), mod);\n\n// src/comparison/index.ts\nvar comparison_exports = {};\n__export(comparison_exports, {\n shallowEqual: () => shallowEqual\n});\nmodule.exports = __toCommonJS(comparison_exports);\n\n// src/comparison/shallowEqual.ts\nfunction shallowEqual(objA, objB) {\n if (Object.is(objA, objB)) {\n return true;\n }\n if (typeof objA !== \"object\" || objA === null || typeof objB !== \"object\" || objB === null) {\n return false;\n }\n if (objA instanceof Map && objB instanceof Map) {\n if (objA.size !== objB.size) return false;\n for (const [key, value] of objA) {\n if (!Object.is(value, objB.get(key))) {\n return false;\n }\n }\n return true;\n }\n if (objA instanceof Set && objB instanceof Set) {\n if (objA.size !== objB.size) return false;\n for (const value of objA) {\n if (!objB.has(value)) {\n return false;\n }\n }\n return true;\n }\n const keysA = Object.keys(objA);\n if (keysA.length !== Object.keys(objB).length) {\n return false;\n }\n for (let i = 0; i < keysA.length; i++) {\n if (!Object.prototype.hasOwnProperty.call(objB, keysA[i]) || !Object.is(objA[keysA[i]], objB[keysA[i]])) {\n return false;\n }\n }\n return true;\n}\n// Annotate the CommonJS export names for ESM import in node:\n0 && (module.exports = {\n shallowEqual\n});\n","export * from \"./store\";\n","import React, {\n createContext,\n useCallback,\n useContext,\n useMemo,\n useRef,\n} from \"react\";\nimport { useSyncExternalStore } from \"use-sync-external-store/shim\";\nimport { shallowEqual } from \"@pixotope/utils/comparison\";\n\nconst LIB_NAME = \"@pixotope/react-context-store\";\n\nexport type SelectorOptions<Selected> = (\n first: Selected,\n second: Selected\n) => boolean;\n\nexport type ContextOptions = {\n /**\n * If true, the store state will be preserved across mounts and un-mounts\n * of the Provider.\n * @default false\n */\n global?: boolean;\n /**\n * A function that will be used to compare the selected state.\n * @default shallowEqual\n */\n compare?: SelectorOptions<unknown>;\n};\n\ntype SetterArgs<Store> = Store | ((prev: Store) => Store);\ntype ExtractActionKeys<T> = {\n [K in keyof T]: T[K] extends (\n stateProps: never,\n action: infer A\n ) => void | Promise<void>\n ? A extends ActionablePayload<infer Payload>\n ? Payload extends undefined\n ? () => void\n : (payload: Payload) => void\n : () => void\n : never;\n};\n\ntype Prettify<T> = {\n [K in keyof T]: T[K];\n} & {};\n\nexport type ActionablePayload<Payload = any> = {\n payload: Payload;\n};\n\ntype StateActionProps<Store> = {\n set: (value: SetterArgs<Store>) => void;\n get: () => Store;\n};\n\ntype Actions<Store> = {\n [key: string]: (\n stateProps: StateActionProps<Store>,\n action: ActionablePayload\n ) => void | Promise<void>;\n};\n\ntype ContextReturnType<Store, A extends Actions<Store>> = {\n Provider: React.FC<{ children: React.ReactNode }>;\n useStore: <SelectorOutput = Store>(\n selector?: (store: Store) => SelectorOutput,\n options?: SelectorOptions<SelectorOutput>\n ) => {\n get: () => SelectorOutput;\n set: (value: SetterArgs<Store>) => void;\n selector: () => Store;\n };\n useActions: () => Prettify<ExtractActionKeys<A>>;\n useSetStore: () => (value: SetterArgs<Store>) => void;\n subscribe: <SelectorOutput = Store>(\n selector: (store: Store) => SelectorOutput,\n callback: (state: SelectorOutput) => void,\n options?: SelectorOptions<SelectorOutput>\n ) => () => void;\n unsubscribe: (callback: () => void) => void;\n};\n\nfunction isFunction(value: any): value is (prev: any) => any {\n return typeof value === \"function\";\n}\n\nexport function createContextStore<Store, A extends Actions<Store>>(\n ...args: Extract<A, { payload: A }> extends { payload: infer Payload }\n ? [initialState: Store, actions?: A, options?: ContextOptions]\n : [initialState: Store, options?: ContextOptions]\n): ContextReturnType<Store, A>;\nexport function createContextStore<Store, A extends Actions<Store>>(\n initialState: Store,\n actions: A = {} as A,\n options: ContextOptions = {}\n): ContextReturnType<Store, A> {\n let globalStore: Store | undefined = options.global\n ? initialState\n : undefined;\n\n function useStoreData({\n defaultSubscriber = [],\n }: {\n defaultSubscriber?: ((state: Store) => void)[];\n } = {}): {\n get: () => Store;\n set: (value: SetterArgs<Store>) => void;\n subscribe: (callback: (state: Store) => void) => () => void;\n } {\n const store = useRef<Store>(globalStore ?? initialState);\n\n const get = useCallback(() => store.current, []);\n\n const subscribers = useRef(\n new Set<(state: Store) => void>(defaultSubscriber)\n );\n\n const set = useCallback((value: SetterArgs<Store>) => {\n store.current = isFunction(value) ? value(store.current) : value;\n\n if (options.global) {\n globalStore = store.current;\n }\n\n subscribers.current.forEach((callback) => callback(store.current));\n }, []);\n\n const subscribe = useCallback((callback: (state: Store) => void) => {\n subscribers.current.add(callback);\n\n return () => subscribers.current.delete(callback);\n }, []);\n\n return {\n get,\n set,\n subscribe,\n };\n }\n\n type UseStoreDataReturnType = ReturnType<typeof useStoreData>;\n\n const StoreContext = createContext<UseStoreDataReturnType | null>(null);\n\n /**\n * This is the provider that will be used to wrap the react component tree\n * to provide the store to all the components in the tree.\n */\n function Provider({ children }: { children: React.ReactNode }) {\n return (\n <StoreContext.Provider\n value={useStoreData({ defaultSubscriber: [observable.broadcast] })}\n >\n {children}\n </StoreContext.Provider>\n );\n }\n\n /**\n * This holds all the observables that are subscribed to the store\n * but not being actively used by any component. Helps to broadcast\n * store updates to all the observables that are interested in reacting\n * to state outside the react component tree.\n */\n const observers = new Set<(state: Store) => void>();\n\n function subscribeExternal<SelectorOutput = Store>(\n selector: (store: Store) => SelectorOutput = (store) =>\n store as unknown as SelectorOutput,\n callback: (state: SelectorOutput) => void,\n compare: SelectorOptions<SelectorOutput> = (first, second) =>\n first === second\n ): () => void {\n let lastSelectedState: SelectorOutput | undefined;\n\n const shouldSendUpdates = (newSelectedState: SelectorOutput) => {\n if (\n lastSelectedState === undefined ||\n !compare(lastSelectedState, newSelectedState)\n ) {\n lastSelectedState = newSelectedState;\n\n return true;\n }\n\n return false;\n };\n\n const selectedCb = (store: Store) => {\n const selected = selector(store);\n const shouldSend = shouldSendUpdates(selected);\n\n if (shouldSend) {\n callback(selector(store));\n }\n };\n observers.add(selectedCb);\n\n return () => observers.delete(selectedCb);\n }\n\n /**\n * A simple observable that proxy store updates to all the subscribers\n * that are interested in reacting to state outside the react component tree.\n */\n const observable = {\n subscribe: subscribeExternal,\n unsubscribe: (callback: (state: Store) => void) => {\n observers.delete(callback);\n },\n broadcast: (state: Store) => {\n observers.forEach((callback) => callback(state));\n },\n } as const;\n\n /**\n * This is the hook that will be used to access the store from any component\n * in the react component tree.\n * @param selector A function that will be used to select the part of the store\n * that is needed by the component.\n * @param options Options to customize the behavior of the hook.\n * @returns An object with the selected state, a function to update the store\n * and a function to get the entire store.\n * @example\n * ```tsx\n * const { get, set } = useStore(store => store.user);\n * const { get, set } = useStore(store => store.user, { deepEqual: false });\n * const { get, set } = useStore(store => store.user, {\n * compare: (first, second) => first.id === second.id\n * });\n * ```\n */\n function useStore<SelectorOutput = Store>(\n selector: (store: Store) => SelectorOutput = (store) =>\n store as unknown as SelectorOutput,\n compare: SelectorOptions<SelectorOutput> = options.compare ?? shallowEqual\n ): {\n get: () => SelectorOutput;\n set: (value: SetterArgs<Store>) => void;\n selector: () => Store;\n } {\n const store = useContext(StoreContext);\n const lastSelectedState = useRef<SelectorOutput | undefined>(undefined);\n\n if (!store) {\n throw new Error(\n `[${LIB_NAME}] Store not found. Make sure the component is wrapped in a ${Provider} component.`\n );\n }\n\n const state = useSyncExternalStore(\n store.subscribe,\n () => {\n const selectedState = selector(store.get());\n\n if (\n lastSelectedState.current === undefined ||\n !compare(lastSelectedState.current, selectedState)\n ) {\n lastSelectedState.current = selectedState;\n }\n\n return lastSelectedState.current;\n },\n () => selector(initialState)\n );\n\n return {\n get: () => state,\n set: store.set,\n selector: () => store.get(),\n };\n }\n\n function useActions() {\n const store = useContext(StoreContext);\n\n if (!store) {\n throw new Error(\n `[${LIB_NAME}] Store not found. Make sure the component is wrapped in a ${Provider} component.`\n );\n }\n\n const actionProxy = useMemo(\n () =>\n new Proxy(actions, {\n get: (target, prop) => {\n const action = target[prop as string];\n\n if (action) {\n return (args: Parameters<typeof action>[\"1\"]) => {\n action(\n {\n set: store.set,\n get: store.get,\n },\n {\n payload: args,\n }\n );\n };\n }\n },\n set: () => {\n throw new Error(`[${LIB_NAME}] Actions cannot be updated`);\n },\n }),\n []\n );\n\n return actionProxy as unknown as ExtractActionKeys<typeof actions>;\n }\n\n const useSetStore = () => useStore(() => false).set;\n\n return {\n Provider,\n useStore,\n useSetStore,\n useActions,\n subscribe: observable.subscribe,\n unsubscribe: observable.unsubscribe,\n };\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA;AAAA,4JAAAA,UAAA;AAAA;AAWA,QAAIC,SAAQ,QAAQ,OAAO;AAC3B,aAAS,GAAG,GAAG,GAAG;AAChB,aAAQ,MAAM,MAAM,MAAM,KAAK,IAAI,MAAM,IAAI,MAAQ,MAAM,KAAK,MAAM;AAAA,IACxE;AACA,QAAI,WAAW,eAAe,OAAO,OAAO,KAAK,OAAO,KAAK;AAA7D,QACE,WAAWA,OAAM;AADnB,QAEE,YAAYA,OAAM;AAFpB,QAGE,kBAAkBA,OAAM;AAH1B,QAIE,gBAAgBA,OAAM;AACxB,aAAS,uBAAuB,WAAW,aAAa;AACtD,UAAI,QAAQ,YAAY,GACtB,YAAY,SAAS,EAAE,MAAM,EAAE,OAAc,YAAyB,EAAE,CAAC,GACzE,OAAO,UAAU,CAAC,EAAE,MACpB,cAAc,UAAU,CAAC;AAC3B;AAAA,QACE,WAAY;AACV,eAAK,Q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aAAa;AACpB,UAAM,YAAQ,yBAAW,YAAY;AAErC,QAAI,CAAC,OAAO;AACV,YAAM,IAAI;AAAA,QACR,IAAI,QAAQ,8DAA8D,QAAQ;AAAA,MACpF;AAAA,IACF;AAEA,UAAM,kBAAc;AAAA,MAClB,MACE,IAAI,MAAM,SAAS;AAAA,QACjB,KAAK,CAAC,QAAQ,SAAS;AACrB,gBAAM,SAAS,OAAO,IAAc;AAEpC,cAAI,QAAQ;AACV,mBAAO,CAAC,SAAyC;AAC/C;AAAA,gBACE;AAAA,kBACE,KAAK,MAAM;AAAA,kBACX,KAAK,MAAM;AAAA,gBACb;AAAA,gBACA;AAAA,kBACE,SAAS;AAAA,gBACX;AAAA,cACF;AAAA,YACF;AAAA,UACF;AAAA,QACF;AAAA,QACA,KAAK,MAAM;AACT,gBAAM,IAAI,MAAM,IAAI,QAAQ,6BAA6B;AAAA,QAC3D;AAAA,MACF,CAAC;AAAA,MACH,CAAC;AAAA,IACH;AAEA,WAAO;AAAA,EACT;AAEA,QAAM,cAAc,MAAM,SAAS,MAAM,KAAK,EAAE;AAEhD,SAAO;AAAA,IACL;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,IACA,WAAW,WAAW;AAAA,IACtB,aAAa,WAAW;AAAA,EAC1B;AACF;","names":["exports","React","exports","React","exports","module","exports","module","__defProp","__getOwnPropDesc","__getOwnPropNames","__hasOwnProp","__export","__copyProps","__toCommonJS","shallowEqual","React"]}
|
package/dist/index.d.cts
ADDED
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
import React from 'react';
|
|
2
|
+
|
|
3
|
+
type SelectorOptions<Selected> = (first: Selected, second: Selected) => boolean;
|
|
4
|
+
type ContextOptions = {
|
|
5
|
+
/**
|
|
6
|
+
* If true, the store state will be preserved across mounts and un-mounts
|
|
7
|
+
* of the Provider.
|
|
8
|
+
* @default false
|
|
9
|
+
*/
|
|
10
|
+
global?: boolean;
|
|
11
|
+
/**
|
|
12
|
+
* A function that will be used to compare the selected state.
|
|
13
|
+
* @default shallowEqual
|
|
14
|
+
*/
|
|
15
|
+
compare?: SelectorOptions<unknown>;
|
|
16
|
+
};
|
|
17
|
+
type SetterArgs<Store> = Store | ((prev: Store) => Store);
|
|
18
|
+
type ExtractActionKeys<T> = {
|
|
19
|
+
[K in keyof T]: T[K] extends (stateProps: never, action: infer A) => void | Promise<void> ? A extends ActionablePayload<infer Payload> ? Payload extends undefined ? () => void : (payload: Payload) => void : () => void : never;
|
|
20
|
+
};
|
|
21
|
+
type Prettify<T> = {
|
|
22
|
+
[K in keyof T]: T[K];
|
|
23
|
+
} & {};
|
|
24
|
+
type ActionablePayload<Payload = any> = {
|
|
25
|
+
payload: Payload;
|
|
26
|
+
};
|
|
27
|
+
type StateActionProps<Store> = {
|
|
28
|
+
set: (value: SetterArgs<Store>) => void;
|
|
29
|
+
get: () => Store;
|
|
30
|
+
};
|
|
31
|
+
type Actions<Store> = {
|
|
32
|
+
[key: string]: (stateProps: StateActionProps<Store>, action: ActionablePayload) => void | Promise<void>;
|
|
33
|
+
};
|
|
34
|
+
type ContextReturnType<Store, A extends Actions<Store>> = {
|
|
35
|
+
Provider: React.FC<{
|
|
36
|
+
children: React.ReactNode;
|
|
37
|
+
}>;
|
|
38
|
+
useStore: <SelectorOutput = Store>(selector?: (store: Store) => SelectorOutput, options?: SelectorOptions<SelectorOutput>) => {
|
|
39
|
+
get: () => SelectorOutput;
|
|
40
|
+
set: (value: SetterArgs<Store>) => void;
|
|
41
|
+
selector: () => Store;
|
|
42
|
+
};
|
|
43
|
+
useActions: () => Prettify<ExtractActionKeys<A>>;
|
|
44
|
+
useSetStore: () => (value: SetterArgs<Store>) => void;
|
|
45
|
+
subscribe: <SelectorOutput = Store>(selector: (store: Store) => SelectorOutput, callback: (state: SelectorOutput) => void, options?: SelectorOptions<SelectorOutput>) => () => void;
|
|
46
|
+
unsubscribe: (callback: () => void) => void;
|
|
47
|
+
};
|
|
48
|
+
declare function createContextStore<Store, A extends Actions<Store>>(...args: Extract<A, {
|
|
49
|
+
payload: A;
|
|
50
|
+
}> extends {
|
|
51
|
+
payload: infer Payload;
|
|
52
|
+
} ? [initialState: Store, actions?: A, options?: ContextOptions] : [initialState: Store, options?: ContextOptions]): ContextReturnType<Store, A>;
|
|
53
|
+
|
|
54
|
+
export { type ActionablePayload, type ContextOptions, type SelectorOptions, createContextStore };
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
import React from 'react';
|
|
2
|
+
|
|
3
|
+
type SelectorOptions<Selected> = (first: Selected, second: Selected) => boolean;
|
|
4
|
+
type ContextOptions = {
|
|
5
|
+
/**
|
|
6
|
+
* If true, the store state will be preserved across mounts and un-mounts
|
|
7
|
+
* of the Provider.
|
|
8
|
+
* @default false
|
|
9
|
+
*/
|
|
10
|
+
global?: boolean;
|
|
11
|
+
/**
|
|
12
|
+
* A function that will be used to compare the selected state.
|
|
13
|
+
* @default shallowEqual
|
|
14
|
+
*/
|
|
15
|
+
compare?: SelectorOptions<unknown>;
|
|
16
|
+
};
|
|
17
|
+
type SetterArgs<Store> = Store | ((prev: Store) => Store);
|
|
18
|
+
type ExtractActionKeys<T> = {
|
|
19
|
+
[K in keyof T]: T[K] extends (stateProps: never, action: infer A) => void | Promise<void> ? A extends ActionablePayload<infer Payload> ? Payload extends undefined ? () => void : (payload: Payload) => void : () => void : never;
|
|
20
|
+
};
|
|
21
|
+
type Prettify<T> = {
|
|
22
|
+
[K in keyof T]: T[K];
|
|
23
|
+
} & {};
|
|
24
|
+
type ActionablePayload<Payload = any> = {
|
|
25
|
+
payload: Payload;
|
|
26
|
+
};
|
|
27
|
+
type StateActionProps<Store> = {
|
|
28
|
+
set: (value: SetterArgs<Store>) => void;
|
|
29
|
+
get: () => Store;
|
|
30
|
+
};
|
|
31
|
+
type Actions<Store> = {
|
|
32
|
+
[key: string]: (stateProps: StateActionProps<Store>, action: ActionablePayload) => void | Promise<void>;
|
|
33
|
+
};
|
|
34
|
+
type ContextReturnType<Store, A extends Actions<Store>> = {
|
|
35
|
+
Provider: React.FC<{
|
|
36
|
+
children: React.ReactNode;
|
|
37
|
+
}>;
|
|
38
|
+
useStore: <SelectorOutput = Store>(selector?: (store: Store) => SelectorOutput, options?: SelectorOptions<SelectorOutput>) => {
|
|
39
|
+
get: () => SelectorOutput;
|
|
40
|
+
set: (value: SetterArgs<Store>) => void;
|
|
41
|
+
selector: () => Store;
|
|
42
|
+
};
|
|
43
|
+
useActions: () => Prettify<ExtractActionKeys<A>>;
|
|
44
|
+
useSetStore: () => (value: SetterArgs<Store>) => void;
|
|
45
|
+
subscribe: <SelectorOutput = Store>(selector: (store: Store) => SelectorOutput, callback: (state: SelectorOutput) => void, options?: SelectorOptions<SelectorOutput>) => () => void;
|
|
46
|
+
unsubscribe: (callback: () => void) => void;
|
|
47
|
+
};
|
|
48
|
+
declare function createContextStore<Store, A extends Actions<Store>>(...args: Extract<A, {
|
|
49
|
+
payload: A;
|
|
50
|
+
}> extends {
|
|
51
|
+
payload: infer Payload;
|
|
52
|
+
} ? [initialState: Store, actions?: A, options?: ContextOptions] : [initialState: Store, options?: ContextOptions]): ContextReturnType<Store, A>;
|
|
53
|
+
|
|
54
|
+
export { type ActionablePayload, type ContextOptions, type SelectorOptions, createContextStore };
|