()
| 48 | }; |
| 49 | |
| 50 | function SessionPage() { |
| 51 | const { config, srcbooks, session } = useLoaderData() as SessionLoaderDataType; |
| 52 | |
| 53 | // Because we use refs for our state, we need a way to trigger |
| 54 | // component re-renders when the ref state changes. |
| 55 | // |
| 56 | // https://legacy.reactjs.org/docs/hooks-faq.html#is-there-something-like-forceupdate |
| 57 | // |
| 58 | const [, forceComponentRerender] = useReducer((x) => x + 1, 0); |
| 59 | |
| 60 | const channelRef = useRef(SessionChannel.create(session.id)); |
| 61 | const connectedSessionIdRef = useRef<SessionType['id'] | null>(null); |
| 62 | const connectedSessionLanguageRef = useRef<SessionType['language'] | null>(null); |
| 63 | const channel = channelRef.current; |
| 64 | |
| 65 | useEffect(() => { |
| 66 | if (connectedSessionIdRef.current === session.id) { |
| 67 | return; |
| 68 | } |
| 69 | |
| 70 | const oldChannel = channelRef.current; |
| 71 | |
| 72 | // Disconnect from the previously connected session |
| 73 | if (connectedSessionIdRef.current) { |
| 74 | oldChannel.unsubscribe(); |
| 75 | |
| 76 | if (connectedSessionLanguageRef.current === 'typescript') { |
| 77 | oldChannel.push('tsserver:stop', {}); |
| 78 | } |
| 79 | } |
| 80 | |
| 81 | // Reconnect to the new session |
| 82 | channelRef.current = SessionChannel.create(session.id); |
| 83 | connectedSessionIdRef.current = session.id; |
| 84 | connectedSessionLanguageRef.current = session.language; |
| 85 | |
| 86 | const channel = channelRef.current; |
| 87 | |
| 88 | channel.subscribe(); |
| 89 | |
| 90 | if (session.language === 'typescript') { |
| 91 | channel.push('tsserver:start', {}); |
| 92 | } |
| 93 | |
| 94 | forceComponentRerender(); |
| 95 | }, [session.id, session.language, forceComponentRerender]); |
| 96 | |
| 97 | return ( |
| 98 | <CellsProvider cells={session.cells}> |
| 99 | <PackageJsonProvider channel={channel}> |
| 100 | <TsConfigProvider session={session} channel={channel}> |
| 101 | {VITE_SRCBOOK_DEBUG_RENDER_SESSION_AS_READ_ONLY ? ( |
| 102 | <Session readOnly session={session} srcbooks={srcbooks} config={config} /> |
| 103 | ) : ( |
| 104 | <Session session={session} channel={channel} srcbooks={srcbooks} config={config} /> |
| 105 | )} |
| 106 | </TsConfigProvider> |
| 107 | </PackageJsonProvider> |
nothing calls this directly
no test coverage detected