Skip to content

Commit e63b5d1

Browse files
committed
Only prefetch once
1 parent 267e2d5 commit e63b5d1

4 files changed

Lines changed: 53 additions & 39 deletions

File tree

generate/index.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
#!/usr/bin/env -S node --stack_size=800 -r ts-node/register
22

3-
import Handlebars from 'handlebars';
43
import { existsSync, mkdirSync, readFileSync, writeFileSync } from 'node:fs';
4+
import Handlebars from 'handlebars';
55
import rimraf from 'rimraf';
66
import extractInfoFromSchema, {
77
type ResourceInfo,
8-
type SchemaInfo
8+
type SchemaInfo,
99
} from './extractInfoFromSchema';
1010
import toSafeName from './toSafeName';
1111

generate/reExportEverything.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
import { readFileSync, writeFileSync } from 'node:fs';
44
import path from 'node:path';
5-
import { Project, SyntaxKind, type Node } from 'ts-morph';
5+
import { type Node, Project, SyntaxKind } from 'ts-morph';
66

77
const repoRoot = path.resolve(__dirname, '..');
88

generate/setClientVersion.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,9 @@
33
import { readFileSync, writeFileSync } from 'node:fs';
44

55
for (const dir of ['cma-client', 'dashboard-client']) {
6-
const version: string = (JSON.parse(
7-
readFileSync(`./packages/${dir}/package.json`, 'utf8'),
8-
) as any).version;
6+
const version: string = (
7+
JSON.parse(readFileSync(`./packages/${dir}/package.json`, 'utf8')) as any
8+
).version;
99

1010
const sourceFilePath = `./packages/${dir}/src/generated/Client.ts`;
1111

packages/cma-client/src/utilities/schemaRepository.ts

Lines changed: 47 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -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

Comments
 (0)