Skip to content

Commit

Permalink
more EventHandle.off in critical places
Browse files Browse the repository at this point in the history
  • Loading branch information
Maksims committed Nov 25, 2024
1 parent b002a00 commit 3f60f9f
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 14 deletions.
12 changes: 10 additions & 2 deletions src/framework/components/element/image-element.js
Original file line number Diff line number Diff line change
Expand Up @@ -260,6 +260,12 @@ class ImageRenderable {
}

class ImageElement {
/**
* @type {import('../../../core/event-handle.js').EventHandle|null}
* @private
*/
_evtSetMeshes = null;

constructor(element) {
this._element = element;
this._entity = element.entity;
Expand Down Expand Up @@ -791,7 +797,8 @@ class ImageElement {

// Hook up event handlers on sprite asset
_bindSprite(sprite) {
sprite.on('set:meshes', this._onSpriteMeshesChange, this);
this._evtSetMeshes?.off();
this._evtSetMeshes = sprite.on('set:meshes', this._onSpriteMeshesChange, this);
sprite.on('set:pixelsPerUnit', this._onSpritePpuChange, this);
sprite.on('set:atlas', this._onAtlasTextureChange, this);
if (sprite.atlas) {
Expand All @@ -800,7 +807,8 @@ class ImageElement {
}

_unbindSprite(sprite) {
sprite.off('set:meshes', this._onSpriteMeshesChange, this);
this._evtSetMeshes?.off();
this._evtSetMeshes = null;
sprite.off('set:pixelsPerUnit', this._onSpritePpuChange, this);
sprite.off('set:atlas', this._onAtlasTextureChange, this);
if (sprite.atlas) {
Expand Down
15 changes: 10 additions & 5 deletions src/framework/components/particle-system/component.js
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,12 @@ class ParticleSystemComponent extends Component {
*/
_evtLayerRemoved = null;

/**
* @type {import('../../../core/event-handle.js').EventHandle|null}
* @private
*/
_evtSetMeshes = null;

/**
* Create a new ParticleSystemComponent.
*
Expand Down Expand Up @@ -1735,9 +1741,8 @@ class ParticleSystemComponent extends Component {
asset.off('unload', this._onRenderAssetUnload, this);
asset.off('remove', this._onRenderAssetRemove, this);

if (asset.resource) {
asset.resource.off('set:meshes', this._onRenderSetMeshes, this);
}
this._evtSetMeshes?.off();
this._evtSetMeshes = null;
}

_onRenderAssetLoad(asset) {
Expand All @@ -1758,8 +1763,8 @@ class ParticleSystemComponent extends Component {
return;
}

render.off('set:meshes', this._onRenderSetMeshes, this);
render.on('set:meshes', this._onRenderSetMeshes, this);
this._evtSetMeshes?.off();
this._evtSetMeshes = render.on('set:meshes', this._onRenderSetMeshes, this);

if (render.meshes) {
this._onRenderSetMeshes(render.meshes);
Expand Down
15 changes: 10 additions & 5 deletions src/framework/components/render/component.js
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,12 @@ class RenderComponent extends Component {
*/
_evtLayerRemoved = null;

/**
* @type {import('../../../core/event-handle.js').EventHandle|null}
* @private
*/
_evtSetMeshes = null;

/**
* Create a new RenderComponent.
*
Expand Down Expand Up @@ -908,8 +914,8 @@ class RenderComponent extends Component {

if (this._assetReference.asset) {
const render = this._assetReference.asset.resource;
render.off('set:meshes', this._onSetMeshes, this);
render.on('set:meshes', this._onSetMeshes, this);
this._evtSetMeshes?.off();
this._evtSetMeshes = render.on('set:meshes', this._onSetMeshes, this);
if (render.meshes) {
this._onSetMeshes(render.meshes);
}
Expand Down Expand Up @@ -984,9 +990,8 @@ class RenderComponent extends Component {
}

_onRenderAssetRemove() {
if (this._assetReference.asset && this._assetReference.asset.resource) {
this._assetReference.asset.resource.off('set:meshes', this._onSetMeshes, this);
}
this._evtSetMeshes?.off();
this._evtSetMeshes = null;

this._onRenderAssetUnload();
}
Expand Down
11 changes: 9 additions & 2 deletions src/framework/components/sprite/sprite-animation-clip.js
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,12 @@ class SpriteAnimationClip extends EventHandler {
*/
static EVENT_LOOP = 'loop';

/**
* @type {import('../../../core/event-handle.js').EventHandle|null}
* @private
*/
_evtSetMeshes = null;

/**
* Create a new SpriteAnimationClip instance.
*
Expand Down Expand Up @@ -169,7 +175,8 @@ class SpriteAnimationClip extends EventHandler {
*/
set sprite(value) {
if (this._sprite) {
this._sprite.off('set:meshes', this._onSpriteMeshesChange, this);
this._evtSetMeshes?.off();
this._evtSetMeshes = null;
this._sprite.off('set:pixelsPerUnit', this._onSpritePpuChanged, this);
this._sprite.off('set:atlas', this._onSpriteMeshesChange, this);
if (this._sprite.atlas) {
Expand All @@ -180,7 +187,7 @@ class SpriteAnimationClip extends EventHandler {
this._sprite = value;

if (this._sprite) {
this._sprite.on('set:meshes', this._onSpriteMeshesChange, this);
this._evtSetMeshes = this._sprite.on('set:meshes', this._onSpriteMeshesChange, this);
this._sprite.on('set:pixelsPerUnit', this._onSpritePpuChanged, this);
this._sprite.on('set:atlas', this._onSpriteMeshesChange, this);

Expand Down

0 comments on commit 3f60f9f

Please sign in to comment.