Tone.js/Tone/core/clock/TransportEvent.ts

91 lines
1.7 KiB
TypeScript
Raw Normal View History

import { Seconds, Ticks } from "../type/Units";
2019-05-22 03:37:03 +00:00
import { noOp } from "../util/Interface";
2019-07-11 13:57:06 +00:00
type Transport = import("../clock/Transport").Transport;
2019-05-22 03:37:03 +00:00
export interface TransportEventOptions {
callback: (time: number) => void;
once: boolean;
time: Ticks;
}
/**
2019-09-15 18:39:55 +00:00
* TransportEvent is an internal class used by [[Transport]]
2019-05-22 03:37:03 +00:00
* to schedule events. Do no invoke this class directly, it is
* handled from within Tone.Transport.
*/
export class TransportEvent {
/**
* Reference to the Transport that created it
*/
protected transport: Transport;
/**
* The unique id of the event
*/
id: number = TransportEvent._eventId++;
/**
* The time the event starts
*/
time: Ticks;
/**
* The callback to invoke
*/
private callback?: (time: Seconds) => void;
/**
* If the event should be removed after being invoked.
*/
private _once: boolean;
2019-08-21 20:59:01 +00:00
/**
2019-10-23 03:04:52 +00:00
* @param transport The transport object which the event belongs to
2019-08-21 20:59:01 +00:00
*/
2019-05-22 03:37:03 +00:00
constructor(transport: Transport, opts: Partial<TransportEventOptions>) {
const options: TransportEventOptions = Object.assign(TransportEvent.getDefaults(), opts);
this.transport = transport;
this.callback = options.callback;
this._once = options.once;
this.time = options.time;
}
static getDefaults(): TransportEventOptions {
return {
callback: noOp,
once: false,
time: 0,
};
}
/**
* Current ID counter
*/
2019-11-17 18:09:19 +00:00
private static _eventId = 0;
2019-05-22 03:37:03 +00:00
/**
* Invoke the event callback.
* @param time The AudioContext time in seconds of the event
*/
invoke(time: Seconds): void {
if (this.callback) {
this.callback(time);
if (this._once) {
this.transport.clear(this.id);
}
}
}
/**
* Clean up
*/
dispose(): this {
this.callback = undefined;
return this;
}
}