core/loaders/payload.loader.ts
core/loaders/payload.loader.ts is a file in Bane's Lab Site. 31 lines of code and 7 definitions.
import { PAYLOAD_UNAVAILABLE } from "#configuration/strings/report.strings";
import { reportFailure } from "#core/reporters/failure.reporter";
const held = new Map<string, Promise<unknown>>();
const fetchPayload = async function fetchPayload(path: string): Promise<unknown> {
try {
const response = await fetch(path);
if (!response.ok) {
reportFailure(PAYLOAD_UNAVAILABLE, path);
return null;
}
const data: unknown = await response.json();
return data;
} catch (error) {
reportFailure(PAYLOAD_UNAVAILABLE, error);
return null;
}
};
export const loadPayload = async function loadPayload(path: string): Promise<unknown> {
const pending = held.get(path);
if (pending !== undefined) {
return pending;
}
const loading = fetchPayload(path).then((payload) => {
if (payload === null) {
held.delete(path);
}
return payload;
});
held.set(path, loading);
return loading;
};