Skip to content

Instantly share code, notes, and snippets.

@schmidtsonian
Created April 12, 2019 14:34
Show Gist options
  • Save schmidtsonian/99719fa592b99ad91e37cbc24c487fd2 to your computer and use it in GitHub Desktop.
Save schmidtsonian/99719fa592b99ad91e37cbc24c487fd2 to your computer and use it in GitHub Desktop.
JavaScript Singleton watcher utility
// Events class is here: https://gist.github.com/schmidtsonian/8a023034a2f5600b455afd2e2aed7019
import Events from './events';
/**
* Watcher
*
* @class Watcher
*/
export default class Watcher {
/**
* Creates an instance of Watcher.
*
* @memberOf Watcher
*/
constructor() {
if (!Watcher.instance) {
Watcher.instance = this;
}
this.events = new Events();
return Watcher.instance;
}
_bindings() {
const that = this;
function onEnterFrame() {
that._check();
requestAnimationFrame(onEnterFrame);
}
requestAnimationFrame(onEnterFrame);
}
_check() {
this.events.trigger('watcher');
return this;
}
init() {
this._bindings();
return this;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment