Replace Atom.setIn()
and Atom.reduceIn()
with the type-safe Atom.setByPointer()
and Atom.reduceByPointer()
This commit is contained in:
parent
5b6306bde9
commit
ac9d8b4481
15 changed files with 82 additions and 223 deletions
|
@ -240,11 +240,11 @@ export default class Project {
|
|||
sheetId: SheetId,
|
||||
instanceId: SheetInstanceId = 'default' as SheetInstanceId,
|
||||
): Sheet {
|
||||
let template = this._sheetTemplates.getState()[sheetId]
|
||||
let template = this._sheetTemplates.get()[sheetId]
|
||||
|
||||
if (!template) {
|
||||
template = new SheetTemplate(this, sheetId)
|
||||
this._sheetTemplates.setIn([sheetId], template)
|
||||
this._sheetTemplates.reduce((s) => ({...s, [sheetId]: template}))
|
||||
}
|
||||
|
||||
return template.getInstance(instanceId)
|
||||
|
|
|
@ -14,11 +14,11 @@ class ProjectsSingleton {
|
|||
* We're trusting here that each project id is unique
|
||||
*/
|
||||
add(id: ProjectId, project: Project) {
|
||||
this.atom.reduceState(['projects', id], () => project)
|
||||
this.atom.setByPointer((p) => p.projects[id], project)
|
||||
}
|
||||
|
||||
get(id: ProjectId): Project | undefined {
|
||||
return this.atom.getState().projects[id]
|
||||
return this.atom.get().projects[id]
|
||||
}
|
||||
|
||||
has(id: ProjectId) {
|
||||
|
|
|
@ -120,11 +120,11 @@ export default class AudioPlaybackController implements IPlaybackController {
|
|||
}
|
||||
|
||||
private get _playing() {
|
||||
return this._state.getState().playing
|
||||
return this._state.get().playing
|
||||
}
|
||||
|
||||
private set _playing(playing: boolean) {
|
||||
this._state.setIn(['playing'], playing)
|
||||
this._state.setByPointer((p) => p.playing, playing)
|
||||
}
|
||||
|
||||
destroy() {}
|
||||
|
@ -140,11 +140,11 @@ export default class AudioPlaybackController implements IPlaybackController {
|
|||
}
|
||||
|
||||
private _updatePositionInState(time: number) {
|
||||
this._state.reduceState(['position'], () => time)
|
||||
this._state.reduce((s) => ({...s, position: time}))
|
||||
}
|
||||
|
||||
getCurrentPosition() {
|
||||
return this._state.getState().position
|
||||
return this._state.get().position
|
||||
}
|
||||
|
||||
play(
|
||||
|
|
|
@ -66,19 +66,19 @@ export default class DefaultPlaybackController implements IPlaybackController {
|
|||
}
|
||||
|
||||
private _updatePositionInState(time: number) {
|
||||
this._state.reduceState(['position'], () => time)
|
||||
this._state.setByPointer((p) => p.position, time)
|
||||
}
|
||||
|
||||
getCurrentPosition() {
|
||||
return this._state.getState().position
|
||||
return this._state.get().position
|
||||
}
|
||||
|
||||
get playing() {
|
||||
return this._state.getState().playing
|
||||
return this._state.get().playing
|
||||
}
|
||||
|
||||
set playing(playing: boolean) {
|
||||
this._state.setIn(['playing'], playing)
|
||||
this._state.setByPointer((p) => p.playing, playing)
|
||||
}
|
||||
|
||||
play(
|
||||
|
|
|
@ -256,7 +256,11 @@ export default class SheetObject implements IdentityPrismProvider {
|
|||
const updateSequenceValueFromItsPrism = () => {
|
||||
const triple = pr.getValue()
|
||||
|
||||
if (!triple) return valsAtom.setIn(pathToProp, undefined)
|
||||
if (!triple)
|
||||
return valsAtom.setByPointer(
|
||||
(p) => pointerDeep(p, pathToProp),
|
||||
undefined,
|
||||
)
|
||||
|
||||
const leftDeserialized = deserializeAndSanitize(triple.left)
|
||||
|
||||
|
@ -266,7 +270,10 @@ export default class SheetObject implements IdentityPrismProvider {
|
|||
: leftDeserialized
|
||||
|
||||
if (triple.right === undefined)
|
||||
return valsAtom.setIn(pathToProp, left)
|
||||
return valsAtom.setByPointer(
|
||||
(p) => pointerDeep(p, pathToProp),
|
||||
left,
|
||||
)
|
||||
|
||||
const rightDeserialized = deserializeAndSanitize(triple.right)
|
||||
const right =
|
||||
|
@ -274,8 +281,8 @@ export default class SheetObject implements IdentityPrismProvider {
|
|||
? propConfig.default
|
||||
: rightDeserialized
|
||||
|
||||
return valsAtom.setIn(
|
||||
pathToProp,
|
||||
return valsAtom.setByPointer(
|
||||
(p) => pointerDeep(p, pathToProp),
|
||||
interpolate(left, right, triple.progression),
|
||||
)
|
||||
}
|
||||
|
|
|
@ -69,7 +69,7 @@ export default class Sheet {
|
|||
|
||||
const object = objTemplate.createInstance(this, nativeObject, config)
|
||||
|
||||
this._objects.setIn([objectKey], object)
|
||||
this._objects.setByPointer((p) => p[objectKey], object)
|
||||
|
||||
return object
|
||||
}
|
||||
|
@ -79,7 +79,7 @@ export default class Sheet {
|
|||
}
|
||||
|
||||
deleteObject(objectKey: ObjectAddressKey) {
|
||||
this._objects.reduceState([], (state) => {
|
||||
this._objects.reduce((state) => {
|
||||
const newState = {...state}
|
||||
delete newState[objectKey]
|
||||
return newState
|
||||
|
|
|
@ -43,7 +43,7 @@ export default class SheetTemplate {
|
|||
|
||||
if (!inst) {
|
||||
inst = new Sheet(this, instanceId)
|
||||
this._instances.setIn([instanceId], inst)
|
||||
this._instances.setByPointer((p) => p[instanceId], inst)
|
||||
}
|
||||
|
||||
return inst
|
||||
|
@ -65,7 +65,7 @@ export default class SheetTemplate {
|
|||
config,
|
||||
actions,
|
||||
)
|
||||
this._objectTemplates.setIn([objectKey], template)
|
||||
this._objectTemplates.setByPointer((p) => p[objectKey], template)
|
||||
}
|
||||
|
||||
return template
|
||||
|
|
|
@ -188,7 +188,7 @@ export class Studio {
|
|||
|
||||
setCoreBits(coreBits: CoreBits) {
|
||||
this._corePrivateApi = coreBits.privateAPI
|
||||
this._coreAtom.setIn(['core'], coreBits.coreExports)
|
||||
this._coreAtom.setByPointer((p) => p.core, coreBits.coreExports)
|
||||
this._setProjectsP(coreBits.projectsP)
|
||||
}
|
||||
|
||||
|
|
|
@ -49,7 +49,7 @@ let lastLockId = 0
|
|||
const FrameStampPositionProvider: React.FC<{
|
||||
layoutP: Pointer<SequenceEditorPanelLayout>
|
||||
}> = ({children, layoutP}) => {
|
||||
const locksAtom = useMemo(() => new Atom<{list: LockItem[]}>({list: []}), [])
|
||||
const locksAtom = useMemo(() => new Atom<LockItem[]>([]), [])
|
||||
const currentD = useMemo(
|
||||
() =>
|
||||
prism(() => {
|
||||
|
@ -57,7 +57,7 @@ const FrameStampPositionProvider: React.FC<{
|
|||
.memo('p', () => pointerPositionInUnitSpace(layoutP), [layoutP])
|
||||
.getValue()
|
||||
|
||||
const locks = val(locksAtom.pointer.list)
|
||||
const locks = val(locksAtom.pointer)
|
||||
|
||||
if (locks.length > 0) {
|
||||
return last(locks)!.position
|
||||
|
@ -69,7 +69,7 @@ const FrameStampPositionProvider: React.FC<{
|
|||
)
|
||||
const getLock = useCallback(() => {
|
||||
const id = lastLockId++
|
||||
locksAtom.reduceState(['list'], (list) => [
|
||||
locksAtom.reduce((list) => [
|
||||
...list,
|
||||
{
|
||||
id,
|
||||
|
@ -78,13 +78,11 @@ const FrameStampPositionProvider: React.FC<{
|
|||
])
|
||||
|
||||
const unlock = () => {
|
||||
locksAtom.reduceState(['list'], (list) =>
|
||||
list.filter((lock) => lock.id !== id),
|
||||
)
|
||||
locksAtom.reduce((list) => list.filter((lock) => lock.id !== id))
|
||||
}
|
||||
|
||||
const set = (posInUnitSpace: number) => {
|
||||
locksAtom.reduceState(['list'], (list) => {
|
||||
locksAtom.reduce((list) => {
|
||||
const index = list.findIndex((lock) => lock.id === id)
|
||||
if (index === -1) {
|
||||
console.warn(`Lock is already freed. This is a bug.`)
|
||||
|
|
|
@ -7,6 +7,7 @@ import {prism, pointerToPrism} from '@theatre/dataverse'
|
|||
import {Atom} from '@theatre/dataverse'
|
||||
import {usePrismInstance} from '@theatre/react'
|
||||
import {selectClosestHTMLAncestor} from '@theatre/studio/utils/selectClosestHTMLAncestor'
|
||||
import pointerDeep from '@theatre/shared/utils/pointerDeep'
|
||||
|
||||
/** To mean the presence value */
|
||||
export enum PresenceFlag {
|
||||
|
@ -53,12 +54,15 @@ function createPresenceContext(options: {
|
|||
flag: rel.flag,
|
||||
}
|
||||
const path = [rel.affects, itemKey, relationId]
|
||||
relationsAtom.setIn(path, presence)
|
||||
relationsAtom.setByPointer((p) => pointerDeep(p, path), presence)
|
||||
return path
|
||||
})
|
||||
return () => {
|
||||
for (const pathToUndo of undoAtPaths) {
|
||||
relationsAtom.setIn(pathToUndo, undefined)
|
||||
relationsAtom.setByPointer(
|
||||
(p) => pointerDeep(p, pathToUndo),
|
||||
undefined,
|
||||
)
|
||||
}
|
||||
}
|
||||
},
|
||||
|
@ -98,16 +102,16 @@ function createPresenceContext(options: {
|
|||
return usePrismInstance(focusD)
|
||||
},
|
||||
setUserHover(itemKeyOpt) {
|
||||
const prev = currentUserHoverItemB.getState()
|
||||
const prev = currentUserHoverItemB.get()
|
||||
if (prev === itemKeyOpt) {
|
||||
return
|
||||
}
|
||||
if (prev) {
|
||||
currentUserHoverFlagItemsAtom.setIn([prev], false)
|
||||
currentUserHoverFlagItemsAtom.setByPointer((p) => p[prev], false)
|
||||
}
|
||||
currentUserHoverItemB.setState(itemKeyOpt)
|
||||
currentUserHoverItemB.set(itemKeyOpt)
|
||||
if (itemKeyOpt) {
|
||||
currentUserHoverFlagItemsAtom.setIn([itemKeyOpt], true)
|
||||
currentUserHoverFlagItemsAtom.setByPointer((p) => p[itemKeyOpt], true)
|
||||
}
|
||||
},
|
||||
}
|
||||
|
|
|
@ -27,7 +27,7 @@ export const getMounter = () => {
|
|||
props: Props,
|
||||
portalNode: HTMLElement,
|
||||
) {
|
||||
theAtom.reduceState([], (s) => {
|
||||
theAtom.reduce((s) => {
|
||||
return {
|
||||
byId: {...s.byId, [id]: {comp, props, portalNode}},
|
||||
set: {...s.set, [id]: true},
|
||||
|
@ -36,7 +36,7 @@ export const getMounter = () => {
|
|||
}
|
||||
|
||||
function unmount() {
|
||||
theAtom.reduceState([], (s) => {
|
||||
theAtom.reduce((s) => {
|
||||
const set = {...s.set}
|
||||
const byId = {...s.byId}
|
||||
delete set[id]
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue