@sanity/client 6.27.2 → 6.27.3-canary.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/dist/index.cjs CHANGED
@@ -896,6 +896,28 @@ function _listen(query, params, opts = {}) {
896
896
  )
897
897
  );
898
898
  }
899
+ function shareReplayLatest(configOrPredicate, config2) {
900
+ return _shareReplayLatest(
901
+ typeof configOrPredicate == "function" ? { predicate: configOrPredicate, ...config2 } : configOrPredicate
902
+ );
903
+ }
904
+ function _shareReplayLatest(config2) {
905
+ return (source) => {
906
+ let latest, emitted = !1;
907
+ const { predicate, ...shareConfig } = config2, wrapped = source.pipe(
908
+ rxjs.tap((value) => {
909
+ config2.predicate(value) && (emitted = !0, latest = value);
910
+ }),
911
+ rxjs.finalize(() => {
912
+ emitted = !1, latest = void 0;
913
+ }),
914
+ rxjs.share(shareConfig)
915
+ ), emitLatest = new rxjs.Observable((subscriber) => {
916
+ emitted && subscriber.next(latest), subscriber.complete();
917
+ });
918
+ return rxjs.merge(wrapped, emitLatest);
919
+ };
920
+ }
899
921
  const requiredApiVersion = "2021-03-25";
900
922
  class LiveClient {
901
923
  #client;
@@ -930,6 +952,9 @@ class LiveClient {
930
952
  includeDrafts && token && (esOptions.headers = {
931
953
  Authorization: `Bearer ${token}`
932
954
  }), includeDrafts && withCredentials && (esOptions.withCredentials = !0);
955
+ const key = `${url.href}::${JSON.stringify(esOptions)}`, existing = eventsCache.get(key);
956
+ if (existing)
957
+ return existing;
933
958
  const events = connectEventSource(() => (
934
959
  // use polyfill if there is no global EventSource or if we need to set headers
935
960
  (typeof EventSource > "u" || esOptions.headers ? eventSourcePolyfill : rxjs.of(EventSource)).pipe(operators.map((EventSource2) => new EventSource2(url.href, esOptions)))
@@ -957,8 +982,13 @@ class LiveClient {
957
982
  rxjs.catchError(() => {
958
983
  throw new CorsOriginError({ projectId });
959
984
  })
985
+ ), observable = rxjs.concat(checkCors, events).pipe(
986
+ operators.finalize(() => eventsCache.delete(key)),
987
+ shareReplayLatest({
988
+ predicate: (event) => event.type === "welcome"
989
+ })
960
990
  );
961
- return rxjs.concat(checkCors, events);
991
+ return eventsCache.set(key, observable), observable;
962
992
  }
963
993
  }
964
994
  function fetchObservable(url, init) {
@@ -974,6 +1004,7 @@ function fetchObservable(url, init) {
974
1004
  ), () => controller.abort();
975
1005
  });
976
1006
  }
1007
+ const eventsCache = /* @__PURE__ */ new Map();
977
1008
  class ObservableDatasetsClient {
978
1009
  #client;
979
1010
  #httpRequest;
@@ -1492,7 +1523,7 @@ function defineDeprecatedCreateClient(createClient2) {
1492
1523
  return config.printNoDefaultExport(), createClient2(config$1);
1493
1524
  };
1494
1525
  }
1495
- var name = "@sanity/client", version = "6.27.2";
1526
+ var name = "@sanity/client", version = "6.27.3-canary.1";
1496
1527
  const middleware = [
1497
1528
  middleware$1.debug({ verbose: !0, namespace: "sanity:client" }),
1498
1529
  middleware$1.headers({ "User-Agent": `${name} ${version}` }),