tldraw/packages/state
David Sheldrick b133c59391
Use a global singleton for tlstate (#2322)
One minor issue with signia is that it uses global state for
bookkeeping, so it is potentially disastrous if there is more than one
version of it included in a bundle.

To prevent that being an issue before we had a warning that would
trigger if signia detects multiple initializations.

> Multiple versions of @tldraw/state detected. This will cause
unexpected behavior. Please add "resolutions" (yarn/pnpm) or "overrides"
(npm) in your package.json to ensure only one version of @tldraw/state
is loaded.

Alas I think this warning triggers too often in development
environments, e.g. during HMR or janky bundlers.


Something that can prevent the need for this particular warning is
having a global singleton version of signia that we only instantiate
once, and then re-use that one on subsequent module initializations. We
didn't do this before because it has a few downsides:

- breaks HMR if you are working on signia itself, since updated modules
won't be used and you'll need to do a full refresh.
- introduces the possibility of breakage if we remove or even add APIs
to signia. We can't rely on having the latest version of signia be the
first to instantiate, and we can't allow later instantiations to take
precedence since atoms n stuff may have already been created with the
prior version. To mitigate this I've introduced a `apiVersion` const
that we can increment when we make any kind of additions or removals. If
there is a mismatch between the `apiVersion` in the global singleton vs
the currently-initializing module, then it throws.

Ultimately i think the pros outweigh the cons here, i.e. far fewer
people will see and have to deal with the error message shown above, and
fewer people should encounter a situation where the editor appears to
load but nothing changes when you interact with it.


### Change Type

- [x] `patch` — Bug fix

[^1]: publishes a `patch` release, for devDependencies use `internal`
[^2]: will not publish a new version

### Release Notes

- Make a global singleton for tlstate.
2023-12-14 13:35:34 +00:00
..
api Use a global singleton for tlstate (#2322) 2023-12-14 13:35:34 +00:00
src Use a global singleton for tlstate (#2322) 2023-12-14 13:35:34 +00:00
api-extractor.json Incorporate signia as @tldraw/state (#1620) 2023-06-20 13:31:26 +00:00
api-report.md Use a global singleton for tlstate (#2322) 2023-12-14 13:35:34 +00:00
CHANGELOG.md Update CHANGELOG.md [skip ci] 2023-12-12 14:50:43 +00:00
docs-ordering.json Incorporate signia as @tldraw/state (#1620) 2023-06-20 13:31:26 +00:00
package.json Update CHANGELOG.md [skip ci] 2023-12-12 14:50:43 +00:00
README.md Incorporate signia as @tldraw/state (#1620) 2023-06-20 13:31:26 +00:00
tsconfig.json Incorporate signia as @tldraw/state (#1620) 2023-06-20 13:31:26 +00:00

@tldraw/state

...