Skip to content

Commit

Permalink
fix memory leak in BackgroundNode, #850, phetsims/graphing-lines#153
Browse files Browse the repository at this point in the history
(cherry picked from commit e3797d4)
  • Loading branch information
pixelzoom committed Apr 9, 2024
1 parent a168473 commit f12ace6
Showing 1 changed file with 9 additions and 1 deletion.
10 changes: 9 additions & 1 deletion js/BackgroundNode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import optionize from '../../phet-core/js/optionize.js';
import StrictOmit from '../../phet-core/js/types/StrictOmit.js';
import { Node, NodeOptions, Rectangle, RectangleOptions } from '../../scenery/js/imports.js';
import sceneryPhet from './sceneryPhet.js';
import Bounds2 from '../../dot/js/Bounds2.js';

type SelfOptions = {
xMargin?: number; // set the x margin between the Node content and background edge
Expand Down Expand Up @@ -50,11 +51,18 @@ export default class BackgroundNode extends Node {
const wrapperNode = new Node( { children: [ node ] } );

// Size the background rectangle to fit the Node.
node.boundsProperty.link( bounds => {
const boundsListener = ( bounds: Bounds2 ) => {
if ( !bounds.isEmpty() ) {
this.background.setRect( 0, 0, node.width + 2 * options.xMargin, node.height + 2 * options.yMargin );
wrapperNode.center = this.background.center;
}
};
node.boundsProperty.link( boundsListener );

this.disposeEmitter.addListener( () => {
if ( node.boundsProperty.hasListener( boundsListener ) ) {
node.boundsProperty.unlink( boundsListener );
}
} );

options.children = [ this.background, wrapperNode ];
Expand Down

0 comments on commit f12ace6

Please sign in to comment.