@sanity/client 5.4.0 → 5.4.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@sanity/client",
3
- "version": "5.4.0",
3
+ "version": "5.4.1",
4
4
  "description": "Client for retrieving, creating and patching data from Sanity.io",
5
5
  "keywords": [
6
6
  "sanity",
@@ -1,4 +1,3 @@
1
- import polyfilledEventSource from '@sanity/eventsource'
2
1
  import {Observable} from 'rxjs'
3
2
 
4
3
  import type {ObservableSanityClient, SanityClient} from '../SanityClient'
@@ -12,7 +11,6 @@ import {encodeQueryString} from './encodeQueryString'
12
11
  // unknown range of headers, but an average EventSource request from Chrome seems
13
12
  // to have around 700 bytes of cruft, so let us account for 1.2K to be "safe"
14
13
  const MAX_URL_LENGTH = 16000 - 1200
15
- const EventSource = polyfilledEventSource
16
14
 
17
15
  const possibleOptions = [
18
16
  'includePreviousRevision',
@@ -86,7 +84,15 @@ export function _listen<R extends Record<string, Any> = Record<string, Any>>(
86
84
  }
87
85
 
88
86
  return new Observable((observer) => {
89
- let es = getEventSource()
87
+ let es: InstanceType<typeof import('@sanity/eventsource')>
88
+ getEventSource()
89
+ .then((eventSource) => {
90
+ es = eventSource
91
+ })
92
+ .catch((reason) => {
93
+ observer.error(reason)
94
+ stop()
95
+ })
90
96
  let reconnectTimer: NodeJS.Timeout
91
97
  let stopped = false
92
98
 
@@ -107,7 +113,7 @@ export function _listen<R extends Record<string, Any> = Record<string, Any>>(
107
113
  // automatically, in which case it sets readyState to `CONNECTING`, but in some cases
108
114
  // (like when a laptop lid is closed), it closes the connection. In these cases we need
109
115
  // to explicitly reconnect.
110
- if (es.readyState === EventSource.CLOSED) {
116
+ if (es.readyState === es.CLOSED) {
111
117
  unsubscribe()
112
118
  clearTimeout(reconnectTimer)
113
119
  reconnectTimer = setTimeout(open, 100)
@@ -130,6 +136,7 @@ export function _listen<R extends Record<string, Any> = Record<string, Any>>(
130
136
  }
131
137
 
132
138
  function unsubscribe() {
139
+ if (!es) return
133
140
  es.removeEventListener('error', onError)
134
141
  es.removeEventListener('channelError', onChannelError)
135
142
  es.removeEventListener('disconnect', onDisconnect)
@@ -143,7 +150,8 @@ export function _listen<R extends Record<string, Any> = Record<string, Any>>(
143
150
  }
144
151
  }
145
152
 
146
- function getEventSource() {
153
+ async function getEventSource(): Promise<InstanceType<typeof import('@sanity/eventsource')>> {
154
+ const {default: EventSource} = await import('@sanity/eventsource')
147
155
  const evs = new EventSource(uri, esOptions)
148
156
  evs.addEventListener('error', onError)
149
157
  evs.addEventListener('channelError', onChannelError)
@@ -153,7 +161,14 @@ export function _listen<R extends Record<string, Any> = Record<string, Any>>(
153
161
  }
154
162
 
155
163
  function open() {
156
- es = getEventSource()
164
+ getEventSource()
165
+ .then((eventSource) => {
166
+ es = eventSource
167
+ })
168
+ .catch((reason) => {
169
+ observer.error(reason)
170
+ stop()
171
+ })
157
172
  }
158
173
 
159
174
  function stop() {