From dcab3dec9107be23c3599468a095d34680c37640 Mon Sep 17 00:00:00 2001 From: Michael Kauzmann Date: Mon, 1 Apr 2024 16:31:39 -0600 Subject: [PATCH] better handling of Nodes that delay their content creation, https://github.com/phetsims/binder/issues/27 Signed-off-by: Michael Kauzmann --- js/documentation/InstanceRegistry.ts | 28 ++++++++++++++++++++++++---- 1 file changed, 24 insertions(+), 4 deletions(-) diff --git a/js/documentation/InstanceRegistry.ts b/js/documentation/InstanceRegistry.ts index bae2398..8cbfcaa 100644 --- a/js/documentation/InstanceRegistry.ts +++ b/js/documentation/InstanceRegistry.ts @@ -8,15 +8,26 @@ */ import phetCore from '../phetCore.js'; +import Bounds2 from '../../../dot/js/Bounds2.js'; +import TReadOnlyProperty from '../../../axon/js/TReadOnlyProperty.js'; type NodeLike = { toDataURL: ( callback: ( data: string ) => void ) => void; + boundsProperty: TReadOnlyProperty; }; +type ComponentMap = Record; + +function registerImplementation( instance: NodeLike, key: string, map: ComponentMap ): void { + instance.toDataURL( dataURL => { + map[ key ].push( dataURL ); + } ); +} + class InstanceRegistry { // Per named component, store image URIs of what their usages look like - public static componentMap: Record = {}; + public static componentMap: ComponentMap = {}; /** * Adds a screenshot of the given scenery Node @@ -29,9 +40,18 @@ class InstanceRegistry { InstanceRegistry.componentMap[ key ] = InstanceRegistry.componentMap[ key ] || []; try { - instance.toDataURL( dataURL => { - InstanceRegistry.componentMap[ key ].push( dataURL ); - } ); + if ( instance.boundsProperty.value.isFinite() ) { + registerImplementation( instance, key, InstanceRegistry.componentMap ); + } + else { + const boundsListener = ( bounds: Bounds2 ) => { + if ( bounds.isFinite() ) { + registerImplementation( instance, key, InstanceRegistry.componentMap ); + instance.boundsProperty.unlink( boundsListener ); // less for memory, and more to not double add + } + }; + instance.boundsProperty.lazyLink( boundsListener ); + } } catch( e ) {