From 5d6a194458bf492cbd487f769881e087edeba9b3 Mon Sep 17 00:00:00 2001 From: WofWca Date: Wed, 21 Apr 2021 11:46:28 +0800 Subject: [PATCH] improvement: add `disposeOld` parameter to `setContext` This makes it harder to introduce a resource leak --- Tone/core/Global.ts | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/Tone/core/Global.ts b/Tone/core/Global.ts index e02c0b94..c4e3c471 100644 --- a/Tone/core/Global.ts +++ b/Tone/core/Global.ts @@ -30,9 +30,15 @@ export function getContext(): BaseContext { /** * Set the default audio context + * @param context + * @param disposeOld Pass `true` if you don't need the old context to dispose it. * @category Core */ -export function setContext(context: BaseContext | AnyAudioContext): void { +export function setContext(context: BaseContext | AnyAudioContext, disposeOld = false): void { + if (disposeOld) { + globalContext.dispose(); + } + if (isAudioContext(context)) { globalContext = new Context(context); } else if (isOfflineAudioContext(context)) {