Skip to content

Commit

Permalink
micro-optimizations
Browse files Browse the repository at this point in the history
  • Loading branch information
xaviergonz committed Jan 27, 2024
1 parent cb6edca commit df6ac43
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 11 deletions.
12 changes: 8 additions & 4 deletions packages/lib/src/parent/core.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,22 +22,26 @@ export function parentPathEquals(
return parentPath1.parent === parentPath2.parent && parentPath1.path === parentPath2.path
}

function createParentPathAtom(obj: object) {
return getOrCreate(objectParentsAtoms, obj, () => createAtom("parentAtom"))
function createParentPathAtom() {
return createAtom("parentAtom")
}

function getOrCreateParentPathAtom(obj: object) {
return getOrCreate(objectParentsAtoms, obj, createParentPathAtom)
}

/**
* @internal
*/
export function reportParentPathObserved(node: object) {
createParentPathAtom(node).reportObserved()
getOrCreateParentPathAtom(node).reportObserved()
}

/**
* @internal
*/
export function reportParentPathChanged(node: object) {
createParentPathAtom(node).reportChanged()
getOrCreateParentPathAtom(node).reportChanged()
}

/**
Expand Down
10 changes: 7 additions & 3 deletions packages/lib/src/rootStore/rootStore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,15 @@ import { attachToRootStore, detachFromRootStore } from "./attachDetach"

const rootStoreRegistry = new WeakMap<object, { atom: IAtom; is: boolean }>()

const getOrCreateRootStoreEntry = (node: object) =>
getOrCreate(rootStoreRegistry, node, () => ({
function createRootStoreEntry() {
return {
atom: createAtom("rootStore"),
is: false,
}))
}
}

const getOrCreateRootStoreEntry = (node: object) =>
getOrCreate(rootStoreRegistry, node, createRootStoreEntry)

/**
* Registers a model / tree node object as a root store tree.
Expand Down
12 changes: 8 additions & 4 deletions packages/lib/src/snapshot/internal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,15 +78,19 @@ export const setNewInternalSnapshot = action(
transformFn: SnapshotTransformFn | undefined,
markAsFrozen = false
): void => {
const transformed: any = transformFn ? transformFn(untransformed) : untransformed

const sn: SnapshotData = {
untransformed,
transformFn,
transformed: transformFn ? transformFn(untransformed) : untransformed,
transformed,
atom: createAtom("snapshot"),
}
frozenState.set(sn.untransformed, markAsFrozen)
if (sn.transformed !== undefined) {
frozenState.set(sn.transformed, markAsFrozen)

frozenState.set(untransformed, markAsFrozen)

if (transformed !== undefined && transformed !== untransformed) {
frozenState.set(transformed, markAsFrozen)
}

snapshots.set(value, sn)
Expand Down

0 comments on commit df6ac43

Please sign in to comment.