Skip to content

Commit

Permalink
fix(listen): lazy init event source (#181)
Browse files Browse the repository at this point in the history
  • Loading branch information
stipsan committed Mar 30, 2023
1 parent 752198a commit a84ee1c
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 9 deletions.
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@sanity/client",
"version": "5.4.0",
"version": "5.4.1-lazy-event-source.0",
"description": "Client for retrieving, creating and patching data from Sanity.io",
"keywords": [
"sanity",
Expand Down
2 changes: 2 additions & 0 deletions rollup.config.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ module.exports = {
file: 'umd/sanityClient.js',
format: 'umd',
name: 'SanityClient',
dynamicImportInCjs: false,
inlineDynamicImports: true,
},
plugins: [nodeResolve({browser: true}), commonjs()],
}
27 changes: 21 additions & 6 deletions src/data/listen.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import polyfilledEventSource from '@sanity/eventsource'
import {Observable} from 'rxjs'

import type {ObservableSanityClient, SanityClient} from '../SanityClient'
Expand All @@ -12,7 +11,6 @@ import {encodeQueryString} from './encodeQueryString'
// unknown range of headers, but an average EventSource request from Chrome seems
// to have around 700 bytes of cruft, so let us account for 1.2K to be "safe"
const MAX_URL_LENGTH = 16000 - 1200
const EventSource = polyfilledEventSource

const possibleOptions = [
'includePreviousRevision',
Expand Down Expand Up @@ -86,7 +84,15 @@ export function _listen<R extends Record<string, Any> = Record<string, Any>>(
}

return new Observable((observer) => {
let es = getEventSource()
let es: InstanceType<typeof import('@sanity/eventsource')>
getEventSource()
.then((eventSource) => {
es = eventSource
})
.catch((reason) => {
observer.error(reason)
stop()
})
let reconnectTimer: NodeJS.Timeout
let stopped = false

Expand All @@ -107,7 +113,7 @@ export function _listen<R extends Record<string, Any> = Record<string, Any>>(
// automatically, in which case it sets readyState to `CONNECTING`, but in some cases
// (like when a laptop lid is closed), it closes the connection. In these cases we need
// to explicitly reconnect.
if (es.readyState === EventSource.CLOSED) {
if (es.readyState === es.CLOSED) {
unsubscribe()
clearTimeout(reconnectTimer)
reconnectTimer = setTimeout(open, 100)
Expand All @@ -130,6 +136,7 @@ export function _listen<R extends Record<string, Any> = Record<string, Any>>(
}

function unsubscribe() {
if (!es) return
es.removeEventListener('error', onError)
es.removeEventListener('channelError', onChannelError)
es.removeEventListener('disconnect', onDisconnect)
Expand All @@ -143,7 +150,8 @@ export function _listen<R extends Record<string, Any> = Record<string, Any>>(
}
}

function getEventSource() {
async function getEventSource(): Promise<InstanceType<typeof import('@sanity/eventsource')>> {
const {default: EventSource} = await import('@sanity/eventsource')
const evs = new EventSource(uri, esOptions)
evs.addEventListener('error', onError)
evs.addEventListener('channelError', onChannelError)
Expand All @@ -153,7 +161,14 @@ export function _listen<R extends Record<string, Any> = Record<string, Any>>(
}

function open() {
es = getEventSource()
getEventSource()
.then((eventSource) => {
es = eventSource
})
.catch((reason) => {
observer.error(reason)
stop()
})
}

function stop() {
Expand Down
1 change: 1 addition & 0 deletions tsconfig.settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
"skipLibCheck": true,

// Module resolution
"module": "esnext",
"moduleResolution": "node",
"allowSyntheticDefaultImports": true,
"esModuleInterop": true,
Expand Down

0 comments on commit a84ee1c

Please sign in to comment.