(embedEl: HTMLElement, plugin: CodeSpacePlugin, sourcePath: string)
| 584 | } |
| 585 | |
| 586 | function scheduleProcessCodeEmbed(embedEl: HTMLElement, plugin: CodeSpacePlugin, sourcePath: string) { |
| 587 | // Obsidian may update embed attributes shortly after insertion; debounce to avoid duplicate renders. |
| 588 | rememberSourcePath(embedEl, sourcePath); |
| 589 | pendingEmbedRequests.set(embedEl, { sourcePath }); |
| 590 | |
| 591 | const ownerWindow = embedEl.ownerDocument.defaultView ?? window; |
| 592 | const existing = pendingEmbedTimers.get(embedEl); |
| 593 | if (existing) ownerWindow.clearTimeout(existing); |
| 594 | |
| 595 | const timer = ownerWindow.setTimeout(() => { |
| 596 | pendingEmbedTimers.delete(embedEl); |
| 597 | const req = pendingEmbedRequests.get(embedEl); |
| 598 | pendingEmbedRequests.delete(embedEl); |
| 599 | |
| 600 | // Token-gate async read/render so stale runs can't overwrite newer renders (prevents flicker). |
| 601 | const token = (embedRenderTokens.get(embedEl) ?? 0) + 1; |
| 602 | embedRenderTokens.set(embedEl, token); |
| 603 | |
| 604 | void processCodeEmbed(embedEl, plugin, req?.sourcePath ?? sourcePath, token); |
| 605 | }, 40); |
| 606 | |
| 607 | pendingEmbedTimers.set(embedEl, timer); |
| 608 | } |
| 609 | |
| 610 | export function registerCodeEmbedProcessor(plugin: CodeSpacePlugin) { |
| 611 | // Install observer for the main window document to catch any embeds that the post processor misses. |
no test coverage detected