Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- export default class Updater {
- constructor(options) {
- this.options = Object.assign({}, Updater.DefaultOptions, options || {});
- this.callbacks = [];
- this.start = Date.now();
- this.last = Date.now();
- this.interval = setInterval(() => this.update(), this.options.updateRate);
- }
- static install(Vue, options) {
- let instance = Vue.prototype.$updater;
- if (!instance) {
- instance = new Updater(options);
- }
- Vue.prototype.$updater = instance;
- }
- addListener(callback) {
- if (-1 !== this.callbacks.indexOf(callback) && (true !== this.options.mute)) {
- console.warn('Duplicate updater handler detected.');
- }
- if (typeof(callback) !== 'function') {
- throw new Error('Expected argument one to be a function, got ' + typeof(callback));
- }
- this.callbacks.push(callback);
- }
- removeListener(callback) {
- do {
- let index = this.callbacks.indexOf(callback);
- if (-1 !== index) {
- this.callbacks.splice(index, 1);
- }
- } while(-1 !== index);
- }
- update() {
- const now = Date.now();
- const deltaTime = now - this.last;
- const fromStart = now - this.start;
- for (let k in this.callbacks) {
- this.callbacks[k].call(this, deltaTime, fromStart, now);
- }
- this.last = now;
- }
- }
- Updater.DefaultOptions = {
- updateRate: 33
- };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement