@hairy/react-lib 1.6.0 → 1.6.2

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
@@ -5,6 +5,9 @@ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
5
5
  var __getOwnPropNames = Object.getOwnPropertyNames;
6
6
  var __getProtoOf = Object.getPrototypeOf;
7
7
  var __hasOwnProp = Object.prototype.hasOwnProperty;
8
+ var __esm = (fn, res) => function __init() {
9
+ return fn && (res = (0, fn[__getOwnPropNames(fn)[0]])(fn = 0)), res;
10
+ };
8
11
  var __export = (target, all) => {
9
12
  for (var name in all)
10
13
  __defProp(target, name, { get: all[name], enumerable: true });
@@ -27,6 +30,24 @@ var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__ge
27
30
  ));
28
31
  var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
29
32
 
33
+ // ../../node_modules/.pnpm/tslib@2.8.1/node_modules/tslib/tslib.es6.mjs
34
+ var __assign;
35
+ var init_tslib_es6 = __esm({
36
+ "../../node_modules/.pnpm/tslib@2.8.1/node_modules/tslib/tslib.es6.mjs"() {
37
+ "use strict";
38
+ __assign = function() {
39
+ __assign = Object.assign || function __assign2(t) {
40
+ for (var s, i = 1, n = arguments.length; i < n; i++) {
41
+ s = arguments[i];
42
+ for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p)) t[p] = s[p];
43
+ }
44
+ return t;
45
+ };
46
+ return __assign.apply(this, arguments);
47
+ };
48
+ }
49
+ });
50
+
30
51
  // src/index.ts
31
52
  var index_exports = {};
32
53
  __export(index_exports, {
@@ -171,19 +192,86 @@ function useAsyncCallback(fun) {
171
192
  return [loading, execute, error];
172
193
  }
173
194
 
195
+ // ../../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
196
+ init_tslib_es6();
197
+ var import_react8 = require("react");
198
+
199
+ // ../../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
200
+ var import_react7 = require("react");
201
+ function useMountedState() {
202
+ var mountedRef = (0, import_react7.useRef)(false);
203
+ var get = (0, import_react7.useCallback)(function() {
204
+ return mountedRef.current;
205
+ }, []);
206
+ (0, import_react7.useEffect)(function() {
207
+ mountedRef.current = true;
208
+ return function() {
209
+ mountedRef.current = false;
210
+ };
211
+ }, []);
212
+ return get;
213
+ }
214
+
215
+ // ../../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
216
+ function useAsyncFn(fn, deps, initialState) {
217
+ if (deps === void 0) {
218
+ deps = [];
219
+ }
220
+ if (initialState === void 0) {
221
+ initialState = { loading: false };
222
+ }
223
+ var lastCallId = (0, import_react8.useRef)(0);
224
+ var isMounted = useMountedState();
225
+ var _a = (0, import_react8.useState)(initialState), state = _a[0], set = _a[1];
226
+ var callback = (0, import_react8.useCallback)(function() {
227
+ var args = [];
228
+ for (var _i = 0; _i < arguments.length; _i++) {
229
+ args[_i] = arguments[_i];
230
+ }
231
+ var callId = ++lastCallId.current;
232
+ if (!state.loading) {
233
+ set(function(prevState) {
234
+ return __assign(__assign({}, prevState), { loading: true });
235
+ });
236
+ }
237
+ return fn.apply(void 0, args).then(function(value) {
238
+ isMounted() && callId === lastCallId.current && set({ value, loading: false });
239
+ return value;
240
+ }, function(error) {
241
+ isMounted() && callId === lastCallId.current && set({ error, loading: false });
242
+ return error;
243
+ });
244
+ }, deps);
245
+ return [state, callback];
246
+ }
247
+
248
+ // ../../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
249
+ var import_react9 = require("react");
250
+ var useEffectOnce = function(effect) {
251
+ (0, import_react9.useEffect)(effect, []);
252
+ };
253
+ var useEffectOnce_default = useEffectOnce;
254
+
255
+ // ../../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/useMount.js
256
+ var useMount = function(fn) {
257
+ useEffectOnce_default(function() {
258
+ fn();
259
+ });
260
+ };
261
+ var useMount_default = useMount;
262
+
174
263
  // src/hooks/useAsyncState.ts
175
- var import_react_use = require("react-use");
176
264
  function useAsyncState(fn, deps, options) {
177
- const [state, _fn] = (0, import_react_use.useAsyncFn)(fn, deps, options?.initial);
178
- (0, import_react_use.useMount)(() => options?.immediate && _fn());
265
+ const [state, _fn] = useAsyncFn(fn, deps, options?.initial);
266
+ useMount_default(() => options?.immediate && _fn());
179
267
  return [state, _fn];
180
268
  }
181
269
 
182
270
  // src/hooks/useDebounce.ts
183
- var import_react7 = require("react");
271
+ var import_react10 = require("react");
184
272
  function useDebounce(value, delay) {
185
- const [debouncedValue, setDebouncedValue] = (0, import_react7.useState)(value);
186
- (0, import_react7.useEffect)(() => {
273
+ const [debouncedValue, setDebouncedValue] = (0, import_react10.useState)(value);
274
+ (0, import_react10.useEffect)(() => {
187
275
  const handler = setTimeout(() => setDebouncedValue(value), delay);
188
276
  return () => clearTimeout(handler);
189
277
  }, [value, delay]);
@@ -192,14 +280,14 @@ function useDebounce(value, delay) {
192
280
 
193
281
  // src/hooks/useEventBus.ts
194
282
  var import_mitt = __toESM(require("mitt"), 1);
195
- var import_react8 = require("react");
283
+ var import_react11 = require("react");
196
284
  var emitter = (0, import_mitt.default)();
197
285
  function useEventBus(key) {
198
- const onRef = (0, import_react8.useRef)();
286
+ const onRef = (0, import_react11.useRef)();
199
287
  function on(listener) {
200
288
  emitter.on(key, listener);
201
289
  onRef.current = listener;
202
- (0, import_react8.useEffect)(() => {
290
+ (0, import_react11.useEffect)(() => {
203
291
  if (!onRef.current)
204
292
  return;
205
293
  emitter.off(key, onRef.current);
@@ -222,12 +310,11 @@ function useEventBus(key) {
222
310
  }
223
311
 
224
312
  // src/hooks/useFetchIntercept.ts
225
- var import_react_use2 = require("react-use");
226
313
  function useFetchResponseIntercept(intercept) {
227
- (0, import_react_use2.useMount)(() => fetchResponseIntercept(intercept));
314
+ useMount_default(() => fetchResponseIntercept(intercept));
228
315
  }
229
316
  function useFetchRequestIntercept(intercept) {
230
- (0, import_react_use2.useMount)(() => fetchRequestIntercept(intercept));
317
+ useMount_default(() => fetchRequestIntercept(intercept));
231
318
  }
232
319
  function fetchResponseIntercept(intercept) {
233
320
  const { fetch: originalFetch } = window;
@@ -246,23 +333,23 @@ function fetchRequestIntercept(intercept) {
246
333
  }
247
334
 
248
335
  // src/hooks/useMounted.ts
249
- var import_react9 = require("react");
336
+ var import_react12 = require("react");
250
337
  function useMounted() {
251
- const [mounted, setMounted] = (0, import_react9.useState)(false);
252
- (0, import_react9.useEffect)(() => setMounted(true), []);
338
+ const [mounted, setMounted] = (0, import_react12.useState)(false);
339
+ (0, import_react12.useEffect)(() => setMounted(true), []);
253
340
  return mounted;
254
341
  }
255
342
 
256
343
  // src/hooks/useWatch.ts
257
- var import_react10 = require("react");
344
+ var import_react13 = require("react");
258
345
  function useWatch(source, callback, options = {}) {
259
- const firstUpdate = (0, import_react10.useRef)(false);
260
- const then = (0, import_react10.useRef)();
261
- const deps = (0, import_react10.useMemo)(
346
+ const firstUpdate = (0, import_react13.useRef)(false);
347
+ const then = (0, import_react13.useRef)();
348
+ const deps = (0, import_react13.useMemo)(
262
349
  () => Array.isArray(source) ? source : [source],
263
350
  [source]
264
351
  );
265
- (0, import_react10.useEffect)(() => {
352
+ (0, import_react13.useEffect)(() => {
266
353
  if (!firstUpdate.current)
267
354
  recordFirst();
268
355
  else
package/dist/index.d.ts CHANGED
@@ -1,8 +1,7 @@
1
1
  import { BooleanLike } from '@hairy/utils';
2
2
  import * as react from 'react';
3
3
  import { ReactNode, PropsWithChildren, ReactElement, FC, ComponentClass, DependencyList, DetailedHTMLProps, HTMLAttributes } from 'react';
4
- import { FunctionReturningPromise, PromiseType } from 'react-use/lib/misc/types';
5
- import { AsyncState, AsyncFnReturn } from 'react-use/lib/useAsyncFn';
4
+ import { FunctionReturningPromise as FunctionReturningPromise$1, PromiseType as PromiseType$1 } from './misc/types';
6
5
 
7
6
  interface CaseProps {
8
7
  cond?: BooleanLike;
@@ -55,6 +54,32 @@ declare function Trans({ i18nKey, ...additionalProps }: TransProps): ReactNode[]
55
54
 
56
55
  declare function useAsyncCallback<T extends (...args: any[]) => any>(fun: T): readonly [boolean, T, Error | undefined];
57
56
 
57
+ declare type PromiseType<P extends Promise<any>> = P extends Promise<infer T> ? T : never;
58
+ declare type FunctionReturningPromise = (...args: any[]) => Promise<any>;
59
+
60
+ declare type AsyncState<T> = {
61
+ loading: boolean;
62
+ error?: undefined;
63
+ value?: undefined;
64
+ } | {
65
+ loading: true;
66
+ error?: Error | undefined;
67
+ value?: T;
68
+ } | {
69
+ loading: false;
70
+ error: Error;
71
+ value?: undefined;
72
+ } | {
73
+ loading: false;
74
+ error?: undefined;
75
+ value: T;
76
+ };
77
+ declare type StateFromFunctionReturningPromise$1<T extends FunctionReturningPromise$1> = AsyncState<PromiseType$1<ReturnType<T>>>;
78
+ declare type AsyncFnReturn<T extends FunctionReturningPromise$1 = FunctionReturningPromise$1> = [
79
+ StateFromFunctionReturningPromise$1<T>,
80
+ T
81
+ ];
82
+
58
83
  type StateFromFunctionReturningPromise<T extends FunctionReturningPromise> = AsyncState<PromiseType<ReturnType<T>>>;
59
84
  interface UseAsyncStateOptions<T extends FunctionReturningPromise> {
60
85
  immediate?: boolean;
package/dist/index.js CHANGED
@@ -1,3 +1,26 @@
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
+
1
24
  // src/components/condition/Case.ts
2
25
  function Case(props) {
3
26
  return props.children;
@@ -117,19 +140,86 @@ function useAsyncCallback(fun) {
117
140
  return [loading, execute, error];
118
141
  }
119
142
 
143
+ // ../../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
144
+ init_tslib_es6();
145
+ import { useCallback as useCallback2, useRef as useRef2, useState as useState2 } from "react";
146
+
147
+ // ../../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
148
+ import { useCallback, useEffect, useRef } from "react";
149
+ function useMountedState() {
150
+ var mountedRef = useRef(false);
151
+ var get = useCallback(function() {
152
+ return mountedRef.current;
153
+ }, []);
154
+ useEffect(function() {
155
+ mountedRef.current = true;
156
+ return function() {
157
+ mountedRef.current = false;
158
+ };
159
+ }, []);
160
+ return get;
161
+ }
162
+
163
+ // ../../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
164
+ function useAsyncFn(fn, deps, initialState) {
165
+ if (deps === void 0) {
166
+ deps = [];
167
+ }
168
+ if (initialState === void 0) {
169
+ initialState = { loading: false };
170
+ }
171
+ var lastCallId = useRef2(0);
172
+ var isMounted = useMountedState();
173
+ var _a = useState2(initialState), state = _a[0], set = _a[1];
174
+ var callback = useCallback2(function() {
175
+ var args = [];
176
+ for (var _i = 0; _i < arguments.length; _i++) {
177
+ args[_i] = arguments[_i];
178
+ }
179
+ var callId = ++lastCallId.current;
180
+ if (!state.loading) {
181
+ set(function(prevState) {
182
+ return __assign(__assign({}, prevState), { loading: true });
183
+ });
184
+ }
185
+ return fn.apply(void 0, args).then(function(value) {
186
+ isMounted() && callId === lastCallId.current && set({ value, loading: false });
187
+ return value;
188
+ }, function(error) {
189
+ isMounted() && callId === lastCallId.current && set({ error, loading: false });
190
+ return error;
191
+ });
192
+ }, deps);
193
+ return [state, callback];
194
+ }
195
+
196
+ // ../../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
197
+ import { useEffect as useEffect2 } from "react";
198
+ var useEffectOnce = function(effect) {
199
+ useEffect2(effect, []);
200
+ };
201
+ var useEffectOnce_default = useEffectOnce;
202
+
203
+ // ../../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/useMount.js
204
+ var useMount = function(fn) {
205
+ useEffectOnce_default(function() {
206
+ fn();
207
+ });
208
+ };
209
+ var useMount_default = useMount;
210
+
120
211
  // src/hooks/useAsyncState.ts
121
- import { useAsyncFn, useMount } from "react-use";
122
212
  function useAsyncState(fn, deps, options) {
123
213
  const [state, _fn] = useAsyncFn(fn, deps, options?.initial);
124
- useMount(() => options?.immediate && _fn());
214
+ useMount_default(() => options?.immediate && _fn());
125
215
  return [state, _fn];
126
216
  }
127
217
 
128
218
  // src/hooks/useDebounce.ts
129
- import { useEffect, useState as useState2 } from "react";
219
+ import { useEffect as useEffect3, useState as useState3 } from "react";
130
220
  function useDebounce(value, delay) {
131
- const [debouncedValue, setDebouncedValue] = useState2(value);
132
- useEffect(() => {
221
+ const [debouncedValue, setDebouncedValue] = useState3(value);
222
+ useEffect3(() => {
133
223
  const handler = setTimeout(() => setDebouncedValue(value), delay);
134
224
  return () => clearTimeout(handler);
135
225
  }, [value, delay]);
@@ -138,14 +228,14 @@ function useDebounce(value, delay) {
138
228
 
139
229
  // src/hooks/useEventBus.ts
140
230
  import mitt from "mitt";
141
- import { useEffect as useEffect2, useRef } from "react";
231
+ import { useEffect as useEffect4, useRef as useRef3 } from "react";
142
232
  var emitter = mitt();
143
233
  function useEventBus(key) {
144
- const onRef = useRef();
234
+ const onRef = useRef3();
145
235
  function on(listener) {
146
236
  emitter.on(key, listener);
147
237
  onRef.current = listener;
148
- useEffect2(() => {
238
+ useEffect4(() => {
149
239
  if (!onRef.current)
150
240
  return;
151
241
  emitter.off(key, onRef.current);
@@ -168,12 +258,11 @@ function useEventBus(key) {
168
258
  }
169
259
 
170
260
  // src/hooks/useFetchIntercept.ts
171
- import { useMount as useMount2 } from "react-use";
172
261
  function useFetchResponseIntercept(intercept) {
173
- useMount2(() => fetchResponseIntercept(intercept));
262
+ useMount_default(() => fetchResponseIntercept(intercept));
174
263
  }
175
264
  function useFetchRequestIntercept(intercept) {
176
- useMount2(() => fetchRequestIntercept(intercept));
265
+ useMount_default(() => fetchRequestIntercept(intercept));
177
266
  }
178
267
  function fetchResponseIntercept(intercept) {
179
268
  const { fetch: originalFetch } = window;
@@ -192,23 +281,23 @@ function fetchRequestIntercept(intercept) {
192
281
  }
193
282
 
194
283
  // src/hooks/useMounted.ts
195
- import { useEffect as useEffect3, useState as useState3 } from "react";
284
+ import { useEffect as useEffect5, useState as useState4 } from "react";
196
285
  function useMounted() {
197
- const [mounted, setMounted] = useState3(false);
198
- useEffect3(() => setMounted(true), []);
286
+ const [mounted, setMounted] = useState4(false);
287
+ useEffect5(() => setMounted(true), []);
199
288
  return mounted;
200
289
  }
201
290
 
202
291
  // src/hooks/useWatch.ts
203
- import { useEffect as useEffect4, useMemo as useMemo2, useRef as useRef2 } from "react";
292
+ import { useEffect as useEffect6, useMemo as useMemo2, useRef as useRef4 } from "react";
204
293
  function useWatch(source, callback, options = {}) {
205
- const firstUpdate = useRef2(false);
206
- const then = useRef2();
294
+ const firstUpdate = useRef4(false);
295
+ const then = useRef4();
207
296
  const deps = useMemo2(
208
297
  () => Array.isArray(source) ? source : [source],
209
298
  [source]
210
299
  );
211
- useEffect4(() => {
300
+ useEffect6(() => {
212
301
  if (!firstUpdate.current)
213
302
  recordFirst();
214
303
  else
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@hairy/react-lib",
3
3
  "type": "module",
4
- "version": "1.6.0",
4
+ "version": "1.6.2",
5
5
  "description": "Library for react",
6
6
  "author": "Hairyf <wwu710632@gmail.com>",
7
7
  "license": "MIT",
@@ -28,8 +28,7 @@
28
28
  },
29
29
  "dependencies": {
30
30
  "html-parse-stringify": "^3.0.1",
31
- "mitt": "^3.0.1",
32
- "react-use": "^17.6.0"
31
+ "mitt": "^3.0.1"
33
32
  },
34
33
  "devDependencies": {
35
34
  "@types/node": "^20.11.7",
@@ -38,7 +37,8 @@
38
37
  "react": "^18.2.0",
39
38
  "react-dom": "^18.2.0",
40
39
  "react-i18next": "^14.1.2",
41
- "@hairy/utils": "1.6.0"
40
+ "react-use": "^17.6.0",
41
+ "@hairy/utils": "1.6.2"
42
42
  },
43
43
  "scripts": {
44
44
  "build": "tsup",