mirror of
https://github.com/Tonejs/Tone.js
synced 2024-11-16 00:27:58 +00:00
87 lines
1.7 KiB
TypeScript
87 lines
1.7 KiB
TypeScript
import { noOp } from "../util/Interface";
|
|
|
|
type Transport = import("../clock/Transport").Transport;
|
|
|
|
export interface TransportEventOptions {
|
|
callback: (time: number) => void;
|
|
once: boolean;
|
|
time: Ticks;
|
|
}
|
|
|
|
/**
|
|
* TransportEvent is an internal class used by (Tone.Transport)[Transport]
|
|
* to schedule events. Do no invoke this class directly, it is
|
|
* handled from within Tone.Transport.
|
|
* @param Transport The transport object which the event belongs to
|
|
*/
|
|
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;
|
|
|
|
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
|
|
*/
|
|
private static _eventId: number = 0;
|
|
|
|
/**
|
|
* 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;
|
|
}
|
|
}
|