Skip to content

Commit d195c8d

Browse files
committed
feat: use source in presence store
1 parent fbd7658 commit d195c8d

File tree

2 files changed

+27
-22
lines changed

2 files changed

+27
-22
lines changed

packages/core/src/presence/presenceStore.test.ts

Lines changed: 21 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import {afterEach, beforeEach, describe, expect, it, vi} from 'vitest'
44

55
import {getTokenState} from '../auth/authStore'
66
import {getClient} from '../client/clientStore'
7+
import {sourceFor} from '../config/sanityConfig'
78
import {createSanityInstance, type SanityInstance} from '../store/createSanityInstance'
89
import {type SanityUser} from '../users/types'
910
import {getUserState} from '../users/usersStore'
@@ -17,6 +18,7 @@ vi.mock('../users/usersStore')
1718
vi.mock('./bifurTransport')
1819

1920
describe('presenceStore', () => {
21+
const source = sourceFor({projectId: 'test-project', dataset: 'test-dataset'})
2022
let instance: SanityInstance
2123
let mockClient: SanityClient
2224
let mockTokenState: Subject<string | null>
@@ -78,7 +80,7 @@ describe('presenceStore', () => {
7880

7981
describe('getPresence', () => {
8082
it('creates bifur transport with correct parameters', () => {
81-
getPresence(instance, {})
83+
getPresence(instance, {source})
8284

8385
expect(createBifurTransport).toHaveBeenCalledWith({
8486
client: mockClient,
@@ -88,21 +90,21 @@ describe('presenceStore', () => {
8890
})
8991

9092
it('sends rollCall message on initialization', () => {
91-
getPresence(instance, {})
93+
getPresence(instance, {source})
9294

9395
expect(mockDispatchMessage).toHaveBeenCalledWith({type: 'rollCall'})
9496
})
9597

9698
it('returns empty array when no users present', () => {
97-
const source = getPresence(instance, {})
98-
expect(source.getCurrent()).toEqual([])
99+
const stateSource = getPresence(instance, {source})
100+
expect(stateSource.getCurrent()).toEqual([])
99101
})
100102

101103
it('handles state events from other users', async () => {
102-
const source = getPresence(instance, {})
104+
const stateSource = getPresence(instance, {source})
103105

104106
// Subscribe to initialize the store
105-
const unsubscribe = source.subscribe(() => {})
107+
const unsubscribe = stateSource.subscribe(() => {})
106108

107109
// Wait a bit for initialization
108110
await firstValueFrom(of(null).pipe(delay(10)))
@@ -127,7 +129,7 @@ describe('presenceStore', () => {
127129
// Wait for processing
128130
await firstValueFrom(of(null).pipe(delay(20)))
129131

130-
const presence = source.getCurrent()
132+
const presence = stateSource.getCurrent()
131133
expect(presence).toHaveLength(1)
132134
expect(presence[0].sessionId).toBe('other-session')
133135
expect(presence[0].locations).toEqual(locations)
@@ -136,8 +138,8 @@ describe('presenceStore', () => {
136138
})
137139

138140
it('ignores events from own session', async () => {
139-
const source = getPresence(instance, {})
140-
const unsubscribe = source.subscribe(() => {})
141+
const stateSource = getPresence(instance, {source})
142+
const unsubscribe = stateSource.subscribe(() => {})
141143

142144
await firstValueFrom(of(null).pipe(delay(10)))
143145

@@ -151,15 +153,15 @@ describe('presenceStore', () => {
151153

152154
await firstValueFrom(of(null).pipe(delay(20)))
153155

154-
const presence = source.getCurrent()
156+
const presence = stateSource.getCurrent()
155157
expect(presence).toHaveLength(0)
156158

157159
unsubscribe()
158160
})
159161

160162
it('handles disconnect events', async () => {
161-
const source = getPresence(instance, {})
162-
const unsubscribe = source.subscribe(() => {})
163+
const stateSource = getPresence(instance, {source})
164+
const unsubscribe = stateSource.subscribe(() => {})
163165

164166
await firstValueFrom(of(null).pipe(delay(10)))
165167

@@ -173,7 +175,7 @@ describe('presenceStore', () => {
173175
})
174176

175177
await firstValueFrom(of(null).pipe(delay(20)))
176-
expect(source.getCurrent()).toHaveLength(1)
178+
expect(stateSource.getCurrent()).toHaveLength(1)
177179

178180
// Then disconnect them
179181
mockIncomingEvents.next({
@@ -184,14 +186,14 @@ describe('presenceStore', () => {
184186
})
185187

186188
await firstValueFrom(of(null).pipe(delay(20)))
187-
expect(source.getCurrent()).toHaveLength(0)
189+
expect(stateSource.getCurrent()).toHaveLength(0)
188190

189191
unsubscribe()
190192
})
191193

192194
it('fetches user data for present users', async () => {
193-
const source = getPresence(instance, {})
194-
const unsubscribe = source.subscribe(() => {})
195+
const stateSource = getPresence(instance, {source})
196+
const unsubscribe = stateSource.subscribe(() => {})
195197

196198
await firstValueFrom(of(null).pipe(delay(10)))
197199

@@ -222,8 +224,8 @@ describe('presenceStore', () => {
222224
})
223225

224226
it('handles presence events correctly', async () => {
225-
const source = getPresence(instance, {})
226-
const unsubscribe = source.subscribe(() => {})
227+
const stateSource = getPresence(instance, {source})
228+
const unsubscribe = stateSource.subscribe(() => {})
227229

228230
await firstValueFrom(of(null).pipe(delay(10)))
229231

@@ -237,7 +239,7 @@ describe('presenceStore', () => {
237239

238240
await firstValueFrom(of(null).pipe(delay(50)))
239241

240-
const presence = source.getCurrent()
242+
const presence = stateSource.getCurrent()
241243
expect(presence).toHaveLength(1)
242244
expect(presence[0].sessionId).toBe('other-session')
243245

packages/react/src/hooks/presence/usePresence.ts

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import {getPresence, type UserPresence} from '@sanity/sdk'
22
import {useCallback, useMemo, useSyncExternalStore} from 'react'
33

4-
import {useSanityInstance} from '../context/useSanityInstance'
4+
import {useSanityInstanceAndSource} from '../context/useSanityInstance'
55

66
/**
77
* A hook for subscribing to presence information for the current project.
@@ -10,8 +10,11 @@ import {useSanityInstance} from '../context/useSanityInstance'
1010
export function usePresence(): {
1111
locations: UserPresence[]
1212
} {
13-
const sanityInstance = useSanityInstance()
14-
const source = useMemo(() => getPresence(sanityInstance, {}), [sanityInstance])
13+
const [sanityInstance, actualSource] = useSanityInstanceAndSource({})
14+
const source = useMemo(
15+
() => getPresence(sanityInstance, {source: actualSource}),
16+
[sanityInstance, actualSource],
17+
)
1518
const subscribe = useCallback((callback: () => void) => source.subscribe(callback), [source])
1619
const locations = useSyncExternalStore(
1720
subscribe,

0 commit comments

Comments
 (0)