diff options
| -rw-r--r-- | demo/src/task-scheduler.spec.ts | 66 | ||||
| -rw-r--r-- | demo/src/task-scheduler.ts | 23 |
2 files changed, 9 insertions, 80 deletions
diff --git a/demo/src/task-scheduler.spec.ts b/demo/src/task-scheduler.spec.ts deleted file mode 100644 index 9b3eeff..0000000 --- a/demo/src/task-scheduler.spec.ts +++ /dev/null @@ -1,66 +0,0 @@ -/** - * remove-me @jest-environment jsdom - */ -/// <reference lib="dom" /> - -import { MicroTaskScheduler } from './task-scheduler'; - -/* - * We're skipping this test due to JSDOM issue: - * https://github.com/jsdom/jsdom/issues/2961 - * - * It should pass on Node >= 12.0, but since the core library - * is expected to run on Node >= 10.0, and this test only applies - * to the demo project, it's better disabling it for now than - * getting false negatives. - * - * When this test is eventually re-enabled, don't forget to remove - * the `remove-me` text at the top of this file to re-enable the - * loading of jsdom. - */ -// eslint-disable-next-line jest/no-disabled-tests -describe.skip('task-scheduler', () => { - let taskScheduler: MicroTaskScheduler; - let task: jest.Mock; - - beforeEach(() => { - taskScheduler = new MicroTaskScheduler(); - task = jest.fn(); - }); - - it('should execute task', async () => { - taskScheduler.start(); - taskScheduler.postTask(task); - await new Promise((resolve) => setTimeout(resolve, 0)); - expect(task).toHaveBeenCalledTimes(1); - }); - - it('should execute task twice when posted twice', async () => { - taskScheduler.start(); - taskScheduler.postTask(task); - taskScheduler.postTask(task); - await new Promise((resolve) => setTimeout(resolve, 0)); - expect(task).toHaveBeenCalledTimes(2); - }); - - it('should not execute task when not started', async () => { - taskScheduler.postTask(task); - await new Promise((resolve) => setTimeout(resolve, 0)); - expect(task).not.toHaveBeenCalled(); - }); - - it('should not execute task when stopped', async () => { - taskScheduler.start(); - taskScheduler.stop(); - taskScheduler.postTask(task); - await new Promise((resolve) => setTimeout(resolve, 0)); - expect(task).not.toHaveBeenCalled(); - }); - - it('should not register listener twice', async () => { - const addEventListenerSpy = jest.spyOn(window, 'addEventListener'); - taskScheduler.start(); - taskScheduler.start(); - expect(addEventListenerSpy).toHaveBeenCalledTimes(1); - }); -}); diff --git a/demo/src/task-scheduler.ts b/demo/src/task-scheduler.ts index 5364d92..46413ef 100644 --- a/demo/src/task-scheduler.ts +++ b/demo/src/task-scheduler.ts @@ -1,39 +1,34 @@ -// Faster setTimeout(fn, 0) implementation using postMessage API -// Based on https://dbaron.org/log/20100309-faster-timeouts export type IMicroTaskCallback = () => void; export class MicroTaskScheduler { - readonly messageName = 'zero-timeout-message'; - + private readonly channel = new MessageChannel(); private executionQueue: Array<IMicroTaskCallback> = []; private stopped = true; start() { if (this.stopped) { this.stopped = false; - window.addEventListener('message', this.handleMessage, true); + this.channel.port2.onmessage = this.handleMessage; } } stop() { this.stopped = true; - window.removeEventListener('message', this.handleMessage, true); + this.executionQueue.splice(0, this.executionQueue.length); + this.channel.port2.onmessage = null; } postTask(fn: IMicroTaskCallback) { if (!this.stopped) { this.executionQueue.push(fn); - window.postMessage(this.messageName, '*'); + this.channel.port1.postMessage(null); } } - private handleMessage = (event: MessageEvent) => { - if (event.data === this.messageName) { - event.stopPropagation(); - const executeJob = this.executionQueue.shift(); - if (executeJob !== undefined) { - executeJob(); - } + private handleMessage = () => { + const executeJob = this.executionQueue.shift(); + if (executeJob !== undefined) { + executeJob(); } }; } |
