react-terminal-viewer
Version:
<h1 align="center"> react-terminal-viewer </h1>
160 lines • 7.38 kB
JavaScript
function _slicedToArray(arr, i) { return _arrayWithHoles(arr) || _iterableToArrayLimit(arr, i) || _unsupportedIterableToArray(arr, i) || _nonIterableRest(); }
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."); }
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); }
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; }
function _iterableToArrayLimit(arr, i) { var _i = null == arr ? null : "undefined" != typeof Symbol && arr[Symbol.iterator] || arr["@@iterator"]; if (null != _i) { var _s, _e, _x, _r, _arr = [], _n = !0, _d = !1; try { if (_x = (_i = _i.call(arr)).next, 0 === i) { if (Object(_i) !== _i) return; _n = !1; } else for (; !(_n = (_s = _x.call(_i)).done) && (_arr.push(_s.value), _arr.length !== i); _n = !0); } catch (err) { _d = !0, _e = err; } finally { try { if (!_n && null != _i.return && (_r = _i.return(), Object(_r) !== _r)) return; } finally { if (_d) throw _e; } } return _arr; } }
function _arrayWithHoles(arr) { if (Array.isArray(arr)) return arr; }
/* eslint-disable @typescript-eslint/no-explicit-any */
import { useEffect, useRef, useState } from 'react';
import { v4 as uuidv4 } from 'uuid';
import WebWorkerClient from "../Addon/WorkerLog/WebWorkerClient";
import globalContext from "../Addon/WorkerLog/GlobalContext";
import LogWorker from "../Addon/WorkerLog/LogWorker";
export var useWorkerLogs = function useWorkerLogs(string, changeCallback) {
var prefixRef = useRef('prefix');
var _useState = useState([]),
_useState2 = _slicedToArray(_useState, 2),
logs = _useState2[0],
setLogs = _useState2[1];
var _useState3 = useState(false),
_useState4 = _slicedToArray(_useState3, 2),
working = _useState4[0],
setWorking = _useState4[1];
var _useState5 = useState({
count: 0,
map: {},
markedKey: 0
}),
_useState6 = _slicedToArray(_useState5, 2),
searcherResult = _useState6[0],
setSearcherResult = _useState6[1];
var logIdRef = useRef(0);
var searchIdRef = useRef(0);
var searcherRef = useRef(undefined);
var searcherCallbackRef = useRef();
var changeCallbackRef = useRef();
var prevKeyword = useRef('');
var hlIndexRef = useRef(0);
useEffect(function () {
prefixRef.current = uuidv4();
}, []);
useEffect(function () {
changeCallbackRef.current = changeCallback;
searcherRef.current = {
findPrevious: function findPrevious(keyword, options) {
setWorking(true);
var count = searcherResult.count || 0;
if (prevKeyword.current !== keyword) {
hlIndexRef.current = 0;
count = 0;
}
var hlIndex = prevKeyword.current === keyword ? hlIndexRef.current : 0;
var worker = globalContext.getWorker();
searchIdRef.current += 1;
if (count > 0) {
hlIndex = hlIndex - 1 < 0 ? count - 1 : hlIndex - 1;
} else {
hlIndex = 0;
}
hlIndexRef.current = hlIndex;
worker === null || worker === void 0 ? void 0 : worker.send('search-log', {
id: "".concat(prefixRef.current, "-").concat(logIdRef.current),
searchId: "".concat(prefixRef.current, "-").concat(searchIdRef.current),
keyword: keyword,
hlIndex: hlIndex,
string: string,
options: options
});
prevKeyword.current = keyword;
},
findNext: function findNext(keyword, options) {
setWorking(true);
var count = searcherResult.count || 0;
if (prevKeyword.current !== keyword) {
hlIndexRef.current = 0;
count = 0;
}
var hlIndex = prevKeyword.current === keyword ? hlIndexRef.current : 0;
var worker = globalContext.getWorker();
searchIdRef.current += 1;
if (count > 0) {
hlIndex = hlIndex + 1 >= count ? 0 : hlIndex + 1;
} else {
hlIndex = 0;
}
hlIndexRef.current = hlIndex;
worker === null || worker === void 0 ? void 0 : worker.send('search-log', {
id: "".concat(prefixRef.current, "-").concat(logIdRef.current),
searchId: "".concat(prefixRef.current, "-").concat(searchIdRef.current),
keyword: keyword,
hlIndex: hlIndex,
string: string,
options: options
});
prevKeyword.current = keyword;
},
onDidChangeResults: function onDidChangeResults(c) {
searcherCallbackRef.current = c;
}
};
if (!globalContext.getWorker()) {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
globalContext.setWorker(new WebWorkerClient(new LogWorker(globalContext.getWorkerPath())));
}
});
useEffect(function () {
setWorking(true);
var worker = globalContext.getWorker();
logIdRef.current += 1;
worker === null || worker === void 0 ? void 0 : worker.send('highlight-log', {
id: "".concat(prefixRef.current, "-").concat(logIdRef.current),
string: string
});
}, [string]);
useEffect(function () {
var worker = globalContext.getWorker();
var onHighlightSuccess = function onHighlightSuccess(_ref) {
var id = _ref.id,
array = _ref.logs;
// 需处理 Web Worker 处理不及时的异步问题
var _ref2 = id.match(/(.*)-(\d)/) || [0, 'prefix', 0],
_ref3 = _slicedToArray(_ref2, 3),
prefix = _ref3[1],
number = _ref3[2];
if (prefix === prefixRef.current && Number(number) <= logIdRef.current) {
setWorking(false);
setLogs(array);
}
};
var onSearchSuccess = function onSearchSuccess(_ref4) {
var searchId = _ref4.searchId,
result = _ref4.searcherResult;
if (searchId === "".concat(prefixRef.current, "-").concat(searchIdRef.current)) {
setWorking(false);
setSearcherResult(result);
}
if (typeof changeCallbackRef.current === 'function') {
changeCallbackRef.current(result);
}
if (typeof searcherCallbackRef.current === 'function') {
searcherCallbackRef.current({
resultIndex: result.count > 0 ? hlIndexRef.current : 0,
resultCount: result.count
});
}
};
worker === null || worker === void 0 ? void 0 : worker.on('highlight-log-success', onHighlightSuccess);
worker === null || worker === void 0 ? void 0 : worker.on('search-log-success', onSearchSuccess);
return function () {
worker === null || worker === void 0 ? void 0 : worker.off('highlight-log-success', onHighlightSuccess);
worker === null || worker === void 0 ? void 0 : worker.off('search-log-success', onSearchSuccess);
};
}, []);
return {
logs: logs,
working: working,
searcherRef: searcherRef,
searcherResult: searcherResult
};
};
export default useWorkerLogs;