Skip to content

Commit

Permalink
experiment reduce amount of checks in runtime (#174)
Browse files Browse the repository at this point in the history
  • Loading branch information
lifeart authored Sep 16, 2024
1 parent 86de47b commit 862965b
Showing 1 changed file with 24 additions and 22 deletions.
46 changes: 24 additions & 22 deletions src/utils/runtime.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,9 @@ export function takeRenderingControl() {
hasExternalUpdate = true;
return () => {
hasExternalUpdate = false;
}
};
}

export function scheduleRevalidate() {
if (hasExternalUpdate) {
return;
Expand All @@ -33,39 +33,41 @@ export function scheduleRevalidate() {
}
}
revalidateScheduled = true;
Promise.resolve().then(async () => {
await syncDom();
if (resolveRender !== undefined) {
resolveRender();
resolveRender = undefined;
queueMicrotask(async () => {
try {
await syncDom();
if (resolveRender !== undefined) {
resolveRender();
resolveRender = undefined;
}
} finally {
revalidateScheduled = false;
}
revalidateScheduled = false;
});
}
}
export async function syncDom() {
const sharedTags = new Set<MergedCell>();
const sharedTags: MergedCell[] = [];
const executedTags: WeakSet<MergedCell> = new WeakSet();
setIsRendering(true);
for (const cell of tagsToRevalidate) {
await executeTag(cell);
// we always have related tags
if (relatedTags.has(cell)) {
const subTags = relatedTags.get(cell)!;

const subTags = relatedTags.get(cell)!;
if (subTags !== undefined) {
relatedTags.delete(cell);
subTags.forEach((tag) => {
sharedTags.add(tag);
});
sharedTags.push(...subTags.values());
subTags.clear();
}
}
tagsToRevalidate.clear();
// sort shared tags by id
const sharedTagsArray = Array.from(sharedTags);
sharedTags.clear();
// sort tags in order of creation to avoid stale logic
sharedTagsArray.sort((a, b) => a.id - b.id);
for (const tag of sharedTagsArray) {
await executeTag(tag);
sharedTags.sort((a, b) => a.id - b.id);
for (const tag of sharedTags) {
if (!executedTags.has(tag)) {
executedTags.add(tag);
await executeTag(tag);
}
}
tagsToRevalidate.clear();
setIsRendering(false);
}

0 comments on commit 862965b

Please sign in to comment.