Skip to content

Commit 1e79797

Browse files
authored
fix(core): emit setting from localStorage immediately upon subscription (#7560)
1 parent d2ca86d commit 1e79797

File tree

1 file changed

+3
-14
lines changed

1 file changed

+3
-14
lines changed

packages/sanity/src/core/store/key-value/localStorageSWR.ts

+3-14
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,16 @@
11
import {isEqual} from 'lodash'
2-
import {fromEvent, merge, NEVER} from 'rxjs'
3-
import {distinctUntilChanged, filter, map, tap} from 'rxjs/operators'
2+
import {merge, of} from 'rxjs'
3+
import {distinctUntilChanged, tap} from 'rxjs/operators'
44

55
import {localStoreStorage} from './storage/localStoreStorage'
66
import {type KeyValueStore, type KeyValueStoreValue} from './types'
77

8-
// Whether or not to enable instant user sync between tabs
9-
// if set to true, the setting will update instantly across all tabs
10-
const ENABLE_CROSS_TAB_SYNC = false
11-
128
/**
139
* Wraps a KeyValueStore and adds Stale-While-Revalidate (SWR) behavior to it
1410
*/
1511
export function withLocalStorageSWR(wrappedStore: KeyValueStore): KeyValueStore {
16-
const storageEvent = ENABLE_CROSS_TAB_SYNC ? fromEvent<StorageEvent>(window, 'storage') : NEVER
17-
1812
function getKey(key: string) {
19-
const lsUpdates = storageEvent.pipe(
20-
filter((event) => event.key === key),
21-
map(() => localStoreStorage.getKey(key)),
22-
)
23-
24-
return merge(lsUpdates, wrappedStore.getKey(key)).pipe(
13+
return merge(of(localStoreStorage.getKey(key)), wrappedStore.getKey(key)).pipe(
2514
distinctUntilChanged(isEqual),
2615
tap((value) => {
2716
localStoreStorage.setKey(key, value)

0 commit comments

Comments
 (0)