Tonejs / Tone.js

A Web Audio framework for making interactive music in the browser.
https://tonejs.github.io
MIT License
13.39k stars 976 forks source link

import * as Tone from 'tone' produces warnings due to module side effects #1102

Open benallfree opened 2 years ago

benallfree commented 2 years ago

In looking at https://github.com/Tonejs/Tone.js/blob/dev/Tone/index.ts#L33, I see this line:

export const Transport = getContext().transport;

If this executes before Tone.start(), it will produce the The AudioContext was not allowed to start. It must be resumed (or created) after a user gesture on the page. warning. Same note with Master, Destination, Listener and Draw.

Yet, there is no way to import without executing these inline getContext() calls.

Everything seems to work anyway, but simply importing Tone produces warnings.

I see getters for most of these, is there a reason those aren't required?

import { getTransport } from 'tone'
tambien commented 2 years ago

That behavior is in there for backwards compatibility since the singleton Transport object has been like that since the beginning. The intention has always been to move to getTransport(), but the question of when is a little tricky since it would likely break many projects, so it would require a good deal of messaging and some warnings before that happens. But it's also tricky to detect when people use the singleton Transport vs the singleton getter (getTransport()).

benallfree commented 2 years ago

What about starting a new branch with side effects and the singleton removed?

Maybe npm i tone.js@pure or something?

oskarrough commented 11 months ago

Would it be enough to remove the inline Transport call on https://github.com/Tonejs/Tone.js/blob/dev/Tone/index.ts#L33 and release a new major version of Tone.js?