Skip to content

Commit

Permalink
Migrate platform to use a "real" host interface style.
Browse files Browse the repository at this point in the history
  • Loading branch information
rwjblue committed Jan 19, 2018
1 parent af9ac8e commit 79b3956
Show file tree
Hide file tree
Showing 2 changed files with 77 additions and 47 deletions.
53 changes: 53 additions & 0 deletions lib/backburner/host-platform.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
export interface IPlatform {
setTimeout(fn: () => void, ms: number): any;
clearTimeout(id: any): void;
next(): any;
clearNext(timerId: any): void;
now(): number;
}

const SET_TIMEOUT = setTimeout;
const NOOP = () => {};

export function buildDefaultPlatform(flush: () => void): IPlatform {
let next;
let clearNext = NOOP;

if (typeof Promise === 'function') {
const autorunPromise = Promise.resolve();
next = () => autorunPromise.then(flush);

// supports IE11
} else if (typeof MutationObserver === 'function') {
let iterations = 0;
let observer = new MutationObserver(flush);
let node = document.createTextNode('');
observer.observe(node, { characterData: true });

next = () => {
iterations = ++iterations % 2;
node.data = '' + iterations;
return iterations;
};

} else {
next = () => SET_TIMEOUT(flush, 0);
}

return {
setTimeout(fn, ms) {
return SET_TIMEOUT(fn, ms);
},

clearTimeout(timerId: number) {
return clearTimeout(timerId);
},

now() {
return Date.now();
},

next,
clearNext,
};
}
71 changes: 24 additions & 47 deletions lib/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,12 @@
export {
buildDefaultPlatform,
IPlatform
} from './backburner/host-platform';

import {
buildDefaultPlatform,
IPlatform,
} from './backburner/host-platform';
import {
findItem,
findTimer,
Expand All @@ -16,7 +25,6 @@ import Queue, { QUEUE_STATE } from './backburner/queue';
type Timer = any;

const noop = function() {};
const SET_TIMEOUT = setTimeout;

function parseArgs() {
let length = arguments.length;
Expand Down Expand Up @@ -45,14 +53,22 @@ function parseArgs() {
return [target, method, args];
}

export interface IBackburnerOptions {
defaultQueue?: string;
onBegin?: (currentInstance: DeferredActionQueues, previousInstance: DeferredActionQueues) => void;
onEnd?: (currentInstance: DeferredActionQueues, nextInstance: DeferredActionQueues) => void;
mustYield?: () => boolean;
_buildPlatform: (flush: () => void) => IPlatform;
}

export default class Backburner {
public static Queue = Queue;

public DEBUG = false;

public currentInstance: DeferredActionQueues | null = null;

public options: any;
public options: IBackburnerOptions;

private _onBegin: Function;
private _onEnd: Function;
Expand All @@ -67,22 +83,16 @@ export default class Backburner {

private _timerTimeoutId: number | null = null;
private _timers: any[];
private _platform: {
setTimeout(fn: () => void, ms: number): number;
clearTimeout(id: number): void;
next(fn: () => void): number;
clearNext(fn): void;
now(): number;
};
private _platform: IPlatform;

private _boundRunExpiredTimers: () => void;

private _autorun: number | null = null;
private _boundAutorunEnd: () => void;

constructor(queueNames: string[], options: any = {} ) {
constructor(queueNames: string[], options: IBackburnerOptions) {
this.queueNames = queueNames;
this.options = options;
this.options = options || {};
if (!this.options.defaultQueue) {
this.options.defaultQueue = queueNames[0];
}
Expand Down Expand Up @@ -111,41 +121,8 @@ export default class Backburner {
this.end(true /* fromAutorun */);
};

let _platform = this.options._platform || {};
let platform = Object.create(null);
platform.setTimeout = _platform.setTimeout || ((fn, ms) => setTimeout(fn, ms));
platform.clearTimeout = _platform.clearTimeout || ((id) => clearTimeout(id));
platform.clearNext = _platform.clearNext || platform.clearTimeout;
platform.now = _platform.now || (() => Date.now());

// TODO: do we need this?
if (_platform.next) {
platform.next = _platform.next;

// supports Node and "evergreen" browsers
} else if (typeof Promise === 'function') {
const autorunPromise = Promise.resolve();
platform.next = () => autorunPromise.then(this._boundAutorunEnd);

// supports IE11
} else if (typeof MutationObserver === 'function') {
let iterations = 0;
let observer = new MutationObserver(this._boundAutorunEnd);
let node = document.createTextNode('');
observer.observe(node, { characterData: true });

platform.next = () => {
iterations = ++iterations % 2;
node.data = '' + iterations;
return iterations;
};

// do we need this fallback?
} else {
platform.next = () => SET_TIMEOUT(this._boundAutorunEnd, 0);
}

this._platform = platform;
let builder = this.options._buildPlatform || buildDefaultPlatform;
this._platform = builder(this._boundAutorunEnd);
}

/*
Expand Down Expand Up @@ -797,6 +774,6 @@ export default class Backburner {

private _scheduleAutorun() {
const next = this._platform.next;
this._autorun = next(this._boundAutorunEnd);
this._autorun = next();
}
}

0 comments on commit 79b3956

Please sign in to comment.