@hairy/react-lib 1.46.0 → 1.49.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,504 @@
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
+ */
220
76
  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");
77
+ const deferred = (0, valtio.ref)(new _hairy_utils.Deferred());
78
+ const exposer = {
79
+ fn,
80
+ args,
81
+ deferred,
82
+ id: ++Trigger.id
83
+ };
84
+ Trigger.tasks.set(exposer.id, exposer);
85
+ deferred.then(() => Trigger.tasks.delete(exposer.id));
86
+ return deferred;
87
+ }
88
+
89
+ //#endregion
90
+ //#region src/utils/wrapper.ts
230
91
  function wrapper(asChild, props, children) {
231
- return asChild ? (0, import_react.createElement)(asChild, props, children) : children;
92
+ return asChild ? (0, react.createElement)(asChild, props, children) : children;
232
93
  }
233
94
 
234
- // src/components/condition/Case.ts
95
+ //#endregion
96
+ //#region src/components/condition/Case.ts
235
97
  function Case(props) {
236
- const { cond, children, tag, as: asChild, ...attrs } = props;
237
- return wrapper(tag || asChild, attrs, children);
98
+ const { cond, children, tag, as: asChild, ...attrs } = props;
99
+ return wrapper(tag || asChild, attrs, children);
238
100
  }
239
101
 
240
- // src/components/condition/Default.ts
102
+ //#endregion
103
+ //#region src/components/condition/Default.ts
241
104
  function Default(props) {
242
- const { children, tag, as: asChild, ...attrs } = props;
243
- return wrapper(tag || asChild, attrs, children);
105
+ const { children, tag, as: asChild, ...attrs } = props;
106
+ return wrapper(tag || asChild, attrs, children);
244
107
  }
245
108
 
246
- // src/components/condition/If.ts
247
- var import_react2 = require("react");
248
-
249
- // src/components/condition/Then.ts
109
+ //#endregion
110
+ //#region src/components/condition/Then.ts
250
111
  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);
112
+ const { children, cond, else: _else, then, tag, as: asChild, ...attrs } = props;
113
+ return Object.keys(props).includes("cond") ? wrapper(If, props, children) : wrapper(tag || asChild, attrs, children);
253
114
  }
254
115
 
255
- // src/components/condition/If.ts
116
+ //#endregion
117
+ //#region src/components/condition/If.ts
256
118
  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);
119
+ const { then, cond, else: _else, children = props.then, tag, as: asChild, ...attrs } = props;
120
+ const elements = react.Children.toArray(children);
121
+ const thenChild = elements.find((c) => c.type === Then);
122
+ const elseChild = elements.find((c) => c.type === Else);
123
+ return wrapper(tag || asChild, attrs, thenChild || elseChild ? cond ? thenChild : elseChild : cond ? children : _else);
263
124
  }
264
125
 
265
- // src/components/condition/Else.ts
126
+ //#endregion
127
+ //#region src/components/condition/Else.ts
266
128
  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);
129
+ const { children, tag, as: asChild, ...attrs } = props;
130
+ return Object.keys(props).includes("cond") ? wrapper(If, props, children) : wrapper(tag || asChild, attrs, children);
269
131
  }
270
132
 
271
- // src/components/condition/Switch.ts
272
- var import_react3 = require("react");
133
+ //#endregion
134
+ //#region src/components/condition/Switch.ts
273
135
  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");
136
+ const isUseValue = props.value !== void 0;
137
+ let matchingCase;
138
+ let defaultCase;
139
+ react.Children.forEach(props.children, (child) => {
140
+ if (!(0, react.isValidElement)(child) || matchingCase) return;
141
+ if (child.type === Case) {
142
+ const cond = child?.props?.cond;
143
+ if (isUseValue ? props.value === cond : cond) {
144
+ matchingCase = child;
145
+ return;
146
+ }
147
+ }
148
+ if (!defaultCase && child.type === Default) defaultCase = child;
149
+ });
150
+ return matchingCase ?? defaultCase ?? null;
151
+ }
152
+
153
+ //#endregion
154
+ //#region src/components/condition/Unless.ts
295
155
  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);
156
+ const { cond, then, else: _else, tag, as: asChild, children = props.then, ...attrs } = props;
157
+ const elements = react.Children.toArray(children);
158
+ const thenChild = elements.find((c) => c.type === Then);
159
+ const elseChild = elements.find((c) => c.type === Else);
160
+ return wrapper(tag || asChild, attrs, thenChild || elseChild ? !cond ? elseChild : thenChild : !cond ? children : _else);
302
161
  }
303
162
 
304
- // src/components/utils/Injector.ts
305
- var import_react5 = require("react");
163
+ //#endregion
164
+ //#region src/components/utils/Injector.ts
306
165
  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
- );
166
+ 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
167
  }
316
168
  function repack(c) {
317
- return c.component ? c : { component: c };
169
+ return c.component ? c : { component: c };
318
170
  }
319
171
 
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)();
172
+ //#endregion
173
+ //#region src/components/utils/Trigger.ts
174
+ const pendingTasks = (0, valtio_utils.proxyMap)();
325
175
  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;
176
+ const Component = () => {
177
+ try {
178
+ exposer.deferred.resolve(exposer.fn(...exposer.args));
179
+ } catch (error) {
180
+ exposer.deferred.reject(error);
181
+ }
182
+ return null;
183
+ };
184
+ Component.key = exposer.id;
185
+ return Component;
336
186
  }
337
187
  function renderTracker(Tracker) {
338
- return (0, import_react6.createElement)(Tracker, { key: Tracker.key });
339
- }
188
+ return (0, react.createElement)(Tracker, { key: Tracker.key });
189
+ }
190
+ /**
191
+ * @example
192
+ * ```tsx
193
+ * import { Trigger } from '@hairy/lib-react'
194
+ *
195
+ * // Use triggers to capture context
196
+ * function App() {
197
+ * return (
198
+ * <YourContext.Provider>
199
+ * <Trigger />
200
+ * </YourContext.Provider>
201
+ * )
202
+ * }
203
+ *
204
+ * // Obtain externally
205
+ * import { track } from '@hairy/lib-react'
206
+ * const context = await track(() => useContext(YourContext))
207
+ * console.log(context) // { ... }
208
+ * ```
209
+ */
340
210
  function Trigger() {
341
- const values = [...(0, import_valtio2.useSnapshot)(pendingTasks).values()];
342
- return values.map(createTracker).map(renderTracker);
211
+ return [...(0, valtio.useSnapshot)(pendingTasks).values()].map(createTracker).map(renderTracker);
343
212
  }
344
213
  Trigger.id = 0;
345
214
  Trigger.tasks = pendingTasks;
346
215
 
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
- }
216
+ //#endregion
217
+ //#region src/hooks/tryUseCallback.ts
218
+ const tryUseCallback = (callback, deps) => {
219
+ try {
220
+ return (0, react.useCallback)(callback, deps);
221
+ } catch {
222
+ return callback;
223
+ }
355
224
  };
356
225
 
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
- }
226
+ //#endregion
227
+ //#region src/hooks/tryUseEffect.ts
228
+ const tryUseEffect = (effect, deps) => {
229
+ try {
230
+ (0, react.useEffect)(effect, deps);
231
+ } catch {}
364
232
  };
365
233
 
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
- }
234
+ //#endregion
235
+ //#region src/hooks/tryUseInsertionEffect.ts
236
+ const tryUseInsertionEffect = (callback, deps) => {
237
+ try {
238
+ (0, react.useInsertionEffect)(callback, deps);
239
+ } catch {}
373
240
  };
374
241
 
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
- }
242
+ //#endregion
243
+ //#region src/hooks/tryUseReducer.ts
244
+ const tryUseReducer = (reducer, initializerArg, initializer) => {
245
+ try {
246
+ return (0, react.useReducer)(reducer, initializerArg, initializer);
247
+ } catch {
248
+ return [initializerArg, () => {}];
249
+ }
384
250
  };
385
251
 
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
- }
252
+ //#endregion
253
+ //#region src/hooks/tryUseRef.ts
254
+ const tryUseRef = (initialValue) => {
255
+ try {
256
+ return (0, react.useRef)(initialValue);
257
+ } catch {
258
+ return { current: initialValue };
259
+ }
394
260
  };
395
261
 
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
- }
262
+ //#endregion
263
+ //#region src/hooks/tryUseState.ts
264
+ const tryUseState = (initialState) => {
265
+ try {
266
+ return (0, react.useState)(initialState);
267
+ } catch {
268
+ return [(0, _hairy_utils.isFunction)(initialState) ? initialState() : initialState, _hairy_utils.noop];
269
+ }
404
270
  };
405
271
 
406
- // src/hooks/useUpdate.ts
407
- var updateReducer = (num) => (num + 1) % 1e6;
272
+ //#endregion
273
+ //#region src/hooks/useUpdate.ts
274
+ const updateReducer = (num) => (num + 1) % 1e6;
408
275
  function useUpdate() {
409
- const [, update] = tryUseReducer(updateReducer, 0);
410
- return update;
276
+ const [, update] = tryUseReducer(updateReducer, 0);
277
+ return update;
411
278
  }
412
279
 
413
- // src/hooks/tryUseUpdate.ts
280
+ //#endregion
281
+ //#region src/hooks/tryUseUpdate.ts
414
282
  function tryUseUpdate() {
415
- try {
416
- return useUpdate();
417
- } catch {
418
- return () => {
419
- };
420
- }
283
+ try {
284
+ return useUpdate();
285
+ } catch {
286
+ return () => {};
287
+ }
421
288
  }
422
289
 
423
- // src/hooks/useAsyncCallback.ts
424
- var import_react13 = require("react");
290
+ //#endregion
291
+ //#region src/hooks/useAsyncCallback.ts
425
292
  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");
293
+ const [state, set] = (0, react.useState)({ loading: false });
294
+ async function refetch(...args) {
295
+ set({ loading: true });
296
+ return fun(...args).then((value) => {
297
+ set({ loading: false });
298
+ return value;
299
+ }).catch((err) => {
300
+ set({
301
+ loading: false,
302
+ error: err
303
+ });
304
+ return Promise.reject(err);
305
+ });
306
+ }
307
+ return [
308
+ state.loading,
309
+ refetch,
310
+ state.error
311
+ ];
312
+ }
313
+
314
+ //#endregion
315
+ //#region src/hooks/useAsyncState.ts
442
316
  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");
317
+ const [value, set] = (0, react.useState)(options?.initial);
318
+ const [loading, refetch, error] = useAsyncCallback(async (...args) => fun(...args).then(set));
319
+ (0, react.useEffect)(() => {
320
+ refetch();
321
+ }, deps);
322
+ return [{
323
+ value,
324
+ loading,
325
+ error
326
+ }, refetch];
327
+ }
328
+
329
+ //#endregion
330
+ //#region src/hooks/useDebounce.ts
456
331
  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;
332
+ const [debouncedValue, setDebouncedValue] = (0, react.useState)(value);
333
+ (0, react.useEffect)(() => {
334
+ const handler = setTimeout(() => setDebouncedValue(value), delay);
335
+ return () => clearTimeout(handler);
336
+ }, [value, delay]);
337
+ return debouncedValue;
463
338
  }
464
339
 
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)();
340
+ //#endregion
341
+ //#region src/hooks/useEventBus.ts
342
+ const emitter = (0, mitt.default)();
469
343
  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
344
+ const onRef = (0, react.useRef)(void 0);
345
+ function on(listener) {
346
+ emitter.on(key, listener);
347
+ onRef.current = listener;
348
+ (0, react.useEffect)(() => {
349
+ if (!onRef.current) return;
350
+ emitter.off(key, onRef.current);
351
+ emitter.on(key, listener);
352
+ onRef.current = listener;
353
+ return () => emitter.off(key, listener);
354
+ }, [listener]);
355
+ }
356
+ function emit(event) {
357
+ emitter.emit(key, event);
358
+ }
359
+ function off(listener) {
360
+ emitter.off(key, listener);
361
+ }
362
+ return {
363
+ on,
364
+ emit,
365
+ off
366
+ };
367
+ }
368
+
369
+ //#endregion
370
+ //#region src/hooks/useFetchIntercept.ts
512
371
  function useFetchResponseIntercept(intercept) {
513
- useMount_default(() => fetchResponseIntercept(intercept));
372
+ (0, react_use.useMount)(() => fetchResponseIntercept(intercept));
514
373
  }
515
374
  function useFetchRequestIntercept(intercept) {
516
- useMount_default(() => fetchRequestIntercept(intercept));
375
+ (0, react_use.useMount)(() => fetchRequestIntercept(intercept));
517
376
  }
518
377
  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
- };
378
+ const { fetch: originalFetch } = window;
379
+ window.fetch = async (...args) => {
380
+ const [resource, config] = args;
381
+ return intercept(await originalFetch(resource, config), config);
382
+ };
525
383
  }
526
384
  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
- };
385
+ const { fetch: originalFetch } = window;
386
+ window.fetch = async (...args) => {
387
+ const [resource, config] = args;
388
+ return intercept(originalFetch, resource, config);
389
+ };
532
390
  }
533
391
 
534
- // src/hooks/useMounted.ts
535
- var import_react18 = require("react");
392
+ //#endregion
393
+ //#region src/hooks/useMounted.ts
536
394
  function useMounted() {
537
- const [mounted, setMounted] = (0, import_react18.useState)(false);
538
- (0, import_react18.useEffect)(() => setMounted(true), []);
539
- return mounted;
395
+ const [mounted, setMounted] = (0, react.useState)(false);
396
+ (0, react.useEffect)(() => setMounted(true), []);
397
+ return mounted;
540
398
  }
541
399
 
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");
400
+ //#endregion
401
+ //#region src/hooks/usePrevious.ts
550
402
  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;
403
+ const ref = (0, react.useRef)(void 0);
404
+ (0, react.useEffect)(() => {
405
+ ref.current = value;
406
+ });
407
+ return ref.current;
556
408
  }
557
409
 
558
- // src/hooks/useWatch.ts
410
+ //#endregion
411
+ //#region src/hooks/useWatch.ts
559
412
  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
413
+ const firstUpdate = (0, react.useRef)(false);
414
+ const then = (0, react.useRef)(void 0);
415
+ const oldValue = usePrevious(source);
416
+ (0, react.useEffect)(() => {
417
+ if (!firstUpdate.current) recordFirst();
418
+ else callback(source, oldValue);
419
+ }, (0, react.useMemo)(() => Array.isArray(source) ? source : [source], [source]));
420
+ async function recordFirst() {
421
+ if (then.current) return;
422
+ then.current = Promise.resolve(source);
423
+ then.current.then(() => firstUpdate.current = true);
424
+ if (options.immediate) then.current.then((value) => callback(value, oldValue));
425
+ }
426
+ }
427
+
428
+ //#endregion
429
+ //#region src/hooks/useOffsetPagination.ts
584
430
  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
431
+ const [page, setPage] = (0, react.useState)(options.page || 1);
432
+ const [pageSize, setPageSize] = (0, react.useState)(options.pageSize || 10);
433
+ const total = options.total || 0;
434
+ const pageCount = (0, react.useMemo)(() => Math.max(1, Math.ceil(total / pageSize)), [total, pageSize]);
435
+ const isFirstPage = (0, react.useMemo)(() => page === 1, [page]);
436
+ const isLastPage = (0, react.useMemo)(() => page === pageCount, [page, pageCount]);
437
+ function next() {
438
+ setPage((page) => Math.min(pageCount, page + 1));
439
+ }
440
+ function prev() {
441
+ setPage((page) => Math.max(1, page - 1));
442
+ }
443
+ function pageChange(page) {
444
+ setPage(() => Math.max(1, Math.min(page, pageCount)));
445
+ }
446
+ function pageSizeChange(limit) {
447
+ setPageSize(limit);
448
+ pageChange(1);
449
+ }
450
+ const pagination = {
451
+ next,
452
+ prev,
453
+ page,
454
+ pageSize,
455
+ isFirstPage,
456
+ pageSizeChange,
457
+ pageChange,
458
+ isLastPage,
459
+ pageCount,
460
+ total
461
+ };
462
+ useWatch(page, () => options.onChange?.(pagination));
463
+ useWatch(pageSize, () => options.onPageSizeChange?.(pagination));
464
+ useWatch(pageCount, () => options.onPageCountChange?.(pagination));
465
+ return pagination;
466
+ }
467
+
468
+ //#endregion
469
+ //#region src/hooks/useWhenever.ts
623
470
  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
- */
471
+ useWatch(source, (value, oldValue) => value && cb(value, oldValue), options);
472
+ }
473
+
474
+ //#endregion
475
+ exports.Case = Case;
476
+ exports.Default = Default;
477
+ exports.Else = Else;
478
+ exports.If = If;
479
+ exports.Injector = Injector;
480
+ exports.Switch = Switch;
481
+ exports.Then = Then;
482
+ exports.Trigger = Trigger;
483
+ exports.Unless = Unless;
484
+ exports.cls = cls;
485
+ exports.track = track;
486
+ exports.tryUseCallback = tryUseCallback;
487
+ exports.tryUseEffect = tryUseEffect;
488
+ exports.tryUseInsertionEffect = tryUseInsertionEffect;
489
+ exports.tryUseReducer = tryUseReducer;
490
+ exports.tryUseRef = tryUseRef;
491
+ exports.tryUseState = tryUseState;
492
+ exports.tryUseUpdate = tryUseUpdate;
493
+ exports.useAsyncCallback = useAsyncCallback;
494
+ exports.useAsyncState = useAsyncState;
495
+ exports.useDebounce = useDebounce;
496
+ exports.useEventBus = useEventBus;
497
+ exports.useFetchRequestIntercept = useFetchRequestIntercept;
498
+ exports.useFetchResponseIntercept = useFetchResponseIntercept;
499
+ exports.useMounted = useMounted;
500
+ exports.useOffsetPagination = useOffsetPagination;
501
+ exports.useUpdate = useUpdate;
502
+ exports.useWatch = useWatch;
503
+ exports.useWhenever = useWhenever;
504
+ exports.wrapper = wrapper;