-
Notifications
You must be signed in to change notification settings - Fork 427
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fixup! feat(preview): add
unstable_observeDocument(s)
to preview st…
…ore (#7176)
- Loading branch information
Showing
2 changed files
with
51 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,38 +1,63 @@ | ||
import {type MutationEvent, type SanityClient, type WelcomeEvent} from '@sanity/client' | ||
import {defer, merge, timer} from 'rxjs' | ||
import { | ||
type MutationEvent, | ||
type ReconnectEvent, | ||
type SanityClient, | ||
type WelcomeEvent, | ||
} from '@sanity/client' | ||
import {merge, timer} from 'rxjs' | ||
import {filter, share, shareReplay} from 'rxjs/operators' | ||
|
||
/** | ||
* @internal | ||
* Creates a listener that will emit 'welcome' for all new subscribers immediately, and thereafter emit at every mutation event | ||
*/ | ||
export function createGlobalListener(client: SanityClient) { | ||
const allEvents$ = defer(() => | ||
client.listen( | ||
const allEvents$ = client | ||
.listen( | ||
'*[!(_id in path("_.**"))]', | ||
{}, | ||
{ | ||
events: ['welcome', 'mutation'], | ||
events: ['welcome', 'mutation', 'reconnect'], | ||
includeResult: false, | ||
includePreviousRevision: false, | ||
includeMutations: false, | ||
visibility: 'query', | ||
effectFormat: 'mendoza', | ||
tag: 'preview.global', | ||
}, | ||
), | ||
).pipe( | ||
filter( | ||
(event): event is WelcomeEvent | MutationEvent => | ||
event.type === 'welcome' || event.type === 'mutation', | ||
), | ||
share({resetOnRefCountZero: () => timer(2000), resetOnComplete: true}), | ||
) | ||
.pipe( | ||
share({ | ||
resetOnRefCountZero: () => timer(2000), | ||
}), | ||
) | ||
|
||
// Reconnect events emitted in case the connection is lost | ||
const reconnect = allEvents$.pipe( | ||
filter((event): event is ReconnectEvent => event.type === 'reconnect'), | ||
) | ||
|
||
// Welcome events are emitted when the listener is (re)connected | ||
const welcome = allEvents$.pipe( | ||
filter((event): event is WelcomeEvent => event.type === 'welcome'), | ||
) | ||
|
||
// Mutation events coming from the listener | ||
const mutations = allEvents$.pipe( | ||
filter((event): event is MutationEvent => event.type === 'mutation'), | ||
) | ||
|
||
// Replay the latest connection event that was emitted either when the connection was disconnected ('reconnect'), established or re-established ('welcome') | ||
const connectionEvent = merge(welcome, reconnect).pipe( | ||
shareReplay({bufferSize: 1, refCount: true}), | ||
) | ||
|
||
const welcome$ = allEvents$.pipe( | ||
filter((event) => event.type === 'welcome'), | ||
shareReplay({refCount: true, bufferSize: 1}), | ||
// Emit the welcome event if the latest connection event was the 'welcome' event. | ||
// Downstream subscribers will typically map the welcome event to an initial fetch | ||
const replayWelcome = connectionEvent.pipe( | ||
filter((latestConnectionEvent) => latestConnectionEvent.type === 'welcome'), | ||
) | ||
const mutations$ = allEvents$.pipe(filter((event) => event.type === 'mutation')).pipe(share()) | ||
return merge(welcome$, mutations$) | ||
|
||
// Combine into a single stream | ||
return merge(replayWelcome, mutations, reconnect) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters