Skip to content

Commit

Permalink
fix: parent reference missing, dispose (#13)
Browse files Browse the repository at this point in the history
  • Loading branch information
Konstantin Timoshenko authored Mar 21, 2022
1 parent b9c7122 commit 9459cc4
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 2 deletions.
18 changes: 16 additions & 2 deletions src/reconciler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -84,9 +84,14 @@ export const switchInstance = (instance: Instance, type: string, props: Instance
instance.children = []
}

if (instance.__attached) {
Object.values(instance.__attached).forEach((attach) => appendChild(newInstance, attach))
}

const parent = instance.parent;
// Replace instance in scene-graph
removeChild(instance.parent, instance)
appendChild(instance.parent, newInstance)
removeChild(parent, instance)
appendChild(parent, newInstance)

return newInstance
}
Expand All @@ -100,6 +105,8 @@ export const appendChild = (parentInstance: Instance, child: Instance) => {
// Attach material, geometry, fog, etc.
if (child.attach) {
parentInstance[child.attach] = child
parentInstance.__attached = parentInstance.__attached || {};
parentInstance.__attached[child.attach] = child;
} else {
child.setParent?.(parentInstance)
}
Expand All @@ -118,6 +125,13 @@ export const removeChild = (parentInstance: Instance, child: Instance) => {
parentInstance?.removeChild?.(child)
}

if (parentInstance?.__attached?.[child.attach]) {
delete parentInstance.__attached[child.attach];
parentInstance[child.attach] = null;
}

if (child.dispose) return child.dispose()
if (child.remove) return child.remove()
// TODO: handle dispose
}

Expand Down
3 changes: 3 additions & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,11 @@ export type Catalogue = { [key: string]: any }
export type BaseInstance = Omit<OGL.Transform, 'children' | 'attach'> & {
isPrimitive?: boolean
__handlers?: EventHandlers
__attached?: Record<string, BaseInstance>;
children: Instance[]
attach?: string
remove?(): void
dispose?(): void
}

/**
Expand Down

0 comments on commit 9459cc4

Please sign in to comment.