Simplify prism.state()

This commit is contained in:
Aria Minaei 2022-12-01 13:01:01 +01:00
parent ee68112867
commit 63228fd86a

View file

@ -1,4 +1,3 @@
import Box from '../../Box'
import type Ticker from '../../Ticker' import type Ticker from '../../Ticker'
import type {$IntentionalAny, VoidFn} from '../../types' import type {$IntentionalAny, VoidFn} from '../../types'
import Stack from '../../utils/Stack' import Stack from '../../utils/Stack'
@ -489,17 +488,20 @@ class HotScope implements PrismScope {
} }
state<T>(key: string, initialValue: T): [T, (val: T) => void] { state<T>(key: string, initialValue: T): [T, (val: T) => void] {
const {b, setValue} = this.memo( const {value, setValue} = this.memo(
'state/' + key, 'state/' + key,
() => { () => {
const b = new Box<T>(initialValue) const value = {current: initialValue}
const setValue = (val: T) => b.set(val) const setValue = (newValue: T) => {
return {b, setValue} value.current = newValue
this._hotHandle.forceStale()
}
return {value, setValue}
}, },
[], [],
) )
return [b.derivation.getValue(), setValue] return [value.current, setValue]
} }
sub(key: string): HotScope { sub(key: string): HotScope {