@hairy/react-lib 1.14.0 → 1.16.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 +137 -151
- package/dist/index.d.ts +72 -60
- package/dist/index.global.js +147 -160
- package/dist/index.js +129 -145
- package/package.json +2 -2
package/dist/index.js
CHANGED
|
@@ -1,26 +1,3 @@
|
|
|
1
|
-
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
2
|
-
var __esm = (fn, res) => function __init() {
|
|
3
|
-
return fn && (res = (0, fn[__getOwnPropNames(fn)[0]])(fn = 0)), res;
|
|
4
|
-
};
|
|
5
|
-
|
|
6
|
-
// ../../node_modules/.pnpm/tslib@2.8.1/node_modules/tslib/tslib.es6.mjs
|
|
7
|
-
var __assign;
|
|
8
|
-
var init_tslib_es6 = __esm({
|
|
9
|
-
"../../node_modules/.pnpm/tslib@2.8.1/node_modules/tslib/tslib.es6.mjs"() {
|
|
10
|
-
"use strict";
|
|
11
|
-
__assign = function() {
|
|
12
|
-
__assign = Object.assign || function __assign2(t) {
|
|
13
|
-
for (var s, i = 1, n = arguments.length; i < n; i++) {
|
|
14
|
-
s = arguments[i];
|
|
15
|
-
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p)) t[p] = s[p];
|
|
16
|
-
}
|
|
17
|
-
return t;
|
|
18
|
-
};
|
|
19
|
-
return __assign.apply(this, arguments);
|
|
20
|
-
};
|
|
21
|
-
}
|
|
22
|
-
});
|
|
23
|
-
|
|
24
1
|
// src/utils/cls.ts
|
|
25
2
|
var hasOwn = {}.hasOwnProperty;
|
|
26
3
|
function cls(...args) {
|
|
@@ -168,80 +145,26 @@ function renderNodes(tokens, values) {
|
|
|
168
145
|
// src/hooks/useAsyncCallback.ts
|
|
169
146
|
import { useState } from "react";
|
|
170
147
|
function useAsyncCallback(fun) {
|
|
171
|
-
const [
|
|
172
|
-
const [loading, setLoading] = useState(false);
|
|
148
|
+
const [state, set] = useState({ loading: false });
|
|
173
149
|
async function execute(...args) {
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
const result = await fun(...args);
|
|
177
|
-
setLoading(false);
|
|
178
|
-
return result;
|
|
179
|
-
} catch (error2) {
|
|
180
|
-
setLoading(false);
|
|
181
|
-
setError(error2);
|
|
182
|
-
throw error2;
|
|
183
|
-
}
|
|
184
|
-
}
|
|
185
|
-
return [loading, execute, error];
|
|
186
|
-
}
|
|
187
|
-
|
|
188
|
-
// ../../node_modules/.pnpm/react-use@17.6.0_react-dom@18.3.1_react@18.3.1__react@18.3.1/node_modules/react-use/esm/useAsyncFn.js
|
|
189
|
-
init_tslib_es6();
|
|
190
|
-
import { useCallback as useCallback2, useRef as useRef2, useState as useState2 } from "react";
|
|
191
|
-
|
|
192
|
-
// ../../node_modules/.pnpm/react-use@17.6.0_react-dom@18.3.1_react@18.3.1__react@18.3.1/node_modules/react-use/esm/useMountedState.js
|
|
193
|
-
import { useCallback, useEffect, useRef } from "react";
|
|
194
|
-
function useMountedState() {
|
|
195
|
-
var mountedRef = useRef(false);
|
|
196
|
-
var get = useCallback(function() {
|
|
197
|
-
return mountedRef.current;
|
|
198
|
-
}, []);
|
|
199
|
-
useEffect(function() {
|
|
200
|
-
mountedRef.current = true;
|
|
201
|
-
return function() {
|
|
202
|
-
mountedRef.current = false;
|
|
203
|
-
};
|
|
204
|
-
}, []);
|
|
205
|
-
return get;
|
|
206
|
-
}
|
|
207
|
-
|
|
208
|
-
// ../../node_modules/.pnpm/react-use@17.6.0_react-dom@18.3.1_react@18.3.1__react@18.3.1/node_modules/react-use/esm/useAsyncFn.js
|
|
209
|
-
function useAsyncFn(fn, deps, initialState) {
|
|
210
|
-
if (deps === void 0) {
|
|
211
|
-
deps = [];
|
|
212
|
-
}
|
|
213
|
-
if (initialState === void 0) {
|
|
214
|
-
initialState = { loading: false };
|
|
215
|
-
}
|
|
216
|
-
var lastCallId = useRef2(0);
|
|
217
|
-
var isMounted = useMountedState();
|
|
218
|
-
var _a = useState2(initialState), state = _a[0], set = _a[1];
|
|
219
|
-
var callback = useCallback2(function() {
|
|
220
|
-
var args = [];
|
|
221
|
-
for (var _i = 0; _i < arguments.length; _i++) {
|
|
222
|
-
args[_i] = arguments[_i];
|
|
223
|
-
}
|
|
224
|
-
var callId = ++lastCallId.current;
|
|
225
|
-
if (!state.loading) {
|
|
226
|
-
set(function(prevState) {
|
|
227
|
-
return __assign(__assign({}, prevState), { loading: true });
|
|
228
|
-
});
|
|
229
|
-
}
|
|
230
|
-
return fn.apply(void 0, args).then(function(value) {
|
|
231
|
-
isMounted() && callId === lastCallId.current && set({ value, loading: false });
|
|
150
|
+
return fun(...args).then((value) => {
|
|
151
|
+
set({ loading: false });
|
|
232
152
|
return value;
|
|
233
|
-
}
|
|
234
|
-
|
|
235
|
-
return
|
|
153
|
+
}).catch((err) => {
|
|
154
|
+
set({ loading: false, error: err });
|
|
155
|
+
return Promise.reject(err);
|
|
236
156
|
});
|
|
237
|
-
}
|
|
238
|
-
return [state,
|
|
157
|
+
}
|
|
158
|
+
return [state.loading, execute, state.error];
|
|
239
159
|
}
|
|
240
160
|
|
|
161
|
+
// src/hooks/useAsyncState.ts
|
|
162
|
+
import { useEffect as useEffect2, useState as useState2 } from "react";
|
|
163
|
+
|
|
241
164
|
// ../../node_modules/.pnpm/react-use@17.6.0_react-dom@18.3.1_react@18.3.1__react@18.3.1/node_modules/react-use/esm/useEffectOnce.js
|
|
242
|
-
import { useEffect
|
|
165
|
+
import { useEffect } from "react";
|
|
243
166
|
var useEffectOnce = function(effect) {
|
|
244
|
-
|
|
167
|
+
useEffect(effect, []);
|
|
245
168
|
};
|
|
246
169
|
var useEffectOnce_default = useEffectOnce;
|
|
247
170
|
|
|
@@ -254,10 +177,14 @@ var useMount = function(fn) {
|
|
|
254
177
|
var useMount_default = useMount;
|
|
255
178
|
|
|
256
179
|
// src/hooks/useAsyncState.ts
|
|
257
|
-
function useAsyncState(
|
|
258
|
-
const [
|
|
259
|
-
|
|
260
|
-
|
|
180
|
+
function useAsyncState(fun, options) {
|
|
181
|
+
const [value, set] = useState2(options?.initial);
|
|
182
|
+
const [loading, execute, error] = useAsyncCallback(async (...args) => fun(...args).then(set));
|
|
183
|
+
useMount_default(() => options?.immediate && execute());
|
|
184
|
+
useEffect2(() => {
|
|
185
|
+
execute();
|
|
186
|
+
}, [execute]);
|
|
187
|
+
return [{ value, loading, error }, execute];
|
|
261
188
|
}
|
|
262
189
|
|
|
263
190
|
// src/hooks/useDebounce.ts
|
|
@@ -273,10 +200,10 @@ function useDebounce(value, delay) {
|
|
|
273
200
|
|
|
274
201
|
// src/hooks/useEventBus.ts
|
|
275
202
|
import mitt from "mitt";
|
|
276
|
-
import { useEffect as useEffect4, useRef
|
|
203
|
+
import { useEffect as useEffect4, useRef } from "react";
|
|
277
204
|
var emitter = mitt();
|
|
278
205
|
function useEventBus(key) {
|
|
279
|
-
const onRef =
|
|
206
|
+
const onRef = useRef();
|
|
280
207
|
function on(listener) {
|
|
281
208
|
emitter.on(key, listener);
|
|
282
209
|
onRef.current = listener;
|
|
@@ -334,10 +261,10 @@ function useMounted() {
|
|
|
334
261
|
}
|
|
335
262
|
|
|
336
263
|
// src/hooks/useWatch.ts
|
|
337
|
-
import { useEffect as useEffect6, useMemo as useMemo2, useRef as
|
|
264
|
+
import { useEffect as useEffect6, useMemo as useMemo2, useRef as useRef2 } from "react";
|
|
338
265
|
function useWatch(source, callback, options = {}) {
|
|
339
|
-
const firstUpdate =
|
|
340
|
-
const then =
|
|
266
|
+
const firstUpdate = useRef2(false);
|
|
267
|
+
const then = useRef2();
|
|
341
268
|
const deps = useMemo2(
|
|
342
269
|
() => Array.isArray(source) ? source : [source],
|
|
343
270
|
[source]
|
|
@@ -364,7 +291,8 @@ function useWhenever(source, cb, options) {
|
|
|
364
291
|
}
|
|
365
292
|
|
|
366
293
|
// src/storage/defineStore.ts
|
|
367
|
-
import {
|
|
294
|
+
import { createElement as createElement4 } from "react";
|
|
295
|
+
import { proxy as proxy2, subscribe as subscribe2, useSnapshot } from "valtio";
|
|
368
296
|
|
|
369
297
|
// ../util-core/src/util/json.ts
|
|
370
298
|
function jsonTryParse(text) {
|
|
@@ -375,7 +303,7 @@ function jsonTryParse(text) {
|
|
|
375
303
|
}
|
|
376
304
|
}
|
|
377
305
|
|
|
378
|
-
// src/storage/
|
|
306
|
+
// src/storage/persistant.ts
|
|
379
307
|
import { proxy, subscribe } from "valtio";
|
|
380
308
|
function proxyWithPersistant(keyOrOptions, initialObject) {
|
|
381
309
|
let options;
|
|
@@ -400,77 +328,133 @@ function proxyWithPersistant(keyOrOptions, initialObject) {
|
|
|
400
328
|
// src/storage/defineStore.ts
|
|
401
329
|
function defineStore(store, options = {}) {
|
|
402
330
|
const state = typeof store.state === "function" ? store.state() : store.state;
|
|
331
|
+
const getters = store.getters || {};
|
|
403
332
|
const actions = store.actions || {};
|
|
404
|
-
const
|
|
333
|
+
const status = {};
|
|
334
|
+
status.finished = false;
|
|
335
|
+
status.loading = false;
|
|
336
|
+
status.error = null;
|
|
337
|
+
const $state = options.persist ? proxyWithPersistant(options.persist, state) : proxy2(state);
|
|
338
|
+
const $status = proxy2(status);
|
|
405
339
|
const $actions = {};
|
|
406
|
-
|
|
407
|
-
|
|
340
|
+
const $getters = {};
|
|
341
|
+
setupActions($state, actions, $actions, $status);
|
|
342
|
+
setupGetters(state, $state, getters, $getters);
|
|
343
|
+
setupStatus($actions, $status);
|
|
408
344
|
function $subscribe(listener) {
|
|
409
345
|
return subscribe2($state, () => listener($state));
|
|
410
346
|
}
|
|
347
|
+
$subscribe.status = function(listener) {
|
|
348
|
+
return subscribe2($status, () => listener($status));
|
|
349
|
+
};
|
|
411
350
|
function $patch(patch) {
|
|
412
351
|
if (typeof patch === "function")
|
|
413
352
|
patch($state);
|
|
414
353
|
else
|
|
415
354
|
Object.assign($state, patch);
|
|
416
355
|
}
|
|
356
|
+
function $signal(fn) {
|
|
357
|
+
return createElement4(() => fn(useSnapshot($state)));
|
|
358
|
+
}
|
|
359
|
+
$signal.status = function(fn) {
|
|
360
|
+
return createElement4(() => fn(useSnapshot($status)));
|
|
361
|
+
};
|
|
417
362
|
return {
|
|
418
363
|
$subscribe,
|
|
419
364
|
$patch,
|
|
420
365
|
$state,
|
|
366
|
+
$status,
|
|
421
367
|
$actions,
|
|
368
|
+
$getters,
|
|
369
|
+
$signal,
|
|
422
370
|
...$actions
|
|
423
371
|
};
|
|
424
372
|
}
|
|
425
|
-
|
|
426
|
-
|
|
427
|
-
|
|
428
|
-
|
|
429
|
-
|
|
373
|
+
function track(action, status) {
|
|
374
|
+
let loadings = 0;
|
|
375
|
+
const tracking = () => loadings++ === 0 && (status.loading = true);
|
|
376
|
+
const done = () => !--loadings && (status.loading = false);
|
|
377
|
+
const fulfilled = () => {
|
|
378
|
+
status.finished = true;
|
|
379
|
+
done();
|
|
380
|
+
};
|
|
381
|
+
const rejected = (error) => {
|
|
382
|
+
status.error = error;
|
|
383
|
+
done();
|
|
384
|
+
};
|
|
385
|
+
return function(...args) {
|
|
386
|
+
tracking();
|
|
387
|
+
try {
|
|
388
|
+
const result = action(...args);
|
|
389
|
+
if (result instanceof Promise)
|
|
390
|
+
result.then(fulfilled).catch(rejected);
|
|
391
|
+
else
|
|
392
|
+
fulfilled();
|
|
393
|
+
} catch (error) {
|
|
394
|
+
rejected(error);
|
|
395
|
+
}
|
|
396
|
+
};
|
|
397
|
+
}
|
|
398
|
+
function setupActions($state, actions, $actions, $status) {
|
|
399
|
+
for (const key in actions) {
|
|
400
|
+
$status[key] = { finished: false, loading: false, error: null };
|
|
401
|
+
$actions[key] = track(actions[key].bind($state), $status[key]);
|
|
402
|
+
}
|
|
403
|
+
}
|
|
404
|
+
function setupGetters(state, $state, getters, $getters) {
|
|
405
|
+
for (const key in getters) {
|
|
406
|
+
Object.defineProperty(state, key, {
|
|
407
|
+
get: () => getters[key].call($state),
|
|
408
|
+
enumerable: true
|
|
409
|
+
});
|
|
410
|
+
Object.defineProperty($getters, key, {
|
|
411
|
+
get: () => state[key],
|
|
412
|
+
enumerable: true
|
|
413
|
+
});
|
|
414
|
+
}
|
|
415
|
+
}
|
|
416
|
+
function setupStatus($actions, $status) {
|
|
417
|
+
Object.defineProperty($status, "loading", {
|
|
418
|
+
get: () => Object.keys($actions).some((key) => $status[key].loading),
|
|
419
|
+
enumerable: true
|
|
420
|
+
});
|
|
421
|
+
Object.defineProperty($status, "finished", {
|
|
422
|
+
get: () => Object.keys($actions).every((key) => $status[key].finished),
|
|
423
|
+
enumerable: true
|
|
424
|
+
});
|
|
425
|
+
Object.defineProperty($status, "error", {
|
|
426
|
+
get: () => Object.keys($actions).find((key) => $status[key].error),
|
|
427
|
+
enumerable: true
|
|
428
|
+
});
|
|
430
429
|
}
|
|
431
430
|
|
|
432
431
|
// src/storage/defineAsyncStore.ts
|
|
433
|
-
function defineAsyncStore(options) {
|
|
432
|
+
function defineAsyncStore(fetch, options = {}) {
|
|
434
433
|
const store = defineStore(
|
|
435
434
|
{
|
|
436
|
-
state: () => ({
|
|
437
|
-
|
|
438
|
-
|
|
439
|
-
|
|
440
|
-
|
|
441
|
-
|
|
435
|
+
state: () => ({ value: options.initial }),
|
|
436
|
+
actions: {
|
|
437
|
+
fetch,
|
|
438
|
+
refresh(value) {
|
|
439
|
+
this.value = value || options.initial;
|
|
440
|
+
}
|
|
441
|
+
}
|
|
442
442
|
},
|
|
443
|
-
{
|
|
443
|
+
{ persist: options.persist }
|
|
444
444
|
);
|
|
445
|
-
|
|
446
|
-
const fn = options.setup();
|
|
447
|
-
const state = useStore(store);
|
|
448
|
-
function fetch(...args) {
|
|
449
|
-
if (state.loading)
|
|
450
|
-
return;
|
|
451
|
-
store.$state.loading = true;
|
|
452
|
-
store.$state.promise = fn(...args);
|
|
453
|
-
store.$state.promise.then((value) => store.$state.value = value).finally(() => store.$state.loading = false).catch((error) => {
|
|
454
|
-
store.$state.error = error;
|
|
455
|
-
throw error;
|
|
456
|
-
});
|
|
457
|
-
return store.$state.promise;
|
|
458
|
-
}
|
|
459
|
-
function refresh(value) {
|
|
460
|
-
store.$state.value = value || options.initial;
|
|
461
|
-
}
|
|
462
|
-
return [state, fetch, refresh];
|
|
463
|
-
}
|
|
464
|
-
return use;
|
|
445
|
+
return store;
|
|
465
446
|
}
|
|
466
447
|
|
|
467
|
-
// src/storage/
|
|
468
|
-
|
|
469
|
-
|
|
470
|
-
|
|
471
|
-
|
|
472
|
-
|
|
473
|
-
|
|
448
|
+
// src/storage/useStatus.tsx
|
|
449
|
+
import { useSnapshot as useSnapshot2 } from "valtio";
|
|
450
|
+
function useStatus(store) {
|
|
451
|
+
return useSnapshot2(store.$status);
|
|
452
|
+
}
|
|
453
|
+
|
|
454
|
+
// src/storage/useStore.ts
|
|
455
|
+
import { useSnapshot as useSnapshot3 } from "valtio";
|
|
456
|
+
function useStore(store) {
|
|
457
|
+
return useSnapshot3(store.$state);
|
|
474
458
|
}
|
|
475
459
|
export {
|
|
476
460
|
Case,
|
|
@@ -484,7 +468,6 @@ export {
|
|
|
484
468
|
Unless,
|
|
485
469
|
cls,
|
|
486
470
|
defineAsyncStore,
|
|
487
|
-
defineAsyncStorePlain,
|
|
488
471
|
defineStore,
|
|
489
472
|
proxyWithPersistant,
|
|
490
473
|
useAsyncCallback,
|
|
@@ -494,6 +477,7 @@ export {
|
|
|
494
477
|
useFetchRequestIntercept,
|
|
495
478
|
useFetchResponseIntercept,
|
|
496
479
|
useMounted,
|
|
480
|
+
useStatus,
|
|
497
481
|
useStore,
|
|
498
482
|
useWatch,
|
|
499
483
|
useWhenever,
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@hairy/react-lib",
|
|
3
3
|
"type": "module",
|
|
4
|
-
"version": "1.
|
|
4
|
+
"version": "1.16.0",
|
|
5
5
|
"description": "Library for react",
|
|
6
6
|
"author": "Hairyf <wwu710632@gmail.com>",
|
|
7
7
|
"license": "MIT",
|
|
@@ -38,7 +38,7 @@
|
|
|
38
38
|
"react-dom": "^18.2.0",
|
|
39
39
|
"react-i18next": "^14.1.2",
|
|
40
40
|
"react-use": "^17.6.0",
|
|
41
|
-
"@hairy/utils": "1.
|
|
41
|
+
"@hairy/utils": "1.16.0"
|
|
42
42
|
},
|
|
43
43
|
"scripts": {
|
|
44
44
|
"build": "tsup",
|