@@ -103,6 +103,7 @@ export class SchemaRepository {
103103 private pluginsPromise : Promise < RawApiTypes . Plugin [ ] > | null = null ;
104104 private pluginsById : Map < string , RawApiTypes . Plugin > = new Map ( ) ;
105105 private pluginsByPackageName : Map < string , RawApiTypes . Plugin > = new Map ( ) ;
106+ private prefetchPromise : Promise < void > | null = null ;
106107
107108 /**
108109 * Creates a new SchemaRepository instance.
@@ -457,42 +458,52 @@ export class SchemaRepository {
457458 * @returns Promise that resolves when all data has been fetched and cached
458459 */
459460 async prefetchAllModelsAndFields ( ) : Promise < void > {
460- const { included } = await this . client . site . rawFind ( {
461- include : 'item_types,item_types.fields' ,
462- } ) ;
463-
464- if ( ! included ) {
465- throw new Error ( 'This should not happen' ) ;
461+ if ( this . prefetchPromise ) {
462+ return this . prefetchPromise ;
466463 }
467464
468- const allItemTypes = included . filter (
469- ( item ) : item is RawApiTypes . ItemType => item . type === 'item_type' ,
470- ) ;
471- const allFields = included . filter (
472- ( item ) : item is RawApiTypes . Field => item . type === 'field' ,
473- ) ;
465+ const prefetch = async ( ) => {
466+ const { included } = await this . client . site . rawFind ( {
467+ include : 'item_types,item_types.fields' ,
468+ } ) ;
474469
475- // Populate item types caches
476- this . itemTypesPromise = Promise . resolve ( allItemTypes ) ;
477- for ( const itemType of allItemTypes ) {
478- this . itemTypesByApiKey . set ( itemType . attributes . api_key , itemType ) ;
479- this . itemTypesById . set ( itemType . id , itemType ) ;
480- }
470+ if ( ! included ) {
471+ throw new Error ( 'This should not happen' ) ;
472+ }
481473
482- // Group fields by item type and populate fields cache
483- const fieldsByItemTypeId = new Map < string , RawApiTypes . Field [ ] > ( ) ;
484- for ( const field of allFields ) {
485- const itemTypeId = field . relationships . item_type . data . id ;
486- if ( ! fieldsByItemTypeId . has ( itemTypeId ) ) {
487- fieldsByItemTypeId . set ( itemTypeId , [ ] ) ;
474+ const allItemTypes = included . filter (
475+ ( item ) : item is RawApiTypes . ItemType => item . type === 'item_type' ,
476+ ) ;
477+ const allFields = included . filter (
478+ ( item ) : item is RawApiTypes . Field => item . type === 'field' ,
479+ ) ;
480+
481+ // Populate item types caches
482+ this . itemTypesPromise = Promise . resolve ( allItemTypes ) ;
483+ for ( const itemType of allItemTypes ) {
484+ this . itemTypesByApiKey . set ( itemType . attributes . api_key , itemType ) ;
485+ this . itemTypesById . set ( itemType . id , itemType ) ;
488486 }
489- fieldsByItemTypeId . get ( itemTypeId ) ! . push ( field ) ;
490- }
491487
492- // Populate the fields cache
493- for ( const [ itemTypeId , fields ] of fieldsByItemTypeId ) {
494- this . fieldsByItemType . set ( itemTypeId , fields ) ;
495- }
488+ // Group fields by item type and populate fields cache
489+ const fieldsByItemTypeId = new Map < string , RawApiTypes . Field [ ] > ( ) ;
490+ for ( const field of allFields ) {
491+ const itemTypeId = field . relationships . item_type . data . id ;
492+ if ( ! fieldsByItemTypeId . has ( itemTypeId ) ) {
493+ fieldsByItemTypeId . set ( itemTypeId , [ ] ) ;
494+ }
495+ fieldsByItemTypeId . get ( itemTypeId ) ! . push ( field ) ;
496+ }
497+
498+ // Populate the fields cache
499+ for ( const [ itemTypeId , fields ] of fieldsByItemTypeId ) {
500+ this . fieldsByItemType . set ( itemTypeId , fields ) ;
501+ }
502+ } ;
503+
504+ this . prefetchPromise = prefetch ( ) ;
505+
506+ return this . prefetchPromise ;
496507 }
497508
498509 /**
@@ -535,11 +546,14 @@ export class SchemaRepository {
535546 }
536547
537548 // Check if this field references other block models that might transitively reference our targets
538- const referencedBlocks = referencedBlockIds
539- . map ( ( id ) => allItemTypes . find ( ( it ) => it . id === id ) ! ) ;
549+ const referencedBlocks = referencedBlockIds . map (
550+ ( id ) => allItemTypes . find ( ( it ) => it . id === id ) ! ,
551+ ) ;
540552
541553 for ( const linkedBlock of referencedBlocks ) {
542- if ( await modelPointsToBlocks ( linkedBlock , new Set ( alreadyExplored ) ) ) {
554+ if (
555+ await modelPointsToBlocks ( linkedBlock , new Set ( alreadyExplored ) )
556+ ) {
543557 return true ;
544558 }
545559 }
0 commit comments