Skip to content

Commit

Permalink
use atoms less often
Browse files Browse the repository at this point in the history
  • Loading branch information
xaviergonz committed Mar 25, 2024
1 parent 61101d9 commit d9b60ba
Show file tree
Hide file tree
Showing 21 changed files with 70 additions and 55 deletions.
2 changes: 1 addition & 1 deletion packages/lib/src/action/middleware.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ export function getActionMiddlewares(obj: object): ActionMiddlewaresIterator {
function findNextIterator() {
let nextIter
while (current && !nextIter) {
current = fastGetParent(current)
current = fastGetParent(current, false)
nextIter = getCurrentIterator()
}
return nextIter
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ export const objectPathSerializer: ActionCallArgumentSerializer<object, ObjectPa

// try to serialize a ref to its path if possible instead
if (targetRoot) {
const rootPath = fastGetRootPath(value)
const rootPath = fastGetRootPath(value, false)
if (rootPath.root === targetRoot) {
return {
targetPath: rootPath.path,
Expand Down
2 changes: 1 addition & 1 deletion packages/lib/src/actionMiddlewares/onActionMiddleware.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ export function onActionMiddleware(
}

function actionContextToActionCall(ctx: SimpleActionContext): ActionCall {
const rootPath = fastGetRootPath(ctx.target)
const rootPath = fastGetRootPath(ctx.target, false)

return {
actionName: ctx.actionName,
Expand Down
2 changes: 1 addition & 1 deletion packages/lib/src/actionMiddlewares/undoMiddleware.ts
Original file line number Diff line number Diff line change
Expand Up @@ -733,7 +733,7 @@ export function undoMiddleware<S>(

const event = {
type: UndoEventType.Single,
targetPath: fastGetRootPath(ctx.target).path,
targetPath: fastGetRootPath(ctx.target, false).path,
actionName: ctx.actionName,
patches,
inversePatches,
Expand Down
24 changes: 14 additions & 10 deletions packages/lib/src/context/context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -120,15 +120,17 @@ class ContextClass<T> implements Context<T> {
this.nodeAtom.get(node)?.reportChanged()
}

private fastGet(node: object): T {
this.reportNodeAtomObserved(node)
private fastGet(node: object, useAtom: boolean): T {
if (useAtom) {
this.reportNodeAtomObserved(node)
}

const obsForNode = this.nodeContextValue.get(node)
if (obsForNode) {
return resolveContextValue(obsForNode)
}

const parent = fastGetParent(node)
const parent = fastGetParent(node, useAtom)
if (!parent) {
const overrideValue = this.overrideContextValue.get()
if (overrideValue) {
Expand All @@ -137,35 +139,37 @@ class ContextClass<T> implements Context<T> {
return this.getDefault()
}

return this.fastGet(parent)
return this.fastGet(parent, useAtom)
}

get(node: object) {
assertTweakedObject(node, "node")

return this.fastGet(node)
return this.fastGet(node, true)
}

private fastGetProviderNode(node: object): object | undefined {
this.reportNodeAtomObserved(node)
private fastGetProviderNode(node: object, useAtom: boolean): object | undefined {
if (useAtom) {
this.reportNodeAtomObserved(node)
}

const obsForNode = this.nodeContextValue.get(node)
if (obsForNode) {
return node
}

const parent = fastGetParent(node)
const parent = fastGetParent(node, useAtom)
if (!parent) {
return undefined
}

return this.fastGetProviderNode(parent)
return this.fastGetProviderNode(parent, useAtom)
}

getProviderNode(node: object): object | undefined {
assertTweakedObject(node, "node")

return this.fastGetProviderNode(node)
return this.fastGetProviderNode(node, true)
}

getDefault(): T {
Expand Down
2 changes: 1 addition & 1 deletion packages/lib/src/parent/coreObjectChildren.ts
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ function invalidateDeepChildren(node: object, obj: ObjectChildrenData) {
currentObj.deepDirty = true
currentObj.deepAtom?.reportChanged()

currentNode = fastGetParent(currentNode)
currentNode = fastGetParent(currentNode, false)
if (currentNode) {
currentObj = getObjectChildrenObject(currentNode)
}
Expand Down
2 changes: 1 addition & 1 deletion packages/lib/src/parent/detach.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ const wrappedInternalDetach = lazy(() =>
function internalDetach(this: object): void {
const node = this

const parentPath = fastGetParentPathIncludingDataObjects(node)
const parentPath = fastGetParentPathIncludingDataObjects(node, false)
if (!parentPath) return

const { parent, path } = parentPath
Expand Down
2 changes: 1 addition & 1 deletion packages/lib/src/parent/findParent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ export function findParentPath<T extends object = any>(
let depth = 0

let parentPath: ParentPath<any> | undefined
while ((parentPath = fastGetParentPath(current))) {
while ((parentPath = fastGetParentPath(current, true))) {
path.unshift(parentPath.path)
current = parentPath.parent
if (predicate(current)) {
Expand Down
47 changes: 29 additions & 18 deletions packages/lib/src/parent/path.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,31 +60,35 @@ export interface RootPath<T extends object> {
export function getParentPath<T extends object = any>(value: object): ParentPath<T> | undefined {
assertTweakedObject(value, "value")

return fastGetParentPath(value)
return fastGetParentPath(value, true)
}

/**
* @internal
*/
export function fastGetParentPath<T extends object = any>(
value: object
value: object,
useAtom: boolean
): ParentPath<T> | undefined {
reportParentPathObserved(value)
if (useAtom) {
reportParentPathObserved(value)
}
return objectParents.get(value) as ParentPath<T> | undefined
}

/**
* @internal
*/
export function fastGetParentPathIncludingDataObjects<T extends object = any>(
value: object
value: object,
useAtom: boolean
): ParentPath<T> | undefined {
const parentModel = dataObjectParent.get(value)
if (parentModel) {
return { parent: parentModel as T, path: "$" }
}

const parentPath = fastGetParentPath(value)
const parentPath = fastGetParentPath(value, useAtom)
if (parentPath && isModel(parentPath.parent)) {
return { parent: parentPath.parent.$ as T, path: parentPath.path }
}
Expand All @@ -101,23 +105,27 @@ export function fastGetParentPathIncludingDataObjects<T extends object = any>(
export function getParent<T extends object = any>(value: object): T | undefined {
assertTweakedObject(value, "value")

return fastGetParent(value)
return fastGetParent(value, true)
}

/**
* @internal
*/
export function fastGetParent<T extends object = any>(value: object): T | undefined {
return fastGetParentPath(value)?.parent
export function fastGetParent<T extends object = any>(
value: object,
useAtom: boolean
): T | undefined {
return fastGetParentPath(value, useAtom)?.parent
}

/**
* @internal
*/
export function fastGetParentIncludingDataObjects<T extends object = any>(
value: object
value: object,
useAtom: boolean
): T | undefined {
return fastGetParentPathIncludingDataObjects(value)?.parent
return fastGetParentPathIncludingDataObjects(value, useAtom)?.parent
}

/**
Expand Down Expand Up @@ -149,19 +157,22 @@ export function fastIsModelDataObject(value: object): boolean {
export function getRootPath<T extends object = any>(value: object): RootPath<T> {
assertTweakedObject(value, "value")

return fastGetRootPath(value)
return fastGetRootPath(value, true)
}

/**
* @internal
*/
export function fastGetRootPath<T extends object = any>(value: object): RootPath<T> {
export function fastGetRootPath<T extends object = any>(
value: object,
useAtom: boolean
): RootPath<T> {
let root = value
const path = [] as WritablePath
const pathObjects = [value] as unknown[]

let parentPath: ParentPath<any> | undefined
while ((parentPath = fastGetParentPath(root))) {
while ((parentPath = fastGetParentPath(root, useAtom))) {
root = parentPath.parent
path.unshift(parentPath.path)
pathObjects.unshift(parentPath.parent)
Expand All @@ -180,17 +191,17 @@ export function fastGetRootPath<T extends object = any>(value: object): RootPath
export function getRoot<T extends object = any>(value: object): T {
assertTweakedObject(value, "value")

return fastGetRoot(value)
return fastGetRoot(value, true)
}

/**
* @internal
*/
export function fastGetRoot<T extends object = any>(value: object): T {
export function fastGetRoot<T extends object = any>(value: object, useAtom: boolean): T {
let root = value

let parentPath: ParentPath<any> | undefined
while ((parentPath = fastGetParentPath(root))) {
while ((parentPath = fastGetParentPath(root, useAtom))) {
root = parentPath.parent
}

Expand All @@ -206,7 +217,7 @@ export function fastGetRoot<T extends object = any>(value: object): T {
export function isRoot(value: object): boolean {
assertTweakedObject(value, "value")

return !fastGetParent(value)
return !fastGetParent(value, true)
}

const unresolved = { resolved: false } as const
Expand Down Expand Up @@ -347,7 +358,7 @@ export function getParentToChildPath(fromParent: object, toChild: object): Path

let current = toChild
let parentPath
while ((parentPath = fastGetParentPath(current))) {
while ((parentPath = fastGetParentPath(current, true))) {
path.unshift(parentPath.path)

current = parentPath.parent
Expand Down
4 changes: 2 additions & 2 deletions packages/lib/src/parent/path2.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,13 @@ export function isChildOfParent(child: object, parent: object): boolean {
assertTweakedObject(child, "child")
assertTweakedObject(parent, "parent")

let currentParent = fastGetParent(child)
let currentParent = fastGetParent(child, true)
while (currentParent) {
if (currentParent === parent) {
return true
}

currentParent = fastGetParent(currentParent)
currentParent = fastGetParent(currentParent, true)
}

return false
Expand Down
8 changes: 4 additions & 4 deletions packages/lib/src/parent/setParent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ export const setParent = action(
}
}

let oldParentPath = fastGetParentPath(value)
let oldParentPath = fastGetParentPath(value, false)
if (parentPathEquals(oldParentPath, parentPath)) {
return value
}
Expand Down Expand Up @@ -88,7 +88,7 @@ export const setParent = action(
getModelMetadata(value).valueType
) {
value = clone(value, { generateNewIds: true })
oldParentPath = fastGetParentPath(value)
oldParentPath = fastGetParentPath(value, false)
}

if (oldParentPath && parentPath) {
Expand All @@ -109,7 +109,7 @@ export const setParent = action(
let oldRoot: any
let oldRootStore: any
if (valueIsModel) {
oldRoot = fastGetRoot(value)
oldRoot = fastGetRoot(value, false)
oldRootStore = fastIsRootStoreNoAtom(oldRoot) ? oldRoot : undefined
}

Expand All @@ -126,7 +126,7 @@ export const setParent = action(
reportParentPathChanged(value)

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

// invoke model root store events
Expand Down
6 changes: 3 additions & 3 deletions packages/lib/src/patch/emitPatch.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { action, isAction } from "mobx"
import { fastGetParentPath } from "../parent/path"
import type { PathElement } from "../parent/pathTypes"
import { freezeInternalSnapshot, getInternalSnapshot } from "../snapshot/internal"
import { assertTweakedObject } from "../tweaker/core"
import { assertIsFunction, deleteFromArray, isPrimitive } from "../utils"
import type { Patch } from "./Patch"
import { freezeInternalSnapshot, getInternalSnapshot } from "../snapshot/internal"

const emptyPatchArray: Patch[] = []

Expand Down Expand Up @@ -147,12 +147,12 @@ function emitPatch(obj: object, patches: Patch[], inversePatches: Patch[]): void
emitPatchForTarget(obj, patches, inversePatches, pathPrefix)

// and also emit subtree listeners all the way to the root
let parentPath = fastGetParentPath(obj)
let parentPath = fastGetParentPath(obj, false)
while (parentPath) {
pathPrefix.unshift(parentPath.path)
emitPatchForTarget(parentPath.parent, patches, inversePatches, pathPrefix)

parentPath = fastGetParentPath(parentPath.parent)
parentPath = fastGetParentPath(parentPath.parent, false)
}
}

Expand Down
4 changes: 2 additions & 2 deletions packages/lib/src/redux/connectReduxDevTools.ts
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ export function connectReduxDevTools(
}
lastLoggedSnapshot = sn

const rootPath = fastGetRootPath(ctx.target)
const rootPath = fastGetRootPath(ctx.target, false)
const name = getActionContextNameAndTypePath(ctx, rootPath, result)

const copy = {
Expand Down Expand Up @@ -170,7 +170,7 @@ export function connectReduxDevTools(
if (ctx.parentContext) {
const parentName = getActionContextNameAndTypePath(
ctx.parentContext,
fastGetRootPath(ctx.parentContext.target),
fastGetRootPath(ctx.parentContext.target, false),
undefined
)
if (parentName) {
Expand Down
2 changes: 1 addition & 1 deletion packages/lib/src/ref/core.ts
Original file line number Diff line number Diff line change
Expand Up @@ -239,7 +239,7 @@ export function getRefsResolvingTo<T extends object>(
const oldBackRefs = getBackRefs(target, refType)
oldBackRefs.forEach(updateRef)

const refsChildrenOfRoot = getDeepChildrenRefs(getDeepObjectChildren(fastGetRoot(target)))
const refsChildrenOfRoot = getDeepChildrenRefs(getDeepObjectChildren(fastGetRoot(target, true)))
let refs: Set<Ref<object>> | undefined
if (refType) {
refs = refsChildrenOfRoot.byType.get(refType.refClass)
Expand Down
4 changes: 2 additions & 2 deletions packages/lib/src/ref/rootRef.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ export const rootRef: <T extends object>(
let cachedTarget: T | undefined

return () => {
const refRoot = fastGetRoot(ref)
const refRoot = fastGetRoot(ref, true)

if (isRefRootCachedTargetOk(ref, refRoot, cachedTarget, getId)) {
return cachedTarget
Expand Down Expand Up @@ -83,6 +83,6 @@ function isRefRootCachedTargetOk<T extends object>(
): cachedTarget is T {
if (!cachedTarget) return false
if (ref.id !== getId(cachedTarget)) return false
if (refRoot !== fastGetRoot(cachedTarget)) return false
if (refRoot !== fastGetRoot(cachedTarget, true)) return false
return true
}
2 changes: 1 addition & 1 deletion packages/lib/src/rootStore/rootStore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,6 @@ export function fastIsRootStoreNoAtom(node: object): boolean {
export function getRootStore<T extends object>(node: object): T | undefined {
assertTweakedObject(node, "node")

const root = fastGetRoot(node)
const root = fastGetRoot(node, true)
return isRootStore(root) ? root : undefined
}
Loading

0 comments on commit d9b60ba

Please sign in to comment.