|
1 | 1 | 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' |
4 | 4 |
|
5 | 5 | import {localStoreStorage} from './storage/localStoreStorage'
|
6 | 6 | import {type KeyValueStore, type KeyValueStoreValue} from './types'
|
7 | 7 |
|
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 |
| - |
12 | 8 | /**
|
13 | 9 | * Wraps a KeyValueStore and adds Stale-While-Revalidate (SWR) behavior to it
|
14 | 10 | */
|
15 | 11 | export function withLocalStorageSWR(wrappedStore: KeyValueStore): KeyValueStore {
|
16 |
| - const storageEvent = ENABLE_CROSS_TAB_SYNC ? fromEvent<StorageEvent>(window, 'storage') : NEVER |
17 |
| - |
18 | 12 | 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( |
25 | 14 | distinctUntilChanged(isEqual),
|
26 | 15 | tap((value) => {
|
27 | 16 | localStoreStorage.setKey(key, value)
|
|
0 commit comments