Skip to content

Commit

Permalink
♻️ extract and export a SchedulingPriority type
Browse files Browse the repository at this point in the history
  • Loading branch information
astoilkov committed Nov 20, 2023
1 parent 96f3c1d commit b20053d
Show file tree
Hide file tree
Showing 6 changed files with 15 additions and 7 deletions.
1 change: 1 addition & 0 deletions index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ export { default as yieldOrContinue } from './src/yieldOrContinue'
// secondary
export { default as yieldControl } from './src/yieldControl'
export { default as isTimeToYield } from './src/isTimeToYield'
export type { default as SchedulingPriority } from './src/SchedulingPriority'

// utility
export { default as Deferred } from './src/Deferred'
Expand Down
3 changes: 3 additions & 0 deletions src/SchedulingPriority.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
type SchedulingPriority = 'user-visible' | 'background'

export default SchedulingPriority
5 changes: 3 additions & 2 deletions src/isTimeToYield.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import state from './state'
import hasValidContext from './hasValidContext'
import SchedulingPriority from './SchedulingPriority'

// #performance
// calling `isTimeToYield()` thousand of times is slow. `lastCall` helps to run logic inside of
Expand All @@ -10,7 +11,7 @@ let lastResult = false
/**
* Determines if it's time to call `yieldControl()`.
*/
export default function isTimeToYield(priority: 'background' | 'user-visible'): boolean {
export default function isTimeToYield(priority: SchedulingPriority): boolean {
if (!hasValidContext()) {
return false
}
Expand All @@ -34,7 +35,7 @@ export default function isTimeToYield(priority: 'background' | 'user-visible'):
return lastResult
}

function calculateDeadline(priority: 'background' | 'user-visible'): number {
function calculateDeadline(priority: SchedulingPriority): number {
if (state.frameWorkStartTime === undefined) {
// silentError()
return -1
Expand Down
5 changes: 3 additions & 2 deletions src/tasks.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,18 @@
import state from './state'
import Deferred from './Deferred'
import { startTracking } from './tracking'
import SchedulingPriority from './SchedulingPriority'

export type Task = {
priority: 'background' | 'user-visible'
priority: SchedulingPriority
deferred: Deferred
}

/**
* Adds a task to the queue and returns the new task.
* @param priority {('background' | 'user-visible')} The priority of the new task.
*/
export function createTask(priority: 'background' | 'user-visible'): Task {
export function createTask(priority: SchedulingPriority): Task {
const item = { priority, deferred: new Deferred() }
const insertIndex =
priority === 'user-visible'
Expand Down
5 changes: 3 additions & 2 deletions src/yieldControl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import isTimeToYield from './isTimeToYield'
import hasValidContext from './hasValidContext'
import { createTask, nextTask, removeTask } from './tasks'
import { cancelPromiseEscape, requestPromiseEscape } from './promiseEscape'
import SchedulingPriority from './SchedulingPriority'

let promiseEscapeId: number | undefined

Expand All @@ -17,7 +18,7 @@ let promiseEscapeId: number | undefined
* resolved second.
* @returns {Promise<void>} The promise that will be resolved when the queue
*/
export default async function yieldControl(priority: 'user-visible' | 'background'): Promise<void> {
export default async function yieldControl(priority: SchedulingPriority): Promise<void> {
if (!hasValidContext()) {
return
}
Expand Down Expand Up @@ -45,7 +46,7 @@ export default async function yieldControl(priority: 'user-visible' | 'backgroun
})
}

async function schedule(priority: 'user-visible' | 'background'): Promise<void> {
async function schedule(priority: SchedulingPriority): Promise<void> {
if (state.frameTimeElapsed) {
await state.onAnimationFrame
}
Expand Down
3 changes: 2 additions & 1 deletion src/yieldOrContinue.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import yieldControl from './yieldControl'
import isTimeToYield from './isTimeToYield'
import SchedulingPriority from './SchedulingPriority'

// disabling ESLint otherwise `requestPromiseEscape()` in `yieldControl()` won't work
// eslint-disable-next-line @typescript-eslint/promise-function-async
export default function yieldOrContinue(priority: 'user-visible' | 'background'): Promise<void> {
export default function yieldOrContinue(priority: SchedulingPriority): Promise<void> {
if (isTimeToYield(priority)) {
return yieldControl(priority)
}
Expand Down

0 comments on commit b20053d

Please sign in to comment.