@lemon-fe/hooks 0.1.150 → 1.1.0-alpha.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/es/config.d.ts +8 -6
- package/es/config.js +2 -1
- package/es/index.d.ts +9 -9
- package/es/index.js +10 -10
- package/es/{useDebounce → use-debounce}/index.d.ts +1 -0
- package/es/{useLatestRef → use-latest-ref}/index.d.ts +1 -0
- package/es/{usePreload → use-preload}/index.d.ts +2 -2
- package/es/use-preload/index.js +77 -0
- package/es/{usePrimaryColor → use-primary-color}/index.js +9 -8
- package/es/{useRequest → use-request}/index.d.ts +9 -0
- package/es/use-request/index.js +158 -0
- package/es/use-throttle/index.js +17 -0
- package/es/{useUpdateEffect → use-update-effect}/index.js +1 -0
- package/package.json +9 -6
- package/README.md +0 -11
- package/es/usePreload/index.js +0 -79
- package/es/useRequest/index.js +0 -153
- package/es/useThrottle/index.js +0 -14
- /package/es/{useAsyncEffect → use-async-effect}/index.d.ts +0 -0
- /package/es/{useAsyncEffect → use-async-effect}/index.js +0 -0
- /package/es/{useDebounce → use-debounce}/index.js +0 -0
- /package/es/{useInterval → use-interval}/index.d.ts +0 -0
- /package/es/{useInterval → use-interval}/index.js +0 -0
- /package/es/{useLatestRef → use-latest-ref}/index.js +0 -0
- /package/es/{usePrimaryColor → use-primary-color}/index.d.ts +0 -0
- /package/es/{useThrottle → use-throttle}/index.d.ts +0 -0
- /package/es/{useUpdateEffect → use-update-effect}/index.d.ts +0 -0
package/es/config.d.ts
CHANGED
|
@@ -1,11 +1,13 @@
|
|
|
1
1
|
import type { ReactElement } from 'react';
|
|
2
2
|
interface Opts<ToastReturnType = any> {
|
|
3
|
-
showToast()
|
|
4
|
-
cancelToast(toastReturn: ToastReturnType)
|
|
5
|
-
showError(err: Error)
|
|
6
|
-
showLoading()
|
|
7
|
-
errorResult(err: Error, refresh: () => void)
|
|
8
|
-
setPrimaryColor(val: string)
|
|
3
|
+
showToast: () => ToastReturnType;
|
|
4
|
+
cancelToast: (toastReturn: ToastReturnType) => void;
|
|
5
|
+
showError: (err: Error) => void;
|
|
6
|
+
showLoading: () => ReactElement | null;
|
|
7
|
+
errorResult: (err: Error, refresh: () => void) => ReactElement | null;
|
|
8
|
+
setPrimaryColor: (val: string) => void;
|
|
9
|
+
/** 默认throttle时间,便于移动端使用 */
|
|
10
|
+
requestThrottleInterval: number;
|
|
9
11
|
}
|
|
10
12
|
export declare const hooksOpts: Opts;
|
|
11
13
|
export declare function config<ToastReturnType = any>(opts: Partial<Opts<ToastReturnType>>): void;
|
package/es/config.js
CHANGED
package/es/index.d.ts
CHANGED
|
@@ -1,10 +1,10 @@
|
|
|
1
|
-
export { default as useDebounce } from './
|
|
2
|
-
export { default as useThrottle } from './
|
|
3
|
-
export { default as useInterval } from './
|
|
4
|
-
export { default as usePrimaryColor } from './
|
|
5
|
-
export { default as useRequest } from './
|
|
6
|
-
export { default as useAsyncEffect } from './
|
|
7
|
-
export { default as usePreload } from './
|
|
8
|
-
export { default as useUpdateEffect } from './
|
|
9
|
-
export { default as useLastestRef } from './
|
|
1
|
+
export { default as useDebounce } from './use-debounce';
|
|
2
|
+
export { default as useThrottle } from './use-throttle';
|
|
3
|
+
export { default as useInterval } from './use-interval';
|
|
4
|
+
export { default as usePrimaryColor } from './use-primary-color';
|
|
5
|
+
export { default as useRequest } from './use-request';
|
|
6
|
+
export { default as useAsyncEffect } from './use-async-effect';
|
|
7
|
+
export { default as usePreload } from './use-preload';
|
|
8
|
+
export { default as useUpdateEffect } from './use-update-effect';
|
|
9
|
+
export { default as useLastestRef } from './use-latest-ref';
|
|
10
10
|
export { config } from './config';
|
package/es/index.js
CHANGED
|
@@ -1,10 +1,10 @@
|
|
|
1
|
-
export { default as useDebounce } from
|
|
2
|
-
export { default as useThrottle } from
|
|
3
|
-
export { default as useInterval } from
|
|
4
|
-
export { default as usePrimaryColor } from
|
|
5
|
-
export { default as useRequest } from
|
|
6
|
-
export { default as useAsyncEffect } from
|
|
7
|
-
export { default as usePreload } from
|
|
8
|
-
export { default as useUpdateEffect } from
|
|
9
|
-
export { default as useLastestRef } from
|
|
10
|
-
export { config } from
|
|
1
|
+
export { default as useDebounce } from "./use-debounce";
|
|
2
|
+
export { default as useThrottle } from "./use-throttle";
|
|
3
|
+
export { default as useInterval } from "./use-interval";
|
|
4
|
+
export { default as usePrimaryColor } from "./use-primary-color";
|
|
5
|
+
export { default as useRequest } from "./use-request";
|
|
6
|
+
export { default as useAsyncEffect } from "./use-async-effect";
|
|
7
|
+
export { default as usePreload } from "./use-preload";
|
|
8
|
+
export { default as useUpdateEffect } from "./use-update-effect";
|
|
9
|
+
export { default as useLastestRef } from "./use-latest-ref";
|
|
10
|
+
export { config } from "./config";
|
|
@@ -1,8 +1,8 @@
|
|
|
1
|
-
import { ReactElement } from 'react';
|
|
1
|
+
import { type ReactElement } from 'react';
|
|
2
2
|
export default function usePreload<TargetProps>(opts: {
|
|
3
3
|
request: () => Promise<TargetProps>;
|
|
4
4
|
onSuccess?: (data: TargetProps) => void;
|
|
5
|
-
} | (() => Promise<TargetProps>), deps?:
|
|
5
|
+
} | (() => Promise<TargetProps>), deps?: any[]): {
|
|
6
6
|
refresh: () => void;
|
|
7
7
|
data: TargetProps | null;
|
|
8
8
|
mask: ReactElement<any, string | import("react").JSXElementConstructor<any>> | null;
|
|
@@ -0,0 +1,77 @@
|
|
|
1
|
+
function _typeof(o) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (o) { return typeof o; } : function (o) { return o && "function" == typeof Symbol && o.constructor === Symbol && o !== Symbol.prototype ? "symbol" : typeof o; }, _typeof(o); }
|
|
2
|
+
function _regeneratorRuntime() { "use strict"; /*! regenerator-runtime -- Copyright (c) 2014-present, Facebook, Inc. -- license (MIT): https://github.com/facebook/regenerator/blob/main/LICENSE */ _regeneratorRuntime = function _regeneratorRuntime() { return e; }; var t, e = {}, r = Object.prototype, n = r.hasOwnProperty, o = Object.defineProperty || function (t, e, r) { t[e] = r.value; }, i = "function" == typeof Symbol ? Symbol : {}, a = i.iterator || "@@iterator", c = i.asyncIterator || "@@asyncIterator", u = i.toStringTag || "@@toStringTag"; function define(t, e, r) { return Object.defineProperty(t, e, { value: r, enumerable: !0, configurable: !0, writable: !0 }), t[e]; } try { define({}, ""); } catch (t) { define = function define(t, e, r) { return t[e] = r; }; } function wrap(t, e, r, n) { var i = e && e.prototype instanceof Generator ? e : Generator, a = Object.create(i.prototype), c = new Context(n || []); return o(a, "_invoke", { value: makeInvokeMethod(t, r, c) }), a; } function tryCatch(t, e, r) { try { return { type: "normal", arg: t.call(e, r) }; } catch (t) { return { type: "throw", arg: t }; } } e.wrap = wrap; var h = "suspendedStart", l = "suspendedYield", f = "executing", s = "completed", y = {}; function Generator() {} function GeneratorFunction() {} function GeneratorFunctionPrototype() {} var p = {}; define(p, a, function () { return this; }); var d = Object.getPrototypeOf, v = d && d(d(values([]))); v && v !== r && n.call(v, a) && (p = v); var g = GeneratorFunctionPrototype.prototype = Generator.prototype = Object.create(p); function defineIteratorMethods(t) { ["next", "throw", "return"].forEach(function (e) { define(t, e, function (t) { return this._invoke(e, t); }); }); } function AsyncIterator(t, e) { function invoke(r, o, i, a) { var c = tryCatch(t[r], t, o); if ("throw" !== c.type) { var u = c.arg, h = u.value; return h && "object" == _typeof(h) && n.call(h, "__await") ? e.resolve(h.__await).then(function (t) { invoke("next", t, i, a); }, function (t) { invoke("throw", t, i, a); }) : e.resolve(h).then(function (t) { u.value = t, i(u); }, function (t) { return invoke("throw", t, i, a); }); } a(c.arg); } var r; o(this, "_invoke", { value: function value(t, n) { function callInvokeWithMethodAndArg() { return new e(function (e, r) { invoke(t, n, e, r); }); } return r = r ? r.then(callInvokeWithMethodAndArg, callInvokeWithMethodAndArg) : callInvokeWithMethodAndArg(); } }); } function makeInvokeMethod(e, r, n) { var o = h; return function (i, a) { if (o === f) throw new Error("Generator is already running"); if (o === s) { if ("throw" === i) throw a; return { value: t, done: !0 }; } for (n.method = i, n.arg = a;;) { var c = n.delegate; if (c) { var u = maybeInvokeDelegate(c, n); if (u) { if (u === y) continue; return u; } } if ("next" === n.method) n.sent = n._sent = n.arg;else if ("throw" === n.method) { if (o === h) throw o = s, n.arg; n.dispatchException(n.arg); } else "return" === n.method && n.abrupt("return", n.arg); o = f; var p = tryCatch(e, r, n); if ("normal" === p.type) { if (o = n.done ? s : l, p.arg === y) continue; return { value: p.arg, done: n.done }; } "throw" === p.type && (o = s, n.method = "throw", n.arg = p.arg); } }; } function maybeInvokeDelegate(e, r) { var n = r.method, o = e.iterator[n]; if (o === t) return r.delegate = null, "throw" === n && e.iterator.return && (r.method = "return", r.arg = t, maybeInvokeDelegate(e, r), "throw" === r.method) || "return" !== n && (r.method = "throw", r.arg = new TypeError("The iterator does not provide a '" + n + "' method")), y; var i = tryCatch(o, e.iterator, r.arg); if ("throw" === i.type) return r.method = "throw", r.arg = i.arg, r.delegate = null, y; var a = i.arg; return a ? a.done ? (r[e.resultName] = a.value, r.next = e.nextLoc, "return" !== r.method && (r.method = "next", r.arg = t), r.delegate = null, y) : a : (r.method = "throw", r.arg = new TypeError("iterator result is not an object"), r.delegate = null, y); } function pushTryEntry(t) { var e = { tryLoc: t[0] }; 1 in t && (e.catchLoc = t[1]), 2 in t && (e.finallyLoc = t[2], e.afterLoc = t[3]), this.tryEntries.push(e); } function resetTryEntry(t) { var e = t.completion || {}; e.type = "normal", delete e.arg, t.completion = e; } function Context(t) { this.tryEntries = [{ tryLoc: "root" }], t.forEach(pushTryEntry, this), this.reset(!0); } function values(e) { if (e || "" === e) { var r = e[a]; if (r) return r.call(e); if ("function" == typeof e.next) return e; if (!isNaN(e.length)) { var o = -1, i = function next() { for (; ++o < e.length;) if (n.call(e, o)) return next.value = e[o], next.done = !1, next; return next.value = t, next.done = !0, next; }; return i.next = i; } } throw new TypeError(_typeof(e) + " is not iterable"); } return GeneratorFunction.prototype = GeneratorFunctionPrototype, o(g, "constructor", { value: GeneratorFunctionPrototype, configurable: !0 }), o(GeneratorFunctionPrototype, "constructor", { value: GeneratorFunction, configurable: !0 }), GeneratorFunction.displayName = define(GeneratorFunctionPrototype, u, "GeneratorFunction"), e.isGeneratorFunction = function (t) { var e = "function" == typeof t && t.constructor; return !!e && (e === GeneratorFunction || "GeneratorFunction" === (e.displayName || e.name)); }, e.mark = function (t) { return Object.setPrototypeOf ? Object.setPrototypeOf(t, GeneratorFunctionPrototype) : (t.__proto__ = GeneratorFunctionPrototype, define(t, u, "GeneratorFunction")), t.prototype = Object.create(g), t; }, e.awrap = function (t) { return { __await: t }; }, defineIteratorMethods(AsyncIterator.prototype), define(AsyncIterator.prototype, c, function () { return this; }), e.AsyncIterator = AsyncIterator, e.async = function (t, r, n, o, i) { void 0 === i && (i = Promise); var a = new AsyncIterator(wrap(t, r, n, o), i); return e.isGeneratorFunction(r) ? a : a.next().then(function (t) { return t.done ? t.value : a.next(); }); }, defineIteratorMethods(g), define(g, u, "Generator"), define(g, a, function () { return this; }), define(g, "toString", function () { return "[object Generator]"; }), e.keys = function (t) { var e = Object(t), r = []; for (var n in e) r.push(n); return r.reverse(), function next() { for (; r.length;) { var t = r.pop(); if (t in e) return next.value = t, next.done = !1, next; } return next.done = !0, next; }; }, e.values = values, Context.prototype = { constructor: Context, reset: function reset(e) { if (this.prev = 0, this.next = 0, this.sent = this._sent = t, this.done = !1, this.delegate = null, this.method = "next", this.arg = t, this.tryEntries.forEach(resetTryEntry), !e) for (var r in this) "t" === r.charAt(0) && n.call(this, r) && !isNaN(+r.slice(1)) && (this[r] = t); }, stop: function stop() { this.done = !0; var t = this.tryEntries[0].completion; if ("throw" === t.type) throw t.arg; return this.rval; }, dispatchException: function dispatchException(e) { if (this.done) throw e; var r = this; function handle(n, o) { return a.type = "throw", a.arg = e, r.next = n, o && (r.method = "next", r.arg = t), !!o; } for (var o = this.tryEntries.length - 1; o >= 0; --o) { var i = this.tryEntries[o], a = i.completion; if ("root" === i.tryLoc) return handle("end"); if (i.tryLoc <= this.prev) { var c = n.call(i, "catchLoc"), u = n.call(i, "finallyLoc"); if (c && u) { if (this.prev < i.catchLoc) return handle(i.catchLoc, !0); if (this.prev < i.finallyLoc) return handle(i.finallyLoc); } else if (c) { if (this.prev < i.catchLoc) return handle(i.catchLoc, !0); } else { if (!u) throw new Error("try statement without catch or finally"); if (this.prev < i.finallyLoc) return handle(i.finallyLoc); } } } }, abrupt: function abrupt(t, e) { for (var r = this.tryEntries.length - 1; r >= 0; --r) { var o = this.tryEntries[r]; if (o.tryLoc <= this.prev && n.call(o, "finallyLoc") && this.prev < o.finallyLoc) { var i = o; break; } } i && ("break" === t || "continue" === t) && i.tryLoc <= e && e <= i.finallyLoc && (i = null); var a = i ? i.completion : {}; return a.type = t, a.arg = e, i ? (this.method = "next", this.next = i.finallyLoc, y) : this.complete(a); }, complete: function complete(t, e) { if ("throw" === t.type) throw t.arg; return "break" === t.type || "continue" === t.type ? this.next = t.arg : "return" === t.type ? (this.rval = this.arg = t.arg, this.method = "return", this.next = "end") : "normal" === t.type && e && (this.next = e), y; }, finish: function finish(t) { for (var e = this.tryEntries.length - 1; e >= 0; --e) { var r = this.tryEntries[e]; if (r.finallyLoc === t) return this.complete(r.completion, r.afterLoc), resetTryEntry(r), y; } }, catch: function _catch(t) { for (var e = this.tryEntries.length - 1; e >= 0; --e) { var r = this.tryEntries[e]; if (r.tryLoc === t) { var n = r.completion; if ("throw" === n.type) { var o = n.arg; resetTryEntry(r); } return o; } } throw new Error("illegal catch attempt"); }, delegateYield: function delegateYield(e, r, n) { return this.delegate = { iterator: values(e), resultName: r, nextLoc: n }, "next" === this.method && (this.arg = t), y; } }, e; }
|
|
3
|
+
function asyncGeneratorStep(gen, resolve, reject, _next, _throw, key, arg) { try { var info = gen[key](arg); var value = info.value; } catch (error) { reject(error); return; } if (info.done) { resolve(value); } else { Promise.resolve(value).then(_next, _throw); } }
|
|
4
|
+
function _asyncToGenerator(fn) { return function () { var self = this, args = arguments; return new Promise(function (resolve, reject) { var gen = fn.apply(self, args); function _next(value) { asyncGeneratorStep(gen, resolve, reject, _next, _throw, "next", value); } function _throw(err) { asyncGeneratorStep(gen, resolve, reject, _next, _throw, "throw", err); } _next(undefined); }); }; }
|
|
5
|
+
function _slicedToArray(arr, i) { return _arrayWithHoles(arr) || _iterableToArrayLimit(arr, i) || _unsupportedIterableToArray(arr, i) || _nonIterableRest(); }
|
|
6
|
+
function _nonIterableRest() { throw new TypeError("Invalid attempt to destructure non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); }
|
|
7
|
+
function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); }
|
|
8
|
+
function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) arr2[i] = arr[i]; return arr2; }
|
|
9
|
+
function _iterableToArrayLimit(r, l) { var t = null == r ? null : "undefined" != typeof Symbol && r[Symbol.iterator] || r["@@iterator"]; if (null != t) { var e, n, i, u, a = [], f = !0, o = !1; try { if (i = (t = t.call(r)).next, 0 === l) { if (Object(t) !== t) return; f = !1; } else for (; !(f = (e = i.call(t)).done) && (a.push(e.value), a.length !== l); f = !0); } catch (r) { o = !0, n = r; } finally { try { if (!f && null != t.return && (u = t.return(), Object(u) !== u)) return; } finally { if (o) throw n; } } return a; } }
|
|
10
|
+
function _arrayWithHoles(arr) { if (Array.isArray(arr)) return arr; }
|
|
11
|
+
import { useEffect, useState } from 'react';
|
|
12
|
+
import { hooksOpts } from "../config";
|
|
13
|
+
import useRequest from "../use-request";
|
|
14
|
+
export default function usePreload(opts, deps) {
|
|
15
|
+
var _useState = useState(null),
|
|
16
|
+
_useState2 = _slicedToArray(_useState, 2),
|
|
17
|
+
data = _useState2[0],
|
|
18
|
+
setData = _useState2[1];
|
|
19
|
+
var _useState3 = useState(null),
|
|
20
|
+
_useState4 = _slicedToArray(_useState3, 2),
|
|
21
|
+
error = _useState4[0],
|
|
22
|
+
setError = _useState4[1];
|
|
23
|
+
var request;
|
|
24
|
+
var _onSuccess;
|
|
25
|
+
if (typeof opts === 'function') {
|
|
26
|
+
request = opts;
|
|
27
|
+
} else {
|
|
28
|
+
request = opts.request;
|
|
29
|
+
_onSuccess = opts.onSuccess;
|
|
30
|
+
}
|
|
31
|
+
var _useRequest = useRequest( /*#__PURE__*/_asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee() {
|
|
32
|
+
var res;
|
|
33
|
+
return _regeneratorRuntime().wrap(function _callee$(_context) {
|
|
34
|
+
while (1) switch (_context.prev = _context.next) {
|
|
35
|
+
case 0:
|
|
36
|
+
setData(null);
|
|
37
|
+
_context.next = 3;
|
|
38
|
+
return request();
|
|
39
|
+
case 3:
|
|
40
|
+
res = _context.sent;
|
|
41
|
+
return _context.abrupt("return", res);
|
|
42
|
+
case 5:
|
|
43
|
+
case "end":
|
|
44
|
+
return _context.stop();
|
|
45
|
+
}
|
|
46
|
+
}, _callee);
|
|
47
|
+
})), {
|
|
48
|
+
onSuccess: function onSuccess(res) {
|
|
49
|
+
if (_onSuccess) {
|
|
50
|
+
_onSuccess(res);
|
|
51
|
+
}
|
|
52
|
+
setData(res);
|
|
53
|
+
},
|
|
54
|
+
onError: function onError(err) {
|
|
55
|
+
setError(err);
|
|
56
|
+
}
|
|
57
|
+
}),
|
|
58
|
+
run = _useRequest.run;
|
|
59
|
+
useEffect(function () {
|
|
60
|
+
run();
|
|
61
|
+
}, deps ? deps : []);
|
|
62
|
+
var handleRefresh = function handleRefresh() {
|
|
63
|
+
setError(null);
|
|
64
|
+
run();
|
|
65
|
+
};
|
|
66
|
+
var mask = null;
|
|
67
|
+
if (error) {
|
|
68
|
+
mask = hooksOpts.errorResult(error, handleRefresh);
|
|
69
|
+
} else if (data === null) {
|
|
70
|
+
mask = hooksOpts.showLoading();
|
|
71
|
+
}
|
|
72
|
+
return {
|
|
73
|
+
refresh: handleRefresh,
|
|
74
|
+
data: data,
|
|
75
|
+
mask: mask
|
|
76
|
+
};
|
|
77
|
+
}
|
|
@@ -1,26 +1,27 @@
|
|
|
1
1
|
function _slicedToArray(arr, i) { return _arrayWithHoles(arr) || _iterableToArrayLimit(arr, i) || _unsupportedIterableToArray(arr, i) || _nonIterableRest(); }
|
|
2
2
|
function _nonIterableRest() { throw new TypeError("Invalid attempt to destructure non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); }
|
|
3
3
|
function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); }
|
|
4
|
-
function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++)
|
|
5
|
-
function _iterableToArrayLimit(
|
|
4
|
+
function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) arr2[i] = arr[i]; return arr2; }
|
|
5
|
+
function _iterableToArrayLimit(r, l) { var t = null == r ? null : "undefined" != typeof Symbol && r[Symbol.iterator] || r["@@iterator"]; if (null != t) { var e, n, i, u, a = [], f = !0, o = !1; try { if (i = (t = t.call(r)).next, 0 === l) { if (Object(t) !== t) return; f = !1; } else for (; !(f = (e = i.call(t)).done) && (a.push(e.value), a.length !== l); f = !0); } catch (r) { o = !0, n = r; } finally { try { if (!f && null != t.return && (u = t.return(), Object(u) !== u)) return; } finally { if (o) throw n; } } return a; } }
|
|
6
6
|
function _arrayWithHoles(arr) { if (Array.isArray(arr)) return arr; }
|
|
7
7
|
import { useState, useEffect } from 'react';
|
|
8
|
-
import
|
|
8
|
+
import { hooksOpts } from "../config";
|
|
9
|
+
import Subject from "../subject";
|
|
9
10
|
import colorString from 'color-string';
|
|
10
|
-
import
|
|
11
|
-
import { hooksOpts } from '../config';
|
|
11
|
+
import isEqual from 'lodash/isEqual';
|
|
12
12
|
var subject = Subject();
|
|
13
13
|
var primaryColor = '#fff';
|
|
14
|
-
if (document) {
|
|
15
|
-
primaryColor = getComputedStyle(document.documentElement).getPropertyValue('--ant-primary-color').trim();
|
|
14
|
+
if (globalThis.document) {
|
|
15
|
+
primaryColor = getComputedStyle(globalThis.document.documentElement).getPropertyValue('--ant-primary-color').trim();
|
|
16
16
|
subject.subscribe({
|
|
17
17
|
next: function next(color) {
|
|
18
18
|
primaryColor = color;
|
|
19
|
-
document.documentElement.style.setProperty('--lemon-primary-color', "".concat(colorString.get.rgb(color).slice(0, 3).join(',')));
|
|
19
|
+
globalThis.document.documentElement.style.setProperty('--lemon-primary-color', "".concat(colorString.get.rgb(color).slice(0, 3).join(',')));
|
|
20
20
|
hooksOpts.setPrimaryColor(color);
|
|
21
21
|
}
|
|
22
22
|
});
|
|
23
23
|
}
|
|
24
|
+
|
|
24
25
|
/**
|
|
25
26
|
* 设置主题色
|
|
26
27
|
* @param color 主色调 必须是hex或者rgb
|
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
/// <reference types="lodash" />
|
|
1
2
|
/**
|
|
2
3
|
* 发起网络请求, 主要面向场景: 提交单据、点击列表打开详情页前请求数据,自动捕获异常
|
|
3
4
|
* @param request 网络请求方法
|
|
@@ -39,6 +40,14 @@ export default function useRequest<T extends (...args: any[]) => Promise<any> |
|
|
|
39
40
|
* 是否需要手动执行 默认 true,非手动模式默认会返回request返回的数据,data
|
|
40
41
|
*/
|
|
41
42
|
manual?: boolean;
|
|
43
|
+
/**
|
|
44
|
+
* 自动模式data的默认值,否则就是null
|
|
45
|
+
*/
|
|
46
|
+
defaultData?: K;
|
|
47
|
+
/**
|
|
48
|
+
* 非手动模式自动刷新的依赖项
|
|
49
|
+
*/
|
|
50
|
+
refreshDeps?: any[];
|
|
42
51
|
}): {
|
|
43
52
|
/**
|
|
44
53
|
* 手动执行函数
|
|
@@ -0,0 +1,158 @@
|
|
|
1
|
+
function _typeof(o) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (o) { return typeof o; } : function (o) { return o && "function" == typeof Symbol && o.constructor === Symbol && o !== Symbol.prototype ? "symbol" : typeof o; }, _typeof(o); }
|
|
2
|
+
function _regeneratorRuntime() { "use strict"; /*! regenerator-runtime -- Copyright (c) 2014-present, Facebook, Inc. -- license (MIT): https://github.com/facebook/regenerator/blob/main/LICENSE */ _regeneratorRuntime = function _regeneratorRuntime() { return e; }; var t, e = {}, r = Object.prototype, n = r.hasOwnProperty, o = Object.defineProperty || function (t, e, r) { t[e] = r.value; }, i = "function" == typeof Symbol ? Symbol : {}, a = i.iterator || "@@iterator", c = i.asyncIterator || "@@asyncIterator", u = i.toStringTag || "@@toStringTag"; function define(t, e, r) { return Object.defineProperty(t, e, { value: r, enumerable: !0, configurable: !0, writable: !0 }), t[e]; } try { define({}, ""); } catch (t) { define = function define(t, e, r) { return t[e] = r; }; } function wrap(t, e, r, n) { var i = e && e.prototype instanceof Generator ? e : Generator, a = Object.create(i.prototype), c = new Context(n || []); return o(a, "_invoke", { value: makeInvokeMethod(t, r, c) }), a; } function tryCatch(t, e, r) { try { return { type: "normal", arg: t.call(e, r) }; } catch (t) { return { type: "throw", arg: t }; } } e.wrap = wrap; var h = "suspendedStart", l = "suspendedYield", f = "executing", s = "completed", y = {}; function Generator() {} function GeneratorFunction() {} function GeneratorFunctionPrototype() {} var p = {}; define(p, a, function () { return this; }); var d = Object.getPrototypeOf, v = d && d(d(values([]))); v && v !== r && n.call(v, a) && (p = v); var g = GeneratorFunctionPrototype.prototype = Generator.prototype = Object.create(p); function defineIteratorMethods(t) { ["next", "throw", "return"].forEach(function (e) { define(t, e, function (t) { return this._invoke(e, t); }); }); } function AsyncIterator(t, e) { function invoke(r, o, i, a) { var c = tryCatch(t[r], t, o); if ("throw" !== c.type) { var u = c.arg, h = u.value; return h && "object" == _typeof(h) && n.call(h, "__await") ? e.resolve(h.__await).then(function (t) { invoke("next", t, i, a); }, function (t) { invoke("throw", t, i, a); }) : e.resolve(h).then(function (t) { u.value = t, i(u); }, function (t) { return invoke("throw", t, i, a); }); } a(c.arg); } var r; o(this, "_invoke", { value: function value(t, n) { function callInvokeWithMethodAndArg() { return new e(function (e, r) { invoke(t, n, e, r); }); } return r = r ? r.then(callInvokeWithMethodAndArg, callInvokeWithMethodAndArg) : callInvokeWithMethodAndArg(); } }); } function makeInvokeMethod(e, r, n) { var o = h; return function (i, a) { if (o === f) throw new Error("Generator is already running"); if (o === s) { if ("throw" === i) throw a; return { value: t, done: !0 }; } for (n.method = i, n.arg = a;;) { var c = n.delegate; if (c) { var u = maybeInvokeDelegate(c, n); if (u) { if (u === y) continue; return u; } } if ("next" === n.method) n.sent = n._sent = n.arg;else if ("throw" === n.method) { if (o === h) throw o = s, n.arg; n.dispatchException(n.arg); } else "return" === n.method && n.abrupt("return", n.arg); o = f; var p = tryCatch(e, r, n); if ("normal" === p.type) { if (o = n.done ? s : l, p.arg === y) continue; return { value: p.arg, done: n.done }; } "throw" === p.type && (o = s, n.method = "throw", n.arg = p.arg); } }; } function maybeInvokeDelegate(e, r) { var n = r.method, o = e.iterator[n]; if (o === t) return r.delegate = null, "throw" === n && e.iterator.return && (r.method = "return", r.arg = t, maybeInvokeDelegate(e, r), "throw" === r.method) || "return" !== n && (r.method = "throw", r.arg = new TypeError("The iterator does not provide a '" + n + "' method")), y; var i = tryCatch(o, e.iterator, r.arg); if ("throw" === i.type) return r.method = "throw", r.arg = i.arg, r.delegate = null, y; var a = i.arg; return a ? a.done ? (r[e.resultName] = a.value, r.next = e.nextLoc, "return" !== r.method && (r.method = "next", r.arg = t), r.delegate = null, y) : a : (r.method = "throw", r.arg = new TypeError("iterator result is not an object"), r.delegate = null, y); } function pushTryEntry(t) { var e = { tryLoc: t[0] }; 1 in t && (e.catchLoc = t[1]), 2 in t && (e.finallyLoc = t[2], e.afterLoc = t[3]), this.tryEntries.push(e); } function resetTryEntry(t) { var e = t.completion || {}; e.type = "normal", delete e.arg, t.completion = e; } function Context(t) { this.tryEntries = [{ tryLoc: "root" }], t.forEach(pushTryEntry, this), this.reset(!0); } function values(e) { if (e || "" === e) { var r = e[a]; if (r) return r.call(e); if ("function" == typeof e.next) return e; if (!isNaN(e.length)) { var o = -1, i = function next() { for (; ++o < e.length;) if (n.call(e, o)) return next.value = e[o], next.done = !1, next; return next.value = t, next.done = !0, next; }; return i.next = i; } } throw new TypeError(_typeof(e) + " is not iterable"); } return GeneratorFunction.prototype = GeneratorFunctionPrototype, o(g, "constructor", { value: GeneratorFunctionPrototype, configurable: !0 }), o(GeneratorFunctionPrototype, "constructor", { value: GeneratorFunction, configurable: !0 }), GeneratorFunction.displayName = define(GeneratorFunctionPrototype, u, "GeneratorFunction"), e.isGeneratorFunction = function (t) { var e = "function" == typeof t && t.constructor; return !!e && (e === GeneratorFunction || "GeneratorFunction" === (e.displayName || e.name)); }, e.mark = function (t) { return Object.setPrototypeOf ? Object.setPrototypeOf(t, GeneratorFunctionPrototype) : (t.__proto__ = GeneratorFunctionPrototype, define(t, u, "GeneratorFunction")), t.prototype = Object.create(g), t; }, e.awrap = function (t) { return { __await: t }; }, defineIteratorMethods(AsyncIterator.prototype), define(AsyncIterator.prototype, c, function () { return this; }), e.AsyncIterator = AsyncIterator, e.async = function (t, r, n, o, i) { void 0 === i && (i = Promise); var a = new AsyncIterator(wrap(t, r, n, o), i); return e.isGeneratorFunction(r) ? a : a.next().then(function (t) { return t.done ? t.value : a.next(); }); }, defineIteratorMethods(g), define(g, u, "Generator"), define(g, a, function () { return this; }), define(g, "toString", function () { return "[object Generator]"; }), e.keys = function (t) { var e = Object(t), r = []; for (var n in e) r.push(n); return r.reverse(), function next() { for (; r.length;) { var t = r.pop(); if (t in e) return next.value = t, next.done = !1, next; } return next.done = !0, next; }; }, e.values = values, Context.prototype = { constructor: Context, reset: function reset(e) { if (this.prev = 0, this.next = 0, this.sent = this._sent = t, this.done = !1, this.delegate = null, this.method = "next", this.arg = t, this.tryEntries.forEach(resetTryEntry), !e) for (var r in this) "t" === r.charAt(0) && n.call(this, r) && !isNaN(+r.slice(1)) && (this[r] = t); }, stop: function stop() { this.done = !0; var t = this.tryEntries[0].completion; if ("throw" === t.type) throw t.arg; return this.rval; }, dispatchException: function dispatchException(e) { if (this.done) throw e; var r = this; function handle(n, o) { return a.type = "throw", a.arg = e, r.next = n, o && (r.method = "next", r.arg = t), !!o; } for (var o = this.tryEntries.length - 1; o >= 0; --o) { var i = this.tryEntries[o], a = i.completion; if ("root" === i.tryLoc) return handle("end"); if (i.tryLoc <= this.prev) { var c = n.call(i, "catchLoc"), u = n.call(i, "finallyLoc"); if (c && u) { if (this.prev < i.catchLoc) return handle(i.catchLoc, !0); if (this.prev < i.finallyLoc) return handle(i.finallyLoc); } else if (c) { if (this.prev < i.catchLoc) return handle(i.catchLoc, !0); } else { if (!u) throw new Error("try statement without catch or finally"); if (this.prev < i.finallyLoc) return handle(i.finallyLoc); } } } }, abrupt: function abrupt(t, e) { for (var r = this.tryEntries.length - 1; r >= 0; --r) { var o = this.tryEntries[r]; if (o.tryLoc <= this.prev && n.call(o, "finallyLoc") && this.prev < o.finallyLoc) { var i = o; break; } } i && ("break" === t || "continue" === t) && i.tryLoc <= e && e <= i.finallyLoc && (i = null); var a = i ? i.completion : {}; return a.type = t, a.arg = e, i ? (this.method = "next", this.next = i.finallyLoc, y) : this.complete(a); }, complete: function complete(t, e) { if ("throw" === t.type) throw t.arg; return "break" === t.type || "continue" === t.type ? this.next = t.arg : "return" === t.type ? (this.rval = this.arg = t.arg, this.method = "return", this.next = "end") : "normal" === t.type && e && (this.next = e), y; }, finish: function finish(t) { for (var e = this.tryEntries.length - 1; e >= 0; --e) { var r = this.tryEntries[e]; if (r.finallyLoc === t) return this.complete(r.completion, r.afterLoc), resetTryEntry(r), y; } }, catch: function _catch(t) { for (var e = this.tryEntries.length - 1; e >= 0; --e) { var r = this.tryEntries[e]; if (r.tryLoc === t) { var n = r.completion; if ("throw" === n.type) { var o = n.arg; resetTryEntry(r); } return o; } } throw new Error("illegal catch attempt"); }, delegateYield: function delegateYield(e, r, n) { return this.delegate = { iterator: values(e), resultName: r, nextLoc: n }, "next" === this.method && (this.arg = t), y; } }, e; }
|
|
3
|
+
function asyncGeneratorStep(gen, resolve, reject, _next, _throw, key, arg) { try { var info = gen[key](arg); var value = info.value; } catch (error) { reject(error); return; } if (info.done) { resolve(value); } else { Promise.resolve(value).then(_next, _throw); } }
|
|
4
|
+
function _asyncToGenerator(fn) { return function () { var self = this, args = arguments; return new Promise(function (resolve, reject) { var gen = fn.apply(self, args); function _next(value) { asyncGeneratorStep(gen, resolve, reject, _next, _throw, "next", value); } function _throw(err) { asyncGeneratorStep(gen, resolve, reject, _next, _throw, "throw", err); } _next(undefined); }); }; }
|
|
5
|
+
function _slicedToArray(arr, i) { return _arrayWithHoles(arr) || _iterableToArrayLimit(arr, i) || _unsupportedIterableToArray(arr, i) || _nonIterableRest(); }
|
|
6
|
+
function _nonIterableRest() { throw new TypeError("Invalid attempt to destructure non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); }
|
|
7
|
+
function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); }
|
|
8
|
+
function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) arr2[i] = arr[i]; return arr2; }
|
|
9
|
+
function _iterableToArrayLimit(r, l) { var t = null == r ? null : "undefined" != typeof Symbol && r[Symbol.iterator] || r["@@iterator"]; if (null != t) { var e, n, i, u, a = [], f = !0, o = !1; try { if (i = (t = t.call(r)).next, 0 === l) { if (Object(t) !== t) return; f = !1; } else for (; !(f = (e = i.call(t)).done) && (a.push(e.value), a.length !== l); f = !0); } catch (r) { o = !0, n = r; } finally { try { if (!f && null != t.return && (u = t.return(), Object(u) !== u)) return; } finally { if (o) throw n; } } return a; } }
|
|
10
|
+
function _arrayWithHoles(arr) { if (Array.isArray(arr)) return arr; }
|
|
11
|
+
import { useRef, useEffect, useMemo, useState, useCallback } from 'react';
|
|
12
|
+
import { hooksOpts } from "../config";
|
|
13
|
+
import useLastestRef from "../use-latest-ref";
|
|
14
|
+
import debounce from 'lodash/debounce';
|
|
15
|
+
import throttle from 'lodash/throttle';
|
|
16
|
+
|
|
17
|
+
/**
|
|
18
|
+
* 发起网络请求, 主要面向场景: 提交单据、点击列表打开详情页前请求数据,自动捕获异常
|
|
19
|
+
* @param request 网络请求方法
|
|
20
|
+
* @param options 参数
|
|
21
|
+
* @returns
|
|
22
|
+
*/
|
|
23
|
+
export default function useRequest(request) {
|
|
24
|
+
var options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
|
|
25
|
+
var onSuccess = options.onSuccess,
|
|
26
|
+
onError = options.onError,
|
|
27
|
+
_options$throttleInte = options.throttleInterval,
|
|
28
|
+
throttleInterval = _options$throttleInte === void 0 ? hooksOpts.requestThrottleInterval : _options$throttleInte,
|
|
29
|
+
_options$debounceInte = options.debounceInterval,
|
|
30
|
+
debounceInterval = _options$debounceInte === void 0 ? 0 : _options$debounceInte,
|
|
31
|
+
loadingState = options.loadingState,
|
|
32
|
+
toast = options.toast,
|
|
33
|
+
onLoading = options.onLoading,
|
|
34
|
+
_options$manual = options.manual,
|
|
35
|
+
manual = _options$manual === void 0 ? true : _options$manual,
|
|
36
|
+
_options$defaultData = options.defaultData,
|
|
37
|
+
defaultData = _options$defaultData === void 0 ? null : _options$defaultData,
|
|
38
|
+
_options$refreshDeps = options.refreshDeps,
|
|
39
|
+
refreshDeps = _options$refreshDeps === void 0 ? [] : _options$refreshDeps;
|
|
40
|
+
var _useState = useState(false),
|
|
41
|
+
_useState2 = _slicedToArray(_useState, 2),
|
|
42
|
+
loading = _useState2[0],
|
|
43
|
+
setLoading = _useState2[1];
|
|
44
|
+
var _useState3 = useState(defaultData),
|
|
45
|
+
_useState4 = _slicedToArray(_useState3, 2),
|
|
46
|
+
data = _useState4[0],
|
|
47
|
+
setData = _useState4[1];
|
|
48
|
+
var mounted = useRef(false);
|
|
49
|
+
var toastRef = useRef(null);
|
|
50
|
+
var fetchId = useRef(0);
|
|
51
|
+
var start = useCallback(function () {
|
|
52
|
+
if (toast && !toastRef.current) {
|
|
53
|
+
toastRef.current = hooksOpts.showToast() || true;
|
|
54
|
+
}
|
|
55
|
+
if (loadingState) {
|
|
56
|
+
setLoading(true);
|
|
57
|
+
}
|
|
58
|
+
if (onLoading) {
|
|
59
|
+
onLoading(true);
|
|
60
|
+
}
|
|
61
|
+
}, []);
|
|
62
|
+
var end = useCallback(function () {
|
|
63
|
+
if (toastRef.current) {
|
|
64
|
+
hooksOpts.cancelToast(toastRef.current);
|
|
65
|
+
toastRef.current = null;
|
|
66
|
+
}
|
|
67
|
+
if (!mounted.current) {
|
|
68
|
+
return;
|
|
69
|
+
}
|
|
70
|
+
if (loadingState) {
|
|
71
|
+
setLoading(false);
|
|
72
|
+
}
|
|
73
|
+
if (onLoading) {
|
|
74
|
+
onLoading(false);
|
|
75
|
+
}
|
|
76
|
+
}, []);
|
|
77
|
+
var runRef = useLastestRef( /*#__PURE__*/_asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee() {
|
|
78
|
+
var id,
|
|
79
|
+
_res,
|
|
80
|
+
_args = arguments;
|
|
81
|
+
return _regeneratorRuntime().wrap(function _callee$(_context) {
|
|
82
|
+
while (1) switch (_context.prev = _context.next) {
|
|
83
|
+
case 0:
|
|
84
|
+
fetchId.current += 1;
|
|
85
|
+
start();
|
|
86
|
+
id = fetchId.current;
|
|
87
|
+
_context.prev = 3;
|
|
88
|
+
_context.next = 6;
|
|
89
|
+
return request.apply(void 0, _args);
|
|
90
|
+
case 6:
|
|
91
|
+
_res = _context.sent;
|
|
92
|
+
if (mounted.current && id === fetchId.current) {
|
|
93
|
+
end();
|
|
94
|
+
onSuccess === null || onSuccess === void 0 || onSuccess(_res);
|
|
95
|
+
if (!manual) {
|
|
96
|
+
setData(_res);
|
|
97
|
+
}
|
|
98
|
+
} else if (!mounted.current) {
|
|
99
|
+
end();
|
|
100
|
+
}
|
|
101
|
+
_context.next = 14;
|
|
102
|
+
break;
|
|
103
|
+
case 10:
|
|
104
|
+
_context.prev = 10;
|
|
105
|
+
_context.t0 = _context["catch"](3);
|
|
106
|
+
end();
|
|
107
|
+
if (onError) {
|
|
108
|
+
onError(_context.t0);
|
|
109
|
+
} else {
|
|
110
|
+
hooksOpts.showError(_context.t0);
|
|
111
|
+
}
|
|
112
|
+
case 14:
|
|
113
|
+
case "end":
|
|
114
|
+
return _context.stop();
|
|
115
|
+
}
|
|
116
|
+
}, _callee, null, [[3, 10]]);
|
|
117
|
+
})));
|
|
118
|
+
var formatedRun = useMemo(function () {
|
|
119
|
+
var cb = function cb() {
|
|
120
|
+
return runRef.current.apply(runRef, arguments);
|
|
121
|
+
};
|
|
122
|
+
if (debounceInterval) {
|
|
123
|
+
return debounce(cb, debounceInterval);
|
|
124
|
+
}
|
|
125
|
+
if (throttleInterval) {
|
|
126
|
+
return throttle(cb, throttleInterval, {
|
|
127
|
+
trailing: false
|
|
128
|
+
});
|
|
129
|
+
}
|
|
130
|
+
return cb;
|
|
131
|
+
}, []);
|
|
132
|
+
useEffect(function () {
|
|
133
|
+
if (!manual) {
|
|
134
|
+
runRef.current();
|
|
135
|
+
}
|
|
136
|
+
}, refreshDeps);
|
|
137
|
+
useEffect(function () {
|
|
138
|
+
mounted.current = true;
|
|
139
|
+
return function () {
|
|
140
|
+
mounted.current = false;
|
|
141
|
+
end();
|
|
142
|
+
};
|
|
143
|
+
}, []);
|
|
144
|
+
return {
|
|
145
|
+
/**
|
|
146
|
+
* 手动执行函数
|
|
147
|
+
*/
|
|
148
|
+
run: formatedRun,
|
|
149
|
+
/**
|
|
150
|
+
* loading状态
|
|
151
|
+
*/
|
|
152
|
+
loading: loading,
|
|
153
|
+
/**
|
|
154
|
+
* 仅在manual=true会返回,否则返回null
|
|
155
|
+
*/
|
|
156
|
+
data: data
|
|
157
|
+
};
|
|
158
|
+
}
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
function _typeof(o) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (o) { return typeof o; } : function (o) { return o && "function" == typeof Symbol && o.constructor === Symbol && o !== Symbol.prototype ? "symbol" : typeof o; }, _typeof(o); }
|
|
2
|
+
function ownKeys(e, r) { var t = Object.keys(e); if (Object.getOwnPropertySymbols) { var o = Object.getOwnPropertySymbols(e); r && (o = o.filter(function (r) { return Object.getOwnPropertyDescriptor(e, r).enumerable; })), t.push.apply(t, o); } return t; }
|
|
3
|
+
function _objectSpread(e) { for (var r = 1; r < arguments.length; r++) { var t = null != arguments[r] ? arguments[r] : {}; r % 2 ? ownKeys(Object(t), !0).forEach(function (r) { _defineProperty(e, r, t[r]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys(Object(t)).forEach(function (r) { Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r)); }); } return e; }
|
|
4
|
+
function _defineProperty(obj, key, value) { key = _toPropertyKey(key); if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
|
|
5
|
+
function _toPropertyKey(t) { var i = _toPrimitive(t, "string"); return "symbol" == _typeof(i) ? i : String(i); }
|
|
6
|
+
function _toPrimitive(t, r) { if ("object" != _typeof(t) || !t) return t; var e = t[Symbol.toPrimitive]; if (void 0 !== e) { var i = e.call(t, r || "default"); if ("object" != _typeof(i)) return i; throw new TypeError("@@toPrimitive must return a primitive value."); } return ("string" === r ? String : Number)(t); }
|
|
7
|
+
import { useRef, useCallback } from 'react';
|
|
8
|
+
import throttle from 'lodash/throttle';
|
|
9
|
+
export default function useThrottle(cb, duration, options) {
|
|
10
|
+
var ref = useRef(cb);
|
|
11
|
+
ref.current = cb;
|
|
12
|
+
return useCallback(throttle(function () {
|
|
13
|
+
return ref.current.apply(ref, arguments);
|
|
14
|
+
}, duration, _objectSpread({
|
|
15
|
+
trailing: false
|
|
16
|
+
}, options)), []);
|
|
17
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@lemon-fe/hooks",
|
|
3
|
-
"version": "0.1
|
|
3
|
+
"version": "1.1.0-alpha.1",
|
|
4
4
|
"description": "> TODO: description",
|
|
5
5
|
"author": "鲁盛杰 <lusj@cnlemon.net>",
|
|
6
6
|
"homepage": "",
|
|
@@ -9,9 +9,6 @@
|
|
|
9
9
|
"files": [
|
|
10
10
|
"es"
|
|
11
11
|
],
|
|
12
|
-
"scripts": {
|
|
13
|
-
"test": "echo \"Error: run tests from root\" && exit 1"
|
|
14
|
-
},
|
|
15
12
|
"publishConfig": {
|
|
16
13
|
"registry": "https://registry.npmjs.org"
|
|
17
14
|
},
|
|
@@ -24,7 +21,13 @@
|
|
|
24
21
|
},
|
|
25
22
|
"devDependencies": {
|
|
26
23
|
"@types/color-string": "^1.5.2",
|
|
24
|
+
"@types/lodash": "^4.14.186",
|
|
25
|
+
"antd": "^4.24.8",
|
|
27
26
|
"react": "^17.0.2"
|
|
28
27
|
},
|
|
29
|
-
"
|
|
30
|
-
|
|
28
|
+
"scripts": {
|
|
29
|
+
"test": "echo \"Error: run tests from root\" && exit 1",
|
|
30
|
+
"build": "father build",
|
|
31
|
+
"prepublish": "npm run build"
|
|
32
|
+
}
|
|
33
|
+
}
|
package/README.md
DELETED
package/es/usePreload/index.js
DELETED
|
@@ -1,79 +0,0 @@
|
|
|
1
|
-
function _typeof(obj) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (obj) { return typeof obj; } : function (obj) { return obj && "function" == typeof Symbol && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }, _typeof(obj); }
|
|
2
|
-
function _regeneratorRuntime() { "use strict"; /*! regenerator-runtime -- Copyright (c) 2014-present, Facebook, Inc. -- license (MIT): https://github.com/facebook/regenerator/blob/main/LICENSE */ _regeneratorRuntime = function _regeneratorRuntime() { return exports; }; var exports = {}, Op = Object.prototype, hasOwn = Op.hasOwnProperty, $Symbol = "function" == typeof Symbol ? Symbol : {}, iteratorSymbol = $Symbol.iterator || "@@iterator", asyncIteratorSymbol = $Symbol.asyncIterator || "@@asyncIterator", toStringTagSymbol = $Symbol.toStringTag || "@@toStringTag"; function define(obj, key, value) { return Object.defineProperty(obj, key, { value: value, enumerable: !0, configurable: !0, writable: !0 }), obj[key]; } try { define({}, ""); } catch (err) { define = function define(obj, key, value) { return obj[key] = value; }; } function wrap(innerFn, outerFn, self, tryLocsList) { var protoGenerator = outerFn && outerFn.prototype instanceof Generator ? outerFn : Generator, generator = Object.create(protoGenerator.prototype), context = new Context(tryLocsList || []); return generator._invoke = function (innerFn, self, context) { var state = "suspendedStart"; return function (method, arg) { if ("executing" === state) throw new Error("Generator is already running"); if ("completed" === state) { if ("throw" === method) throw arg; return doneResult(); } for (context.method = method, context.arg = arg;;) { var delegate = context.delegate; if (delegate) { var delegateResult = maybeInvokeDelegate(delegate, context); if (delegateResult) { if (delegateResult === ContinueSentinel) continue; return delegateResult; } } if ("next" === context.method) context.sent = context._sent = context.arg;else if ("throw" === context.method) { if ("suspendedStart" === state) throw state = "completed", context.arg; context.dispatchException(context.arg); } else "return" === context.method && context.abrupt("return", context.arg); state = "executing"; var record = tryCatch(innerFn, self, context); if ("normal" === record.type) { if (state = context.done ? "completed" : "suspendedYield", record.arg === ContinueSentinel) continue; return { value: record.arg, done: context.done }; } "throw" === record.type && (state = "completed", context.method = "throw", context.arg = record.arg); } }; }(innerFn, self, context), generator; } function tryCatch(fn, obj, arg) { try { return { type: "normal", arg: fn.call(obj, arg) }; } catch (err) { return { type: "throw", arg: err }; } } exports.wrap = wrap; var ContinueSentinel = {}; function Generator() {} function GeneratorFunction() {} function GeneratorFunctionPrototype() {} var IteratorPrototype = {}; define(IteratorPrototype, iteratorSymbol, function () { return this; }); var getProto = Object.getPrototypeOf, NativeIteratorPrototype = getProto && getProto(getProto(values([]))); NativeIteratorPrototype && NativeIteratorPrototype !== Op && hasOwn.call(NativeIteratorPrototype, iteratorSymbol) && (IteratorPrototype = NativeIteratorPrototype); var Gp = GeneratorFunctionPrototype.prototype = Generator.prototype = Object.create(IteratorPrototype); function defineIteratorMethods(prototype) { ["next", "throw", "return"].forEach(function (method) { define(prototype, method, function (arg) { return this._invoke(method, arg); }); }); } function AsyncIterator(generator, PromiseImpl) { function invoke(method, arg, resolve, reject) { var record = tryCatch(generator[method], generator, arg); if ("throw" !== record.type) { var result = record.arg, value = result.value; return value && "object" == _typeof(value) && hasOwn.call(value, "__await") ? PromiseImpl.resolve(value.__await).then(function (value) { invoke("next", value, resolve, reject); }, function (err) { invoke("throw", err, resolve, reject); }) : PromiseImpl.resolve(value).then(function (unwrapped) { result.value = unwrapped, resolve(result); }, function (error) { return invoke("throw", error, resolve, reject); }); } reject(record.arg); } var previousPromise; this._invoke = function (method, arg) { function callInvokeWithMethodAndArg() { return new PromiseImpl(function (resolve, reject) { invoke(method, arg, resolve, reject); }); } return previousPromise = previousPromise ? previousPromise.then(callInvokeWithMethodAndArg, callInvokeWithMethodAndArg) : callInvokeWithMethodAndArg(); }; } function maybeInvokeDelegate(delegate, context) { var method = delegate.iterator[context.method]; if (undefined === method) { if (context.delegate = null, "throw" === context.method) { if (delegate.iterator.return && (context.method = "return", context.arg = undefined, maybeInvokeDelegate(delegate, context), "throw" === context.method)) return ContinueSentinel; context.method = "throw", context.arg = new TypeError("The iterator does not provide a 'throw' method"); } return ContinueSentinel; } var record = tryCatch(method, delegate.iterator, context.arg); if ("throw" === record.type) return context.method = "throw", context.arg = record.arg, context.delegate = null, ContinueSentinel; var info = record.arg; return info ? info.done ? (context[delegate.resultName] = info.value, context.next = delegate.nextLoc, "return" !== context.method && (context.method = "next", context.arg = undefined), context.delegate = null, ContinueSentinel) : info : (context.method = "throw", context.arg = new TypeError("iterator result is not an object"), context.delegate = null, ContinueSentinel); } function pushTryEntry(locs) { var entry = { tryLoc: locs[0] }; 1 in locs && (entry.catchLoc = locs[1]), 2 in locs && (entry.finallyLoc = locs[2], entry.afterLoc = locs[3]), this.tryEntries.push(entry); } function resetTryEntry(entry) { var record = entry.completion || {}; record.type = "normal", delete record.arg, entry.completion = record; } function Context(tryLocsList) { this.tryEntries = [{ tryLoc: "root" }], tryLocsList.forEach(pushTryEntry, this), this.reset(!0); } function values(iterable) { if (iterable) { var iteratorMethod = iterable[iteratorSymbol]; if (iteratorMethod) return iteratorMethod.call(iterable); if ("function" == typeof iterable.next) return iterable; if (!isNaN(iterable.length)) { var i = -1, next = function next() { for (; ++i < iterable.length;) { if (hasOwn.call(iterable, i)) return next.value = iterable[i], next.done = !1, next; } return next.value = undefined, next.done = !0, next; }; return next.next = next; } } return { next: doneResult }; } function doneResult() { return { value: undefined, done: !0 }; } return GeneratorFunction.prototype = GeneratorFunctionPrototype, define(Gp, "constructor", GeneratorFunctionPrototype), define(GeneratorFunctionPrototype, "constructor", GeneratorFunction), GeneratorFunction.displayName = define(GeneratorFunctionPrototype, toStringTagSymbol, "GeneratorFunction"), exports.isGeneratorFunction = function (genFun) { var ctor = "function" == typeof genFun && genFun.constructor; return !!ctor && (ctor === GeneratorFunction || "GeneratorFunction" === (ctor.displayName || ctor.name)); }, exports.mark = function (genFun) { return Object.setPrototypeOf ? Object.setPrototypeOf(genFun, GeneratorFunctionPrototype) : (genFun.__proto__ = GeneratorFunctionPrototype, define(genFun, toStringTagSymbol, "GeneratorFunction")), genFun.prototype = Object.create(Gp), genFun; }, exports.awrap = function (arg) { return { __await: arg }; }, defineIteratorMethods(AsyncIterator.prototype), define(AsyncIterator.prototype, asyncIteratorSymbol, function () { return this; }), exports.AsyncIterator = AsyncIterator, exports.async = function (innerFn, outerFn, self, tryLocsList, PromiseImpl) { void 0 === PromiseImpl && (PromiseImpl = Promise); var iter = new AsyncIterator(wrap(innerFn, outerFn, self, tryLocsList), PromiseImpl); return exports.isGeneratorFunction(outerFn) ? iter : iter.next().then(function (result) { return result.done ? result.value : iter.next(); }); }, defineIteratorMethods(Gp), define(Gp, toStringTagSymbol, "Generator"), define(Gp, iteratorSymbol, function () { return this; }), define(Gp, "toString", function () { return "[object Generator]"; }), exports.keys = function (object) { var keys = []; for (var key in object) { keys.push(key); } return keys.reverse(), function next() { for (; keys.length;) { var key = keys.pop(); if (key in object) return next.value = key, next.done = !1, next; } return next.done = !0, next; }; }, exports.values = values, Context.prototype = { constructor: Context, reset: function reset(skipTempReset) { if (this.prev = 0, this.next = 0, this.sent = this._sent = undefined, this.done = !1, this.delegate = null, this.method = "next", this.arg = undefined, this.tryEntries.forEach(resetTryEntry), !skipTempReset) for (var name in this) { "t" === name.charAt(0) && hasOwn.call(this, name) && !isNaN(+name.slice(1)) && (this[name] = undefined); } }, stop: function stop() { this.done = !0; var rootRecord = this.tryEntries[0].completion; if ("throw" === rootRecord.type) throw rootRecord.arg; return this.rval; }, dispatchException: function dispatchException(exception) { if (this.done) throw exception; var context = this; function handle(loc, caught) { return record.type = "throw", record.arg = exception, context.next = loc, caught && (context.method = "next", context.arg = undefined), !!caught; } for (var i = this.tryEntries.length - 1; i >= 0; --i) { var entry = this.tryEntries[i], record = entry.completion; if ("root" === entry.tryLoc) return handle("end"); if (entry.tryLoc <= this.prev) { var hasCatch = hasOwn.call(entry, "catchLoc"), hasFinally = hasOwn.call(entry, "finallyLoc"); if (hasCatch && hasFinally) { if (this.prev < entry.catchLoc) return handle(entry.catchLoc, !0); if (this.prev < entry.finallyLoc) return handle(entry.finallyLoc); } else if (hasCatch) { if (this.prev < entry.catchLoc) return handle(entry.catchLoc, !0); } else { if (!hasFinally) throw new Error("try statement without catch or finally"); if (this.prev < entry.finallyLoc) return handle(entry.finallyLoc); } } } }, abrupt: function abrupt(type, arg) { for (var i = this.tryEntries.length - 1; i >= 0; --i) { var entry = this.tryEntries[i]; if (entry.tryLoc <= this.prev && hasOwn.call(entry, "finallyLoc") && this.prev < entry.finallyLoc) { var finallyEntry = entry; break; } } finallyEntry && ("break" === type || "continue" === type) && finallyEntry.tryLoc <= arg && arg <= finallyEntry.finallyLoc && (finallyEntry = null); var record = finallyEntry ? finallyEntry.completion : {}; return record.type = type, record.arg = arg, finallyEntry ? (this.method = "next", this.next = finallyEntry.finallyLoc, ContinueSentinel) : this.complete(record); }, complete: function complete(record, afterLoc) { if ("throw" === record.type) throw record.arg; return "break" === record.type || "continue" === record.type ? this.next = record.arg : "return" === record.type ? (this.rval = this.arg = record.arg, this.method = "return", this.next = "end") : "normal" === record.type && afterLoc && (this.next = afterLoc), ContinueSentinel; }, finish: function finish(finallyLoc) { for (var i = this.tryEntries.length - 1; i >= 0; --i) { var entry = this.tryEntries[i]; if (entry.finallyLoc === finallyLoc) return this.complete(entry.completion, entry.afterLoc), resetTryEntry(entry), ContinueSentinel; } }, catch: function _catch(tryLoc) { for (var i = this.tryEntries.length - 1; i >= 0; --i) { var entry = this.tryEntries[i]; if (entry.tryLoc === tryLoc) { var record = entry.completion; if ("throw" === record.type) { var thrown = record.arg; resetTryEntry(entry); } return thrown; } } throw new Error("illegal catch attempt"); }, delegateYield: function delegateYield(iterable, resultName, nextLoc) { return this.delegate = { iterator: values(iterable), resultName: resultName, nextLoc: nextLoc }, "next" === this.method && (this.arg = undefined), ContinueSentinel; } }, exports; }
|
|
3
|
-
function asyncGeneratorStep(gen, resolve, reject, _next, _throw, key, arg) { try { var info = gen[key](arg); var value = info.value; } catch (error) { reject(error); return; } if (info.done) { resolve(value); } else { Promise.resolve(value).then(_next, _throw); } }
|
|
4
|
-
function _asyncToGenerator(fn) { return function () { var self = this, args = arguments; return new Promise(function (resolve, reject) { var gen = fn.apply(self, args); function _next(value) { asyncGeneratorStep(gen, resolve, reject, _next, _throw, "next", value); } function _throw(err) { asyncGeneratorStep(gen, resolve, reject, _next, _throw, "throw", err); } _next(undefined); }); }; }
|
|
5
|
-
function _slicedToArray(arr, i) { return _arrayWithHoles(arr) || _iterableToArrayLimit(arr, i) || _unsupportedIterableToArray(arr, i) || _nonIterableRest(); }
|
|
6
|
-
function _nonIterableRest() { throw new TypeError("Invalid attempt to destructure non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); }
|
|
7
|
-
function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); }
|
|
8
|
-
function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) { arr2[i] = arr[i]; } return arr2; }
|
|
9
|
-
function _iterableToArrayLimit(arr, i) { var _i = arr == null ? null : typeof Symbol !== "undefined" && arr[Symbol.iterator] || arr["@@iterator"]; if (_i == null) return; var _arr = []; var _n = true; var _d = false; var _s, _e; try { for (_i = _i.call(arr); !(_n = (_s = _i.next()).done); _n = true) { _arr.push(_s.value); if (i && _arr.length === i) break; } } catch (err) { _d = true; _e = err; } finally { try { if (!_n && _i["return"] != null) _i["return"](); } finally { if (_d) throw _e; } } return _arr; }
|
|
10
|
-
function _arrayWithHoles(arr) { if (Array.isArray(arr)) return arr; }
|
|
11
|
-
import { useEffect, useState } from 'react';
|
|
12
|
-
import useRequest from '../useRequest';
|
|
13
|
-
import { hooksOpts } from '../config';
|
|
14
|
-
export default function usePreload(opts, deps) {
|
|
15
|
-
var _useState = useState(null),
|
|
16
|
-
_useState2 = _slicedToArray(_useState, 2),
|
|
17
|
-
data = _useState2[0],
|
|
18
|
-
setData = _useState2[1];
|
|
19
|
-
var _useState3 = useState(null),
|
|
20
|
-
_useState4 = _slicedToArray(_useState3, 2),
|
|
21
|
-
error = _useState4[0],
|
|
22
|
-
setError = _useState4[1];
|
|
23
|
-
var request;
|
|
24
|
-
var _onSuccess;
|
|
25
|
-
if (typeof opts === 'function') {
|
|
26
|
-
request = opts;
|
|
27
|
-
} else {
|
|
28
|
-
request = opts.request;
|
|
29
|
-
_onSuccess = opts.onSuccess;
|
|
30
|
-
}
|
|
31
|
-
var _useRequest = useRequest( /*#__PURE__*/_asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee() {
|
|
32
|
-
var res;
|
|
33
|
-
return _regeneratorRuntime().wrap(function _callee$(_context) {
|
|
34
|
-
while (1) {
|
|
35
|
-
switch (_context.prev = _context.next) {
|
|
36
|
-
case 0:
|
|
37
|
-
setData(null);
|
|
38
|
-
_context.next = 3;
|
|
39
|
-
return request();
|
|
40
|
-
case 3:
|
|
41
|
-
res = _context.sent;
|
|
42
|
-
return _context.abrupt("return", res);
|
|
43
|
-
case 5:
|
|
44
|
-
case "end":
|
|
45
|
-
return _context.stop();
|
|
46
|
-
}
|
|
47
|
-
}
|
|
48
|
-
}, _callee);
|
|
49
|
-
})), {
|
|
50
|
-
onSuccess: function onSuccess(res) {
|
|
51
|
-
if (_onSuccess) {
|
|
52
|
-
_onSuccess(res);
|
|
53
|
-
}
|
|
54
|
-
setData(res);
|
|
55
|
-
},
|
|
56
|
-
onError: function onError(err) {
|
|
57
|
-
setError(err);
|
|
58
|
-
}
|
|
59
|
-
}),
|
|
60
|
-
run = _useRequest.run;
|
|
61
|
-
useEffect(function () {
|
|
62
|
-
run();
|
|
63
|
-
}, deps ? deps : []);
|
|
64
|
-
var handleRefresh = function handleRefresh() {
|
|
65
|
-
setError(null);
|
|
66
|
-
run();
|
|
67
|
-
};
|
|
68
|
-
var mask = null;
|
|
69
|
-
if (error) {
|
|
70
|
-
mask = hooksOpts.errorResult(error, handleRefresh);
|
|
71
|
-
} else if (data === null) {
|
|
72
|
-
mask = hooksOpts.showLoading();
|
|
73
|
-
}
|
|
74
|
-
return {
|
|
75
|
-
refresh: handleRefresh,
|
|
76
|
-
data: data,
|
|
77
|
-
mask: mask
|
|
78
|
-
};
|
|
79
|
-
}
|
package/es/useRequest/index.js
DELETED
|
@@ -1,153 +0,0 @@
|
|
|
1
|
-
function _typeof(obj) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (obj) { return typeof obj; } : function (obj) { return obj && "function" == typeof Symbol && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }, _typeof(obj); }
|
|
2
|
-
function _regeneratorRuntime() { "use strict"; /*! regenerator-runtime -- Copyright (c) 2014-present, Facebook, Inc. -- license (MIT): https://github.com/facebook/regenerator/blob/main/LICENSE */ _regeneratorRuntime = function _regeneratorRuntime() { return exports; }; var exports = {}, Op = Object.prototype, hasOwn = Op.hasOwnProperty, $Symbol = "function" == typeof Symbol ? Symbol : {}, iteratorSymbol = $Symbol.iterator || "@@iterator", asyncIteratorSymbol = $Symbol.asyncIterator || "@@asyncIterator", toStringTagSymbol = $Symbol.toStringTag || "@@toStringTag"; function define(obj, key, value) { return Object.defineProperty(obj, key, { value: value, enumerable: !0, configurable: !0, writable: !0 }), obj[key]; } try { define({}, ""); } catch (err) { define = function define(obj, key, value) { return obj[key] = value; }; } function wrap(innerFn, outerFn, self, tryLocsList) { var protoGenerator = outerFn && outerFn.prototype instanceof Generator ? outerFn : Generator, generator = Object.create(protoGenerator.prototype), context = new Context(tryLocsList || []); return generator._invoke = function (innerFn, self, context) { var state = "suspendedStart"; return function (method, arg) { if ("executing" === state) throw new Error("Generator is already running"); if ("completed" === state) { if ("throw" === method) throw arg; return doneResult(); } for (context.method = method, context.arg = arg;;) { var delegate = context.delegate; if (delegate) { var delegateResult = maybeInvokeDelegate(delegate, context); if (delegateResult) { if (delegateResult === ContinueSentinel) continue; return delegateResult; } } if ("next" === context.method) context.sent = context._sent = context.arg;else if ("throw" === context.method) { if ("suspendedStart" === state) throw state = "completed", context.arg; context.dispatchException(context.arg); } else "return" === context.method && context.abrupt("return", context.arg); state = "executing"; var record = tryCatch(innerFn, self, context); if ("normal" === record.type) { if (state = context.done ? "completed" : "suspendedYield", record.arg === ContinueSentinel) continue; return { value: record.arg, done: context.done }; } "throw" === record.type && (state = "completed", context.method = "throw", context.arg = record.arg); } }; }(innerFn, self, context), generator; } function tryCatch(fn, obj, arg) { try { return { type: "normal", arg: fn.call(obj, arg) }; } catch (err) { return { type: "throw", arg: err }; } } exports.wrap = wrap; var ContinueSentinel = {}; function Generator() {} function GeneratorFunction() {} function GeneratorFunctionPrototype() {} var IteratorPrototype = {}; define(IteratorPrototype, iteratorSymbol, function () { return this; }); var getProto = Object.getPrototypeOf, NativeIteratorPrototype = getProto && getProto(getProto(values([]))); NativeIteratorPrototype && NativeIteratorPrototype !== Op && hasOwn.call(NativeIteratorPrototype, iteratorSymbol) && (IteratorPrototype = NativeIteratorPrototype); var Gp = GeneratorFunctionPrototype.prototype = Generator.prototype = Object.create(IteratorPrototype); function defineIteratorMethods(prototype) { ["next", "throw", "return"].forEach(function (method) { define(prototype, method, function (arg) { return this._invoke(method, arg); }); }); } function AsyncIterator(generator, PromiseImpl) { function invoke(method, arg, resolve, reject) { var record = tryCatch(generator[method], generator, arg); if ("throw" !== record.type) { var result = record.arg, value = result.value; return value && "object" == _typeof(value) && hasOwn.call(value, "__await") ? PromiseImpl.resolve(value.__await).then(function (value) { invoke("next", value, resolve, reject); }, function (err) { invoke("throw", err, resolve, reject); }) : PromiseImpl.resolve(value).then(function (unwrapped) { result.value = unwrapped, resolve(result); }, function (error) { return invoke("throw", error, resolve, reject); }); } reject(record.arg); } var previousPromise; this._invoke = function (method, arg) { function callInvokeWithMethodAndArg() { return new PromiseImpl(function (resolve, reject) { invoke(method, arg, resolve, reject); }); } return previousPromise = previousPromise ? previousPromise.then(callInvokeWithMethodAndArg, callInvokeWithMethodAndArg) : callInvokeWithMethodAndArg(); }; } function maybeInvokeDelegate(delegate, context) { var method = delegate.iterator[context.method]; if (undefined === method) { if (context.delegate = null, "throw" === context.method) { if (delegate.iterator.return && (context.method = "return", context.arg = undefined, maybeInvokeDelegate(delegate, context), "throw" === context.method)) return ContinueSentinel; context.method = "throw", context.arg = new TypeError("The iterator does not provide a 'throw' method"); } return ContinueSentinel; } var record = tryCatch(method, delegate.iterator, context.arg); if ("throw" === record.type) return context.method = "throw", context.arg = record.arg, context.delegate = null, ContinueSentinel; var info = record.arg; return info ? info.done ? (context[delegate.resultName] = info.value, context.next = delegate.nextLoc, "return" !== context.method && (context.method = "next", context.arg = undefined), context.delegate = null, ContinueSentinel) : info : (context.method = "throw", context.arg = new TypeError("iterator result is not an object"), context.delegate = null, ContinueSentinel); } function pushTryEntry(locs) { var entry = { tryLoc: locs[0] }; 1 in locs && (entry.catchLoc = locs[1]), 2 in locs && (entry.finallyLoc = locs[2], entry.afterLoc = locs[3]), this.tryEntries.push(entry); } function resetTryEntry(entry) { var record = entry.completion || {}; record.type = "normal", delete record.arg, entry.completion = record; } function Context(tryLocsList) { this.tryEntries = [{ tryLoc: "root" }], tryLocsList.forEach(pushTryEntry, this), this.reset(!0); } function values(iterable) { if (iterable) { var iteratorMethod = iterable[iteratorSymbol]; if (iteratorMethod) return iteratorMethod.call(iterable); if ("function" == typeof iterable.next) return iterable; if (!isNaN(iterable.length)) { var i = -1, next = function next() { for (; ++i < iterable.length;) { if (hasOwn.call(iterable, i)) return next.value = iterable[i], next.done = !1, next; } return next.value = undefined, next.done = !0, next; }; return next.next = next; } } return { next: doneResult }; } function doneResult() { return { value: undefined, done: !0 }; } return GeneratorFunction.prototype = GeneratorFunctionPrototype, define(Gp, "constructor", GeneratorFunctionPrototype), define(GeneratorFunctionPrototype, "constructor", GeneratorFunction), GeneratorFunction.displayName = define(GeneratorFunctionPrototype, toStringTagSymbol, "GeneratorFunction"), exports.isGeneratorFunction = function (genFun) { var ctor = "function" == typeof genFun && genFun.constructor; return !!ctor && (ctor === GeneratorFunction || "GeneratorFunction" === (ctor.displayName || ctor.name)); }, exports.mark = function (genFun) { return Object.setPrototypeOf ? Object.setPrototypeOf(genFun, GeneratorFunctionPrototype) : (genFun.__proto__ = GeneratorFunctionPrototype, define(genFun, toStringTagSymbol, "GeneratorFunction")), genFun.prototype = Object.create(Gp), genFun; }, exports.awrap = function (arg) { return { __await: arg }; }, defineIteratorMethods(AsyncIterator.prototype), define(AsyncIterator.prototype, asyncIteratorSymbol, function () { return this; }), exports.AsyncIterator = AsyncIterator, exports.async = function (innerFn, outerFn, self, tryLocsList, PromiseImpl) { void 0 === PromiseImpl && (PromiseImpl = Promise); var iter = new AsyncIterator(wrap(innerFn, outerFn, self, tryLocsList), PromiseImpl); return exports.isGeneratorFunction(outerFn) ? iter : iter.next().then(function (result) { return result.done ? result.value : iter.next(); }); }, defineIteratorMethods(Gp), define(Gp, toStringTagSymbol, "Generator"), define(Gp, iteratorSymbol, function () { return this; }), define(Gp, "toString", function () { return "[object Generator]"; }), exports.keys = function (object) { var keys = []; for (var key in object) { keys.push(key); } return keys.reverse(), function next() { for (; keys.length;) { var key = keys.pop(); if (key in object) return next.value = key, next.done = !1, next; } return next.done = !0, next; }; }, exports.values = values, Context.prototype = { constructor: Context, reset: function reset(skipTempReset) { if (this.prev = 0, this.next = 0, this.sent = this._sent = undefined, this.done = !1, this.delegate = null, this.method = "next", this.arg = undefined, this.tryEntries.forEach(resetTryEntry), !skipTempReset) for (var name in this) { "t" === name.charAt(0) && hasOwn.call(this, name) && !isNaN(+name.slice(1)) && (this[name] = undefined); } }, stop: function stop() { this.done = !0; var rootRecord = this.tryEntries[0].completion; if ("throw" === rootRecord.type) throw rootRecord.arg; return this.rval; }, dispatchException: function dispatchException(exception) { if (this.done) throw exception; var context = this; function handle(loc, caught) { return record.type = "throw", record.arg = exception, context.next = loc, caught && (context.method = "next", context.arg = undefined), !!caught; } for (var i = this.tryEntries.length - 1; i >= 0; --i) { var entry = this.tryEntries[i], record = entry.completion; if ("root" === entry.tryLoc) return handle("end"); if (entry.tryLoc <= this.prev) { var hasCatch = hasOwn.call(entry, "catchLoc"), hasFinally = hasOwn.call(entry, "finallyLoc"); if (hasCatch && hasFinally) { if (this.prev < entry.catchLoc) return handle(entry.catchLoc, !0); if (this.prev < entry.finallyLoc) return handle(entry.finallyLoc); } else if (hasCatch) { if (this.prev < entry.catchLoc) return handle(entry.catchLoc, !0); } else { if (!hasFinally) throw new Error("try statement without catch or finally"); if (this.prev < entry.finallyLoc) return handle(entry.finallyLoc); } } } }, abrupt: function abrupt(type, arg) { for (var i = this.tryEntries.length - 1; i >= 0; --i) { var entry = this.tryEntries[i]; if (entry.tryLoc <= this.prev && hasOwn.call(entry, "finallyLoc") && this.prev < entry.finallyLoc) { var finallyEntry = entry; break; } } finallyEntry && ("break" === type || "continue" === type) && finallyEntry.tryLoc <= arg && arg <= finallyEntry.finallyLoc && (finallyEntry = null); var record = finallyEntry ? finallyEntry.completion : {}; return record.type = type, record.arg = arg, finallyEntry ? (this.method = "next", this.next = finallyEntry.finallyLoc, ContinueSentinel) : this.complete(record); }, complete: function complete(record, afterLoc) { if ("throw" === record.type) throw record.arg; return "break" === record.type || "continue" === record.type ? this.next = record.arg : "return" === record.type ? (this.rval = this.arg = record.arg, this.method = "return", this.next = "end") : "normal" === record.type && afterLoc && (this.next = afterLoc), ContinueSentinel; }, finish: function finish(finallyLoc) { for (var i = this.tryEntries.length - 1; i >= 0; --i) { var entry = this.tryEntries[i]; if (entry.finallyLoc === finallyLoc) return this.complete(entry.completion, entry.afterLoc), resetTryEntry(entry), ContinueSentinel; } }, catch: function _catch(tryLoc) { for (var i = this.tryEntries.length - 1; i >= 0; --i) { var entry = this.tryEntries[i]; if (entry.tryLoc === tryLoc) { var record = entry.completion; if ("throw" === record.type) { var thrown = record.arg; resetTryEntry(entry); } return thrown; } } throw new Error("illegal catch attempt"); }, delegateYield: function delegateYield(iterable, resultName, nextLoc) { return this.delegate = { iterator: values(iterable), resultName: resultName, nextLoc: nextLoc }, "next" === this.method && (this.arg = undefined), ContinueSentinel; } }, exports; }
|
|
3
|
-
function asyncGeneratorStep(gen, resolve, reject, _next, _throw, key, arg) { try { var info = gen[key](arg); var value = info.value; } catch (error) { reject(error); return; } if (info.done) { resolve(value); } else { Promise.resolve(value).then(_next, _throw); } }
|
|
4
|
-
function _asyncToGenerator(fn) { return function () { var self = this, args = arguments; return new Promise(function (resolve, reject) { var gen = fn.apply(self, args); function _next(value) { asyncGeneratorStep(gen, resolve, reject, _next, _throw, "next", value); } function _throw(err) { asyncGeneratorStep(gen, resolve, reject, _next, _throw, "throw", err); } _next(undefined); }); }; }
|
|
5
|
-
function _slicedToArray(arr, i) { return _arrayWithHoles(arr) || _iterableToArrayLimit(arr, i) || _unsupportedIterableToArray(arr, i) || _nonIterableRest(); }
|
|
6
|
-
function _nonIterableRest() { throw new TypeError("Invalid attempt to destructure non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); }
|
|
7
|
-
function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); }
|
|
8
|
-
function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) { arr2[i] = arr[i]; } return arr2; }
|
|
9
|
-
function _iterableToArrayLimit(arr, i) { var _i = arr == null ? null : typeof Symbol !== "undefined" && arr[Symbol.iterator] || arr["@@iterator"]; if (_i == null) return; var _arr = []; var _n = true; var _d = false; var _s, _e; try { for (_i = _i.call(arr); !(_n = (_s = _i.next()).done); _n = true) { _arr.push(_s.value); if (i && _arr.length === i) break; } } catch (err) { _d = true; _e = err; } finally { try { if (!_n && _i["return"] != null) _i["return"](); } finally { if (_d) throw _e; } } return _arr; }
|
|
10
|
-
function _arrayWithHoles(arr) { if (Array.isArray(arr)) return arr; }
|
|
11
|
-
import { useRef, useEffect, useMemo, useState, useCallback } from 'react';
|
|
12
|
-
import debounce from 'lodash/debounce';
|
|
13
|
-
import throttle from 'lodash/throttle';
|
|
14
|
-
import { hooksOpts } from '../config';
|
|
15
|
-
import useLastestRef from '../useLatestRef';
|
|
16
|
-
/**
|
|
17
|
-
* 发起网络请求, 主要面向场景: 提交单据、点击列表打开详情页前请求数据,自动捕获异常
|
|
18
|
-
* @param request 网络请求方法
|
|
19
|
-
* @param options 参数
|
|
20
|
-
* @returns
|
|
21
|
-
*/
|
|
22
|
-
export default function useRequest(request) {
|
|
23
|
-
var options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
|
|
24
|
-
var onSuccess = options.onSuccess,
|
|
25
|
-
onError = options.onError,
|
|
26
|
-
_options$throttleInte = options.throttleInterval,
|
|
27
|
-
throttleInterval = _options$throttleInte === void 0 ? 0 : _options$throttleInte,
|
|
28
|
-
_options$debounceInte = options.debounceInterval,
|
|
29
|
-
debounceInterval = _options$debounceInte === void 0 ? 0 : _options$debounceInte,
|
|
30
|
-
loadingState = options.loadingState,
|
|
31
|
-
toast = options.toast,
|
|
32
|
-
onLoading = options.onLoading,
|
|
33
|
-
_options$manual = options.manual,
|
|
34
|
-
manual = _options$manual === void 0 ? true : _options$manual;
|
|
35
|
-
var _useState = useState(false),
|
|
36
|
-
_useState2 = _slicedToArray(_useState, 2),
|
|
37
|
-
loading = _useState2[0],
|
|
38
|
-
setLoading = _useState2[1];
|
|
39
|
-
var _useState3 = useState(null),
|
|
40
|
-
_useState4 = _slicedToArray(_useState3, 2),
|
|
41
|
-
data = _useState4[0],
|
|
42
|
-
setData = _useState4[1];
|
|
43
|
-
var mounted = useRef(false);
|
|
44
|
-
var toastRef = useRef(null);
|
|
45
|
-
var fetchId = useRef(0);
|
|
46
|
-
var start = useCallback(function () {
|
|
47
|
-
if (toast && !toastRef.current) {
|
|
48
|
-
toastRef.current = hooksOpts.showToast() || true;
|
|
49
|
-
}
|
|
50
|
-
if (loadingState) {
|
|
51
|
-
setLoading(true);
|
|
52
|
-
}
|
|
53
|
-
if (onLoading) {
|
|
54
|
-
onLoading(true);
|
|
55
|
-
}
|
|
56
|
-
}, []);
|
|
57
|
-
var end = useCallback(function () {
|
|
58
|
-
if (toastRef.current) {
|
|
59
|
-
hooksOpts.cancelToast(toastRef.current);
|
|
60
|
-
toastRef.current = null;
|
|
61
|
-
}
|
|
62
|
-
if (!mounted.current) {
|
|
63
|
-
return;
|
|
64
|
-
}
|
|
65
|
-
if (loadingState) {
|
|
66
|
-
setLoading(false);
|
|
67
|
-
}
|
|
68
|
-
if (onLoading) {
|
|
69
|
-
onLoading(false);
|
|
70
|
-
}
|
|
71
|
-
}, []);
|
|
72
|
-
var runRef = useLastestRef( /*#__PURE__*/_asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee() {
|
|
73
|
-
var id,
|
|
74
|
-
res,
|
|
75
|
-
_args = arguments;
|
|
76
|
-
return _regeneratorRuntime().wrap(function _callee$(_context) {
|
|
77
|
-
while (1) {
|
|
78
|
-
switch (_context.prev = _context.next) {
|
|
79
|
-
case 0:
|
|
80
|
-
fetchId.current += 1;
|
|
81
|
-
start();
|
|
82
|
-
id = fetchId.current;
|
|
83
|
-
_context.prev = 3;
|
|
84
|
-
_context.next = 6;
|
|
85
|
-
return request.apply(void 0, _args);
|
|
86
|
-
case 6:
|
|
87
|
-
res = _context.sent;
|
|
88
|
-
if (mounted.current && id === fetchId.current) {
|
|
89
|
-
end();
|
|
90
|
-
onSuccess === null || onSuccess === void 0 ? void 0 : onSuccess(res);
|
|
91
|
-
if (!manual) {
|
|
92
|
-
setData(res);
|
|
93
|
-
}
|
|
94
|
-
} else if (!mounted.current) {
|
|
95
|
-
end();
|
|
96
|
-
}
|
|
97
|
-
_context.next = 14;
|
|
98
|
-
break;
|
|
99
|
-
case 10:
|
|
100
|
-
_context.prev = 10;
|
|
101
|
-
_context.t0 = _context["catch"](3);
|
|
102
|
-
end();
|
|
103
|
-
if (onError) {
|
|
104
|
-
onError(_context.t0);
|
|
105
|
-
} else {
|
|
106
|
-
hooksOpts.showError(_context.t0);
|
|
107
|
-
}
|
|
108
|
-
case 14:
|
|
109
|
-
case "end":
|
|
110
|
-
return _context.stop();
|
|
111
|
-
}
|
|
112
|
-
}
|
|
113
|
-
}, _callee, null, [[3, 10]]);
|
|
114
|
-
})));
|
|
115
|
-
var formatedRun = useMemo(function () {
|
|
116
|
-
var cb = function cb() {
|
|
117
|
-
return runRef.current.apply(runRef, arguments);
|
|
118
|
-
};
|
|
119
|
-
if (debounceInterval) {
|
|
120
|
-
return debounce(cb, debounceInterval);
|
|
121
|
-
}
|
|
122
|
-
if (throttleInterval) {
|
|
123
|
-
return throttle(cb, throttleInterval, {
|
|
124
|
-
trailing: false
|
|
125
|
-
});
|
|
126
|
-
}
|
|
127
|
-
return cb;
|
|
128
|
-
}, []);
|
|
129
|
-
useEffect(function () {
|
|
130
|
-
mounted.current = true;
|
|
131
|
-
if (!manual) {
|
|
132
|
-
runRef.current();
|
|
133
|
-
}
|
|
134
|
-
return function () {
|
|
135
|
-
mounted.current = false;
|
|
136
|
-
end();
|
|
137
|
-
};
|
|
138
|
-
}, []);
|
|
139
|
-
return {
|
|
140
|
-
/**
|
|
141
|
-
* 手动执行函数
|
|
142
|
-
*/
|
|
143
|
-
run: formatedRun,
|
|
144
|
-
/**
|
|
145
|
-
* loading状态
|
|
146
|
-
*/
|
|
147
|
-
loading: loading,
|
|
148
|
-
/**
|
|
149
|
-
* 仅在manual=true会返回,否则返回null
|
|
150
|
-
*/
|
|
151
|
-
data: data
|
|
152
|
-
};
|
|
153
|
-
}
|
package/es/useThrottle/index.js
DELETED
|
@@ -1,14 +0,0 @@
|
|
|
1
|
-
function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); enumerableOnly && (symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; })), keys.push.apply(keys, symbols); } return keys; }
|
|
2
|
-
function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = null != arguments[i] ? arguments[i] : {}; i % 2 ? ownKeys(Object(source), !0).forEach(function (key) { _defineProperty(target, key, source[key]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)) : ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } return target; }
|
|
3
|
-
function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
|
|
4
|
-
import { useRef, useCallback } from 'react';
|
|
5
|
-
import throttle from 'lodash/throttle';
|
|
6
|
-
export default function useThrottle(cb, duration, options) {
|
|
7
|
-
var ref = useRef(cb);
|
|
8
|
-
ref.current = cb;
|
|
9
|
-
return useCallback(throttle(function () {
|
|
10
|
-
return ref.current.apply(ref, arguments);
|
|
11
|
-
}, duration, _objectSpread({
|
|
12
|
-
trailing: false
|
|
13
|
-
}, options)), []);
|
|
14
|
-
}
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|