Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(): Object dispose #8673

Merged
merged 6 commits into from
Feb 11, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

## [next]

- fix(): object dispose removes canvas/event refs [#8673](https://github.com/fabricjs/fabric.js/issues/8673)
- fix(test): Textbox `fromObject` test is incorrectly trying to restore an instance [#8686](https://github.com/fabricjs/fabric.js/pull/8686)
- TS(): Moved cache properties to static properties on classes [#xxxx](https://github.com/fabricjs/fabric.js/pull/xxxx)
- refactor(): Moved cache properties to static properties on classes [#8662](https://github.com/fabricjs/fabric.js/pull/8662)
Expand Down
16 changes: 11 additions & 5 deletions src/Observable.ts
Original file line number Diff line number Diff line change
Expand Up @@ -123,14 +123,20 @@ export class Observable<EventSpec> {
}

/**
* Stops event observing for a particular event handler. Calling this method
* without arguments removes all handlers for all events
* @param {string} eventName Event name (eg. 'after:render')
* @param {EventRegistryObject} handlers key/value pairs (eg. {'after:render': handler, 'selection:cleared': handler})
* @param {Function} handler Function to be deleted from EventListeners
* unsubscribe an event listener
* @param {string} eventName event name (eg. 'after:render')
* @param {TEventCallback} handler event listener to unsubscribe
*/
off<K extends keyof EventSpec>(eventName: K, handler: TEventCallback): void;
/**
* unsubscribe event listeners
* @param handlers handlers key/value pairs (eg. {'after:render': handler, 'selection:cleared': handler})
*/
off(handlers: EventRegistryObject): void;
/**
* unsubscribe all event listeners
*/
off(): void;
off<K extends keyof EventSpec>(
arg0?: K | EventRegistryObject,
handler?: TEventCallback
Expand Down
4 changes: 2 additions & 2 deletions src/canvas/StaticCanvas.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1654,11 +1654,11 @@ export class StaticCanvas<
this.cancelRequestedRender();
this.forEachObject((object) => object.dispose());
this._objects = [];
if (this.backgroundImage && this.backgroundImage.dispose) {
if (this.backgroundImage) {
this.backgroundImage.dispose();
}
this.backgroundImage = null;
if (this.overlayImage && this.overlayImage.dispose) {
if (this.overlayImage) {
this.overlayImage.dispose();
}
this.overlayImage = null;
Expand Down
8 changes: 3 additions & 5 deletions src/shapes/Object/Object.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1830,11 +1830,9 @@ export class FabricObject<
* override if necessary to dispose artifacts such as `clipPath`
*/
dispose() {
// todo verify this.
// runningAnimations is always truthy
if (runningAnimations) {
runningAnimations.cancelByTarget(this);
}
runningAnimations.cancelByTarget(this);
this.off();
this._set('canvas', undefined);
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

using _set to propagate to all children

}

/**
Expand Down
7 changes: 7 additions & 0 deletions test/unit/object.js
Original file line number Diff line number Diff line change
Expand Up @@ -1316,12 +1316,19 @@
});
QUnit.test('dispose', function (assert) {
var object = new fabric.Object({ fill: 'blue', width: 100, height: 100 });
let off = false;
object.off = () => {
off = true;
}
object.canvas = canvas;
assert.ok(typeof object.dispose === 'function');
object.animate({ fill: 'red' });
const findAnimationsByTarget = target => fabric.runningAnimations.filter(({ target: t }) => target === t);
assert.equal(findAnimationsByTarget(object).length, 1, 'runningAnimations should include the animation');
object.dispose();
assert.equal(findAnimationsByTarget(object).length, 0, 'runningAnimations should be empty after dispose');
assert.ok(!object.canvas, 'cleared canvas');
assert.ok(off, 'unsubscribe events');
});
QUnit.test('prototype changes', function (assert) {
var object = new fabric.Object();
Expand Down