|
| 1 | +/** |
| 2 | + * Dev: duplicate-copy detection. |
| 3 | + * |
| 4 | + * Two copies of Solid on a page are only a bug when they meet: one copy's |
| 5 | + * code runs inside the other's computation or component and finds no owner |
| 6 | + * or listener of its own (each copy keeps them in its own module state), so |
| 7 | + * it neither reacts nor sees the other's providers. That is the shape of |
| 8 | + * every duplicate report on record: a library's precompiled JSX on copy B |
| 9 | + * reading the app's props or signals from copy A, a runtime that bundled its |
| 10 | + * own solid-js. Copies that never meet (a devtools panel rendering into its |
| 11 | + * own root, an isolated widget) are fine at any version, so nothing is |
| 12 | + * checked when a copy loads. |
| 13 | + * |
| 14 | + * Every copy from 2.0 on shares one global record, `Solid$$copies`. It is a |
| 15 | + * contract between all future versions of Solid, so its shape never changes: |
| 16 | + * |
| 17 | + * - `copies`: each copy adds 1 when it loads. |
| 18 | + * - `running`: while `copies > 1`, each copy names itself here (an object |
| 19 | + * with a `rooted` boolean, compared by identity) around its computations |
| 20 | + * and components, and restores the previous value after. |
| 21 | + * |
| 22 | + * A copy that sees `copies < 2` skips the slot entirely, so a page with one |
| 23 | + * copy pays one property read per check. v1 keeps its own load-time `Solid$$` |
| 24 | + * check, which this never sets, so neither side warns about the other. |
| 25 | + */ |
| 26 | +interface SolidCopy { |
| 27 | + /** This copy has created a root of its own (it renders by itself). */ |
| 28 | + rooted: boolean; |
| 29 | +} |
| 30 | + |
| 31 | +interface SolidCopies { |
| 32 | + copies: number; |
| 33 | + running: SolidCopy | undefined; |
| 34 | +} |
| 35 | + |
| 36 | +declare global { |
| 37 | + var Solid$$copies: SolidCopies | undefined; |
| 38 | +} |
| 39 | + |
| 40 | +const COPY: SolidCopy = { rooted: false }; |
| 41 | +let warned = false; |
| 42 | +let shared: SolidCopies; |
| 43 | + |
| 44 | +if (__DEV__) { |
| 45 | + shared = globalThis.Solid$$copies ||= { copies: 0, running: undefined }; |
| 46 | + shared.copies++; |
| 47 | +} |
| 48 | + |
| 49 | +function warnOnce(detail: string): void { |
| 50 | + if (warned) return; |
| 51 | + warned = true; |
| 52 | + console.warn( |
| 53 | + `You appear to have multiple instances of Solid: ${detail} Make sure every package resolves the same solid-js (e.g. \`resolve.dedupe\` in Vite) and that libraries keep solid-js external.` |
| 54 | + ); |
| 55 | +} |
| 56 | + |
| 57 | +export function markCopyRooted(): void { |
| 58 | + COPY.rooted = true; |
| 59 | +} |
| 60 | + |
| 61 | +/** |
| 62 | + * Marks this copy as the one running; returns the slot's previous value. A |
| 63 | + * nested run of the same copy (most runs inside a flush) only reads the slot. |
| 64 | + */ |
| 65 | +export function enterCopy(): SolidCopy | undefined { |
| 66 | + if (!__DEV__ || shared.copies < 2) return COPY; |
| 67 | + const prev = shared.running; |
| 68 | + if (prev !== COPY) shared.running = COPY; |
| 69 | + return prev; |
| 70 | +} |
| 71 | + |
| 72 | +export function exitCopy(prev: SolidCopy | undefined): void { |
| 73 | + if (prev !== COPY) shared.running = prev; |
| 74 | +} |
| 75 | + |
| 76 | +/** @internal Runs `fn` with this copy marked as the one running. */ |
| 77 | +export function runAsSolidCopy<T>(fn: () => T): T { |
| 78 | + if (!__DEV__ || shared.copies < 2) return fn(); |
| 79 | + const prev = enterCopy(); |
| 80 | + try { |
| 81 | + return fn(); |
| 82 | + } finally { |
| 83 | + exitCopy(prev); |
| 84 | + } |
| 85 | +} |
| 86 | + |
| 87 | +/** @internal An ownerless component or context read: warns if another copy is running. */ |
| 88 | +export function checkForeignOwnerless(): void { |
| 89 | + if (!__DEV__ || shared.copies < 2) return; |
| 90 | + const running = shared.running; |
| 91 | + if (running && running !== COPY) |
| 92 | + warnOnce( |
| 93 | + "a component or useContext() from one copy ran inside a component rendered by another, so it has no owner and will not react or find its providers." |
| 94 | + ); |
| 95 | +} |
| 96 | + |
| 97 | +/** |
| 98 | + * An untracked read: warns if another copy's computation is running, unless |
| 99 | + * both copies render through roots of their own (an app calling into an |
| 100 | + * isolated copy's API, which reads its own state untracked by design). |
| 101 | + */ |
| 102 | +export function checkForeignRead(): void { |
| 103 | + if (!__DEV__ || shared.copies < 2) return; |
| 104 | + const running = shared.running; |
| 105 | + if (running && running !== COPY && !(running.rooted && COPY.rooted)) |
| 106 | + warnOnce( |
| 107 | + "a signal or store from one copy was read inside a computation of another, so the read is not tracked and will not update." |
| 108 | + ); |
| 109 | +} |
0 commit comments