Skip to content

Commit

Permalink
small optimization
Browse files Browse the repository at this point in the history
  • Loading branch information
xaviergonz committed Mar 25, 2024
1 parent 8a9dd27 commit 61101d9
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 22 deletions.
8 changes: 4 additions & 4 deletions packages/lib/src/parent/setParent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { BaseModel } from "../model/BaseModel"
import { getModelMetadata } from "../model/getModelMetadata"
import { isModel } from "../model/utils"
import { attachToRootStore, detachFromRootStore } from "../rootStore/attachDetach"
import { fastIsRootStore } from "../rootStore/rootStore"
import { fastIsRootStoreNoAtom } from "../rootStore/rootStore"
import { clone } from "../snapshot/clone"
import { isTweakedObject } from "../tweaker/core"
import { tryUntweak } from "../tweaker/tweak"
Expand Down Expand Up @@ -57,7 +57,7 @@ export const setParent = action(
return value
}

if (fastIsRootStore(value)) {
if (fastIsRootStoreNoAtom(value)) {
throw failure("root stores cannot be attached to any parents")
}

Expand Down Expand Up @@ -110,7 +110,7 @@ export const setParent = action(
let oldRootStore: any
if (valueIsModel) {
oldRoot = fastGetRoot(value)
oldRootStore = fastIsRootStore(oldRoot) ? oldRoot : undefined
oldRootStore = fastIsRootStoreNoAtom(oldRoot) ? oldRoot : undefined
}

// detach from old
Expand All @@ -127,7 +127,7 @@ export const setParent = action(

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

// invoke model root store events
if (oldRootStore !== newRootStore && (oldRootStore || newRootStore)) {
Expand Down
23 changes: 8 additions & 15 deletions packages/lib/src/rootStore/rootStore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,13 +79,6 @@ export const unregisterRootStore: (node: object) => void = action("unregisterRoo
export function isRootStore(node: object): boolean {
assertTweakedObject(node, "node")

return fastIsRootStore(node)
}

/**
* @internal
*/
export function fastIsRootStore(node: object): boolean {
const entry = getOrCreateRootStoreEntry(node)
if (!entry.atom) {
entry.atom = createAtom("rootStore")
Expand All @@ -94,6 +87,13 @@ export function fastIsRootStore(node: object): boolean {
return entry.is
}

/**
* @internal
*/
export function fastIsRootStoreNoAtom(node: object): boolean {
return !!rootStoreRegistry.get(node)?.is
}

/**
* Gets the root store of a given tree child, or undefined if none.
*
Expand All @@ -104,13 +104,6 @@ export function fastIsRootStore(node: object): boolean {
export function getRootStore<T extends object>(node: object): T | undefined {
assertTweakedObject(node, "node")

return fastGetRootStore(node)
}

/**
* @internal
*/
export function fastGetRootStore<T extends object>(node: object): T | undefined {
const root = fastGetRoot(node)
return fastIsRootStore(root) ? root : undefined
return isRootStore(root) ? root : undefined
}
11 changes: 8 additions & 3 deletions packages/lib/src/treeUtils/sandbox.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,12 @@ import { applyPatches } from "../patch/applyPatches"
import { onPatches } from "../patch/emitPatch"
import type { Patch } from "../patch/Patch"
import { PatchRecorder, patchRecorder } from "../patch/patchRecorder"
import { fastIsRootStore, registerRootStore, unregisterRootStore } from "../rootStore/rootStore"
import {
fastIsRootStoreNoAtom,
isRootStore,
registerRootStore,
unregisterRootStore,
} from "../rootStore/rootStore"
import { clone } from "../snapshot/clone"
import { assertTweakedObject } from "../tweaker/core"
import { assertIsFunction, failure } from "../utils"
Expand Down Expand Up @@ -101,7 +106,7 @@ export class SandboxManager {

let wasRS = false
const disposeReactionRS = reaction(
() => fastIsRootStore(subtreeRoot),
() => isRootStore(subtreeRoot),
(isRS) => {
if (isRS !== wasRS) {
wasRS = isRS
Expand Down Expand Up @@ -133,7 +138,7 @@ export class SandboxManager {
disposeReactionRS()
disposeOnPatches()
disposeReadonlyMW()
if (fastIsRootStore(this.subtreeRootClone)) {
if (fastIsRootStoreNoAtom(this.subtreeRootClone)) {
unregisterRootStore(this.subtreeRootClone)
}
this.disposer = () => {}
Expand Down

0 comments on commit 61101d9

Please sign in to comment.