Skip to content

Commit

Permalink
refactor: define find and remove task from queue function
Browse files Browse the repository at this point in the history
  • Loading branch information
hiroki0525 committed Jan 29, 2024
1 parent 59df8d1 commit 4cd1830
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 5 deletions.
6 changes: 4 additions & 2 deletions src/api/cancelIdleTask.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import {
findIdleTasksFromQueue,
idleTaskState as its,
removeIdleTaskFromQueue,
resolveTaskResultWhenCancel,
} from '../internals';
import { IdleTaskKey } from './setIdleTask';
Expand All @@ -13,10 +15,10 @@ const cancelIdleTask = (key: IdleTaskKey): void => {
clearInterval(revalidateIntervalId);
its.idleTaskRevalidateIntervalMap.delete(targetTaskId);
}
const tasks = its.tasks.filter(task => task.id === targetTaskId);
const tasks = findIdleTasksFromQueue(targetTaskId);
resolveTaskResultWhenCancel(tasks);
its.idleTaskResultMap.delete(key);
its.tasks = its.tasks.filter(task => task.id !== targetTaskId);
removeIdleTaskFromQueue(targetTaskId);
};

export default cancelIdleTask;
6 changes: 4 additions & 2 deletions src/api/forceRunIdleTask.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import {
executeTask,
findIdleTasksFromQueue,
getResultFromCache,
idleTaskState as its,
removeIdleTaskFromQueue,
} from '../internals';
import getIdleTaskStatus from './getIdleTaskStatus';
import { IdleTaskKey } from './setIdleTask';
Expand All @@ -12,7 +14,7 @@ const forceRunIdleTask = async (key: IdleTaskKey): Promise<any> => {
return cacheResult;
}
const targetTaskId = key.id;
const tasks = its.tasks.filter(task => task.id === targetTaskId);
const tasks = findIdleTasksFromQueue(targetTaskId);
const latestEnqueuedTask = tasks[tasks.length - 1];
executeTask(latestEnqueuedTask);
const result = getResultFromCache(key);
Expand All @@ -23,7 +25,7 @@ const forceRunIdleTask = async (key: IdleTaskKey): Promise<any> => {
index === latestEnqueuedTaskIndex || task.id !== targetTaskId
);
} else {
its.tasks = its.tasks.filter(task => task.id !== targetTaskId);
removeIdleTaskFromQueue(targetTaskId);
}
return result;
};
Expand Down
3 changes: 2 additions & 1 deletion src/api/setIdleTask.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import {
rIC,
executeTask,
idleTaskState as its,
removeIdleTaskFromQueue,
} from '../internals';

export interface IdleTaskKey {
Expand Down Expand Up @@ -111,7 +112,7 @@ const setIdleTask = (
const { overwriteTask } = options;
const idleTaskKey = overwriteTask ?? Object.freeze({ id: ++id });
if (overwriteTask) {
its.tasks = its.tasks.filter(task => task.id !== idleTaskKey.id);
removeIdleTaskFromQueue(idleTaskKey.id);
}
let resolve, reject;
its.idleTaskResultMap.set(
Expand Down
8 changes: 8 additions & 0 deletions src/internals.ts
Original file line number Diff line number Diff line change
Expand Up @@ -96,3 +96,11 @@ export const idleTaskState: IdleTaskState = {
idleTaskResultMap: new WeakMap(),
idleTaskRevalidateIntervalMap: new Map(),
};

export const removeIdleTaskFromQueue = (taskId: number) => {
idleTaskState.tasks = idleTaskState.tasks.filter(task => task.id !== taskId);
return idleTaskState.tasks;
};

export const findIdleTasksFromQueue = (taskId: number) =>
idleTaskState.tasks.filter(task => task.id === taskId);

0 comments on commit 4cd1830

Please sign in to comment.