Skip to content

Commit

Permalink
try to speed it up a tiny bit
Browse files Browse the repository at this point in the history
  • Loading branch information
xaviergonz committed Jan 24, 2024
1 parent 700e806 commit cb6edca
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 27 deletions.
7 changes: 2 additions & 5 deletions packages/lib/src/parent/core.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,11 @@ const objectParentsAtoms = new WeakMap<object, IAtom>()
*/
export function parentPathEquals(
parentPath1: ParentPath<any> | undefined,
parentPath2: ParentPath<any> | undefined,
comparePath = true
parentPath2: ParentPath<any> | undefined
) {
if (!parentPath1 && !parentPath2) return true
if (!parentPath1 || !parentPath2) return false
const parentEquals = parentPath1.parent === parentPath2.parent
if (!parentEquals) return false
return comparePath ? parentPath1.path === parentPath2.path : true
return parentPath1.parent === parentPath2.parent && parentPath1.path === parentPath2.path
}

function createParentPathAtom(obj: object) {
Expand Down
40 changes: 18 additions & 22 deletions packages/lib/src/parent/setParent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -108,32 +108,30 @@ export const setParent = action(
}
}

let postUntweaker: ReturnType<typeof tryUntweak> | undefined
const postUntweaker = parentPath ? undefined : tryUntweak(value)

if (!parentPath) {
postUntweaker = tryUntweak(value)
}

const attachToNewParent = () => {
// detach from old
if (oldParentPath?.parent) {
removeObjectChild(oldParentPath.parent, value)
}
const valueIsModel = value instanceof BaseModel

// attach to new
objectParents.set(value, parentPath)
if (parentPath?.parent) {
addObjectChild(parentPath.parent, value)
}
reportParentPathChanged(value)
let oldRoot: any
let oldRootStore: any
if (valueIsModel) {
oldRoot = fastGetRoot(value)
oldRootStore = fastIsRootStore(oldRoot) ? oldRoot : undefined
}

if (value instanceof BaseModel) {
const oldRoot = fastGetRoot(value)
const oldRootStore = fastIsRootStore(oldRoot) ? oldRoot : undefined
// detach from old
if (oldParentPath?.parent) {
removeObjectChild(oldParentPath.parent, value)
}

attachToNewParent()
// attach to new
objectParents.set(value, parentPath)
if (parentPath?.parent) {
addObjectChild(parentPath.parent, value)
}
reportParentPathChanged(value)

if (valueIsModel) {
const newRoot = fastGetRoot(value)
const newRootStore = fastIsRootStore(newRoot) ? newRoot : undefined

Expand All @@ -148,8 +146,6 @@ export const setParent = action(
}
})
}
} else {
attachToNewParent()
}

postUntweaker?.()
Expand Down

0 comments on commit cb6edca

Please sign in to comment.