Commit 76480dc
committed
Put the Svelte binding on ConnectionManager; add reconnect() for token swaps
# Description of Changes
A Svelte app that lets a user start anonymous and then sign in (e.g. via Google)
has no clean way to apply the new token. The Svelte provider holds its
connection in a module-level singleton that's reused across remounts, so even a
`{#key}` swap keeps the old token, and there's no reconnect path — the only
thing that actually works is a hard `window.location.reload()`, which throws
away all client state and flickers the UI on every sign-in / sign-out. The
Svelte binding also lacks the auto-reconnect that the React and Solid bindings
already have, so a dropped socket just stays dropped.
This rebuilds `createSpacetimeDBProvider` on top of the shared
`ConnectionManager` — the same pool React and Solid already use — and surfaces a
`reconnect(builder)` method on the context value, closing both gaps at once:
- The connection is owned by `ConnectionManager`, keyed by uri + database name.
Its reference counting and deferred release absorb rapid mount/unmount cycles
(HMR, `{#key}`), replacing the bespoke singleton, and the binding gets the
manager's exponential-backoff auto-reconnect on unexpected drops for free.
- `ConnectionState` gains `reconnect(builder)` (backed by the new
`ConnectionManager.rebuild`): tears down the current connection and reconnects
with a fresh builder carrying a new token, no page reload. `useTable` /
`useReducer` already react to the connection store, so subscriptions re-bind
to the new connection automatically.
- `getConnection` is now non-generic (`DbConnectionImpl<any> | null`), matching
the React and Solid `ConnectionState`, so the bindings line up.
`useTable`, `useReducer` and `useSpacetimeDB` are unchanged.
Motivated by #5373; hangs off the reconnect tracking issue #1936.
# API and ABI breaking changes
Minor, TypeScript-only: `ConnectionState.getConnection` is no longer generic —
it returns `DbConnectionImpl<any> | null`, as the React and Solid bindings
already do. Call sites that wrote `getConnection<MyConn>()` lose the type
argument; `getConnection()` is unchanged. The provider's return type stays
`Writable<ConnectionState>`.
# Expected complexity level and risk
2. Low. The Svelte provider is now a thin adapter: it mirrors the manager's
store into a Svelte store and wires up `getConnection` / `reconnect` / `retain`
/ `release`. The connection-lifecycle logic that used to be bespoke here — the
module-level singleton, deferred cleanup, and reconnect — now lives in the
well-tested, shared `ConnectionManager` that the React and Solid bindings
already depend on.
# Testing
- [x] `tsc -p tsconfig.build.json` typechecks; `build:js` emits the Svelte
ESM/CJS + browser bundles cleanly; eslint + prettier clean.
- [x] Full vitest suite green (225 tests), including the new `rebuild()` unit
tests from the previous commit.
- [ ] Reviewer: this package has no Svelte component-test harness today (the
pre-existing Svelte binding had none), so the provider wiring is covered
indirectly through the `ConnectionManager` unit tests. Worth a manual smoke
test of an anonymous -> signed-in `reconnect(builder)` flow, and a call on
whether to add @testing-library/svelte coverage here.1 parent 46bdbb4 commit 76480dc
2 files changed
Lines changed: 61 additions & 81 deletions
Lines changed: 43 additions & 67 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
3 | 3 | | |
4 | 4 | | |
5 | 5 | | |
6 | | - | |
7 | | - | |
8 | 6 | | |
9 | 7 | | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
10 | 12 | | |
11 | 13 | | |
12 | 14 | | |
13 | 15 | | |
14 | 16 | | |
15 | | - | |
16 | | - | |
17 | | - | |
| 17 | + | |
| 18 | + | |
| 19 | + | |
| 20 | + | |
| 21 | + | |
| 22 | + | |
| 23 | + | |
| 24 | + | |
| 25 | + | |
| 26 | + | |
| 27 | + | |
| 28 | + | |
| 29 | + | |
| 30 | + | |
18 | 31 | | |
19 | 32 | | |
20 | 33 | | |
21 | 34 | | |
22 | 35 | | |
23 | | - | |
| 36 | + | |
| 37 | + | |
| 38 | + | |
| 39 | + | |
24 | 40 | | |
25 | | - | |
| 41 | + | |
26 | 42 | | |
27 | 43 | | |
28 | 44 | | |
29 | 45 | | |
30 | 46 | | |
31 | | - | |
32 | | - | |
33 | | - | |
34 | | - | |
35 | | - | |
36 | | - | |
37 | | - | |
38 | | - | |
39 | | - | |
40 | | - | |
41 | | - | |
42 | | - | |
43 | | - | |
44 | | - | |
45 | | - | |
46 | | - | |
47 | | - | |
48 | | - | |
49 | | - | |
50 | | - | |
51 | 47 | | |
52 | 48 | | |
53 | | - | |
54 | | - | |
55 | | - | |
56 | | - | |
57 | | - | |
58 | | - | |
59 | | - | |
| 49 | + | |
| 50 | + | |
| 51 | + | |
| 52 | + | |
60 | 53 | | |
61 | 54 | | |
62 | | - | |
63 | | - | |
64 | | - | |
65 | | - | |
66 | | - | |
67 | | - | |
68 | | - | |
69 | | - | |
70 | | - | |
71 | | - | |
72 | | - | |
73 | | - | |
74 | | - | |
75 | | - | |
76 | | - | |
77 | | - | |
78 | | - | |
79 | | - | |
80 | | - | |
81 | | - | |
82 | | - | |
83 | | - | |
84 | | - | |
| 55 | + | |
| 56 | + | |
| 57 | + | |
| 58 | + | |
| 59 | + | |
85 | 60 | | |
86 | | - | |
| 61 | + | |
| 62 | + | |
| 63 | + | |
| 64 | + | |
| 65 | + | |
| 66 | + | |
| 67 | + | |
| 68 | + | |
87 | 69 | | |
88 | 70 | | |
89 | | - | |
90 | | - | |
91 | | - | |
92 | | - | |
93 | | - | |
94 | | - | |
95 | | - | |
96 | | - | |
97 | | - | |
| 71 | + | |
| 72 | + | |
98 | 73 | | |
99 | 74 | | |
| 75 | + | |
100 | 76 | | |
101 | 77 | | |
Lines changed: 18 additions & 14 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1 | | - | |
2 | | - | |
3 | | - | |
4 | | - | |
5 | | - | |
6 | | - | |
7 | | - | |
8 | | - | |
9 | | - | |
10 | | - | |
11 | | - | |
12 | | - | |
13 | | - | |
14 | | - | |
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
15 | 6 | | |
16 | 7 | | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
| 15 | + | |
| 16 | + | |
| 17 | + | |
| 18 | + | |
| 19 | + | |
| 20 | + | |
0 commit comments