@hairy/react-lib 1.47.0 → 1.50.0

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/dist/index.cjs CHANGED
@@ -1,862 +1,505 @@
1
- "use strict";
1
+ Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
2
+ //#region \0rolldown/runtime.js
2
3
  var __create = Object.create;
3
4
  var __defProp = Object.defineProperty;
4
5
  var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
5
6
  var __getOwnPropNames = Object.getOwnPropertyNames;
6
7
  var __getProtoOf = Object.getPrototypeOf;
7
8
  var __hasOwnProp = Object.prototype.hasOwnProperty;
8
- var __export = (target, all) => {
9
- for (var name in all)
10
- __defProp(target, name, { get: all[name], enumerable: true });
11
- };
12
9
  var __copyProps = (to, from, except, desc) => {
13
- if (from && typeof from === "object" || typeof from === "function") {
14
- for (let key of __getOwnPropNames(from))
15
- if (!__hasOwnProp.call(to, key) && key !== except)
16
- __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
17
- }
18
- return to;
10
+ if (from && typeof from === "object" || typeof from === "function") {
11
+ for (var keys = __getOwnPropNames(from), i = 0, n = keys.length, key; i < n; i++) {
12
+ key = keys[i];
13
+ if (!__hasOwnProp.call(to, key) && key !== except) {
14
+ __defProp(to, key, {
15
+ get: ((k) => from[k]).bind(null, key),
16
+ enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable
17
+ });
18
+ }
19
+ }
20
+ }
21
+ return to;
19
22
  };
20
- var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
21
- // If the importer is in node compatibility mode or this is not an ESM
22
- // file that has been converted to a CommonJS file using a Babel-
23
- // compatible transform (i.e. "__esModule" has not been set), then set
24
- // "default" to the CommonJS "module.exports" for node compatibility.
25
- isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
26
- mod
27
- ));
28
- var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
29
-
30
- // src/index.ts
31
- var index_exports = {};
32
- __export(index_exports, {
33
- Case: () => Case,
34
- Default: () => Default,
35
- Else: () => Else,
36
- If: () => If,
37
- Injector: () => Injector,
38
- Switch: () => Switch,
39
- Then: () => Then,
40
- Trigger: () => Trigger,
41
- Unless: () => Unless,
42
- cls: () => cls,
43
- defienAsyncStore: () => defienAsyncStore,
44
- defineStore: () => defineStore,
45
- defineStoreAsync: () => defineStoreAsync,
46
- proxyWithPersistant: () => proxyWithPersistant,
47
- track: () => track,
48
- tryUseCallback: () => tryUseCallback,
49
- tryUseEffect: () => tryUseEffect,
50
- tryUseInsertionEffect: () => tryUseInsertionEffect,
51
- tryUseReducer: () => tryUseReducer,
52
- tryUseRef: () => tryUseRef,
53
- tryUseState: () => tryUseState,
54
- tryUseUpdate: () => tryUseUpdate,
55
- useAsyncCallback: () => useAsyncCallback,
56
- useAsyncState: () => useAsyncState,
57
- useDebounce: () => useDebounce,
58
- useEventBus: () => useEventBus,
59
- useFetchRequestIntercept: () => useFetchRequestIntercept,
60
- useFetchResponseIntercept: () => useFetchResponseIntercept,
61
- useMounted: () => useMounted,
62
- useOffsetPagination: () => useOffsetPagination,
63
- useStatus: () => useStatus,
64
- useStore: () => useStore,
65
- useUpdate: () => useUpdate,
66
- useWatch: () => useWatch,
67
- useWhenever: () => useWhenever,
68
- wrapper: () => wrapper
69
- });
70
- module.exports = __toCommonJS(index_exports);
71
-
72
- // src/utils/cls.ts
73
- var hasOwn = {}.hasOwnProperty;
23
+ var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", {
24
+ value: mod,
25
+ enumerable: true
26
+ }) : target, mod));
27
+
28
+ //#endregion
29
+ let _hairy_utils = require("@hairy/utils");
30
+ let valtio = require("valtio");
31
+ let react = require("react");
32
+ let valtio_utils = require("valtio/utils");
33
+ let mitt = require("mitt");
34
+ mitt = __toESM(mitt);
35
+ let react_use = require("react-use");
36
+
37
+ //#region src/utils/cls.ts
38
+ const hasOwn = {}.hasOwnProperty;
39
+ /**
40
+ * A simple JavaScript utility for conditionally joining classNames together.
41
+ */
74
42
  function cls(...args) {
75
- let classes = "";
76
- for (let i = 0; i < args.length; i++) {
77
- const arg = args[i];
78
- if (arg)
79
- classes = cls.append(classes, cls.parse(arg));
80
- }
81
- return classes;
43
+ let classes = "";
44
+ for (let i = 0; i < args.length; i++) {
45
+ const arg = args[i];
46
+ if (arg) classes = cls.append(classes, cls.parse(arg));
47
+ }
48
+ return classes;
82
49
  }
83
50
  cls.parse = function(arg) {
84
- if (typeof arg === "string")
85
- return arg;
86
- if (typeof arg !== "object")
87
- return "";
88
- if (Array.isArray(arg))
89
- return cls.apply(null, arg);
90
- if (arg.toString !== Object.prototype.toString && !arg.toString.toString().includes("[native code]"))
91
- return cls.toString();
92
- let classes = "";
93
- for (const key in arg) {
94
- if (hasOwn.call(arg, key) && arg[key])
95
- classes = cls.append(classes, key);
96
- }
97
- return classes;
51
+ if (typeof arg === "string") return arg;
52
+ if (typeof arg !== "object") return "";
53
+ if (Array.isArray(arg)) return cls.apply(null, arg);
54
+ if (arg.toString !== Object.prototype.toString && !arg.toString.toString().includes("[native code]")) return cls.toString();
55
+ let classes = "";
56
+ for (const key in arg) if (hasOwn.call(arg, key) && arg[key]) classes = cls.append(classes, key);
57
+ return classes;
98
58
  };
99
59
  cls.append = function(value, newClass) {
100
- if (!newClass)
101
- return value;
102
- return value ? `${value} ${newClass}` : newClass;
60
+ if (!newClass) return value;
61
+ return value ? `${value} ${newClass}` : newClass;
103
62
  };
104
63
 
105
- // ../../node_modules/.pnpm/lodash-es@4.17.21/node_modules/lodash-es/_freeGlobal.js
106
- var freeGlobal = typeof global == "object" && global && global.Object === Object && global;
107
- var freeGlobal_default = freeGlobal;
108
-
109
- // ../../node_modules/.pnpm/lodash-es@4.17.21/node_modules/lodash-es/_root.js
110
- var freeSelf = typeof self == "object" && self && self.Object === Object && self;
111
- var root = freeGlobal_default || freeSelf || Function("return this")();
112
- var root_default = root;
113
-
114
- // ../../node_modules/.pnpm/lodash-es@4.17.21/node_modules/lodash-es/_Symbol.js
115
- var Symbol2 = root_default.Symbol;
116
- var Symbol_default = Symbol2;
117
-
118
- // ../../node_modules/.pnpm/lodash-es@4.17.21/node_modules/lodash-es/_getRawTag.js
119
- var objectProto = Object.prototype;
120
- var hasOwnProperty = objectProto.hasOwnProperty;
121
- var nativeObjectToString = objectProto.toString;
122
- var symToStringTag = Symbol_default ? Symbol_default.toStringTag : void 0;
123
- function getRawTag(value) {
124
- var isOwn = hasOwnProperty.call(value, symToStringTag), tag = value[symToStringTag];
125
- try {
126
- value[symToStringTag] = void 0;
127
- var unmasked = true;
128
- } catch (e) {
129
- }
130
- var result = nativeObjectToString.call(value);
131
- if (unmasked) {
132
- if (isOwn) {
133
- value[symToStringTag] = tag;
134
- } else {
135
- delete value[symToStringTag];
136
- }
137
- }
138
- return result;
139
- }
140
- var getRawTag_default = getRawTag;
141
-
142
- // ../../node_modules/.pnpm/lodash-es@4.17.21/node_modules/lodash-es/_objectToString.js
143
- var objectProto2 = Object.prototype;
144
- var nativeObjectToString2 = objectProto2.toString;
145
- function objectToString(value) {
146
- return nativeObjectToString2.call(value);
147
- }
148
- var objectToString_default = objectToString;
149
-
150
- // ../../node_modules/.pnpm/lodash-es@4.17.21/node_modules/lodash-es/_baseGetTag.js
151
- var nullTag = "[object Null]";
152
- var undefinedTag = "[object Undefined]";
153
- var symToStringTag2 = Symbol_default ? Symbol_default.toStringTag : void 0;
154
- function baseGetTag(value) {
155
- if (value == null) {
156
- return value === void 0 ? undefinedTag : nullTag;
157
- }
158
- return symToStringTag2 && symToStringTag2 in Object(value) ? getRawTag_default(value) : objectToString_default(value);
159
- }
160
- var baseGetTag_default = baseGetTag;
161
-
162
- // ../../node_modules/.pnpm/lodash-es@4.17.21/node_modules/lodash-es/isObject.js
163
- function isObject(value) {
164
- var type = typeof value;
165
- return value != null && (type == "object" || type == "function");
166
- }
167
- var isObject_default = isObject;
168
-
169
- // ../../node_modules/.pnpm/lodash-es@4.17.21/node_modules/lodash-es/isFunction.js
170
- var asyncTag = "[object AsyncFunction]";
171
- var funcTag = "[object Function]";
172
- var genTag = "[object GeneratorFunction]";
173
- var proxyTag = "[object Proxy]";
174
- function isFunction(value) {
175
- if (!isObject_default(value)) {
176
- return false;
177
- }
178
- var tag = baseGetTag_default(value);
179
- return tag == funcTag || tag == genTag || tag == asyncTag || tag == proxyTag;
180
- }
181
- var isFunction_default = isFunction;
182
-
183
- // ../util-core/src/util/noop.ts
184
- var noop = () => {
185
- };
186
-
187
- // ../util-core/src/util/deferred.ts
188
- var Deferred = class extends Promise {
189
- resolve;
190
- reject;
191
- constructor(executor = noop) {
192
- let _resolve, _reject;
193
- super((resolve_, reject_) => {
194
- _resolve = resolve_;
195
- _reject = reject_;
196
- return executor(resolve_, reject_);
197
- });
198
- this.resolve = (value) => {
199
- _resolve(value);
200
- return this;
201
- };
202
- this.reject = (reason) => {
203
- _reject(reason);
204
- return this;
205
- };
206
- }
207
- };
208
-
209
- // ../util-core/src/util/json.ts
210
- function tryParseJson(text) {
211
- try {
212
- return JSON.parse(text || "");
213
- } catch {
214
- return void 0;
215
- }
216
- }
217
-
218
- // src/utils/track.ts
219
- var import_valtio = require("valtio");
64
+ //#endregion
65
+ //#region src/utils/track.ts
66
+ /**
67
+ * @requires `Trigger` component to be mounted in the tree.
68
+ *
69
+ * @example
70
+ * ```tsx
71
+ * // Obtain externally
72
+ * import { track } from '@hairy/lib-react'
73
+ * const context = await track(() => useContext(YourContext))
74
+ * console.log(context) // { ... }
75
+ * ```
76
+ */
220
77
  function track(fn, ...args) {
221
- const deferred = (0, import_valtio.ref)(new Deferred());
222
- const exposer = { fn, args, deferred, id: ++Trigger.id };
223
- Trigger.tasks.set(exposer.id, exposer);
224
- deferred.then(() => Trigger.tasks.delete(exposer.id));
225
- return deferred;
226
- }
227
-
228
- // src/utils/wrapper.ts
229
- var import_react = require("react");
78
+ const deferred = (0, valtio.ref)(new _hairy_utils.Deferred());
79
+ const exposer = {
80
+ fn,
81
+ args,
82
+ deferred,
83
+ id: ++Trigger.id
84
+ };
85
+ Trigger.tasks.set(exposer.id, exposer);
86
+ deferred.then(() => Trigger.tasks.delete(exposer.id));
87
+ return deferred;
88
+ }
89
+
90
+ //#endregion
91
+ //#region src/utils/wrapper.ts
230
92
  function wrapper(asChild, props, children) {
231
- return asChild ? (0, import_react.createElement)(asChild, props, children) : children;
93
+ return asChild ? (0, react.createElement)(asChild, props, children) : children;
232
94
  }
233
95
 
234
- // src/components/condition/Case.ts
96
+ //#endregion
97
+ //#region src/components/condition/Case.ts
235
98
  function Case(props) {
236
- const { cond, children, tag, as: asChild, ...attrs } = props;
237
- return wrapper(tag || asChild, attrs, children);
99
+ const { cond, children, tag, as: asChild, ...attrs } = props;
100
+ return wrapper(tag || asChild, attrs, children);
238
101
  }
239
102
 
240
- // src/components/condition/Default.ts
103
+ //#endregion
104
+ //#region src/components/condition/Default.ts
241
105
  function Default(props) {
242
- const { children, tag, as: asChild, ...attrs } = props;
243
- return wrapper(tag || asChild, attrs, children);
106
+ const { children, tag, as: asChild, ...attrs } = props;
107
+ return wrapper(tag || asChild, attrs, children);
244
108
  }
245
109
 
246
- // src/components/condition/If.ts
247
- var import_react2 = require("react");
248
-
249
- // src/components/condition/Then.ts
110
+ //#endregion
111
+ //#region src/components/condition/Then.ts
250
112
  function Then(props) {
251
- const { children, cond, else: _else, then, tag, as: asChild, ...attrs } = props;
252
- return Object.keys(props).includes("cond") ? wrapper(If, props, children) : wrapper(tag || asChild, attrs, children);
113
+ const { children, cond, else: _else, then, tag, as: asChild, ...attrs } = props;
114
+ return Object.keys(props).includes("cond") ? wrapper(If, props, children) : wrapper(tag || asChild, attrs, children);
253
115
  }
254
116
 
255
- // src/components/condition/If.ts
117
+ //#endregion
118
+ //#region src/components/condition/If.ts
256
119
  function If(props) {
257
- const { then, cond, else: _else, children = props.then, tag, as: asChild, ...attrs } = props;
258
- const elements = import_react2.Children.toArray(children);
259
- const thenChild = elements.find((c) => c.type === Then);
260
- const elseChild = elements.find((c) => c.type === Else);
261
- const child = thenChild || elseChild ? cond ? thenChild : elseChild : cond ? children : _else;
262
- return wrapper(tag || asChild, attrs, child);
120
+ const { then, cond, else: _else, children = props.then, tag, as: asChild, ...attrs } = props;
121
+ const elements = react.Children.toArray(children);
122
+ const thenChild = elements.find((c) => c.type === Then);
123
+ const elseChild = elements.find((c) => c.type === Else);
124
+ return wrapper(tag || asChild, attrs, thenChild || elseChild ? cond ? thenChild : elseChild : cond ? children : _else);
263
125
  }
264
126
 
265
- // src/components/condition/Else.ts
127
+ //#endregion
128
+ //#region src/components/condition/Else.ts
266
129
  function Else(props) {
267
- const { children, tag, as: asChild, ...attrs } = props;
268
- return Object.keys(props).includes("cond") ? wrapper(If, props, children) : wrapper(tag || asChild, attrs, children);
130
+ const { children, tag, as: asChild, ...attrs } = props;
131
+ return Object.keys(props).includes("cond") ? wrapper(If, props, children) : wrapper(tag || asChild, attrs, children);
269
132
  }
270
133
 
271
- // src/components/condition/Switch.ts
272
- var import_react3 = require("react");
134
+ //#endregion
135
+ //#region src/components/condition/Switch.ts
273
136
  function Switch(props) {
274
- const isUseValue = props.value !== void 0;
275
- let matchingCase;
276
- let defaultCase;
277
- import_react3.Children.forEach(props.children, (child) => {
278
- if (!(0, import_react3.isValidElement)(child) || matchingCase)
279
- return;
280
- if (child.type === Case) {
281
- const cond = child?.props?.cond;
282
- if (isUseValue ? props.value === cond : cond) {
283
- matchingCase = child;
284
- return;
285
- }
286
- }
287
- if (!defaultCase && child.type === Default)
288
- defaultCase = child;
289
- });
290
- return matchingCase ?? defaultCase ?? null;
291
- }
292
-
293
- // src/components/condition/Unless.ts
294
- var import_react4 = require("react");
137
+ const isUseValue = props.value !== void 0;
138
+ let matchingCase;
139
+ let defaultCase;
140
+ react.Children.forEach(props.children, (child) => {
141
+ if (!(0, react.isValidElement)(child) || matchingCase) return;
142
+ if (child.type === Case) {
143
+ const cond = child?.props?.cond;
144
+ if (isUseValue ? props.value === cond : cond) {
145
+ matchingCase = child;
146
+ return;
147
+ }
148
+ }
149
+ if (!defaultCase && child.type === Default) defaultCase = child;
150
+ });
151
+ return matchingCase ?? defaultCase ?? null;
152
+ }
153
+
154
+ //#endregion
155
+ //#region src/components/condition/Unless.ts
295
156
  function Unless(props) {
296
- const { cond, then, else: _else, tag, as: asChild, children = props.then, ...attrs } = props;
297
- const elements = import_react4.Children.toArray(children);
298
- const thenChild = elements.find((c) => c.type === Then);
299
- const elseChild = elements.find((c) => c.type === Else);
300
- const child = thenChild || elseChild ? !cond ? elseChild : thenChild : !cond ? children : _else;
301
- return wrapper(tag || asChild, attrs, child);
157
+ const { cond, then, else: _else, tag, as: asChild, children = props.then, ...attrs } = props;
158
+ const elements = react.Children.toArray(children);
159
+ const thenChild = elements.find((c) => c.type === Then);
160
+ const elseChild = elements.find((c) => c.type === Else);
161
+ return wrapper(tag || asChild, attrs, thenChild || elseChild ? !cond ? elseChild : thenChild : !cond ? children : _else);
302
162
  }
303
163
 
304
- // src/components/utils/Injector.ts
305
- var import_react5 = require("react");
164
+ //#endregion
165
+ //#region src/components/utils/Injector.ts
306
166
  function Injector(props) {
307
- const installs = (0, import_react5.useMemo)(
308
- () => props.install.map(repack).reverse(),
309
- [props.install]
310
- );
311
- return installs.reduce(
312
- (child, { component: Component, props: props2 }) => (0, import_react5.createElement)(Component, props2, child),
313
- props.children
314
- );
167
+ return (0, react.useMemo)(() => props.install.map(repack).reverse(), [props.install]).reduce((child, { component: Component, props }) => (0, react.createElement)(Component, props, child), props.children);
315
168
  }
316
169
  function repack(c) {
317
- return c.component ? c : { component: c };
170
+ return c.component ? c : { component: c };
318
171
  }
319
172
 
320
- // src/components/utils/Trigger.ts
321
- var import_react6 = require("react");
322
- var import_valtio2 = require("valtio");
323
- var import_utils8 = require("valtio/utils");
324
- var pendingTasks = (0, import_utils8.proxyMap)();
173
+ //#endregion
174
+ //#region src/components/utils/Trigger.ts
175
+ const pendingTasks = (0, valtio_utils.proxyMap)();
325
176
  function createTracker(exposer) {
326
- const Component = () => {
327
- try {
328
- exposer.deferred.resolve(exposer.fn(...exposer.args));
329
- } catch (error) {
330
- exposer.deferred.reject(error);
331
- }
332
- return null;
333
- };
334
- Component.key = exposer.id;
335
- return Component;
177
+ const Component = () => {
178
+ try {
179
+ exposer.deferred.resolve(exposer.fn(...exposer.args));
180
+ } catch (error) {
181
+ exposer.deferred.reject(error);
182
+ }
183
+ return null;
184
+ };
185
+ Component.key = exposer.id;
186
+ return Component;
336
187
  }
337
188
  function renderTracker(Tracker) {
338
- return (0, import_react6.createElement)(Tracker, { key: Tracker.key });
339
- }
189
+ return (0, react.createElement)(Tracker, { key: Tracker.key });
190
+ }
191
+ /**
192
+ * @example
193
+ * ```tsx
194
+ * import { Trigger } from '@hairy/lib-react'
195
+ *
196
+ * // Use triggers to capture context
197
+ * function App() {
198
+ * return (
199
+ * <YourContext.Provider>
200
+ * <Trigger />
201
+ * </YourContext.Provider>
202
+ * )
203
+ * }
204
+ *
205
+ * // Obtain externally
206
+ * import { track } from '@hairy/lib-react'
207
+ * const context = await track(() => useContext(YourContext))
208
+ * console.log(context) // { ... }
209
+ * ```
210
+ */
340
211
  function Trigger() {
341
- const values = [...(0, import_valtio2.useSnapshot)(pendingTasks).values()];
342
- return values.map(createTracker).map(renderTracker);
212
+ return [...(0, valtio.useSnapshot)(pendingTasks).values()].map(createTracker).map(renderTracker);
343
213
  }
344
214
  Trigger.id = 0;
345
215
  Trigger.tasks = pendingTasks;
346
216
 
347
- // src/hooks/tryUseCallback.ts
348
- var import_react7 = require("react");
349
- var tryUseCallback = (callback, deps) => {
350
- try {
351
- return (0, import_react7.useCallback)(callback, deps);
352
- } catch {
353
- return callback;
354
- }
217
+ //#endregion
218
+ //#region src/hooks/tryUseCallback.ts
219
+ const tryUseCallback = (callback, deps) => {
220
+ try {
221
+ return (0, react.useCallback)(callback, deps);
222
+ } catch {
223
+ return callback;
224
+ }
355
225
  };
356
226
 
357
- // src/hooks/tryUseEffect.ts
358
- var import_react8 = require("react");
359
- var tryUseEffect = (effect, deps) => {
360
- try {
361
- (0, import_react8.useEffect)(effect, deps);
362
- } catch {
363
- }
227
+ //#endregion
228
+ //#region src/hooks/tryUseEffect.ts
229
+ const tryUseEffect = (effect, deps) => {
230
+ try {
231
+ (0, react.useEffect)(effect, deps);
232
+ } catch {}
364
233
  };
365
234
 
366
- // src/hooks/tryUseInsertionEffect.ts
367
- var import_react9 = require("react");
368
- var tryUseInsertionEffect = (callback, deps) => {
369
- try {
370
- (0, import_react9.useInsertionEffect)(callback, deps);
371
- } catch {
372
- }
235
+ //#endregion
236
+ //#region src/hooks/tryUseInsertionEffect.ts
237
+ const tryUseInsertionEffect = (callback, deps) => {
238
+ try {
239
+ (0, react.useInsertionEffect)(callback, deps);
240
+ } catch {}
373
241
  };
374
242
 
375
- // src/hooks/tryUseReducer.ts
376
- var import_react10 = require("react");
377
- var tryUseReducer = (reducer, initializerArg, initializer) => {
378
- try {
379
- return (0, import_react10.useReducer)(reducer, initializerArg, initializer);
380
- } catch {
381
- return [initializerArg, () => {
382
- }];
383
- }
243
+ //#endregion
244
+ //#region src/hooks/tryUseReducer.ts
245
+ const tryUseReducer = (reducer, initializerArg, initializer) => {
246
+ try {
247
+ return (0, react.useReducer)(reducer, initializerArg, initializer);
248
+ } catch {
249
+ return [initializerArg, () => {}];
250
+ }
384
251
  };
385
252
 
386
- // src/hooks/tryUseRef.ts
387
- var import_react11 = require("react");
388
- var tryUseRef = (initialValue) => {
389
- try {
390
- return (0, import_react11.useRef)(initialValue);
391
- } catch {
392
- return { current: initialValue };
393
- }
253
+ //#endregion
254
+ //#region src/hooks/tryUseRef.ts
255
+ const tryUseRef = (initialValue) => {
256
+ try {
257
+ return (0, react.useRef)(initialValue);
258
+ } catch {
259
+ return { current: initialValue };
260
+ }
394
261
  };
395
262
 
396
- // src/hooks/tryUseState.ts
397
- var import_react12 = require("react");
398
- var tryUseState = (initialState) => {
399
- try {
400
- return (0, import_react12.useState)(initialState);
401
- } catch {
402
- return [isFunction_default(initialState) ? initialState() : initialState, noop];
403
- }
263
+ //#endregion
264
+ //#region src/hooks/tryUseState.ts
265
+ const tryUseState = (initialState) => {
266
+ try {
267
+ return (0, react.useState)(initialState);
268
+ } catch {
269
+ return [(0, _hairy_utils.isFunction)(initialState) ? initialState() : initialState, _hairy_utils.noop];
270
+ }
404
271
  };
405
272
 
406
- // src/hooks/useUpdate.ts
407
- var updateReducer = (num) => (num + 1) % 1e6;
273
+ //#endregion
274
+ //#region src/hooks/useUpdate.ts
275
+ const updateReducer = (num) => (num + 1) % 1e6;
408
276
  function useUpdate() {
409
- const [, update] = tryUseReducer(updateReducer, 0);
410
- return update;
277
+ const [, update] = tryUseReducer(updateReducer, 0);
278
+ return update;
411
279
  }
412
280
 
413
- // src/hooks/tryUseUpdate.ts
281
+ //#endregion
282
+ //#region src/hooks/tryUseUpdate.ts
414
283
  function tryUseUpdate() {
415
- try {
416
- return useUpdate();
417
- } catch {
418
- return () => {
419
- };
420
- }
284
+ try {
285
+ return useUpdate();
286
+ } catch {
287
+ return () => {};
288
+ }
421
289
  }
422
290
 
423
- // src/hooks/useAsyncCallback.ts
424
- var import_react13 = require("react");
291
+ //#endregion
292
+ //#region src/hooks/useAsyncCallback.ts
425
293
  function useAsyncCallback(fun) {
426
- const [state, set] = (0, import_react13.useState)({ loading: false });
427
- async function refetch(...args) {
428
- set({ loading: true });
429
- return fun(...args).then((value) => {
430
- set({ loading: false });
431
- return value;
432
- }).catch((err) => {
433
- set({ loading: false, error: err });
434
- return Promise.reject(err);
435
- });
436
- }
437
- return [state.loading, refetch, state.error];
438
- }
439
-
440
- // src/hooks/useAsyncState.ts
441
- var import_react14 = require("react");
294
+ const [state, set] = (0, react.useState)({ loading: false });
295
+ async function refetch(...args) {
296
+ set({ loading: true });
297
+ return fun(...args).then((value) => {
298
+ set({ loading: false });
299
+ return value;
300
+ }).catch((err) => {
301
+ set({
302
+ loading: false,
303
+ error: err
304
+ });
305
+ return Promise.reject(err);
306
+ });
307
+ }
308
+ return [
309
+ state.loading,
310
+ refetch,
311
+ state.error
312
+ ];
313
+ }
314
+
315
+ //#endregion
316
+ //#region src/hooks/useAsyncState.ts
442
317
  function useAsyncState(fun, deps = [], options) {
443
- const [value, set] = (0, import_react14.useState)(options?.initial);
444
- const [loading, refetch, error] = useAsyncCallback(async (...args) => fun(...args).then(set));
445
- (0, import_react14.useEffect)(
446
- () => {
447
- refetch();
448
- },
449
- deps
450
- );
451
- return [{ value, loading, error }, refetch];
452
- }
453
-
454
- // src/hooks/useDebounce.ts
455
- var import_react15 = require("react");
318
+ const [value, set] = (0, react.useState)(options?.initial);
319
+ const [loading, refetch, error] = useAsyncCallback(async (...args) => fun(...args).then(set));
320
+ (0, react.useEffect)(() => {
321
+ refetch();
322
+ }, deps);
323
+ return [{
324
+ value,
325
+ loading,
326
+ error
327
+ }, refetch];
328
+ }
329
+
330
+ //#endregion
331
+ //#region src/hooks/useDebounce.ts
456
332
  function useDebounce(value, delay) {
457
- const [debouncedValue, setDebouncedValue] = (0, import_react15.useState)(value);
458
- (0, import_react15.useEffect)(() => {
459
- const handler = setTimeout(() => setDebouncedValue(value), delay);
460
- return () => clearTimeout(handler);
461
- }, [value, delay]);
462
- return debouncedValue;
333
+ const [debouncedValue, setDebouncedValue] = (0, react.useState)(value);
334
+ (0, react.useEffect)(() => {
335
+ const handler = setTimeout(() => setDebouncedValue(value), delay);
336
+ return () => clearTimeout(handler);
337
+ }, [value, delay]);
338
+ return debouncedValue;
463
339
  }
464
340
 
465
- // src/hooks/useEventBus.ts
466
- var import_mitt = __toESM(require("mitt"), 1);
467
- var import_react16 = require("react");
468
- var emitter = (0, import_mitt.default)();
341
+ //#endregion
342
+ //#region src/hooks/useEventBus.ts
343
+ const emitter = (0, mitt.default)();
469
344
  function useEventBus(key) {
470
- const onRef = (0, import_react16.useRef)(void 0);
471
- function on(listener) {
472
- emitter.on(key, listener);
473
- onRef.current = listener;
474
- (0, import_react16.useEffect)(() => {
475
- if (!onRef.current)
476
- return;
477
- emitter.off(key, onRef.current);
478
- emitter.on(key, listener);
479
- onRef.current = listener;
480
- return () => emitter.off(key, listener);
481
- }, [listener]);
482
- }
483
- function emit(event) {
484
- emitter.emit(key, event);
485
- }
486
- function off(listener) {
487
- emitter.off(key, listener);
488
- }
489
- return {
490
- on,
491
- emit,
492
- off
493
- };
494
- }
495
-
496
- // ../../node_modules/.pnpm/react-use@17.6.0_react-dom@19.1.0_react@19.1.0__react@19.1.0/node_modules/react-use/esm/useEffectOnce.js
497
- var import_react17 = require("react");
498
- var useEffectOnce = function(effect) {
499
- (0, import_react17.useEffect)(effect, []);
500
- };
501
- var useEffectOnce_default = useEffectOnce;
502
-
503
- // ../../node_modules/.pnpm/react-use@17.6.0_react-dom@19.1.0_react@19.1.0__react@19.1.0/node_modules/react-use/esm/useMount.js
504
- var useMount = function(fn) {
505
- useEffectOnce_default(function() {
506
- fn();
507
- });
508
- };
509
- var useMount_default = useMount;
510
-
511
- // src/hooks/useFetchIntercept.ts
345
+ const onRef = (0, react.useRef)(void 0);
346
+ function on(listener) {
347
+ emitter.on(key, listener);
348
+ onRef.current = listener;
349
+ (0, react.useEffect)(() => {
350
+ if (!onRef.current) return;
351
+ emitter.off(key, onRef.current);
352
+ emitter.on(key, listener);
353
+ onRef.current = listener;
354
+ return () => emitter.off(key, listener);
355
+ }, [listener]);
356
+ }
357
+ function emit(event) {
358
+ emitter.emit(key, event);
359
+ }
360
+ function off(listener) {
361
+ emitter.off(key, listener);
362
+ }
363
+ return {
364
+ on,
365
+ emit,
366
+ off
367
+ };
368
+ }
369
+
370
+ //#endregion
371
+ //#region src/hooks/useFetchIntercept.ts
512
372
  function useFetchResponseIntercept(intercept) {
513
- useMount_default(() => fetchResponseIntercept(intercept));
373
+ (0, react_use.useMount)(() => fetchResponseIntercept(intercept));
514
374
  }
515
375
  function useFetchRequestIntercept(intercept) {
516
- useMount_default(() => fetchRequestIntercept(intercept));
376
+ (0, react_use.useMount)(() => fetchRequestIntercept(intercept));
517
377
  }
518
378
  function fetchResponseIntercept(intercept) {
519
- const { fetch: originalFetch } = window;
520
- window.fetch = async (...args) => {
521
- const [resource, config] = args;
522
- const response = await originalFetch(resource, config);
523
- return intercept(response, config);
524
- };
379
+ const { fetch: originalFetch } = window;
380
+ window.fetch = async (...args) => {
381
+ const [resource, config] = args;
382
+ return intercept(await originalFetch(resource, config), config);
383
+ };
525
384
  }
526
385
  function fetchRequestIntercept(intercept) {
527
- const { fetch: originalFetch } = window;
528
- window.fetch = async (...args) => {
529
- const [resource, config] = args;
530
- return intercept(originalFetch, resource, config);
531
- };
386
+ const { fetch: originalFetch } = window;
387
+ window.fetch = async (...args) => {
388
+ const [resource, config] = args;
389
+ return intercept(originalFetch, resource, config);
390
+ };
532
391
  }
533
392
 
534
- // src/hooks/useMounted.ts
535
- var import_react18 = require("react");
393
+ //#endregion
394
+ //#region src/hooks/useMounted.ts
536
395
  function useMounted() {
537
- const [mounted, setMounted] = (0, import_react18.useState)(false);
538
- (0, import_react18.useEffect)(() => setMounted(true), []);
539
- return mounted;
396
+ const [mounted, setMounted] = (0, react.useState)(false);
397
+ (0, react.useEffect)(() => setMounted(true), []);
398
+ return mounted;
540
399
  }
541
400
 
542
- // src/hooks/useOffsetPagination.ts
543
- var import_react21 = require("react");
544
-
545
- // src/hooks/useWatch.ts
546
- var import_react20 = require("react");
547
-
548
- // src/hooks/usePrevious.ts
549
- var import_react19 = require("react");
401
+ //#endregion
402
+ //#region src/hooks/usePrevious.ts
550
403
  function usePrevious(value) {
551
- const ref2 = (0, import_react19.useRef)(void 0);
552
- (0, import_react19.useEffect)(() => {
553
- ref2.current = value;
554
- });
555
- return ref2.current;
404
+ const ref = (0, react.useRef)(void 0);
405
+ (0, react.useEffect)(() => {
406
+ ref.current = value;
407
+ });
408
+ return ref.current;
556
409
  }
557
410
 
558
- // src/hooks/useWatch.ts
411
+ //#endregion
412
+ //#region src/hooks/useWatch.ts
559
413
  function useWatch(source, callback, options = {}) {
560
- const firstUpdate = (0, import_react20.useRef)(false);
561
- const then = (0, import_react20.useRef)(void 0);
562
- const oldValue = usePrevious(source);
563
- const deps = (0, import_react20.useMemo)(
564
- () => Array.isArray(source) ? source : [source],
565
- [source]
566
- );
567
- (0, import_react20.useEffect)(() => {
568
- if (!firstUpdate.current)
569
- recordFirst();
570
- else
571
- callback(source, oldValue);
572
- }, deps);
573
- async function recordFirst() {
574
- if (then.current)
575
- return;
576
- then.current = Promise.resolve(source);
577
- then.current.then(() => firstUpdate.current = true);
578
- if (options.immediate)
579
- then.current.then((value) => callback(value, oldValue));
580
- }
581
- }
582
-
583
- // src/hooks/useOffsetPagination.ts
414
+ const firstUpdate = (0, react.useRef)(false);
415
+ const then = (0, react.useRef)(void 0);
416
+ const oldValue = usePrevious(source);
417
+ (0, react.useEffect)(() => {
418
+ if (!firstUpdate.current) recordFirst();
419
+ else callback(source, oldValue);
420
+ }, (0, react.useMemo)(() => Array.isArray(source) ? source : [source], [source]));
421
+ async function recordFirst() {
422
+ if (then.current) return;
423
+ then.current = Promise.resolve(source);
424
+ then.current.then(() => firstUpdate.current = true);
425
+ if (options.immediate) then.current.then((value) => callback(value, oldValue));
426
+ }
427
+ }
428
+
429
+ //#endregion
430
+ //#region src/hooks/useOffsetPagination.ts
584
431
  function useOffsetPagination(options) {
585
- const [page, setPage] = (0, import_react21.useState)(options.page || 1);
586
- const [pageSize, setPageSize] = (0, import_react21.useState)(options.pageSize || 10);
587
- const total = options.total || 0;
588
- const pageCount = (0, import_react21.useMemo)(() => Math.max(1, Math.ceil(total / pageSize)), [total, pageSize]);
589
- const isFirstPage = (0, import_react21.useMemo)(() => page === 1, [page]);
590
- const isLastPage = (0, import_react21.useMemo)(() => page === pageCount, [page, pageCount]);
591
- function next() {
592
- setPage((page2) => Math.min(pageCount, page2 + 1));
593
- }
594
- function prev() {
595
- setPage((page2) => Math.max(1, page2 - 1));
596
- }
597
- function pageChange(page2) {
598
- setPage(() => Math.max(1, Math.min(page2, pageCount)));
599
- }
600
- function pageSizeChange(limit) {
601
- setPageSize(limit);
602
- pageChange(1);
603
- }
604
- const pagination = {
605
- next,
606
- prev,
607
- page,
608
- pageSize,
609
- isFirstPage,
610
- pageSizeChange,
611
- pageChange,
612
- isLastPage,
613
- pageCount,
614
- total
615
- };
616
- useWatch(page, () => options.onChange?.(pagination));
617
- useWatch(pageSize, () => options.onPageSizeChange?.(pagination));
618
- useWatch(pageCount, () => options.onPageCountChange?.(pagination));
619
- return pagination;
620
- }
621
-
622
- // src/hooks/useWhenever.ts
432
+ const [page, setPage] = (0, react.useState)(options.page || 1);
433
+ const [pageSize, setPageSize] = (0, react.useState)(options.pageSize || 10);
434
+ const total = options.total || 0;
435
+ const pageCount = (0, react.useMemo)(() => Math.max(1, Math.ceil(total / pageSize)), [total, pageSize]);
436
+ const isFirstPage = (0, react.useMemo)(() => page === 1, [page]);
437
+ const isLastPage = (0, react.useMemo)(() => page === pageCount, [page, pageCount]);
438
+ function next() {
439
+ setPage((page) => Math.min(pageCount, page + 1));
440
+ }
441
+ function prev() {
442
+ setPage((page) => Math.max(1, page - 1));
443
+ }
444
+ function pageChange(page) {
445
+ setPage(() => Math.max(1, Math.min(page, pageCount)));
446
+ }
447
+ function pageSizeChange(limit) {
448
+ setPageSize(limit);
449
+ pageChange(1);
450
+ }
451
+ const pagination = {
452
+ next,
453
+ prev,
454
+ page,
455
+ pageSize,
456
+ isFirstPage,
457
+ pageSizeChange,
458
+ pageChange,
459
+ isLastPage,
460
+ pageCount,
461
+ total
462
+ };
463
+ useWatch(page, () => options.onChange?.(pagination));
464
+ useWatch(pageSize, () => options.onPageSizeChange?.(pagination));
465
+ useWatch(pageCount, () => options.onPageCountChange?.(pagination));
466
+ return pagination;
467
+ }
468
+
469
+ //#endregion
470
+ //#region src/hooks/useWhenever.ts
623
471
  function useWhenever(source, cb, options) {
624
- useWatch(source, (value, oldValue) => value && cb(value, oldValue), options);
625
- }
626
-
627
- // src/storage/defineStore.ts
628
- var import_react22 = require("react");
629
- var import_valtio4 = require("valtio");
630
-
631
- // src/storage/persistant.ts
632
- var import_valtio3 = require("valtio");
633
- function proxyWithPersistant(keyOrOptions, initialObject) {
634
- let options;
635
- if (typeof keyOrOptions === "string") {
636
- options = { id: keyOrOptions };
637
- } else {
638
- options = { ...keyOrOptions };
639
- }
640
- const storage = options.storage || (typeof localStorage !== "undefined" ? localStorage : void 0);
641
- typeof keyOrOptions === "string" && (keyOrOptions = { id: keyOrOptions });
642
- const state = (0, import_valtio3.proxy)(tryParseJson(storage?.getItem(options.id)) || initialObject);
643
- (0, import_valtio3.subscribe)(state, () => {
644
- const pick = options.pick || Object.keys(state);
645
- const statePick = {};
646
- for (const key of pick)
647
- statePick[key] = state[key];
648
- storage?.setItem(options.id, JSON.stringify(statePick));
649
- });
650
- return state;
651
- }
652
-
653
- // src/storage/utils/index.ts
654
- function track2(action, status) {
655
- let loadings = 0;
656
- const tracking = () => loadings++ === 0 && (status.loading = true);
657
- const done = () => !--loadings && (status.loading = false);
658
- const fulfilled = (value) => {
659
- status.finished = true;
660
- done();
661
- return value;
662
- };
663
- const rejected = (error) => {
664
- status.error = error;
665
- done();
666
- throw error;
667
- };
668
- return function(...args) {
669
- tracking();
670
- try {
671
- const value = action(...args);
672
- return value instanceof Promise ? value.then(fulfilled, rejected) : fulfilled(value);
673
- } catch (error) {
674
- rejected(error);
675
- }
676
- };
677
- }
678
-
679
- // src/storage/defineStore.ts
680
- function defineStore(store, options = {}) {
681
- const state = typeof store.state === "function" ? store.state() : store.state;
682
- const getters = store.getters || {};
683
- const actions = store.actions || {};
684
- const status = {};
685
- status.finished = false;
686
- status.loading = false;
687
- status.error = null;
688
- const $status = (0, import_valtio4.proxy)(status);
689
- const $state = options.persist ? proxyWithPersistant(options.persist, state) : (0, import_valtio4.proxy)(state);
690
- const $actions = {};
691
- const $getters = {};
692
- setupActions($state, actions, $actions, $status);
693
- setupGetters(state, $state, getters, $getters);
694
- setupStatus($actions, $status);
695
- function $subscribe(listener) {
696
- return (0, import_valtio4.subscribe)($state, () => listener($state));
697
- }
698
- $subscribe.status = function(listener) {
699
- return (0, import_valtio4.subscribe)($status, () => listener($status));
700
- };
701
- function $patch(patch) {
702
- if (typeof patch === "function")
703
- patch($state);
704
- else
705
- Object.assign($state, patch);
706
- }
707
- function $signal(fn) {
708
- return (0, import_react22.createElement)(() => fn((0, import_valtio4.useSnapshot)($state)));
709
- }
710
- $signal.status = function(fn) {
711
- return (0, import_react22.createElement)(() => fn((0, import_valtio4.useSnapshot)($status)));
712
- };
713
- return {
714
- $subscribe,
715
- $patch,
716
- $state,
717
- $status,
718
- $actions,
719
- $getters,
720
- $signal,
721
- ...$actions
722
- };
723
- }
724
- function setupActions($state, actions, $actions, $status) {
725
- for (const key in actions) {
726
- $status[key] = { finished: false, loading: false, error: null };
727
- $actions[key] = track2(actions[key].bind($state), $status[key]);
728
- Object.defineProperty($state, key, {
729
- get: () => $actions[key]
730
- });
731
- }
732
- }
733
- function setupGetters(state, $state, getters, $getters) {
734
- for (const key in getters) {
735
- Object.defineProperty(state, key, {
736
- get: () => getters[key].call($state),
737
- enumerable: true
738
- });
739
- Object.defineProperty($getters, key, {
740
- get: () => state[key],
741
- enumerable: true
742
- });
743
- }
744
- }
745
- function setupStatus($actions, $status) {
746
- Object.defineProperty($status, "loading", {
747
- get: () => Object.keys($actions).some((key) => $status[key].loading),
748
- enumerable: true
749
- });
750
- Object.defineProperty($status, "finished", {
751
- get: () => Object.keys($actions).every((key) => $status[key].finished),
752
- enumerable: true
753
- });
754
- Object.defineProperty($status, "error", {
755
- get: () => Object.keys($actions).find((key) => $status[key].error),
756
- enumerable: true
757
- });
758
- }
759
-
760
- // src/storage/defineStoreAsync.ts
761
- var import_utils12 = require("valtio/utils");
762
- function defineStoreAsync(fetch, options = {}) {
763
- let persist;
764
- if (typeof options.persist === "string") {
765
- persist = { id: options.persist, pick: ["value"] };
766
- }
767
- if (typeof options.persist === "object") {
768
- persist = { ...options.persist, pick: ["value"] };
769
- }
770
- const store = defineStore(
771
- {
772
- state: () => ({
773
- value: options.initial,
774
- error: void 0,
775
- loading: false,
776
- finished: false
777
- }),
778
- actions: {
779
- async refetch(...args) {
780
- return this.value = await fetch(...args);
781
- },
782
- reset(value) {
783
- this.value = value || options.initial;
784
- }
785
- }
786
- },
787
- { persist }
788
- );
789
- (0, import_utils12.watch)((get) => {
790
- const status = get(store.$status.refetch);
791
- store.$state.error = status.error;
792
- store.$state.loading = status.loading;
793
- store.$state.finished = status.finished;
794
- });
795
- options.immediate && store.refetch();
796
- return store;
797
- }
798
- var defienAsyncStore = defineStoreAsync;
799
-
800
- // src/storage/useStatus.tsx
801
- var import_valtio5 = require("valtio");
802
- function useStatus(store) {
803
- return (0, import_valtio5.useSnapshot)(store.$status);
804
- }
805
-
806
- // src/storage/useStore.ts
807
- var import_valtio6 = require("valtio");
808
- function useStore(store) {
809
- return (0, import_valtio6.useSnapshot)(store.$state);
810
- }
811
- // Annotate the CommonJS export names for ESM import in node:
812
- 0 && (module.exports = {
813
- Case,
814
- Default,
815
- Else,
816
- If,
817
- Injector,
818
- Switch,
819
- Then,
820
- Trigger,
821
- Unless,
822
- cls,
823
- defienAsyncStore,
824
- defineStore,
825
- defineStoreAsync,
826
- proxyWithPersistant,
827
- track,
828
- tryUseCallback,
829
- tryUseEffect,
830
- tryUseInsertionEffect,
831
- tryUseReducer,
832
- tryUseRef,
833
- tryUseState,
834
- tryUseUpdate,
835
- useAsyncCallback,
836
- useAsyncState,
837
- useDebounce,
838
- useEventBus,
839
- useFetchRequestIntercept,
840
- useFetchResponseIntercept,
841
- useMounted,
842
- useOffsetPagination,
843
- useStatus,
844
- useStore,
845
- useUpdate,
846
- useWatch,
847
- useWhenever,
848
- wrapper
849
- });
850
- /*! Bundled license information:
851
-
852
- lodash-es/lodash.js:
853
- (**
854
- * @license
855
- * Lodash (Custom Build) <https://lodash.com/>
856
- * Build: `lodash modularize exports="es" -o ./`
857
- * Copyright OpenJS Foundation and other contributors <https://openjsf.org/>
858
- * Released under MIT license <https://lodash.com/license>
859
- * Based on Underscore.js 1.8.3 <http://underscorejs.org/LICENSE>
860
- * Copyright Jeremy Ashkenas, DocumentCloud and Investigative Reporters & Editors
861
- *)
862
- */
472
+ useWatch(source, (value, oldValue) => value && cb(value, oldValue), options);
473
+ }
474
+
475
+ //#endregion
476
+ exports.Case = Case;
477
+ exports.Default = Default;
478
+ exports.Else = Else;
479
+ exports.If = If;
480
+ exports.Injector = Injector;
481
+ exports.Switch = Switch;
482
+ exports.Then = Then;
483
+ exports.Trigger = Trigger;
484
+ exports.Unless = Unless;
485
+ exports.cls = cls;
486
+ exports.track = track;
487
+ exports.tryUseCallback = tryUseCallback;
488
+ exports.tryUseEffect = tryUseEffect;
489
+ exports.tryUseInsertionEffect = tryUseInsertionEffect;
490
+ exports.tryUseReducer = tryUseReducer;
491
+ exports.tryUseRef = tryUseRef;
492
+ exports.tryUseState = tryUseState;
493
+ exports.tryUseUpdate = tryUseUpdate;
494
+ exports.useAsyncCallback = useAsyncCallback;
495
+ exports.useAsyncState = useAsyncState;
496
+ exports.useDebounce = useDebounce;
497
+ exports.useEventBus = useEventBus;
498
+ exports.useFetchRequestIntercept = useFetchRequestIntercept;
499
+ exports.useFetchResponseIntercept = useFetchResponseIntercept;
500
+ exports.useMounted = useMounted;
501
+ exports.useOffsetPagination = useOffsetPagination;
502
+ exports.useUpdate = useUpdate;
503
+ exports.useWatch = useWatch;
504
+ exports.useWhenever = useWhenever;
505
+ exports.wrapper = wrapper;