mirror of
https://github.com/Tonejs/Tone.js
synced 2024-11-16 00:27:58 +00:00
handling case where window is not defined
either because it's loaded in node.js or because of the context that the script is loaded in
This commit is contained in:
parent
cd73792659
commit
7a099b2310
2 changed files with 39 additions and 14 deletions
|
@ -1,20 +1,26 @@
|
|||
import { version } from "../version";
|
||||
import { hasAudioContext, theWindow } from "./context/AudioContext";
|
||||
import { Context } from "./context/Context";
|
||||
|
||||
/**
|
||||
* This dummy context is used to avoid throwing immediate errors when importing in Node.js
|
||||
*/
|
||||
const dummyContext: Context = {
|
||||
destination: {},
|
||||
transport: {},
|
||||
} as Context;
|
||||
|
||||
/**
|
||||
* The global audio context which is getable and assignable through
|
||||
* getContext and setContext
|
||||
*/
|
||||
let globalContext: Context;
|
||||
|
||||
// @ts-ignore
|
||||
globalContext = window.TONE_CONTEXT;
|
||||
let globalContext: Context = dummyContext;
|
||||
|
||||
/**
|
||||
* Returns the default system-wide AudioContext
|
||||
*/
|
||||
export function getContext(): Context {
|
||||
if (!globalContext) {
|
||||
if (!globalContext && hasAudioContext) {
|
||||
setContext(new Context());
|
||||
}
|
||||
return globalContext;
|
||||
|
@ -26,8 +32,6 @@ export function getContext(): Context {
|
|||
export function setContext(context: Context): void {
|
||||
globalContext = context;
|
||||
context.initialize();
|
||||
// @ts-ignore
|
||||
window.TONE_CONTEXT = context;
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -46,9 +50,8 @@ export function start(): Promise <void> {
|
|||
/**
|
||||
* Log Tone.js + version in the console.
|
||||
*/
|
||||
if (!this.TONE_SILENCE_LOGGING) {
|
||||
if (theWindow && !theWindow.TONE_SILENCE_LOGGING) {
|
||||
let prefix = "v";
|
||||
// @ts-ignore
|
||||
if (version === "dev") {
|
||||
prefix = "";
|
||||
}
|
||||
|
|
|
@ -1,17 +1,38 @@
|
|||
/**
|
||||
* Interface for things that Tone.js adds to the window
|
||||
*/
|
||||
interface ToneWindow extends Window {
|
||||
TONE_AUDIO_CONTEXT?: BaseAudioContext;
|
||||
TONE_SILENCE_LOGGING?: boolean;
|
||||
}
|
||||
|
||||
/**
|
||||
* A reference to the window object
|
||||
*/
|
||||
export const theWindow: ToneWindow | null = typeof self === "object" ? self : null;
|
||||
|
||||
/**
|
||||
* If the browser has a window object which has an AudioContext
|
||||
*/
|
||||
export const hasAudioContext = theWindow && theWindow.hasOwnProperty("AudioContext");
|
||||
|
||||
/**
|
||||
* The global audio context which is getable and assignable through
|
||||
* getAudioContext and setAudioContext
|
||||
*/
|
||||
let globalContext: BaseAudioContext;
|
||||
|
||||
// @ts-ignore
|
||||
globalContext = window.TONE_AUDIO_CONTEXT;
|
||||
// if it was created already, use that one
|
||||
// this enables multiple versions of Tone.js to run on the same page.
|
||||
if (theWindow && theWindow.TONE_AUDIO_CONTEXT) {
|
||||
globalContext = theWindow.TONE_AUDIO_CONTEXT;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the default system-wide AudioContext
|
||||
*/
|
||||
export function getAudioContext(): BaseAudioContext {
|
||||
if (!globalContext) {
|
||||
if (!globalContext && hasAudioContext) {
|
||||
setAudioContext(new AudioContext());
|
||||
}
|
||||
return globalContext;
|
||||
|
@ -22,6 +43,7 @@ export function getAudioContext(): BaseAudioContext {
|
|||
*/
|
||||
export function setAudioContext(context: BaseAudioContext): void {
|
||||
globalContext = context;
|
||||
// @ts-ignore
|
||||
window.TONE_AUDIO_CONTEXT = globalContext;
|
||||
if (theWindow) {
|
||||
theWindow.TONE_AUDIO_CONTEXT = globalContext;
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue