every class calls super.dispose()

makes sure that things don't get disposed twice, which seems to be a common source of issues
This commit is contained in:
Yotam Mann 2019-07-10 23:35:29 -04:00
parent fde2ff65eb
commit 60a63cacf8
13 changed files with 26 additions and 5 deletions

View file

@ -91,6 +91,7 @@ export class Volume extends ToneAudioNode<VolumeOptions> {
* clean up
*/
dispose(): this {
super.dispose();
this.input.dispose();
this.volume.dispose();
return this;

View file

@ -32,10 +32,19 @@ export abstract class Tone {
*/
protected abstract name: string;
/**
* If the instance was already disposed
*/
private _wasDisposed = false;
/**
* disconnect and dispose.
*/
abstract dispose(): this;
dispose(): this {
this.assert(!this._wasDisposed, "Instance has already been disposed");
this._wasDisposed = true;
return this;
}
/**
* Takes a partial options an returns the completed options by filling in the defaults

View file

@ -272,6 +272,7 @@ extends ToneWithContext<ClockOptions> implements Emitter<ClockEvent> {
* Clean up
*/
dispose(): this {
super.dispose();
this.context.off("tick", this._boundLoop);
this._tickSource.dispose();
this._state.dispose();

View file

@ -337,6 +337,7 @@ export class TickSource<Type extends "bpm" | "hertz"> extends ToneWithContext<Ti
* Clean up
*/
dispose(): this {
super.dispose();
this._state.dispose();
this._tickOffset.dispose();
this.frequency.dispose();

View file

@ -372,6 +372,7 @@ export class Context extends Emitter<"statechange" | "tick"> implements BaseAudi
* Clean up. Also closes the audio context.
*/
dispose(): this {
super.dispose();
this._ticker.dispose();
this._timeouts.dispose();
Object.keys(this._constants).map(val => this._constants[val].disconnect());

View file

@ -183,15 +183,15 @@ implements AbstractParam<Type> {
///////////////////////////////////////////////////////////////////////////
setValueAtTime(value: UnitMap[Type], time: Time): this {
time = this.toSeconds(time);
const computedTime = this.toSeconds(time);
const numericValue = this._fromType(value);
this._events.add({
time,
time : computedTime,
type: "setValue",
value: numericValue,
});
this.log("setValue", value, time);
this._param.setValueAtTime(numericValue, time);
this.log("setValue", value, computedTime);
this._param.setValueAtTime(numericValue, computedTime);
return this;
}
@ -389,6 +389,7 @@ implements AbstractParam<Type> {
}
dispose(): this {
super.dispose();
this._events.dispose();
return this;
}

View file

@ -154,6 +154,7 @@ export class ToneAudioBuffer extends Tone {
* clean up
*/
dispose(): this {
super.dispose();
this._buffer = undefined;
return this;
}

View file

@ -249,6 +249,7 @@ extends ToneWithContext<Options> {
* Dispose and disconnect
*/
dispose(): this {
super.dispose();
if (isDefined(this.input)) {
if (isArray(this.input)) {
this.input.forEach(input => {

View file

@ -95,6 +95,7 @@ export class Draw extends ToneWithContext<ToneWithContextOptions> {
}
dispose(): this {
super.dispose();
this._events.dispose();
cancelAnimationFrame(this._animationFrame);
return this;

View file

@ -117,6 +117,7 @@ export class Emitter<EventType extends string = string> extends Tone {
* Clean up
*/
dispose(): this {
super.dispose();
this._events = undefined;
return this;
}

View file

@ -337,6 +337,7 @@ export class IntervalTimeline extends Tone {
* Clean up
*/
dispose(): this {
super.dispose();
if (this._root !== null) {
this._root.traverse(node => node.dispose());
}

View file

@ -367,6 +367,7 @@ export class Timeline<GenericEvent extends TimelineEvent> extends Tone {
* Clean up.
*/
dispose(): this {
super.dispose();
this._timeline = [];
return this;
}

View file

@ -288,6 +288,7 @@ export abstract class Source<Options extends SourceOptions> extends ToneAudioNod
* Clean up.
*/
dispose(): this {
super.dispose();
this.unsync();
this._volume.dispose();
this._state.dispose();