Created
April 12, 2019 14:34
-
-
Save schmidtsonian/99719fa592b99ad91e37cbc24c487fd2 to your computer and use it in GitHub Desktop.
JavaScript Singleton watcher utility
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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