phosphorjs / phosphor

The PhosphorJS Library
BSD 3-Clause "New" or "Revised" License
1.04k stars 166 forks source link

Make Phosphor safe to load on the page more than once #443

Open sccolbert opened 4 years ago

sccolbert commented 4 years ago

Forcing Phosphor to be a singleton on the page is "not nice" for projects which consume Phosphor. It means new functionality in Phosphor is not available for the users of those downstream projects until those projects update their dependencies. For Phosphor 2.0 (which should hopefully be the last major version of Phosphor) I think we can do better.

One idea that I have is to leverage Symbol.for('@phosphor/widgets@2.x') and tag the Widget prototype with that Symbol for each minor release. Since the Symbol is guaranteed to be global on the page, it would let duplicate prototypes function just fine, because we could guarantee they have the needed API. Instead of using the instanceof operator, we could have some convenience functions for checking the presence of the symbols.

This idea may have holes, but it's a good starting point I think.

CC @afshin @blink1073 @jasongrout

jasongrout commented 4 years ago

So this parallels the ideas behind tokens, but now uses Symbols for their global nature (and since they are available now)?

blink1073 commented 4 years ago

We had also discussed using Symbols for tokens themselves so you could have multiple providers.

jasongrout commented 4 years ago

We had also discussed using Symbols for tokens themselves so you could have multiple providers.

That's right. Versioned interfaces.

sccolbert commented 4 years ago

So this parallels the ideas behind tokens, but now uses Symbols for their global nature (and since they are available now)?

Somewhat. I think it's more subtle than first glance. TypeScript may still not like it, for example.

We had also discussed using Symbols for tokens themselves so you could have multiple providers.

Yep.