import {Property} from '@avocado/core'; import {compose, EventEmitter} from '@flecks/core'; import Ticker from './ticker'; export default function TimedIndexMixin(indexName = 'index') { return (Superclass) => { const $$indexCount = `${indexName}Count`; const $$indexRate = `${indexName}Rate`; const decorate = compose( EventEmitter, Property('index', { default: 0, track: true, }), Property('isTicking', { default: true, }), Property($$indexCount, { default: 0, }), Property($$indexRate, { default: 0.1, get: function get() { return this.ticker.frequency; }, set: function set(frequency) { this.ticker.frequency = frequency; }, }), ); return class TimedIndex extends decorate(Superclass) { constructor(...args) { super(...args); this.ticker = new Ticker(0.1); this.ticker.on('tick', this.onTick, this); } destroy() { if (super.destroy) { super.destroy(); } this.ticker.off('tick', this.onTick); } get index() { return super.index; } set index(index) { super.index = 0 === this[$$indexCount] ? 0 : index % this[$$indexCount]; } onTick() { const index = this.index + 1; if (index >= this[$$indexCount]) { this.emit('rollingOver'); } this.index = index; } reset() { this.ticker.reset(); } start() { this.isTicking = true; } stop() { this.isTicking = false; } tick(elapsed) { if (this.isTicking) { this.ticker.tick(elapsed); } } }; }; }