react-terminal-viewer-cicd 2.0.4
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/README.md +181 -0
- package/dist/esm/Addon/EventEmiter.d.ts +18 -0
- package/dist/esm/Addon/EventEmiter.js +64 -0
- package/dist/esm/Addon/HighlightAddon.d.ts +26 -0
- package/dist/esm/Addon/HighlightAddon.js +188 -0
- package/dist/esm/Addon/SearchAddon.d.ts +140 -0
- package/dist/esm/Addon/SearchAddon.js +797 -0
- package/dist/esm/Addon/WorkerLog/GlobalContext.d.ts +10 -0
- package/dist/esm/Addon/WorkerLog/GlobalContext.js +37 -0
- package/dist/esm/Addon/WorkerLog/LimitMap.d.ts +14 -0
- package/dist/esm/Addon/WorkerLog/LimitMap.js +54 -0
- package/dist/esm/Addon/WorkerLog/LogWorker.d.ts +2 -0
- package/dist/esm/Addon/WorkerLog/LogWorker.js +13 -0
- package/dist/esm/Addon/WorkerLog/Logs.d.ts +21 -0
- package/dist/esm/Addon/WorkerLog/Logs.js +60 -0
- package/dist/esm/Addon/WorkerLog/Mark.d.ts +36 -0
- package/dist/esm/Addon/WorkerLog/Mark.js +155 -0
- package/dist/esm/Addon/WorkerLog/Searcher.d.ts +16 -0
- package/dist/esm/Addon/WorkerLog/Searcher.js +88 -0
- package/dist/esm/Addon/WorkerLog/WebWokerServer.d.ts +10 -0
- package/dist/esm/Addon/WorkerLog/WebWokerServer.js +57 -0
- package/dist/esm/Addon/WorkerLog/WebWorkerClient.d.ts +13 -0
- package/dist/esm/Addon/WorkerLog/WebWorkerClient.js +83 -0
- package/dist/esm/Addon/WorkerLog/log.worker.d.ts +1 -0
- package/dist/esm/Addon/WorkerLog/log.worker.js +63 -0
- package/dist/esm/Addon/WorkerLog/types.d.ts +18 -0
- package/dist/esm/Addon/WorkerLog/types.js +1 -0
- package/dist/esm/Addon/useAddon.d.ts +9 -0
- package/dist/esm/Addon/useAddon.js +33 -0
- package/dist/esm/Hooks/useBatchProcess.d.ts +2 -0
- package/dist/esm/Hooks/useBatchProcess.js +47 -0
- package/dist/esm/Hooks/useCache.d.ts +31 -0
- package/dist/esm/Hooks/useCache.js +206 -0
- package/dist/esm/Hooks/useDebounceInput.d.ts +3 -0
- package/dist/esm/Hooks/useDebounceInput.js +26 -0
- package/dist/esm/Hooks/useLocalStore.d.ts +2 -0
- package/dist/esm/Hooks/useLocalStore.js +9 -0
- package/dist/esm/Hooks/useRemote.d.ts +30 -0
- package/dist/esm/Hooks/useRemote.js +398 -0
- package/dist/esm/Hooks/useThrottle.d.ts +2 -0
- package/dist/esm/Hooks/useThrottle.js +13 -0
- package/dist/esm/Hooks/useWorkerLogs.d.ts +9 -0
- package/dist/esm/Hooks/useWorkerLogs.js +162 -0
- package/dist/esm/TerminalViewer/index.d.ts +185 -0
- package/dist/esm/TerminalViewer/index.js +292 -0
- package/dist/esm/TerminalViewer/index.less +17 -0
- package/dist/esm/TerminalViewerBody/index.d.ts +87 -0
- package/dist/esm/TerminalViewerBody/index.js +248 -0
- package/dist/esm/TerminalViewerBody/index.less +51 -0
- package/dist/esm/TerminalViewerHeader/index.d.ts +88 -0
- package/dist/esm/TerminalViewerHeader/index.js +129 -0
- package/dist/esm/TerminalViewerHeader/index.less +143 -0
- package/dist/esm/TerminalViewerToolBar/index.d.ts +46 -0
- package/dist/esm/TerminalViewerToolBar/index.js +28 -0
- package/dist/esm/TerminalViewerToolBar/index.less +32 -0
- package/dist/esm/TerminalViewerVirtualDom/index.d.ts +71 -0
- package/dist/esm/TerminalViewerVirtualDom/index.js +309 -0
- package/dist/esm/TerminalViewerVirtualDom/index.less +68 -0
- package/dist/esm/index.d.ts +9 -0
- package/dist/esm/index.js +9 -0
- package/dist/esm/mock/index.d.ts +12 -0
- package/dist/esm/mock/index.js +98 -0
- package/dist/esm/types.d.ts +30 -0
- package/dist/esm/types.js +1 -0
- package/dist/worker/Addon/EventEmiter.d.ts +18 -0
- package/dist/worker/Addon/HighlightAddon.d.ts +26 -0
- package/dist/worker/Addon/SearchAddon.d.ts +140 -0
- package/dist/worker/Addon/WorkerLog/GlobalContext.d.ts +10 -0
- package/dist/worker/Addon/WorkerLog/LimitMap.d.ts +14 -0
- package/dist/worker/Addon/WorkerLog/LogWorker.d.ts +2 -0
- package/dist/worker/Addon/WorkerLog/Logs.d.ts +21 -0
- package/dist/worker/Addon/WorkerLog/Mark.d.ts +36 -0
- package/dist/worker/Addon/WorkerLog/Searcher.d.ts +16 -0
- package/dist/worker/Addon/WorkerLog/WebWokerServer.d.ts +10 -0
- package/dist/worker/Addon/WorkerLog/WebWorkerClient.d.ts +13 -0
- package/dist/worker/Addon/WorkerLog/log.worker.d.ts +1 -0
- package/dist/worker/Addon/WorkerLog/types.d.ts +18 -0
- package/dist/worker/Addon/useAddon.d.ts +9 -0
- package/dist/worker/Hooks/useBatchProcess.d.ts +2 -0
- package/dist/worker/Hooks/useCache.d.ts +31 -0
- package/dist/worker/Hooks/useDebounceInput.d.ts +3 -0
- package/dist/worker/Hooks/useLocalStore.d.ts +2 -0
- package/dist/worker/Hooks/useRemote.d.ts +30 -0
- package/dist/worker/Hooks/useRemote.test.d.ts +1 -0
- package/dist/worker/Hooks/useThrottle.d.ts +2 -0
- package/dist/worker/Hooks/useWorkerLogs.d.ts +9 -0
- package/dist/worker/TerminalViewer/index.d.ts +185 -0
- package/dist/worker/TerminalViewer/index.test.d.ts +1 -0
- package/dist/worker/TerminalViewerBody/index.d.ts +87 -0
- package/dist/worker/TerminalViewerBody/index.test.d.ts +1 -0
- package/dist/worker/TerminalViewerHeader/index.d.ts +88 -0
- package/dist/worker/TerminalViewerToolBar/index.d.ts +46 -0
- package/dist/worker/TerminalViewerVirtualDom/index.d.ts +71 -0
- package/dist/worker/index.d.ts +9 -0
- package/dist/worker/log.worker.js +2 -0
- package/dist/worker/log.worker.js.map +1 -0
- package/dist/worker/mock/index.d.ts +12 -0
- package/dist/worker/types.d.ts +30 -0
- package/package.json +97 -0
|
@@ -0,0 +1,83 @@
|
|
|
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 _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
|
|
5
|
+
function _defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, _toPropertyKey(descriptor.key), descriptor); } }
|
|
6
|
+
function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); Object.defineProperty(Constructor, "prototype", { writable: false }); return Constructor; }
|
|
7
|
+
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; }
|
|
8
|
+
function _toPropertyKey(t) { var i = _toPrimitive(t, "string"); return "symbol" == _typeof(i) ? i : String(i); }
|
|
9
|
+
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); }
|
|
10
|
+
/* eslint-disable @typescript-eslint/no-explicit-any */
|
|
11
|
+
var WebWorkerClient = /*#__PURE__*/function () {
|
|
12
|
+
function WebWorkerClient(worker) {
|
|
13
|
+
var _this = this;
|
|
14
|
+
_classCallCheck(this, WebWorkerClient);
|
|
15
|
+
_defineProperty(this, "events", void 0);
|
|
16
|
+
_defineProperty(this, "worker", void 0);
|
|
17
|
+
this.events = {};
|
|
18
|
+
this.worker = worker;
|
|
19
|
+
this.worker.onmessage = function (e) {
|
|
20
|
+
var topic = e.data.topic;
|
|
21
|
+
_this.exec(topic, e.data);
|
|
22
|
+
};
|
|
23
|
+
}
|
|
24
|
+
_createClass(WebWorkerClient, [{
|
|
25
|
+
key: "once",
|
|
26
|
+
value: function once(topic, callback) {
|
|
27
|
+
var _this2 = this;
|
|
28
|
+
var on = function on(args) {
|
|
29
|
+
_this2.off(topic, on);
|
|
30
|
+
callback(args);
|
|
31
|
+
};
|
|
32
|
+
return this.on(topic, on);
|
|
33
|
+
}
|
|
34
|
+
}, {
|
|
35
|
+
key: "on",
|
|
36
|
+
value: function on(topic, callback) {
|
|
37
|
+
if (!this.events[topic]) {
|
|
38
|
+
this.events[topic] = [];
|
|
39
|
+
}
|
|
40
|
+
this.events[topic].push(callback);
|
|
41
|
+
return this;
|
|
42
|
+
}
|
|
43
|
+
}, {
|
|
44
|
+
key: "off",
|
|
45
|
+
value: function off(topic, callback) {
|
|
46
|
+
if (!Array.isArray(this.events[topic])) {
|
|
47
|
+
return this;
|
|
48
|
+
}
|
|
49
|
+
this.events[topic] = this.events[topic].filter(function (f) {
|
|
50
|
+
return f !== callback;
|
|
51
|
+
});
|
|
52
|
+
return this;
|
|
53
|
+
}
|
|
54
|
+
}, {
|
|
55
|
+
key: "exec",
|
|
56
|
+
value: function exec(topic, data) {
|
|
57
|
+
if (!Array.isArray(this.events[topic])) {
|
|
58
|
+
return this;
|
|
59
|
+
}
|
|
60
|
+
this.events[topic].forEach(function (callback) {
|
|
61
|
+
callback(data);
|
|
62
|
+
});
|
|
63
|
+
return this;
|
|
64
|
+
}
|
|
65
|
+
}, {
|
|
66
|
+
key: "send",
|
|
67
|
+
value: function send(topic) {
|
|
68
|
+
var payload = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
|
|
69
|
+
this.worker.postMessage(_objectSpread({
|
|
70
|
+
topic: topic
|
|
71
|
+
}, payload));
|
|
72
|
+
return this;
|
|
73
|
+
}
|
|
74
|
+
}, {
|
|
75
|
+
key: "terminate",
|
|
76
|
+
value: function terminate() {
|
|
77
|
+
this.worker.terminate();
|
|
78
|
+
return this;
|
|
79
|
+
}
|
|
80
|
+
}]);
|
|
81
|
+
return WebWorkerClient;
|
|
82
|
+
}();
|
|
83
|
+
export default WebWorkerClient;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
/* eslint-disable no-restricted-globals */
|
|
2
|
+
import WebWorkerServer from "./WebWokerServer";
|
|
3
|
+
import LimitMap from "./LimitMap";
|
|
4
|
+
import Logs from "./Logs";
|
|
5
|
+
var server = new WebWorkerServer(self);
|
|
6
|
+
var logsInstanceMap = new LimitMap({
|
|
7
|
+
limit: 8
|
|
8
|
+
});
|
|
9
|
+
server.on('highlight-log', function (ctx, _ref) {
|
|
10
|
+
var id = _ref.id,
|
|
11
|
+
string = _ref.string;
|
|
12
|
+
if (!logsInstanceMap.has(id)) {
|
|
13
|
+
logsInstanceMap.set(id, new Logs({
|
|
14
|
+
id: id,
|
|
15
|
+
string: string
|
|
16
|
+
}));
|
|
17
|
+
}
|
|
18
|
+
var instance = logsInstanceMap.get(id);
|
|
19
|
+
ctx.send('highlight-log-success', {
|
|
20
|
+
id: id,
|
|
21
|
+
logs: instance === null || instance === void 0 ? void 0 : instance.getArrayData()
|
|
22
|
+
});
|
|
23
|
+
});
|
|
24
|
+
server.on('search-log', function (ctx, _ref2) {
|
|
25
|
+
var id = _ref2.id,
|
|
26
|
+
searchId = _ref2.searchId,
|
|
27
|
+
_ref2$keyword = _ref2.keyword,
|
|
28
|
+
keyword = _ref2$keyword === void 0 ? '' : _ref2$keyword,
|
|
29
|
+
hlIndex = _ref2.hlIndex,
|
|
30
|
+
_ref2$string = _ref2.string,
|
|
31
|
+
string = _ref2$string === void 0 ? '' : _ref2$string,
|
|
32
|
+
options = _ref2.options;
|
|
33
|
+
if (!keyword) {
|
|
34
|
+
ctx.send('search-log-success', {
|
|
35
|
+
id: id,
|
|
36
|
+
searchId: searchId,
|
|
37
|
+
searcherResult: {
|
|
38
|
+
map: {},
|
|
39
|
+
count: 0,
|
|
40
|
+
markedKey: 0
|
|
41
|
+
}
|
|
42
|
+
});
|
|
43
|
+
return;
|
|
44
|
+
}
|
|
45
|
+
if (!logsInstanceMap.has(id)) {
|
|
46
|
+
logsInstanceMap.set(id, new Logs({
|
|
47
|
+
id: id,
|
|
48
|
+
string: string
|
|
49
|
+
}));
|
|
50
|
+
}
|
|
51
|
+
var instance = logsInstanceMap.get(id);
|
|
52
|
+
if (instance) {
|
|
53
|
+
ctx.send('search-log-success', {
|
|
54
|
+
id: id,
|
|
55
|
+
searchId: searchId,
|
|
56
|
+
searcherResult: instance.mark(keyword, hlIndex, options) || {
|
|
57
|
+
map: {},
|
|
58
|
+
count: 0,
|
|
59
|
+
markedKey: 0
|
|
60
|
+
}
|
|
61
|
+
});
|
|
62
|
+
}
|
|
63
|
+
});
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import type { ISearchOptions } from '../SearchAddon';
|
|
2
|
+
export type SearchResultCallback = (result: SearcherResult) => void;
|
|
3
|
+
export type DidChangeResultsCallback = (e?: {
|
|
4
|
+
resultIndex: number;
|
|
5
|
+
resultCount: number;
|
|
6
|
+
}) => void;
|
|
7
|
+
export interface SearcherRef {
|
|
8
|
+
findPrevious: (keyword: string, options: ISearchOptions) => void;
|
|
9
|
+
findNext: (keyword: string, options: ISearchOptions) => void;
|
|
10
|
+
onDidChangeResults: (callback: DidChangeResultsCallback) => void;
|
|
11
|
+
}
|
|
12
|
+
export interface SearcherResult {
|
|
13
|
+
count: number;
|
|
14
|
+
map: {
|
|
15
|
+
[key: string | number]: string;
|
|
16
|
+
};
|
|
17
|
+
markedKey: number | string;
|
|
18
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import { Terminal } from 'xterm';
|
|
2
|
+
import { FitAddon } from 'xterm-addon-fit';
|
|
3
|
+
import { CanvasAddon } from 'xterm-addon-canvas';
|
|
4
|
+
import { WebLinksAddon } from 'xterm-addon-web-links';
|
|
5
|
+
import { SearchAddon } from './SearchAddon';
|
|
6
|
+
import { HighlightAddon } from './HighlightAddon';
|
|
7
|
+
export type AddonType = FitAddon | WebLinksAddon | SearchAddon | HighlightAddon | CanvasAddon;
|
|
8
|
+
declare const useAddon: (terminal: Terminal | undefined, Addon: new () => AddonType, loadable: boolean, callback?: ((term: Terminal, instance: AddonType) => void) | undefined) => AddonType | undefined;
|
|
9
|
+
export default useAddon;
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
function _slicedToArray(arr, i) { return _arrayWithHoles(arr) || _iterableToArrayLimit(arr, i) || _unsupportedIterableToArray(arr, i) || _nonIterableRest(); }
|
|
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
|
+
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++) 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
|
+
function _arrayWithHoles(arr) { if (Array.isArray(arr)) return arr; }
|
|
7
|
+
import { useEffect, useState, useRef } from 'react';
|
|
8
|
+
var useAddon = function useAddon(terminal, Addon, loadable, callback) {
|
|
9
|
+
var _useState = useState(),
|
|
10
|
+
_useState2 = _slicedToArray(_useState, 2),
|
|
11
|
+
addonInstance = _useState2[0],
|
|
12
|
+
setAddonInstance = _useState2[1];
|
|
13
|
+
var ref = useRef(callback);
|
|
14
|
+
useEffect(function () {
|
|
15
|
+
ref.current = callback;
|
|
16
|
+
});
|
|
17
|
+
useEffect(function () {
|
|
18
|
+
var addon = new Addon();
|
|
19
|
+
if (loadable && terminal) {
|
|
20
|
+
terminal.loadAddon(addon);
|
|
21
|
+
setAddonInstance(addon);
|
|
22
|
+
if (ref.current instanceof Function) {
|
|
23
|
+
ref.current(terminal, addon);
|
|
24
|
+
}
|
|
25
|
+
}
|
|
26
|
+
return function () {
|
|
27
|
+
setAddonInstance(undefined);
|
|
28
|
+
addon.dispose();
|
|
29
|
+
};
|
|
30
|
+
}, [terminal, Addon, loadable]);
|
|
31
|
+
return addonInstance;
|
|
32
|
+
};
|
|
33
|
+
export default useAddon;
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
import { useEffect, useRef } from 'react';
|
|
2
|
+
var splitContent = function splitContent(content) {
|
|
3
|
+
var result = [];
|
|
4
|
+
var start = 0;
|
|
5
|
+
var len = content.length;
|
|
6
|
+
var offset = 100000;
|
|
7
|
+
while (start < len) {
|
|
8
|
+
result.push(content.substr(start, offset));
|
|
9
|
+
start += offset;
|
|
10
|
+
}
|
|
11
|
+
return result;
|
|
12
|
+
};
|
|
13
|
+
var batchProcess = function batchProcess(array, callback) {
|
|
14
|
+
var timer = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : -1;
|
|
15
|
+
var index = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : 0;
|
|
16
|
+
var timerId = timer;
|
|
17
|
+
if (index > array.length) {
|
|
18
|
+
return timerId;
|
|
19
|
+
}
|
|
20
|
+
var string = array[index];
|
|
21
|
+
timerId = setTimeout(function () {
|
|
22
|
+
callback(string);
|
|
23
|
+
timerId = batchProcess(array, callback, timer, index + 1);
|
|
24
|
+
}, array.length >= 2 ? 300 : 0);
|
|
25
|
+
return timerId;
|
|
26
|
+
};
|
|
27
|
+
var useBatchProcess = function useBatchProcess(callback, incrementalString) {
|
|
28
|
+
var callbackRef = useRef(callback);
|
|
29
|
+
var timerRef = useRef(-1);
|
|
30
|
+
useEffect(function () {
|
|
31
|
+
callbackRef.current = callback;
|
|
32
|
+
});
|
|
33
|
+
useEffect(function () {
|
|
34
|
+
clearTimeout(timerRef.current);
|
|
35
|
+
var array = splitContent(incrementalString);
|
|
36
|
+
timerRef.current = batchProcess(array, callbackRef.current);
|
|
37
|
+
}, [incrementalString]);
|
|
38
|
+
useEffect(function () {
|
|
39
|
+
return function () {
|
|
40
|
+
clearTimeout(timerRef.current);
|
|
41
|
+
};
|
|
42
|
+
}, []);
|
|
43
|
+
return function () {
|
|
44
|
+
return clearTimeout(timerRef.current);
|
|
45
|
+
};
|
|
46
|
+
};
|
|
47
|
+
export default useBatchProcess;
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
export interface ICacheOptions {
|
|
2
|
+
expires?: number;
|
|
3
|
+
}
|
|
4
|
+
export interface CacheItemValue {
|
|
5
|
+
nextKey?: string | number;
|
|
6
|
+
result: string;
|
|
7
|
+
complete: boolean;
|
|
8
|
+
}
|
|
9
|
+
export type CacheItem = {
|
|
10
|
+
[key: string | number]: CacheItemValue | number | undefined;
|
|
11
|
+
__updateTime__?: number;
|
|
12
|
+
__expiresTime__?: number;
|
|
13
|
+
};
|
|
14
|
+
declare const useCache: (itemKey: number | string | undefined, valueKey: number | string | undefined, cacheOptions?: ICacheOptions) => {
|
|
15
|
+
store: LocalForage;
|
|
16
|
+
setCache: (key: string | number, value: CacheItemValue) => Promise<CacheItem | null>;
|
|
17
|
+
getCache: () => Promise<{
|
|
18
|
+
key: undefined;
|
|
19
|
+
complete: boolean;
|
|
20
|
+
next: undefined;
|
|
21
|
+
result: string;
|
|
22
|
+
} | {
|
|
23
|
+
key: string | number;
|
|
24
|
+
complete: boolean;
|
|
25
|
+
result: string;
|
|
26
|
+
next: undefined;
|
|
27
|
+
}>;
|
|
28
|
+
clearCache: () => Promise<void | null>;
|
|
29
|
+
destroyCache: () => Promise<void>;
|
|
30
|
+
};
|
|
31
|
+
export default useCache;
|
|
@@ -0,0 +1,206 @@
|
|
|
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
|
+
/* eslint-disable no-underscore-dangle */
|
|
6
|
+
import { useCallback, useEffect, useRef } from 'react';
|
|
7
|
+
import useLocalStore from "./useLocalStore";
|
|
8
|
+
var useCache = function useCache(itemKey, valueKey, cacheOptions) {
|
|
9
|
+
var optionsRef = useRef(cacheOptions);
|
|
10
|
+
var store = useLocalStore();
|
|
11
|
+
var getCache = useCallback( /*#__PURE__*/_asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee() {
|
|
12
|
+
var _itemValue;
|
|
13
|
+
var result, currentKey, item, itemValue;
|
|
14
|
+
return _regeneratorRuntime().wrap(function _callee$(_context) {
|
|
15
|
+
while (1) switch (_context.prev = _context.next) {
|
|
16
|
+
case 0:
|
|
17
|
+
result = '';
|
|
18
|
+
currentKey = valueKey;
|
|
19
|
+
if (!(!itemKey || !currentKey)) {
|
|
20
|
+
_context.next = 4;
|
|
21
|
+
break;
|
|
22
|
+
}
|
|
23
|
+
return _context.abrupt("return", {
|
|
24
|
+
key: undefined,
|
|
25
|
+
complete: false,
|
|
26
|
+
next: undefined,
|
|
27
|
+
result: ''
|
|
28
|
+
});
|
|
29
|
+
case 4:
|
|
30
|
+
_context.next = 6;
|
|
31
|
+
return store.getItem(String(itemKey));
|
|
32
|
+
case 6:
|
|
33
|
+
item = _context.sent;
|
|
34
|
+
itemValue = item === null || item === void 0 ? void 0 : item[currentKey];
|
|
35
|
+
case 8:
|
|
36
|
+
if (!(item && itemValue)) {
|
|
37
|
+
_context.next = 22;
|
|
38
|
+
break;
|
|
39
|
+
}
|
|
40
|
+
if (itemValue.complete) {
|
|
41
|
+
result += itemValue.result;
|
|
42
|
+
}
|
|
43
|
+
if (!itemValue.nextKey) {
|
|
44
|
+
_context.next = 19;
|
|
45
|
+
break;
|
|
46
|
+
}
|
|
47
|
+
currentKey = String(itemValue.nextKey);
|
|
48
|
+
if (!item[currentKey]) {
|
|
49
|
+
_context.next = 16;
|
|
50
|
+
break;
|
|
51
|
+
}
|
|
52
|
+
itemValue = item[currentKey];
|
|
53
|
+
_context.next = 17;
|
|
54
|
+
break;
|
|
55
|
+
case 16:
|
|
56
|
+
return _context.abrupt("break", 22);
|
|
57
|
+
case 17:
|
|
58
|
+
_context.next = 20;
|
|
59
|
+
break;
|
|
60
|
+
case 19:
|
|
61
|
+
return _context.abrupt("break", 22);
|
|
62
|
+
case 20:
|
|
63
|
+
_context.next = 8;
|
|
64
|
+
break;
|
|
65
|
+
case 22:
|
|
66
|
+
return _context.abrupt("return", {
|
|
67
|
+
key: currentKey,
|
|
68
|
+
complete: ((_itemValue = itemValue) === null || _itemValue === void 0 ? void 0 : _itemValue.complete) || false,
|
|
69
|
+
result: result,
|
|
70
|
+
next: undefined
|
|
71
|
+
});
|
|
72
|
+
case 23:
|
|
73
|
+
case "end":
|
|
74
|
+
return _context.stop();
|
|
75
|
+
}
|
|
76
|
+
}, _callee);
|
|
77
|
+
})), [itemKey, valueKey, store]);
|
|
78
|
+
var setCache = useCallback( /*#__PURE__*/function () {
|
|
79
|
+
var _ref2 = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee2(key, value) {
|
|
80
|
+
var _optionsRef$current2;
|
|
81
|
+
var item, currentTime, _optionsRef$current, result;
|
|
82
|
+
return _regeneratorRuntime().wrap(function _callee2$(_context2) {
|
|
83
|
+
while (1) switch (_context2.prev = _context2.next) {
|
|
84
|
+
case 0:
|
|
85
|
+
if (itemKey) {
|
|
86
|
+
_context2.next = 2;
|
|
87
|
+
break;
|
|
88
|
+
}
|
|
89
|
+
return _context2.abrupt("return", null);
|
|
90
|
+
case 2:
|
|
91
|
+
_context2.next = 4;
|
|
92
|
+
return store.getItem(String(itemKey));
|
|
93
|
+
case 4:
|
|
94
|
+
item = _context2.sent;
|
|
95
|
+
currentTime = new Date().getTime();
|
|
96
|
+
if (!item || _typeof(item) !== 'object') {
|
|
97
|
+
item = {
|
|
98
|
+
__updateTime__: currentTime,
|
|
99
|
+
__expiresTime__: (_optionsRef$current = optionsRef.current) !== null && _optionsRef$current !== void 0 && _optionsRef$current.expires ? optionsRef.current.expires + currentTime : Number.MAX_SAFE_INTEGER
|
|
100
|
+
};
|
|
101
|
+
}
|
|
102
|
+
item[key] = value;
|
|
103
|
+
item.__updateTime__ = currentTime;
|
|
104
|
+
item.__expiresTime__ = (_optionsRef$current2 = optionsRef.current) !== null && _optionsRef$current2 !== void 0 && _optionsRef$current2.expires ? optionsRef.current.expires + currentTime : Number.MAX_SAFE_INTEGER;
|
|
105
|
+
_context2.next = 12;
|
|
106
|
+
return store.setItem(String(itemKey), item);
|
|
107
|
+
case 12:
|
|
108
|
+
result = _context2.sent;
|
|
109
|
+
return _context2.abrupt("return", result);
|
|
110
|
+
case 14:
|
|
111
|
+
case "end":
|
|
112
|
+
return _context2.stop();
|
|
113
|
+
}
|
|
114
|
+
}, _callee2);
|
|
115
|
+
}));
|
|
116
|
+
return function (_x, _x2) {
|
|
117
|
+
return _ref2.apply(this, arguments);
|
|
118
|
+
};
|
|
119
|
+
}(), [itemKey, store]);
|
|
120
|
+
var clearCache = useCallback( /*#__PURE__*/_asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee3() {
|
|
121
|
+
return _regeneratorRuntime().wrap(function _callee3$(_context3) {
|
|
122
|
+
while (1) switch (_context3.prev = _context3.next) {
|
|
123
|
+
case 0:
|
|
124
|
+
if (itemKey) {
|
|
125
|
+
_context3.next = 2;
|
|
126
|
+
break;
|
|
127
|
+
}
|
|
128
|
+
return _context3.abrupt("return", null);
|
|
129
|
+
case 2:
|
|
130
|
+
return _context3.abrupt("return", store.removeItem(String(itemKey)));
|
|
131
|
+
case 3:
|
|
132
|
+
case "end":
|
|
133
|
+
return _context3.stop();
|
|
134
|
+
}
|
|
135
|
+
}, _callee3);
|
|
136
|
+
})), [itemKey, store]);
|
|
137
|
+
var destroyCache = useCallback(function () {
|
|
138
|
+
return store.clear();
|
|
139
|
+
}, [store]);
|
|
140
|
+
useEffect(function () {
|
|
141
|
+
optionsRef.current = cacheOptions;
|
|
142
|
+
});
|
|
143
|
+
useEffect(function () {
|
|
144
|
+
return function () {
|
|
145
|
+
var _ref4 = optionsRef.current || {
|
|
146
|
+
expires: 0
|
|
147
|
+
},
|
|
148
|
+
expires = _ref4.expires;
|
|
149
|
+
var clearExpiredCaches = /*#__PURE__*/function () {
|
|
150
|
+
var _ref5 = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee5() {
|
|
151
|
+
var keys, currentTime;
|
|
152
|
+
return _regeneratorRuntime().wrap(function _callee5$(_context5) {
|
|
153
|
+
while (1) switch (_context5.prev = _context5.next) {
|
|
154
|
+
case 0:
|
|
155
|
+
_context5.next = 2;
|
|
156
|
+
return store.keys();
|
|
157
|
+
case 2:
|
|
158
|
+
keys = _context5.sent;
|
|
159
|
+
currentTime = new Date().getTime();
|
|
160
|
+
(keys || []).forEach( /*#__PURE__*/function () {
|
|
161
|
+
var _ref6 = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee4(key) {
|
|
162
|
+
var item;
|
|
163
|
+
return _regeneratorRuntime().wrap(function _callee4$(_context4) {
|
|
164
|
+
while (1) switch (_context4.prev = _context4.next) {
|
|
165
|
+
case 0:
|
|
166
|
+
_context4.next = 2;
|
|
167
|
+
return store.getItem(key);
|
|
168
|
+
case 2:
|
|
169
|
+
item = _context4.sent;
|
|
170
|
+
if (item !== null && item !== void 0 && item.__expiresTime__ && expires && currentTime > item.__expiresTime__) {
|
|
171
|
+
store.removeItem(key).then().catch();
|
|
172
|
+
}
|
|
173
|
+
case 4:
|
|
174
|
+
case "end":
|
|
175
|
+
return _context4.stop();
|
|
176
|
+
}
|
|
177
|
+
}, _callee4);
|
|
178
|
+
}));
|
|
179
|
+
return function (_x3) {
|
|
180
|
+
return _ref6.apply(this, arguments);
|
|
181
|
+
};
|
|
182
|
+
}());
|
|
183
|
+
case 5:
|
|
184
|
+
case "end":
|
|
185
|
+
return _context5.stop();
|
|
186
|
+
}
|
|
187
|
+
}, _callee5);
|
|
188
|
+
}));
|
|
189
|
+
return function clearExpiredCaches() {
|
|
190
|
+
return _ref5.apply(this, arguments);
|
|
191
|
+
};
|
|
192
|
+
}();
|
|
193
|
+
if (expires && expires > 0) {
|
|
194
|
+
clearExpiredCaches();
|
|
195
|
+
}
|
|
196
|
+
};
|
|
197
|
+
}, [store]);
|
|
198
|
+
return {
|
|
199
|
+
store: store,
|
|
200
|
+
setCache: setCache,
|
|
201
|
+
getCache: getCache,
|
|
202
|
+
clearCache: clearCache,
|
|
203
|
+
destroyCache: destroyCache
|
|
204
|
+
};
|
|
205
|
+
};
|
|
206
|
+
export default useCache;
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
import { useRef, useEffect, useCallback } from 'react';
|
|
2
|
+
|
|
3
|
+
// eslint-disable-next-line @typescript-eslint/ban-types
|
|
4
|
+
function useDebounceInput(cb) {
|
|
5
|
+
var delay = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 800;
|
|
6
|
+
var cbRef = useRef(cb);
|
|
7
|
+
var timerRef = useRef();
|
|
8
|
+
useEffect(function () {
|
|
9
|
+
cbRef.current = cb;
|
|
10
|
+
});
|
|
11
|
+
useEffect(function () {
|
|
12
|
+
return function () {
|
|
13
|
+
clearTimeout(timerRef.current);
|
|
14
|
+
};
|
|
15
|
+
}, []);
|
|
16
|
+
return useCallback(
|
|
17
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
18
|
+
function (e) {
|
|
19
|
+
var value = e.target.value;
|
|
20
|
+
clearTimeout(timerRef.current);
|
|
21
|
+
timerRef.current = setTimeout(function () {
|
|
22
|
+
cbRef.current(value);
|
|
23
|
+
}, delay);
|
|
24
|
+
}, [delay]);
|
|
25
|
+
}
|
|
26
|
+
export default useDebounceInput;
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
/// <reference types="react" />
|
|
2
|
+
import { ICacheOptions } from './useCache';
|
|
3
|
+
export interface IRemoteOptions {
|
|
4
|
+
fetch?: (cacheValueKey?: string | number) => Promise<FetchResult>;
|
|
5
|
+
cacheKey?: string | number;
|
|
6
|
+
cacheValueKey?: string | number;
|
|
7
|
+
timeout?: number;
|
|
8
|
+
retry?: number;
|
|
9
|
+
enableBatch?: boolean;
|
|
10
|
+
}
|
|
11
|
+
export type UseRemoteHooks = (defaultData: string, callback?: (string: string) => void, options?: IRemoteOptions | undefined, cacheOptions?: ICacheOptions) => {
|
|
12
|
+
data: string;
|
|
13
|
+
refresh: () => void;
|
|
14
|
+
cacheStore: LocalForage;
|
|
15
|
+
loading: boolean;
|
|
16
|
+
};
|
|
17
|
+
export interface FetchResult {
|
|
18
|
+
result: string;
|
|
19
|
+
next: (() => Promise<FetchResult>) | undefined;
|
|
20
|
+
key?: string | number;
|
|
21
|
+
nextKey?: string | number;
|
|
22
|
+
}
|
|
23
|
+
export declare const useSequenceFetch: (fetch: ((cacheValueKey?: string | number) => Promise<FetchResult>) | undefined, callback: (data: string) => Promise<void>, options: Pick<IRemoteOptions, 'timeout' | 'retry' | 'cacheKey' | 'cacheValueKey'>, cacheOptions?: ICacheOptions) => {
|
|
24
|
+
loading: boolean;
|
|
25
|
+
setLoading: import("react").Dispatch<import("react").SetStateAction<boolean>>;
|
|
26
|
+
refresh: () => Promise<void>;
|
|
27
|
+
cacheStore: LocalForage;
|
|
28
|
+
};
|
|
29
|
+
declare const useRemote: UseRemoteHooks;
|
|
30
|
+
export default useRemote;
|