Skip to content

Commit

Permalink
fix(macros/CSSSyntax): cache parsed webref data (#10225)
Browse files Browse the repository at this point in the history
  • Loading branch information
caugner authored Jan 23, 2024
1 parent 6ce14ab commit 078fa86
Showing 1 changed file with 45 additions and 28 deletions.
73 changes: 45 additions & 28 deletions kumascript/src/lib/css-syntax.ts
Original file line number Diff line number Diff line change
Expand Up @@ -98,20 +98,7 @@ export async function getCSSSyntax(
// get the contents of webref
const parsedWebRef = await getParsedWebRef();

// get all the value syntaxes
let values = {};
for (const spec of Object.values(parsedWebRef)) {
// Add parent values.
values = { ...values, ...spec.values };
// Add child values.
[...Object.values(spec.properties), ...Object.values(spec.values)].forEach(
(value) => {
if ("values" in value && Array.isArray(value.values)) {
values = { ...byName(value.values), ...values };
}
}
);
}
const values = await getAllValueSyntaxes();

/**
* Get the spec shortnames for an item, given:
Expand Down Expand Up @@ -529,22 +516,52 @@ export async function getCSSSyntax(
return output;
}

let parsedWebRefCache: null | Promise<WebRefObjectData> = null;
async function getParsedWebRef(): Promise<WebRefObjectData> {
const rawItems = await getRawWebRefData();
if (!parsedWebRefCache) {
parsedWebRefCache = getRawWebRefData().then((rawItems) =>
Object.fromEntries(
Object.entries(rawItems).map(
([name, { spec, properties, atrules, values }]) => [
name,
{
spec,
properties: byName(properties),
atrules: byName(atrules),
values: byName(values),
},
]
)
)
);
}

return Object.fromEntries(
Object.entries(rawItems).map(
([name, { spec, properties, atrules, values }]) => [
name,
{
spec,
properties: byName(properties),
atrules: byName(atrules),
values: byName(values),
},
]
)
);
return parsedWebRefCache;
}

let valueSyntaxesCache = null;
async function getAllValueSyntaxes() {
if (!valueSyntaxesCache) {
valueSyntaxesCache = getParsedWebRef().then((parsedWebRef) => {
let values = {};
for (const spec of Object.values(parsedWebRef)) {
// Add parent values.
values = { ...values, ...spec.values };
// Add child values.
[
...Object.values(spec.properties),
...Object.values(spec.values),
].forEach((value) => {
if ("values" in value && Array.isArray(value.values)) {
values = { ...byName(value.values), ...values };
}
});
}
return values;
});
}

return valueSyntaxesCache;
}

function byName<T extends Named>(items: T[]): Record<string, T> {
Expand Down

0 comments on commit 078fa86

Please sign in to comment.