-
-
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
8 changed files
with
121 additions
and
2 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
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,56 @@ | ||
export type Job<T> = () => PromiseLike<T> | T; | ||
|
||
/** | ||
* Makes sure all callbacks are executed with at least set ms delay | ||
*/ | ||
export default function createScatter(delay: number) { | ||
let lastExecution = -Infinity; | ||
let waiting = false; | ||
const jobs: Job<any>[] = []; | ||
const onEmpty: (() => void)[] = []; | ||
|
||
const trigger = () => { | ||
if (jobs.length && !waiting) { | ||
const now = Date.now(); | ||
|
||
if (lastExecution + delay <= now) { | ||
lastExecution = now; | ||
const job = jobs.shift()!; | ||
job().finally(() => { | ||
trigger(); | ||
}); | ||
if (!jobs.length) { | ||
onEmpty.forEach((cb) => cb()); | ||
} | ||
} else { | ||
waiting = true; | ||
setTimeout(() => { | ||
waiting = false; | ||
trigger(); | ||
}, lastExecution + delay - now); | ||
} | ||
} | ||
}; | ||
|
||
return { | ||
get length() { | ||
return jobs.length; | ||
}, | ||
onEmpty(cb: () => void) { | ||
onEmpty.push(cb); | ||
|
||
return () => { | ||
onEmpty.splice(onEmpty.indexOf(cb), 1); | ||
}; | ||
}, | ||
push<T>(job: Job<T>): Promise<T> { | ||
return new Promise<T>((resolve, reject) => { | ||
jobs.push(() => Promise.resolve(job()).then(resolve, reject)); | ||
trigger(); | ||
}); | ||
}, | ||
clear() { | ||
jobs.length = 0; | ||
}, | ||
}; | ||
} |
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,36 @@ | ||
import test from 'node:test'; | ||
import assert from 'node:assert'; | ||
import { install } from '@sinonjs/fake-timers'; | ||
import { createScatter } from '../src'; | ||
|
||
test('scatter executes jobs after set delay', async () => { | ||
const clock = install(); | ||
const scatter = createScatter(100); | ||
|
||
const check: string[] = []; | ||
|
||
scatter.push(() => { | ||
check.push('One'); | ||
}); | ||
scatter.push(() => { | ||
check.push('Two'); | ||
}); | ||
scatter.onEmpty(() => check.push('Empty')); | ||
|
||
assert.deepEqual(check, ['One']); | ||
assert.deepEqual(scatter.length, 1); | ||
|
||
clock.tick(100); | ||
assert.deepEqual(check, ['One', 'Two', 'Empty']); | ||
assert.deepEqual(scatter.length, 0); | ||
|
||
clock.tick(100); | ||
scatter.push(() => { | ||
check.push('Three'); | ||
}); | ||
|
||
assert.deepEqual(check, ['One', 'Two', 'Empty', 'Three', 'Empty']); | ||
assert.deepEqual(scatter.length, 0); | ||
|
||
clock.uninstall(); | ||
}); |