Skip to content

Visualize Your Ideas With Code

fix: plug memory leaks (#385)

Jacobde0af00a7d2e
diff --git a/packages/2d/src/components/Layout.ts b/packages/2d/src/components/Layout.ts
index 77bde102..09ab1a6b 100644
--- a/packages/2d/src/components/Layout.ts
+++ b/packages/2d/src/components/Layout.ts
@@ -444,8 +444,8 @@ export class Layout extends Node {
@signal()
public declare readonly clip: SimpleSignal<boolean, this>;
- public readonly element: HTMLElement;
- public readonly styles: CSSStyleDeclaration;
+ public element: HTMLElement;
+ public styles: CSSStyleDeclaration;
protected readonly sizeLockCounter = createSignal(0);
@@ -801,6 +801,15 @@ export class Layout extends Node {
}
}
+ public override dispose() {
+ super.dispose();
+ this.sizeLockCounter.context.dispose();
+ this.element.remove();
+ this.element.innerHTML = '';
+ this.element = null as unknown as HTMLElement;
+ this.styles = null as unknown as CSSStyleDeclaration;
+ }
+
public override hit(position: Vector2): Node | null {
const local = position.transformAsPoint(this.localToParent().inverse());
if (this.cacheRect().includes(local)) {
diff --git a/packages/2d/src/components/Node.ts b/packages/2d/src/components/Node.ts
index fe56a91c..ce71216c 100644
--- a/packages/2d/src/components/Node.ts
+++ b/packages/2d/src/components/Node.ts
@@ -379,7 +379,7 @@ export class Node implements Promisable<Node> {
}
}
- private view2D: View2D;
+ protected view2D: View2D;
private realChildren: Node[] = [];
public readonly parent = createSignal<Node | null>(null);
public readonly properties = getPropertiesOf(this);
@@ -870,7 +870,9 @@ export class Node implements Promisable<Node> {
* node to be garbage collected.
*/
public dispose() {
- // do nothing
+ for (const {signal} of this) {
+ signal.context.dispose();
+ }
}
/**
diff --git a/packages/2d/src/components/View2D.ts b/packages/2d/src/components/View2D.ts
index f002a855..f70fedfd 100644
--- a/packages/2d/src/components/View2D.ts
+++ b/packages/2d/src/components/View2D.ts
@@ -30,6 +30,7 @@ export class View2D extends Rect {
fontStyle: 'normal',
...props,
});
+ this.view2D = this;
View2D.shadowRoot.append(this.element);
this.applyFlex();
@@ -41,7 +42,7 @@ export class View2D extends Rect {
public override dispose() {
this.removeChildren();
- this.element.innerText = '';
+ super.dispose();
}
public override render(context: CanvasRenderingContext2D) {
diff --git a/packages/2d/src/scenes/Scene2D.ts b/packages/2d/src/scenes/Scene2D.ts
index f02994f1..0df216a8 100644
--- a/packages/2d/src/scenes/Scene2D.ts
+++ b/packages/2d/src/scenes/Scene2D.ts
@@ -1,37 +1,22 @@
import {
- FullSceneDescription,
GeneratorScene,
Inspectable,
InspectedAttributes,
InspectedElement,
Scene,
SceneRenderEvent,
- ThreadGeneratorFactory,
} from '@motion-canvas/core/lib/scenes';
import {endScene, startScene} from '@motion-canvas/core/lib/utils';
import {Vector2} from '@motion-canvas/core/lib/types';
import {Node, View2D} from '../components';
export class Scene2D extends GeneratorScene<View2D> implements Inspectable {
- private readonly view: View2D;
+ private view: View2D | null = null;
private registeredNodes: Record<string, Node> = {};
private nodeCounters: Record<string, number> = {};
- public constructor(
- description: FullSceneDescription<ThreadGeneratorFactory<View2D>>,
- ) {
- super(description);
- startScene(this);
- const size = this.getSize();
- this.view = new View2D({
- position: size.scale(0.5),
- size,
- });
- endScene(this);
- }
-
public getView(): View2D {
- return this.view;
+ return this.view!;
}
public draw(context: CanvasRenderingContext2D) {
@@ -39,7 +24,7 @@ export class Scene2D extends GeneratorScene<View2D> implements Inspectable {
this.renderLifecycle.dispatch([SceneRenderEvent.BeforeRender, context]);
context.save();
this.renderLifecycle.dispatch([SceneRenderEvent.BeginRender, context]);
- this.view.render(context);
+ this.getView().render(context);
this.renderLifecycle.dispatch([SceneRenderEvent.FinishRender, context]);
context.restore();
this.renderLifecycle.dispatch([SceneRenderEvent.AfterRender, context]);
@@ -52,16 +37,21 @@ export class Scene2D extends GeneratorScene<View2D> implements Inspectable {
}
this.registeredNodes = {};
this.nodeCounters = {};
- this.registerNode(this.view, this.view.key);
+
+ startScene(this);
const size = this.getSize();
- this.view.position(size.scale(0.5));
- this.view.size(size);
+ this.view = new View2D({
+ position: size.scale(0.5),
+ size,
+ });
+ endScene(this);
+
return super.reset(previousScene);
}
public inspectPosition(x: number, y: number): InspectedElement | null {
startScene(this);
- const node = this.view.hit(new Vector2(x, y))?.key ?? null;
+ const node = this.getView().hit(new Vector2(x, y))?.key ?? null;
endScene(this);
return node;
}
diff --git a/packages/core/src/signals/ComputedContext.ts b/packages/core/src/signals/ComputedContext.ts
index 104f0c4a..cbd527a2 100644
--- a/packages/core/src/signals/ComputedContext.ts
+++ b/packages/core/src/signals/ComputedContext.ts
@@ -21,10 +21,14 @@ export class ComputedContext<TValue> extends DependencyContext<any> {
return this.invokable;
}
+ public override dispose() {
+ super.dispose();
+ this.last = undefined;
+ }
+
protected override invoke(...args: any[]): TValue {
if (this.event.isRaised()) {
- this.dependencies.forEach(dep => dep.unsubscribe(this.markDirty));
- this.dependencies.clear();
+ this.clearDependencies();
this.startCollecting();
try {
this.last = this.factory(...args);
diff --git a/packages/core/src/signals/DependencyContext.ts b/packages/core/src/signals/DependencyContext.ts
index d11e2c91..3d1db981 100644
--- a/packages/core/src/signals/DependencyContext.ts
+++ b/packages/core/src/signals/DependencyContext.ts
@@ -56,7 +56,7 @@ export class DependencyContext<TOwner = void>
protected event = new FlagDispatcher();
protected markDirty = () => this.event.raise();
- public constructor(protected readonly owner: TOwner) {
+ public constructor(protected owner: TOwner) {
this.invokable = this.invoke.bind(this);
Object.defineProperty(this.invokable, 'context', {
@@ -92,6 +92,11 @@ export class DependencyContext<TOwner = void>
}
}
+ protected clearDependencies() {
+ this.dependencies.forEach(dep => dep.unsubscribe(this.markDirty));
+ this.dependencies.clear();
+ }
+
protected collect() {
const signal = DependencyContext.collectionStack.at(-1);
if (signal) {
@@ -100,6 +105,11 @@ export class DependencyContext<TOwner = void>
}
}
+ public dispose() {
+ this.clearDependencies();
+ this.owner = null as TOwner;
+ }
+
public async toPromise(): Promise<this> {
let promises = DependencyContext.consumePromises();
do {
diff --git a/packages/core/src/signals/SignalContext.ts b/packages/core/src/signals/SignalContext.ts
index 350f347a..769595ac 100644
--- a/packages/core/src/signals/SignalContext.ts
+++ b/packages/core/src/signals/SignalContext.ts
@@ -121,11 +121,7 @@ export class SignalContext<
this.current = value;
this.markDirty();
-
- if (this.dependencies.size > 0) {
- this.dependencies.forEach(dep => dep.unsubscribe(this.markDirty));
- this.dependencies.clear();
- }
+ this.clearDependencies();
if (!isReactive(value)) {
this.last = this.parse(value);
@@ -136,8 +132,7 @@ export class SignalContext<
public get(): TValue {
if (this.event.isRaised() && isReactive(this.current)) {
- this.dependencies.forEach(dep => dep.unsubscribe(this.markDirty));
- this.dependencies.clear();
+ this.clearDependencies();
this.startCollecting();
try {
this.last = this.parse(this.current());
@@ -237,6 +232,13 @@ export class SignalContext<
});
}
+ public override dispose() {
+ super.dispose();
+ this.initial = undefined;
+ this.current = undefined;
+ this.last = undefined;
+ }
+
/**
* Reset the signal to its initial value (if one has been set).
*