Skip to content

Commit

Permalink
🐜 rename
Browse files Browse the repository at this point in the history
  • Loading branch information
astoilkov committed Nov 20, 2023
1 parent d4e6d1c commit 0e9fbcf
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 12 deletions.
9 changes: 4 additions & 5 deletions src/isTimeToYield.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,7 @@ export default function isTimeToYield(priority: SchedulingPriority = 'user-visib
}

function calculateDeadline(priority: SchedulingPriority): number {
if (state.frameWorkStartTime === undefined) {
// silentError()
if (state.workStartTimeThisFrame === undefined) {
return -1
}

Expand All @@ -46,19 +45,19 @@ function calculateDeadline(priority: SchedulingPriority): number {
// spent the max recommended 100ms doing 'user-blocking' tasks minus 1 frame (16ms):
// - https://developer.mozilla.org/en-US/docs/Web/Performance/How_long_is_too_long#responsiveness_goal
// - Math.round(100 - (1000/60)) = Math.round(83,333) = 83
return state.frameWorkStartTime + 83
return state.workStartTimeThisFrame + 83
}
case 'user-visible': {
// spent 80% percent of the frame's budget running 'user-visible' tasks:
// - Math.round((1000/60) * 0.8) = Math.round(13,333) = 13
return state.frameWorkStartTime + 4
return state.workStartTimeThisFrame + 4
}
case 'background': {
const idleDeadline =
state.idleDeadline === undefined
? Number.MAX_SAFE_INTEGER
: Date.now() + state.idleDeadline.timeRemaining()
return Math.min(state.frameWorkStartTime + 5, idleDeadline)
return Math.min(state.workStartTimeThisFrame + 5, idleDeadline)
}
// istanbul ignore next
default:
Expand Down
4 changes: 2 additions & 2 deletions src/state.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ type State = {
frameTimeElapsed: boolean
onIdleCallback: Deferred
onAnimationFrame: Deferred
frameWorkStartTime: number | undefined
idleDeadline: IdleDeadline | undefined
workStartTimeThisFrame: number | undefined
}

const state: State = {
Expand All @@ -16,7 +16,7 @@ const state: State = {
frameTimeElapsed: false,
onIdleCallback: new Deferred(),
onAnimationFrame: new Deferred(),
frameWorkStartTime: undefined,
workStartTimeThisFrame: undefined,
}

export default state
2 changes: 1 addition & 1 deletion src/tracking.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ export function startTracking(): void {
const reset = (): void => {
state.idleDeadline = undefined
state.frameTimeElapsed = false
state.frameWorkStartTime = undefined
state.workStartTimeThisFrame = undefined
}
const loop = (): void => {
if (typeof requestIdleCallback !== 'undefined') {
Expand Down
8 changes: 4 additions & 4 deletions src/yieldControl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,17 +63,17 @@ async function schedule(priority: SchedulingPriority): Promise<void> {
// istanbul ignore if
if (navigator.scheduling?.isInputPending?.() === true) {
await schedule(priority)
} else if (state.frameWorkStartTime === undefined) {
state.frameWorkStartTime = Date.now()
} else if (state.workStartTimeThisFrame === undefined) {
state.workStartTimeThisFrame = Date.now()
}
} else {
await state.onIdleCallback

// not checking for `navigator.scheduling?.isInputPending?.()` here because idle callbacks
// ensure no input is pending

if (state.frameWorkStartTime === undefined) {
state.frameWorkStartTime = Date.now()
if (state.workStartTimeThisFrame === undefined) {
state.workStartTimeThisFrame = Date.now()
}
}
}

0 comments on commit 0e9fbcf

Please sign in to comment.