Remove Atom.set|getState()

This commit is contained in:
Aria Minaei 2023-01-15 12:42:28 +01:00
parent e0465ed3fa
commit 3c68ed26ad
15 changed files with 33 additions and 50 deletions

View file

@ -150,30 +150,15 @@ export default class Atom<State> implements IdentityPrismProvider {
this._checkUpdates(this._rootScope, oldState, newState) this._checkUpdates(this._rootScope, oldState, newState)
} }
/**
* Gets the current state of the atom.
* @deprecated use {@link Atom.get} instead
*/
getState() {
return this._currentState
}
get() { get() {
return this.getState() return this._currentState
}
/**
* @deprecated use {@link Atom.set} instead
*/
setState(newState: State) {
this.set(newState)
} }
/** /**
* Gets the state of the atom at `path`. * Gets the state of the atom at `path`.
*/ */
getIn(path: (string | number)[]): unknown { getIn(path: (string | number)[]): unknown {
return path.length === 0 ? this.getState() : get(this.getState(), path) return path.length === 0 ? this.get() : get(this.get(), path)
} }
reduce(fn: (state: State) => State) { reduce(fn: (state: State) => State) {

View file

@ -23,11 +23,11 @@ describe(`v2 atom`, () => {
d.onChange(ticker, (c) => { d.onChange(ticker, (c) => {
changes.push(c) changes.push(c)
}) })
a.setState({...data, bar: 1}) a.set({...data, bar: 1})
ticker.tick() ticker.tick()
expect(changes).toHaveLength(1) expect(changes).toHaveLength(1)
expect(changes[0]).toEqual(1) expect(changes[0]).toEqual(1)
a.setState({...data, bar: 1}) a.set({...data, bar: 1})
ticker.tick() ticker.tick()
expect(changes).toHaveLength(1) expect(changes).toHaveLength(1)
}) })

View file

@ -4,7 +4,7 @@
* @packageDocumentation * @packageDocumentation
*/ */
import type { Prism} from '@theatre/dataverse'; import type {Prism} from '@theatre/dataverse'
import {Atom} from '@theatre/dataverse' import {Atom} from '@theatre/dataverse'
import {prism, val} from '@theatre/dataverse' import {prism, val} from '@theatre/dataverse'
import {findIndex} from 'lodash-es' import {findIndex} from 'lodash-es'
@ -62,7 +62,7 @@ export function usePrism<T>(
if (!atomRef.current) { if (!atomRef.current) {
atomRef.current = new Atom(fnAsCallback) atomRef.current = new Atom(fnAsCallback)
} else { } else {
atomRef.current.setState(fnAsCallback) atomRef.current.set(fnAsCallback)
} }
const prsm = useMemo( const prsm = useMemo(

View file

@ -123,7 +123,7 @@ export default class Sequence {
} }
get position() { get position() {
return this._playbackControllerBox.getState().getCurrentPosition() return this._playbackControllerBox.get().getCurrentPosition()
} }
get subUnitsPerUnit(): number { get subUnitsPerUnit(): number {
@ -165,7 +165,7 @@ export default class Sequence {
} }
const dur = this.length const dur = this.length
this._playbackControllerBox this._playbackControllerBox
.getState() .get()
.gotoPosition(position > dur ? dur : position) .gotoPosition(position > dur ? dur : position)
} }
@ -174,7 +174,7 @@ export default class Sequence {
} }
get playing() { get playing() {
return val(this._playbackControllerBox.getState().statePointer.playing) return val(this._playbackControllerBox.get().statePointer.playing)
} }
_makeRangeFromSequenceTemplate(): Prism<IPlaybackRange> { _makeRangeFromSequenceTemplate(): Prism<IPlaybackRange> {
@ -198,9 +198,7 @@ export default class Sequence {
rangeD: Prism<IPlaybackRange>, rangeD: Prism<IPlaybackRange>,
ticker: Ticker, ticker: Ticker,
): Promise<unknown> { ): Promise<unknown> {
return this._playbackControllerBox return this._playbackControllerBox.get().playDynamicRange(rangeD, ticker)
.getState()
.playDynamicRange(rangeD, ticker)
} }
async play( async play(
@ -337,18 +335,18 @@ To fix this, either set \`conf.range[1]\` to be less the duration of the sequenc
ticker: Ticker, ticker: Ticker,
): Promise<boolean> { ): Promise<boolean> {
return this._playbackControllerBox return this._playbackControllerBox
.getState() .get()
.play(iterationCount, range, rate, direction, ticker) .play(iterationCount, range, rate, direction, ticker)
} }
pause() { pause() {
this._playbackControllerBox.getState().pause() this._playbackControllerBox.get().pause()
} }
replacePlaybackController(playbackController: IPlaybackController) { replacePlaybackController(playbackController: IPlaybackController) {
this.pause() this.pause()
const oldController = this._playbackControllerBox.getState() const oldController = this._playbackControllerBox.get()
this._playbackControllerBox.setState(playbackController) this._playbackControllerBox.set(playbackController)
const time = oldController.getCurrentPosition() const time = oldController.getCurrentPosition()
oldController.destroy() oldController.destroy()

View file

@ -331,6 +331,6 @@ export default class SheetObject implements IdentityPrismProvider {
setInitialValue(val: DeepPartialOfSerializableValue<SheetObjectPropsValue>) { setInitialValue(val: DeepPartialOfSerializableValue<SheetObjectPropsValue>) {
this.validateValue(this.propsP, val) this.validateValue(this.propsP, val)
this._initialValue.setState(val) this._initialValue.set(val)
} }
} }

View file

@ -67,7 +67,7 @@ export default class SheetObjectTemplate {
readonly project: Project readonly project: Project
get staticConfig() { get staticConfig() {
return this._config.getState() return this._config.get()
} }
get configPointer() { get configPointer() {
@ -75,7 +75,7 @@ export default class SheetObjectTemplate {
} }
get staticActions() { get staticActions() {
return this._actions.getState() return this._actions.get()
} }
get actionsPointer() { get actionsPointer() {
@ -100,12 +100,12 @@ export default class SheetObjectTemplate {
nativeObject: unknown, nativeObject: unknown,
config: SheetObjectPropTypeConfig, config: SheetObjectPropTypeConfig,
): SheetObject { ): SheetObject {
this._config.setState(config) this._config.set(config)
return new SheetObject(sheet, this, nativeObject) return new SheetObject(sheet, this, nativeObject)
} }
reconfigure(config: SheetObjectPropTypeConfig) { reconfigure(config: SheetObjectPropTypeConfig) {
this._config.setState(config) this._config.set(config)
} }
registerAction(name: string, action: SheetObjectAction) { registerAction(name: string, action: SheetObjectAction) {

View file

@ -75,7 +75,7 @@ export default class Sheet {
} }
getObject(key: ObjectAddressKey): SheetObject | undefined { getObject(key: ObjectAddressKey): SheetObject | undefined {
return this._objects.getState()[key] return this._objects.get()[key]
} }
deleteObject(objectKey: ObjectAddressKey) { deleteObject(objectKey: ObjectAddressKey) {

View file

@ -39,7 +39,7 @@ export default class SheetTemplate {
} }
getInstance(instanceId: SheetInstanceId): Sheet { getInstance(instanceId: SheetInstanceId): Sheet {
let inst = this._instances.getState()[instanceId] let inst = this._instances.get()[instanceId]
if (!inst) { if (!inst) {
inst = new Sheet(this, instanceId) inst = new Sheet(this, instanceId)
@ -55,7 +55,7 @@ export default class SheetTemplate {
config: SheetObjectPropTypeConfig, config: SheetObjectPropTypeConfig,
actions: SheetObjectActionsConfig, actions: SheetObjectActionsConfig,
): SheetObjectTemplate { ): SheetObjectTemplate {
let template = this._objectTemplates.getState()[objectKey] let template = this._objectTemplates.get()[objectKey]
if (!template) { if (!template) {
template = new SheetObjectTemplate( template = new SheetObjectTemplate(

View file

@ -5,6 +5,6 @@ import {Atom, prism} from '@theatre/dataverse'
*/ */
export const valToAtom = <T>(key: string, vals: T): Atom<T> => { export const valToAtom = <T>(key: string, vals: T): Atom<T> => {
const a = prism.memo(key, () => new Atom(vals), []) const a = prism.memo(key, () => new Atom(vals), [])
a.setState(vals) a.set(vals)
return a return a
} }

View file

@ -264,7 +264,7 @@ export class Studio {
} }
get core() { get core() {
return this._coreAtom.getState().core return this._coreAtom.get().core
} }
get coreP() { get coreP() {

View file

@ -113,7 +113,7 @@ function useCaptureSelection(
v: [event.clientY - rect.top, event.clientY - rect.top], v: [event.clientY - rect.top, event.clientY - rect.top],
} }
val(layoutP.selectionAtom).setState({current: undefined}) val(layoutP.selectionAtom).set({current: undefined})
return { return {
onDrag(_dx, _dy, event) { onDrag(_dx, _dy, event) {
@ -140,7 +140,7 @@ function useCaptureSelection(
val(layoutP), val(layoutP),
ref.current, ref.current,
) )
val(layoutP.selectionAtom).setState({current: selection}) val(layoutP.selectionAtom).set({current: selection})
}, },
onDragEnd(_dragHappened) { onDragEnd(_dragHappened) {
ref.current = null ref.current = null

View file

@ -37,10 +37,10 @@ function createWhatPropIsHighlightedState() {
replaceLock(address: WithoutSheetInstance<PropAddress>, cleanup: VoidFn) { replaceLock(address: WithoutSheetInstance<PropAddress>, cleanup: VoidFn) {
const lockId = lastLockId++ const lockId = lastLockId++
const existingState = whatIsHighlighted.getState() const existingState = whatIsHighlighted.get()
if (existingState.hasLock) existingState.cleanup() if (existingState.hasLock) existingState.cleanup()
whatIsHighlighted.setState({ whatIsHighlighted.set({
hasLock: true, hasLock: true,
lockId, lockId,
cleanup, cleanup,
@ -48,10 +48,10 @@ function createWhatPropIsHighlightedState() {
}) })
return function unlock() { return function unlock() {
const curr = whatIsHighlighted.getState() const curr = whatIsHighlighted.get()
if (curr.hasLock && curr.lockId === lockId) { if (curr.hasLock && curr.lockId === lockId) {
curr.cleanup() curr.cleanup()
whatIsHighlighted.setState({hasLock: false}) whatIsHighlighted.set({hasLock: false})
} }
} }
}, },

View file

@ -33,7 +33,7 @@ const ExtensionToolsetRender: React.FC<{
useLayoutEffect(() => { useLayoutEffect(() => {
const detach = extension.toolbars?.[toolbarId]?.( const detach = extension.toolbars?.[toolbarId]?.(
toolsetConfigBox.setState.bind(toolsetConfigBox), toolsetConfigBox.set.bind(toolsetConfigBox),
getStudio()!.publicApi, getStudio()!.publicApi,
) )

View file

@ -7,7 +7,7 @@ export default function useValToAtom<S>(val: S): Atom<S> {
}, []) }, [])
useLayoutEffect(() => { useLayoutEffect(() => {
atom.setState(val) atom.set(val)
}, [val]) }, [val])
return atom return atom

View file

@ -9,7 +9,7 @@ export default function atomFromReduxStore<State>(
store.subscribe(() => { store.subscribe(() => {
const newState = store.getState() const newState = store.getState()
a.setState(newState) a.set(newState)
lastState = newState lastState = newState
}) })