Skip to content

Commit

Permalink
fix error catch
Browse files Browse the repository at this point in the history
  • Loading branch information
MrWangJustToDo committed Jan 1, 2022
1 parent ddf7e6d commit 6981602
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 14 deletions.
4 changes: 2 additions & 2 deletions src/types/utils/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,14 @@ interface Delay {
interface TimeoutMap {
[props: string]: Array<NodeJS.Timeout | void>;
}
interface ResolveMap {
interface ReJectMap {
[props: string]: Array<(() => void) | void>;
}
interface KeyMap {
[props: string]: number;
}

export type { Cancel, Delay, TimeoutMap, ResolveMap, KeyMap };
export type { Cancel, Delay, TimeoutMap, ReJectMap, KeyMap };

/* action */
interface JudgeActionProps<T> {
Expand Down
31 changes: 19 additions & 12 deletions src/utils/delay.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { log } from "./log";
import type { Cancel, Delay, KeyMap, ResolveMap, TimeoutMap } from "types/utils";
import type { Cancel, Delay, KeyMap, ReJectMap, TimeoutMap } from "types/utils";

const timeoutMap: TimeoutMap = {};
const resolveMap: ResolveMap = {};
const rejectMap: ReJectMap = {};
const keyMap: KeyMap = {};
let keyLength = 0;
const maxKeyLength = 200;
Expand All @@ -11,16 +11,16 @@ const cancel: Cancel = (key) => {
if (timeoutMap[key]) {
const length = timeoutMap[key].length;
timeoutMap[key] = timeoutMap[key].map((id) => id && clearTimeout(id)).slice(length);
resolveMap[key] = resolveMap[key].map((resolve) => resolve && resolve()).slice(length);
rejectMap[key] = rejectMap[key].map((reject) => reject && reject()).slice(length);
}
if (keyLength > maxKeyLength) {
const keys = Object.keys(keyMap).sort((key1, key2) => (keyMap[key1] > keyMap[key2] ? 1 : -1));
log(`start delete delay key, currentLength ${keyLength} over max length ${maxKeyLength}`, "normal");
for (const keyItem of keys) {
if (keyItem !== key && !resolveMap[keyItem].length) {
if (keyItem !== key && !rejectMap[keyItem].length) {
delete keyMap[keyItem];
delete timeoutMap[keyItem];
delete resolveMap[keyItem];
delete rejectMap[keyItem];
keyLength--;
}
}
Expand All @@ -29,29 +29,36 @@ const cancel: Cancel = (key) => {

const delay: Delay = (time, action, key) => {
if (key === undefined) {
return new Promise((resolve) => {
return new Promise<void>((resolve) => {
setTimeout(() => {
resolve(action && action());
resolve();
}, time);
}).then(() => {
if (action) return action();
});
} else {
if (!(key in keyMap)) {
keyMap[key] = 1;
timeoutMap[key] = [];
resolveMap[key] = [];
rejectMap[key] = [];
keyLength++;
} else {
keyMap[key]++;
}
cancel(key);
return new Promise((resolve) => {
resolveMap[key].push(resolve);
return new Promise<void>((resolve, reject) => {
rejectMap[key].push(reject);
timeoutMap[key].push(
setTimeout(() => {
resolve(action && action());
resolve();
}, time)
);
});
})
.then(() => {
if (action) return action();
})
// eslint-disable-next-line @typescript-eslint/no-empty-function
.catch(() => {});
}
};

Expand Down

0 comments on commit 6981602

Please sign in to comment.