Skip to content

Commit

Permalink
convert TaskLoop to TypeScript (video-dev#2291)
Browse files Browse the repository at this point in the history
  • Loading branch information
tjenkinson authored and John Bartos committed Jul 1, 2019
1 parent b13c460 commit db011b3
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 19 deletions.
4 changes: 2 additions & 2 deletions src/event-handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,8 @@ class EventHandler {
this.onHandlerDestroyed();
}

onHandlerDestroying () {}
onHandlerDestroyed () {}
protected onHandlerDestroying () {}
protected onHandlerDestroyed () {}

isEventHandler () {
return typeof this.handledEvents === 'object' && this.handledEvents.length && typeof this.onEvent === 'function';
Expand Down
36 changes: 19 additions & 17 deletions src/task-loop.js → src/task-loop.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import EventHandler from './event-handler';
import Hls from './hls';

/**
* Sub-class specialization of EventHandler base class.
Expand Down Expand Up @@ -30,19 +31,20 @@ import EventHandler from './event-handler';
*/

export default class TaskLoop extends EventHandler {
constructor (hls, ...events) {
super(hls, ...events);
private readonly _boundTick: () => void;
private _tickTimer: number | null = null;
private _tickInterval: number | null = null;
private _tickCallCount = 0;

this._tickInterval = null;
this._tickTimer = null;
this._tickCallCount = 0;
constructor (hls: Hls, ...events: string[]) {
super(hls, ...events);
this._boundTick = this.tick.bind(this);
}

/**
* @override
*/
onHandlerDestroying () {
protected onHandlerDestroying () {
// clear all timers before unregistering from event bus
this.clearNextTick();
this.clearInterval();
Expand All @@ -51,24 +53,24 @@ export default class TaskLoop extends EventHandler {
/**
* @returns {boolean}
*/
hasInterval () {
public hasInterval (): boolean {
return !!this._tickInterval;
}

/**
* @returns {boolean}
*/
hasNextTick () {
public hasNextTick (): boolean {
return !!this._tickTimer;
}

/**
* @param {number} millis Interval time (ms)
* @returns {boolean} True when interval has been scheduled, false when already scheduled (no effect)
*/
setInterval (millis) {
public setInterval (millis: number): boolean {
if (!this._tickInterval) {
this._tickInterval = setInterval(this._boundTick, millis);
this._tickInterval = self.setInterval(this._boundTick, millis);
return true;
}
return false;
Expand All @@ -77,9 +79,9 @@ export default class TaskLoop extends EventHandler {
/**
* @returns {boolean} True when interval was cleared, false when none was set (no effect)
*/
clearInterval () {
public clearInterval (): boolean {
if (this._tickInterval) {
clearInterval(this._tickInterval);
self.clearInterval(this._tickInterval);
this._tickInterval = null;
return true;
}
Expand All @@ -89,9 +91,9 @@ export default class TaskLoop extends EventHandler {
/**
* @returns {boolean} True when timeout was cleared, false when none was set (no effect)
*/
clearNextTick () {
public clearNextTick (): boolean {
if (this._tickTimer) {
clearTimeout(this._tickTimer);
self.clearTimeout(this._tickTimer);
this._tickTimer = null;
return true;
}
Expand All @@ -103,7 +105,7 @@ export default class TaskLoop extends EventHandler {
* or in the next one (via setTimeout(,0)) in case it has already been called
* in this tick (in case this is a re-entrant call).
*/
tick () {
public tick (): void {
this._tickCallCount++;
if (this._tickCallCount === 1) {
this.doTick();
Expand All @@ -112,7 +114,7 @@ export default class TaskLoop extends EventHandler {
if (this._tickCallCount > 1) {
// make sure only one timer exists at any time at max
this.clearNextTick();
this._tickTimer = setTimeout(this._boundTick, 0);
this._tickTimer = self.setTimeout(this._boundTick, 0);
}
this._tickCallCount = 0;
}
Expand All @@ -122,5 +124,5 @@ export default class TaskLoop extends EventHandler {
* For subclass to implement task logic
* @abstract
*/
doTick () {}
protected doTick (): void {}
}

0 comments on commit db011b3

Please sign in to comment.