-
-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
54 additions
and
70 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,26 @@ | ||
import type { ReadonlySignal } from '@preact/signals' | ||
import type { Store, StoreValue } from 'nanostores' | ||
|
||
import type { UseStoreOptions } from './core/types' | ||
export type StoreKeys<T> = T extends { | ||
setKey: (k: infer K, v: never) => unknown | ||
} | ||
? K | ||
: never | ||
|
||
export interface UseStoreOptions<SomeStore, Value> { | ||
keys?: StoreKeys<SomeStore>[] | ||
selector?: (state: StoreValue<SomeStore>) => Value | ||
} | ||
|
||
export function useStore< | ||
SomeStore extends Store, | ||
Value = StoreValue<SomeStore> | ||
>( | ||
store: SomeStore, | ||
options: UseStoreOptions<SomeStore, Value> = {} | ||
): ReadonlySignal<Value> | ||
|
||
export function useLegacyStore< | ||
SomeStore extends Store, | ||
Value = StoreValue<SomeStore> | ||
>(store: SomeStore, options: UseStoreOptions<SomeStore, Value> = {}): Value |
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 |
---|---|---|
@@ -1,7 +1,39 @@ | ||
import { useComputed, useSignal } from '@preact/signals' | ||
import { listenKeys } from 'nanostores' | ||
import { useSyncExternalStore } from 'preact/compat' | ||
import { useCallback, useEffect } from 'preact/hooks' | ||
|
||
import { defineStore } from './core/defineStore.js' | ||
function defineStore(store, options = {}, syncExternalStoreFn) { | ||
let subscribe = useCallback( | ||
onChange => { | ||
return options.keys | ||
? listenKeys(store, options.keys, onChange) | ||
: store.listen(onChange) | ||
}, | ||
[options.keys, store] | ||
) | ||
|
||
let get = useCallback(() => { | ||
return (options.selector ?? (s => s))(store.get()) | ||
}, [options.selector, store]) | ||
|
||
return syncExternalStoreFn(subscribe, get) | ||
} | ||
|
||
function useSignalLikeSyncExternalStore(subscribe, getSnapshot) { | ||
let cache = useSignal(getSnapshot()) | ||
|
||
useEffect(() => { | ||
return subscribe(() => (cache.value = getSnapshot())) | ||
}, [cache, subscribe, getSnapshot]) | ||
|
||
return useComputed(() => cache.value) | ||
} | ||
|
||
export function useStore(store, options = {}) { | ||
return defineStore(store, options, useSignalLikeSyncExternalStore) | ||
} | ||
|
||
export function useLegacyStore(store, options = {}) { | ||
return defineStore(store, options, useSyncExternalStore) | ||
} |
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.
This file was deleted.
Oops, something went wrong.