-
Notifications
You must be signed in to change notification settings - Fork 431
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor(core): refactor SettingsStore and useStructureToolSetting (#…
- Loading branch information
1 parent
dbff227
commit 74be48b
Showing
14 changed files
with
85 additions
and
117 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
67 changes: 0 additions & 67 deletions
67
packages/sanity/src/core/store/_legacy/settings/settingsStore.ts
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
import {merge, type Observable, Subject} from 'rxjs' | ||
import {filter, map, switchMap} from 'rxjs/operators' | ||
|
||
import {resolveBackend} from './backends/resolve' | ||
import {type KeyValueStore, type KeyValueStoreValue} from './types' | ||
|
||
/** @internal */ | ||
export function createKeyValueStore(): KeyValueStore { | ||
const storageBackend = resolveBackend() | ||
|
||
const setKey$ = new Subject<{key: string; value: KeyValueStoreValue}>() | ||
|
||
const updates$ = setKey$.pipe( | ||
switchMap((event) => | ||
storageBackend.set(event.key, event.value).pipe( | ||
map((nextValue) => ({ | ||
key: event.key, | ||
value: nextValue, | ||
})), | ||
), | ||
), | ||
) | ||
|
||
const getKey = ( | ||
key: string, | ||
defaultValue: KeyValueStoreValue, | ||
): Observable<KeyValueStoreValue> => { | ||
return merge( | ||
storageBackend.get(key, defaultValue), | ||
updates$.pipe( | ||
filter((update) => update.key === key), | ||
map((update) => update.value), | ||
), | ||
) as Observable<KeyValueStoreValue> | ||
} | ||
|
||
const setKey = (key: string, value: KeyValueStoreValue) => { | ||
setKey$.next({key, value}) | ||
} | ||
|
||
return {getKey, setKey} | ||
} |
File renamed without changes.
File renamed without changes.
2 changes: 1 addition & 1 deletion
2
...tore/_legacy/settings/backends/resolve.ts → .../core/store/key-value/backends/resolve.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
export * from './KeyValueStore' | ||
export * from './types' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
import {type Observable} from 'rxjs' | ||
|
||
type JsonObject = {[Key in string]: KeyValueStoreValue} & { | ||
[Key in string]?: KeyValueStoreValue | undefined | ||
} | ||
type JsonArray = KeyValueStoreValue[] | readonly KeyValueStoreValue[] | ||
type JsonPrimitive = string | number | boolean | null | ||
|
||
/** @internal */ | ||
export type KeyValueStoreValue = JsonPrimitive | JsonObject | JsonArray | ||
|
||
/** @internal */ | ||
export interface KeyValueStore { | ||
getKey(key: string, defaultValue?: KeyValueStoreValue): Observable<KeyValueStoreValue | undefined> | ||
setKey(key: string, value: KeyValueStoreValue): void | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters