Skip to content

Commit

Permalink
better handling of Nodes that delay their content creation, phetsims/…
Browse files Browse the repository at this point in the history
…binder#27

Signed-off-by: Michael Kauzmann <michael.kauzmann@colorado.edu>
  • Loading branch information
zepumph committed Apr 1, 2024
1 parent 9afb48e commit dcab3de
Showing 1 changed file with 24 additions and 4 deletions.
28 changes: 24 additions & 4 deletions js/documentation/InstanceRegistry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<Bounds2>;
};

type ComponentMap = Record<string, string[]>;

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<string, string[]> = {};
public static componentMap: ComponentMap = {};

/**
* Adds a screenshot of the given scenery Node
Expand All @@ -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 ) {

Expand Down

1 comment on commit dcab3de

@zepumph
Copy link
Member Author

@zepumph zepumph commented on dcab3de Apr 5, 2024

Choose a reason for hiding this comment

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

Please sign in to comment.