-
-
Notifications
You must be signed in to change notification settings - Fork 238
/
subscribeKey.ts
31 lines (30 loc) 路 915 Bytes
/
subscribeKey.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
import { subscribe } from '../vanilla'
/**
* subscribeKey
*
* The subscribeKey utility enables subscription to a primitive subproperty of a given state proxy.
* Subscriptions created with subscribeKey will only fire when the specified property changes.
* notifyInSync: same as the parameter to subscribe(); true disables batching of subscriptions.
*
* @example
* import { subscribeKey } from 'valtio/utils'
* subscribeKey(state, 'count', (v) => console.log('state.count has changed to', v))
*/
export function subscribeKey<T extends object, K extends keyof T>(
proxyObject: T,
key: K,
callback: (value: T[K]) => void,
notifyInSync?: boolean
) {
let prevValue = proxyObject[key]
return subscribe(
proxyObject,
() => {
const nextValue = proxyObject[key]
if (!Object.is(prevValue, nextValue)) {
callback((prevValue = nextValue))
}
},
notifyInSync
)
}