Skip to content

Commit

Permalink
Move to cross-tab client to fix running in multiple browser tabs
Browse files Browse the repository at this point in the history
  • Loading branch information
ai committed Sep 8, 2024
1 parent b160053 commit 26ce5f6
Showing 1 changed file with 5 additions and 6 deletions.
11 changes: 5 additions & 6 deletions core/client.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import type { ClientOptions } from '@logux/client'
import { Client } from '@logux/client'
import { type ClientOptions, CrossTabClient } from '@logux/client'
import { TestPair, TestTime } from '@logux/core'
import { SUBPROTOCOL } from '@slowreader/api'
import { atom } from 'nanostores'
Expand All @@ -20,16 +19,16 @@ function getServer(): ClientOptions['server'] {
return testTime ? new TestPair().left : 'ws://localhost:31337/'
}

let prevClient: Client | undefined
let $client = atom<Client | undefined>()
let prevClient: CrossTabClient | undefined
let $client = atom<CrossTabClient | undefined>()
export const client = readonlyExport($client)

onEnvironment(({ logStoreCreator }) => {
let unbindUserId = computeFrom($client, [userId], user => {
prevClient?.destroy()

if (user) {
let logux = new Client({
let logux = new CrossTabClient({
prefix: 'slowreader',
server: getServer(),
store: logStoreCreator(),
Expand All @@ -50,7 +49,7 @@ onEnvironment(({ logStoreCreator }) => {
}
})

export function getClient(): Client {
export function getClient(): CrossTabClient {
let logux = client.get()
if (!logux) {
throw new SlowReaderError('NoClient')
Expand Down

0 comments on commit 26ce5f6

Please sign in to comment.