Skip to content

Commit

Permalink
fix(core): fixing cache issues on build (#1056)
Browse files Browse the repository at this point in the history
* fix(core): fixing cache issues on build

* Create red-actors-draw.md
  • Loading branch information
boyney123 authored Dec 19, 2024
1 parent dd5a8f2 commit c8f1847
Show file tree
Hide file tree
Showing 8 changed files with 70 additions and 34 deletions.
5 changes: 5 additions & 0 deletions .changeset/red-actors-draw.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@eventcatalog/core": patch
---

fix(core): fixing cache issues on build
15 changes: 10 additions & 5 deletions eventcatalog/src/utils/channels.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,16 @@ interface Props {
}

// cache for build time
export let cachedChannels: Channel[] = [];
let cachedChannels: Record<string, Channel[]> = {
allVersions: [],
currentVersions: [],
};

export const getChannels = async ({ getAllVersions = true }: Props = {}): Promise<Channel[]> => {
if (cachedChannels.length > 0) {
return cachedChannels;
const cacheKey = getAllVersions ? 'allVersions' : 'currentVersions';

if (cachedChannels[cacheKey].length > 0) {
return cachedChannels[cacheKey];
}

const channels = await getCollection('channels', (query) => {
Expand All @@ -34,7 +39,7 @@ export const getChannels = async ({ getAllVersions = true }: Props = {}): Promis
const { commands, events, queries } = await getMessages();
const allMessages = [...commands, ...events, ...queries];

cachedChannels = channels.map((channel) => {
cachedChannels[cacheKey] = channels.map((channel) => {
const { latestVersion, versions } = getVersionForCollectionItem(channel, channels);

const messagesForChannel = allMessages.filter((message) => {
Expand Down Expand Up @@ -69,5 +74,5 @@ export const getChannels = async ({ getAllVersions = true }: Props = {}): Promis
};
});

return cachedChannels;
return cachedChannels[cacheKey];
};
8 changes: 4 additions & 4 deletions eventcatalog/src/utils/collections/domains.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,10 @@ let cachedDomains: Record<string, Domain[]> = {
export const getDomains = async ({ getAllVersions = true }: Props = {}): Promise<Domain[]> => {
const cacheKey = getAllVersions ? 'allVersions' : 'currentVersions';

// // Check if we have cached domains for this specific getAllVersions value
// if (cachedDomains[cacheKey].length > 0) {
// return cachedDomains[cacheKey];
// }
// Check if we have cached domains for this specific getAllVersions value
if (cachedDomains[cacheKey].length > 0) {
return cachedDomains[cacheKey];
}

// Get all the domains that are not versioned
const domains = await getCollection('domains', (domain) => {
Expand Down
15 changes: 10 additions & 5 deletions eventcatalog/src/utils/collections/flows.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,16 @@ interface Props {
}

// Cache for build time
let cachedFlows: Flow[] = [];
let cachedFlows: Record<string, Flow[]> = {
allVersions: [],
currentVersions: [],
};

export const getFlows = async ({ getAllVersions = true }: Props = {}): Promise<Flow[]> => {
if (cachedFlows.length > 0) {
return cachedFlows;
const cacheKey = getAllVersions ? 'allVersions' : 'currentVersions';

if (cachedFlows[cacheKey].length > 0) {
return cachedFlows[cacheKey];
}

// Get flows that are not versioned
Expand All @@ -30,7 +35,7 @@ export const getFlows = async ({ getAllVersions = true }: Props = {}): Promise<F
const allMessages = [...events, ...commands];

// @ts-ignore // TODO: Fix this type
cachedFlows = flows.map((flow) => {
cachedFlows[cacheKey] = flows.map((flow) => {
// @ts-ignore
const { latestVersion, versions } = getVersionForCollectionItem(flow, flows);
const steps = flow.data.steps || [];
Expand Down Expand Up @@ -64,5 +69,5 @@ export const getFlows = async ({ getAllVersions = true }: Props = {}): Promise<F
};
});

return cachedFlows;
return cachedFlows[cacheKey];
};
16 changes: 11 additions & 5 deletions eventcatalog/src/utils/collections/services.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,17 @@ interface Props {
}

// Cache for build time
let cachedServices: Service[] = [];
let cachedServices: Record<string, Service[]> = {
allVersions: [],
currentVersions: [],
};

export const getServices = async ({ getAllVersions = true }: Props = {}): Promise<Service[]> => {
if (cachedServices.length > 0) {
return cachedServices;
const cacheKey = getAllVersions ? 'allVersions' : 'currentVersions';

// Check if we have cached domains for this specific getAllVersions value
if (cachedServices[cacheKey].length > 0) {
return cachedServices[cacheKey];
}

// Get services that are not versioned
Expand All @@ -31,7 +37,7 @@ export const getServices = async ({ getAllVersions = true }: Props = {}): Promis
const allMessages = [...events, ...commands, ...queries];

// @ts-ignore // TODO: Fix this type
cachedServices = services.map((service) => {
cachedServices[cacheKey] = services.map((service) => {
const { latestVersion, versions } = getVersionForCollectionItem(service, services);

const sendsMessages = service.data.sends || [];
Expand Down Expand Up @@ -73,7 +79,7 @@ export const getServices = async ({ getAllVersions = true }: Props = {}): Promis
};
});

return cachedServices;
return cachedServices[cacheKey];
};

export const getProducersOfMessage = (services: Service[], message: CollectionEntry<'events' | 'commands' | 'queries'>) => {
Expand Down
15 changes: 10 additions & 5 deletions eventcatalog/src/utils/commands.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,16 @@ interface Props {
}

// cache for build time
export let cachedCommands: Command[] = [];
let cachedCommands: Record<string, Command[]> = {
allVersions: [],
currentVersions: [],
};

export const getCommands = async ({ getAllVersions = true }: Props = {}): Promise<Command[]> => {
if (cachedCommands.length > 0) {
return cachedCommands;
const cacheKey = getAllVersions ? 'allVersions' : 'currentVersions';

if (cachedCommands[cacheKey].length > 0) {
return cachedCommands[cacheKey];
}

const commands = await getCollection('commands', (command) => {
Expand All @@ -32,7 +37,7 @@ export const getCommands = async ({ getAllVersions = true }: Props = {}): Promis
const services = await getCollection('services');
const allChannels = await getCollection('channels');

cachedCommands = commands.map((command) => {
cachedCommands[cacheKey] = commands.map((command) => {
const { latestVersion, versions } = getVersionForCollectionItem(command, commands);

const producers = services.filter((service) => {
Expand Down Expand Up @@ -75,5 +80,5 @@ export const getCommands = async ({ getAllVersions = true }: Props = {}): Promis
};
});

return cachedCommands;
return cachedCommands[cacheKey];
};
15 changes: 10 additions & 5 deletions eventcatalog/src/utils/events.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,16 @@ interface Props {
}

// cache for build time
export let cachedEvents: Event[] = [];
let cachedEvents: Record<string, Event[]> = {
allVersions: [],
currentVersions: [],
};

export const getEvents = async ({ getAllVersions = true }: Props = {}): Promise<Event[]> => {
if (cachedEvents.length > 0) {
return cachedEvents;
const cacheKey = getAllVersions ? 'allVersions' : 'currentVersions';

if (cachedEvents[cacheKey].length > 0) {
return cachedEvents[cacheKey];
}

const events = await getCollection('events', (event) => {
Expand All @@ -32,7 +37,7 @@ export const getEvents = async ({ getAllVersions = true }: Props = {}): Promise<
const services = await getCollection('services');
const allChannels = await getCollection('channels');

cachedEvents = events.map((event) => {
cachedEvents[cacheKey] = events.map((event) => {
const { latestVersion, versions } = getVersionForCollectionItem(event, events);

const producers = services.filter((service) =>
Expand Down Expand Up @@ -75,5 +80,5 @@ export const getEvents = async ({ getAllVersions = true }: Props = {}): Promise<
};
});

return cachedEvents;
return cachedEvents[cacheKey];
};
15 changes: 10 additions & 5 deletions eventcatalog/src/utils/queries.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,16 @@ interface Props {
}

// Cache for build time
let cachedQueries: Query[] = [];
let cachedQueries: Record<string, Query[]> = {
allVersions: [],
currentVersions: [],
};

export const getQueries = async ({ getAllVersions = true }: Props = {}): Promise<Query[]> => {
if (cachedQueries.length > 0) {
return cachedQueries;
const cacheKey = getAllVersions ? 'allVersions' : 'currentVersions';

if (cachedQueries[cacheKey].length > 0) {
return cachedQueries[cacheKey];
}

const queries = await getCollection('queries', (query) => {
Expand All @@ -32,7 +37,7 @@ export const getQueries = async ({ getAllVersions = true }: Props = {}): Promise
const services = await getCollection('services');
const allChannels = await getCollection('channels');

cachedQueries = queries.map((query) => {
cachedQueries[cacheKey] = queries.map((query) => {
const { latestVersion, versions } = getVersionForCollectionItem(query, queries);

const producers = services.filter((service) =>
Expand Down Expand Up @@ -75,5 +80,5 @@ export const getQueries = async ({ getAllVersions = true }: Props = {}): Promise
};
});

return cachedQueries;
return cachedQueries[cacheKey];
};

0 comments on commit c8f1847

Please sign in to comment.