avocado-old/packages/timing/lfo/result.js
2019-04-28 23:45:03 -05:00

74 lines
1.4 KiB
JavaScript

import ModulatedProperty from './modulated-property';
export default class LfoResult {
constructor(object, properties, duration = 0) {
this.duration = duration;
this.elapsed = 0;
this.isRunning = false;
this.object = {};
this.properties = {};
this.start();
this.deferred = {};
this.promise = this.deferred.promise = new Promise((resolve, reject) => {
this.deferred.resolve = resolve;
this.deferred.reject = reject;
});
for (const key in properties) {
this.properties[key] = new ModulatedProperty(object, key, {
frequency: this.duration,
...properties[key]
});
}
}
destroy() {
for (const key in this.properties) {
const property = this.properties[key];
property.destroy();
}
}
property(key) {
return this.properties[key];
}
start() {
this.elapsed = 0;
this.isRunning = true;
}
stop() {
this.isRunning = false;
}
tick(elapsed) {
if (!this.isRunning) {
return;
}
let finished = false;
if (this.duration > 0 && this.duration <= (this.elapsed += elapsed)) {
finished = true;
elapsed = this.elapsed - this.duration;
this.elapsed = this.duration;
}
for (const key in properties) {
properties[key].tick(elapsed);
}
if (finished) {
this.deferred.resolve();
this.stop();
}
}
}