-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
107 additions
and
68 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,3 @@ | ||
export const ImmediatePriority = 1; | ||
export const NormalPriority = 2; | ||
export const LowPriority = 3; | ||
|
||
export type PriorityLevel = | ||
| typeof ImmediatePriority | ||
| typeof NormalPriority | ||
| typeof LowPriority; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,5 @@ | ||
export * from './useAtomValueWithSchedule'; | ||
export * from './useAtomWithSchedule'; | ||
export * from './useSetAtomWithSchedule'; | ||
export * from './constants'; | ||
export { useAtomValueWithSchedule } from './useAtomValueWithSchedule'; | ||
export { useAtomWithSchedule } from './useAtomWithSchedule'; | ||
export { useSetAtomWithSchedule } from './useSetAtomWithSchedule'; | ||
export { LowPriority, NormalPriority, ImmediatePriority } from './constants'; | ||
export * from './types'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
import { Atom, useStore } from 'jotai'; | ||
import { ImmediatePriority, LowPriority, NormalPriority } from './constants'; | ||
|
||
export type SetAtom<Args extends unknown[], Result> = (...args: Args) => Result; | ||
|
||
export type AnyAtom = Atom<unknown>; | ||
|
||
export type Store = ReturnType<typeof useStore>; | ||
|
||
export type Listener = () => void; | ||
|
||
export type PriorityLevel = | ||
| typeof ImmediatePriority | ||
| typeof NormalPriority | ||
| typeof LowPriority; | ||
|
||
export type Options = Parameters<typeof useStore>[0] & { | ||
delay?: number; | ||
priority?: PriorityLevel; | ||
}; | ||
|
||
export type Task = { | ||
priority: PriorityLevel; | ||
subscribe: Listener; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
import { Task } from './types'; | ||
|
||
let isMessageLoopRunning: boolean = false; | ||
|
||
const taskQueue: Array<Task> = []; | ||
|
||
function workLoop(): boolean { | ||
const highestPriority = Math.min(...taskQueue.map((task) => task.priority)); | ||
const highestPriorityList = taskQueue.filter( | ||
(task) => task.priority === highestPriority, | ||
); | ||
highestPriorityList.forEach((task) => { | ||
task.subscribe(); | ||
taskQueue.splice(taskQueue.indexOf(task), 1); | ||
}); | ||
if (taskQueue.length > 0) { | ||
return true; | ||
} | ||
return false; | ||
} | ||
|
||
function handleNextBatch() { | ||
const hasMoreWork = workLoop(); | ||
if (hasMoreWork) { | ||
enqueueWorkExecution(); | ||
} else { | ||
isMessageLoopRunning = false; | ||
} | ||
} | ||
|
||
const channel = new MessageChannel(); | ||
const port = channel.port2; | ||
channel.port1.onmessage = handleNextBatch; | ||
|
||
export const enqueueWorkExecution = () => { | ||
port.postMessage(null); | ||
}; | ||
|
||
export const initiateWorkLoop = () => { | ||
if (!isMessageLoopRunning) { | ||
isMessageLoopRunning = true; | ||
enqueueWorkExecution(); | ||
} | ||
}; | ||
|
||
export const addTask = (newTask: Task) => { | ||
taskQueue.push(newTask); | ||
}; |