Skip to content

Commit

Permalink
move platform methods extracting from constructor
Browse files Browse the repository at this point in the history
  • Loading branch information
bekzod committed Jan 21, 2018
1 parent d663494 commit 0bc9fd1
Showing 1 changed file with 23 additions and 18 deletions.
41 changes: 23 additions & 18 deletions lib/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,26 @@ function parseArgs() {
return [target, method, args];
}

interface IPlatform {
setTimeout(fn: Function, ms: number): number;
clearTimeout(id: number): void;
next(fn: Function): number;
clearNext(id: any): void;
now(): number;
}

function extracPlatform(_platform: IPlatform = {} as IPlatform): IPlatform {
let platform = Object.create(null) as IPlatform;

platform.setTimeout = _platform.setTimeout || ((fn, ms) => setTimeout(fn, ms));
platform.clearTimeout = _platform.clearTimeout || ((id) => clearTimeout(id));
platform.next = _platform.next || ((fn) => SET_TIMEOUT(fn, 0));
platform.clearNext = _platform.clearNext || platform.clearTimeout;
platform.now = _platform.now || (() => Date.now());

return platform;
}

let UUID = 0;

export default class Backburner {
Expand All @@ -58,6 +78,7 @@ export default class Backburner {
private _onEnd: (currentInstance: DeferredActionQueues, nextInstance: DeferredActionQueues | null) => void;
private queueNames: string[];
private instanceStack: DeferredActionQueues[] = [];
private _timers: any[] = [];
private _debouncees: any[] = [];
private _throttlers: any[] = [];
private _eventCallbacks: {
Expand All @@ -69,14 +90,7 @@ export default class Backburner {
};

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

private _boundRunExpiredTimers: () => void;

Expand All @@ -93,16 +107,7 @@ export default class Backburner {
this._onBegin = this.options.onBegin || noop;
this._onEnd = this.options.onEnd || noop;

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.next = _platform.next || ((fn) => SET_TIMEOUT(fn, 0));
platform.clearNext = _platform.clearNext || platform.clearTimeout;
platform.now = _platform.now || (() => Date.now());

this._platform = platform;
this._platform = extracPlatform(this.options._platform);

this._boundRunExpiredTimers = () => {
this._runExpiredTimers();
Expand Down

0 comments on commit 0bc9fd1

Please sign in to comment.