From aeaeeb6181963757a06d860838c0de47bbd4183e Mon Sep 17 00:00:00 2001 From: Jonathan Olson Date: Wed, 1 Mar 2023 13:23:19 -0700 Subject: [PATCH] Pooling improvements for https://github.com/phetsims/phet-core/issues/103 --- js/display/SVGLinearGradient.ts | 9 ++++++++- js/display/SVGRadialGradient.ts | 5 +++++ js/input/BatchedDOMEvent.ts | 4 +++- js/layout/LayoutProxy.ts | 4 +++- js/layout/constraints/FlowLine.ts | 6 ++++-- js/layout/constraints/GridLine.ts | 6 ++++-- js/util/SpriteInstance.ts | 4 +++- 7 files changed, 30 insertions(+), 8 deletions(-) diff --git a/js/display/SVGLinearGradient.ts b/js/display/SVGLinearGradient.ts index 09d726775..98debbaf3 100644 --- a/js/display/SVGLinearGradient.ts +++ b/js/display/SVGLinearGradient.ts @@ -12,7 +12,12 @@ import Pool, { TPoolable } from '../../../phet-core/js/Pool.js'; import { LinearGradient, scenery, SVGBlock, SVGGradient, svgns } from '../imports.js'; export default class SVGLinearGradient extends SVGGradient implements TPoolable { - public override initialize( svgBlock: SVGBlock, gradient: LinearGradient ): void { + + public constructor( svgBlock: SVGBlock, gradient: LinearGradient ) { + super( svgBlock, gradient ); + } + + public override initialize( svgBlock: SVGBlock, gradient: LinearGradient ): this { sceneryLog && sceneryLog.Paints && sceneryLog.Paints( `[SVGLinearGradient] initialize ${gradient.id}` ); sceneryLog && sceneryLog.Paints && sceneryLog.push(); @@ -37,6 +42,8 @@ export default class SVGLinearGradient extends SVGGradient implements TPoolable this.definition.setAttribute( 'y2', '' + linearGradient.end.y ); sceneryLog && sceneryLog.Paints && sceneryLog.pop(); + + return this; } /** diff --git a/js/display/SVGRadialGradient.ts b/js/display/SVGRadialGradient.ts index 09ab7da89..4cb6eccd9 100644 --- a/js/display/SVGRadialGradient.ts +++ b/js/display/SVGRadialGradient.ts @@ -12,6 +12,11 @@ import Pool, { TPoolable } from '../../../phet-core/js/Pool.js'; import { RadialGradient, scenery, SVGBlock, SVGGradient, svgns } from '../imports.js'; export default class SVGRadialGradient extends SVGGradient implements TPoolable { + + public constructor( svgBlock: SVGBlock, gradient: RadialGradient ) { + super( svgBlock, gradient ); + } + public override initialize( svgBlock: SVGBlock, radialGradient: RadialGradient ): this { sceneryLog && sceneryLog.Paints && sceneryLog.Paints( `[SVGRadialGradient] initialize ${radialGradient.id}` ); sceneryLog && sceneryLog.Paints && sceneryLog.push(); diff --git a/js/input/BatchedDOMEvent.ts b/js/input/BatchedDOMEvent.ts index 1c1acf80f..c454516ee 100644 --- a/js/input/BatchedDOMEvent.ts +++ b/js/input/BatchedDOMEvent.ts @@ -38,13 +38,15 @@ export default class BatchedDOMEvent implements TPoolable { this.initialize( domEvent, type, callback ); } - public initialize( domEvent: Event, type: BatchedDOMEventType, callback: BatchedDOMEventCallback ): void { + public initialize( domEvent: Event, type: BatchedDOMEventType, callback: BatchedDOMEventCallback ): this { // called multiple times due to pooling, this should be re-entrant assert && assert( domEvent, 'for some reason, there is no DOM event?' ); this.domEvent = domEvent; this.type = type; this.callback = callback; + + return this; } public run( input: Input ): void { diff --git a/js/layout/LayoutProxy.ts b/js/layout/LayoutProxy.ts index 098677591..1ccb4fc0b 100644 --- a/js/layout/LayoutProxy.ts +++ b/js/layout/LayoutProxy.ts @@ -45,8 +45,10 @@ export default class LayoutProxy { * * DO not call it twice without in-between disposals (follow the above pattern). */ - public initialize( trail: Trail ): void { + public initialize( trail: Trail ): this { this.trail = trail; + + return this; } private checkPreconditions(): void { diff --git a/js/layout/constraints/FlowLine.ts b/js/layout/constraints/FlowLine.ts index 40f4e0616..e9c799f65 100644 --- a/js/layout/constraints/FlowLine.ts +++ b/js/layout/constraints/FlowLine.ts @@ -28,12 +28,14 @@ export default class FlowLine extends LayoutLine { /** * (scenery-internal) */ - public initialize( orientation: Orientation, cells: FlowCell[] ): void { + public initialize( orientation: Orientation, cells: FlowCell[] ): this { this.orientation = orientation; this.cells = cells; this.initializeLayoutLine(); + + return this; } /** @@ -53,7 +55,7 @@ export default class FlowLine extends LayoutLine { /** * (scenery-internal) */ - public static readonly pool = new Pool( FlowLine, { + public static readonly pool = new Pool( FlowLine, { defaultArguments: [ Orientation.HORIZONTAL, [] ] } ); } diff --git a/js/layout/constraints/GridLine.ts b/js/layout/constraints/GridLine.ts index 908bb11ff..8c1000ae1 100644 --- a/js/layout/constraints/GridLine.ts +++ b/js/layout/constraints/GridLine.ts @@ -28,7 +28,7 @@ export default class GridLine extends LayoutLine { /** * (scenery-internal) */ - public initialize( index: number, cells: GridCell[], grow: number ): void { + public initialize( index: number, cells: GridCell[], grow: number ): this { this.index = index; this.cells = cells; @@ -36,6 +36,8 @@ export default class GridLine extends LayoutLine { this.grow = grow; this.initializeLayoutLine(); + + return this; } /** @@ -48,7 +50,7 @@ export default class GridLine extends LayoutLine { /** * (scenery-internal) */ - public static readonly pool = new Pool( GridLine, { + public static readonly pool = new Pool( GridLine, { defaultArguments: [ 0, [], 0 ] } ); } diff --git a/js/util/SpriteInstance.ts b/js/util/SpriteInstance.ts index 346db534f..3fee4222d 100644 --- a/js/util/SpriteInstance.ts +++ b/js/util/SpriteInstance.ts @@ -59,9 +59,11 @@ export default class SpriteInstance { /** * For pooling. Please use SpriteInstance.dirtyFromPool() to grab a copy */ - private initialize(): void { + public initialize(): this { // We need an empty initialization method here, so that we can grab dirty versions and use them for higher // performance. + + return this; } /**