Skip to content

Commit

Permalink
🔀 🔥 deprecate all utility functions
Browse files Browse the repository at this point in the history
  • Loading branch information
astoilkov committed Feb 12, 2024
1 parent bb4366a commit 447e4a2
Show file tree
Hide file tree
Showing 7 changed files with 10 additions and 68 deletions.
5 changes: 0 additions & 5 deletions index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,3 @@ export { default as yieldOrContinue } from './src/yieldOrContinue'
export { default as yieldControl } from './src/yieldControl'
export { default as isTimeToYield } from './src/isTimeToYield'
export type { default as SchedulingStrategy } from './src/SchedulingStrategy'

// utility
export { default as queueTask } from './src/utils/queueTask'
export { default as withResolvers } from './src/utils/withResolvers'
export { default as requestAfterFrame } from './src/utils/requestAfterFrame'
2 changes: 1 addition & 1 deletion playground/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@
<br />
<div>
<code>others:</code>
<button id="queue-task">queueTask()</button>
<button id="queue-task">waitNextTask()</button>
<button id="simulate-work">simulateWork()</button>
<button id="post-task-vs-yield-or-continue">postTask() vs yieldOrContinue()</button>
</div>
Expand Down
19 changes: 7 additions & 12 deletions playground/playground.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,6 @@
import {
isTimeToYield,
queueTask,
SchedulingStrategy,
withResolvers,
yieldOrContinue,
} from '../index'
import { isTimeToYield, SchedulingStrategy, withResolvers, yieldOrContinue } from '../index'
import simulateWork from './utils/simulateWork'
import waitNextTask from '../src/utils/waitNextTask'

document.querySelector('#run-interactive')!.addEventListener('click', () => {
run('interactive')
Expand Down Expand Up @@ -45,7 +40,7 @@ document.querySelector('#post-task-vs-yield-or-continue')!.addEventListener('cli
postTaskVsYieldOrContinue()
})
document.querySelector('#queue-task')!.addEventListener('click', () => {
runQueueTask()
runWaitNextTask()
})

async function run(strategy: SchedulingStrategy, time: number = 1000) {
Expand Down Expand Up @@ -80,10 +75,10 @@ async function runPostTask(priority: 'user-blocking' | 'user-visible' | 'backgro
}
}

async function runQueueTask(time: number = 1000) {
async function runWaitNextTask(time: number = 1000) {
const start = Date.now()
while (Date.now() - start < time) {
await new Promise<void>((resolve) => queueTask(resolve))
await waitNextTask()
simulateWork()
}
}
Expand All @@ -102,10 +97,10 @@ async function postTaskVsYieldOrContinue() {
const start = performance.now()
let count = 0
while (performance.now() - start < 1000) {
await new Promise<void>((resolve) => queueTask(resolve))
await waitNextTask()
count++
}
console.log(count.toString(), '→ queueTask()')
console.log(count.toString(), '→ waitNextTask()')
}
{
const start = performance.now()
Expand Down
8 changes: 0 additions & 8 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -83,14 +83,6 @@ async function findInFiles(query: string) {
}
```

#### `requestAfterFrame(callback)`

_This is a utility function, most people don't need to use it._ The same way `requestAnimationFrame()` queues a `callback` to be executed just before a frame is rendered `requestAfterFrame()` is called just after a frame is rendered.

#### `queueTask(callback)`

_This is a utility function, most people don't need to use it._ The same way `queueMicrotask()` queues a `callback` to be executed in the end of the current microtask, `queueTask()` queues the task for the next task. You learn more at [here](https://developer.mozilla.org/en-US/docs/Web/API/HTML_DOM_API/Microtask_guide#tasks_vs._microtasks).

### More complex scenarios

The library has two more functions available:
Expand Down
8 changes: 2 additions & 6 deletions src/frameTracker.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import withResolvers, { PromiseWithResolvers } from './utils/withResolvers'
import { queueTask } from '../index'
import waitNextTask from './utils/waitNextTask'

class FrameTracker {
#timeoutId?: number
Expand All @@ -10,13 +10,9 @@ class FrameTracker {
this.#deferred = withResolvers()
}

async waitAnimationFrame(): Promise<void> {
return this.#deferred.promise
}

async waitAfterFrame(): Promise<void> {
await this.#deferred.promise
await new Promise<void>((resolve) => queueTask(resolve))
await waitNextTask()
}

start(): void {
Expand Down
23 changes: 0 additions & 23 deletions src/utils/queueTask.ts

This file was deleted.

13 changes: 0 additions & 13 deletions src/utils/requestAfterFrame.ts

This file was deleted.

0 comments on commit 447e4a2

Please sign in to comment.