From 9d3e3cda3eb74eb08590fddc182c1c3c424ebc4a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bj=C3=B8rge=20N=C3=A6ss?= Date: Mon, 30 Sep 2024 10:57:46 +0200 Subject: [PATCH] fix(core): emit setting from localStorage immediately upon subscription --- .../src/core/store/key-value/localStorageSWR.ts | 17 +++-------------- 1 file changed, 3 insertions(+), 14 deletions(-) diff --git a/packages/sanity/src/core/store/key-value/localStorageSWR.ts b/packages/sanity/src/core/store/key-value/localStorageSWR.ts index d4a4273d6ea..366e428e926 100644 --- a/packages/sanity/src/core/store/key-value/localStorageSWR.ts +++ b/packages/sanity/src/core/store/key-value/localStorageSWR.ts @@ -1,27 +1,16 @@ import {isEqual} from 'lodash' -import {fromEvent, merge, NEVER} from 'rxjs' -import {distinctUntilChanged, filter, map, tap} from 'rxjs/operators' +import {merge, of} from 'rxjs' +import {distinctUntilChanged, tap} from 'rxjs/operators' import {localStoreStorage} from './storage/localStoreStorage' import {type KeyValueStore, type KeyValueStoreValue} from './types' -// Whether or not to enable instant user sync between tabs -// if set to true, the setting will update instantly across all tabs -const ENABLE_CROSS_TAB_SYNC = false - /** * Wraps a KeyValueStore and adds Stale-While-Revalidate (SWR) behavior to it */ export function withLocalStorageSWR(wrappedStore: KeyValueStore): KeyValueStore { - const storageEvent = ENABLE_CROSS_TAB_SYNC ? fromEvent(window, 'storage') : NEVER - function getKey(key: string) { - const lsUpdates = storageEvent.pipe( - filter((event) => event.key === key), - map(() => localStoreStorage.getKey(key)), - ) - - return merge(lsUpdates, wrappedStore.getKey(key)).pipe( + return merge(of(localStoreStorage.getKey(key)), wrappedStore.getKey(key)).pipe( distinctUntilChanged(isEqual), tap((value) => { localStoreStorage.setKey(key, value)