Tone.js/Tone/core/context/ContextInitialization.ts

42 lines
1.1 KiB
TypeScript
Raw Permalink Normal View History

//-------------------------------------
2019-07-11 13:57:06 +00:00
// INITIALIZING NEW CONTEXT
//-------------------------------------
import type { Context } from "./Context.js";
2019-07-11 13:57:06 +00:00
/**
* Array of callbacks to invoke when a new context is created
*/
const notifyNewContext: Array<(ctx: Context) => void> = [];
/**
* Used internally to setup a new Context
*/
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.
*/
2019-07-11 13:57:06 +00:00
export function initializeContext(ctx: Context): void {
// add any additional modules
notifyNewContext.forEach((cb) => cb(ctx));
2019-07-11 13:57:06 +00:00
}
/**
* Array of callbacks to invoke when a new context is closed
*/
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 {
// remove any additional modules
notifyCloseContext.forEach((cb) => cb(ctx));
}