mirror of
https://github.com/Tonejs/Tone.js
synced 2024-11-15 16:17:58 +00:00
tear down created classes on context close
This commit is contained in:
parent
4c4db4b614
commit
7f17209530
4 changed files with 34 additions and 3 deletions
|
@ -1,7 +1,7 @@
|
|||
import { Time, TimeClass } from "../../core/type/Time";
|
||||
import { PlaybackState } from "../../core/util/StateTimeline";
|
||||
import { Signal } from "../../signal/Signal";
|
||||
import { onContextInit } from "../context/ContextInitialization";
|
||||
import { onContextClose, onContextInit } from "../context/ContextInitialization";
|
||||
import { Gain } from "../context/Gain";
|
||||
import { Param } from "../context/Param";
|
||||
import { ToneWithContext, ToneWithContextOptions } from "../context/ToneWithContext";
|
||||
|
@ -695,3 +695,7 @@ Emitter.mixin(Transport);
|
|||
onContextInit(context => {
|
||||
context.transport = new Transport({ context });
|
||||
});
|
||||
|
||||
onContextClose(context => {
|
||||
context.transport.dispose();
|
||||
});
|
||||
|
|
|
@ -5,7 +5,7 @@ import { Omit } from "../util/Interface";
|
|||
import { Timeline } from "../util/Timeline";
|
||||
import { isString } from "../util/TypeCheck";
|
||||
import { getAudioContext } from "./AudioContext";
|
||||
import { initializeContext } from "./ContextInitialization";
|
||||
import { closeContext, initializeContext } from "./ContextInitialization";
|
||||
|
||||
type Transport = import("../clock/Transport").Transport;
|
||||
type Destination = import("./Destination").Destination;
|
||||
|
@ -346,6 +346,9 @@ export class Context extends Emitter<"statechange" | "tick"> implements BaseAudi
|
|||
if (this._context instanceof AudioContext) {
|
||||
await this._context.close();
|
||||
}
|
||||
if (this._initialized) {
|
||||
closeContext(this);
|
||||
}
|
||||
return this;
|
||||
}
|
||||
|
||||
|
|
|
@ -16,7 +16,27 @@ export function onContextInit(cb: (ctx: Context) => void): void {
|
|||
notifyNewContext.push(cb);
|
||||
}
|
||||
|
||||
/**
|
||||
* Invoke any classes which need to also be initialized when a new context is created.
|
||||
*/
|
||||
export function initializeContext(ctx: Context): void {
|
||||
// add any additional modules
|
||||
notifyNewContext.forEach(cb => cb(ctx));
|
||||
}
|
||||
|
||||
/**
|
||||
* Array of callbacks to invoke when a new context is created
|
||||
*/
|
||||
const notifyCloseContext: Array<(ctx: Context) => void> = [];
|
||||
|
||||
/**
|
||||
* Used internally to tear down a Context
|
||||
*/
|
||||
export function onContextClose(cb: (ctx: Context) => void): void {
|
||||
notifyCloseContext.push(cb);
|
||||
}
|
||||
|
||||
export function closeContext(ctx: Context): void {
|
||||
// add any additional modules
|
||||
notifyCloseContext.forEach(cb => cb(ctx));
|
||||
}
|
||||
|
|
|
@ -1,7 +1,7 @@
|
|||
import { Volume } from "../../component/channel/Volume";
|
||||
import { connectSeries } from "../Connect";
|
||||
import { optionsFromArguments } from "../util/Defaults";
|
||||
import { onContextInit } from "./ContextInitialization";
|
||||
import { onContextClose, onContextInit } from "./ContextInitialization";
|
||||
import { Gain } from "./Gain";
|
||||
import { Param } from "./Param";
|
||||
import { ToneAudioNode, ToneAudioNodeOptions } from "./ToneAudioNode";
|
||||
|
@ -115,3 +115,7 @@ export class Destination extends ToneAudioNode<DestinationOptions> {
|
|||
onContextInit(context => {
|
||||
context.destination = new Destination({ context });
|
||||
});
|
||||
|
||||
onContextClose(context => {
|
||||
context.destination.dispose();
|
||||
});
|
||||
|
|
Loading…
Reference in a new issue