Tone.js icon indicating copy to clipboard operation
Tone.js copied to clipboard

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

Open benallfree opened this issue 3 years ago • 3 comments

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'

benallfree avatar Jul 20 '22 19:07 benallfree

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()).

tambien avatar Sep 03 '22 13:09 tambien

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

Maybe npm i tone.js@pure or something?

benallfree avatar Sep 05 '22 15:09 benallfree

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?

oskarrough avatar Sep 29 '23 08:09 oskarrough