Skip to content

fix(yivi): start decrypt session on DOM readiness instead of fixed delay#118

Merged
rubenhensen merged 1 commit into
mainfrom
fix/decrypt-session-dom-readiness
Jul 24, 2026
Merged

fix(yivi): start decrypt session on DOM readiness instead of fixed delay#118
rubenhensen merged 1 commit into
mainfrom
fix/decrypt-session-dom-readiness

Conversation

@dobby-coder

@dobby-coder dobby-coder Bot commented Jul 23, 2026

Copy link
Copy Markdown
Contributor

Closes #110

What

retrieveUSKViaYivi in src/yivi/decrypt-session.ts always waited a hardcoded 500 ms setTimeout before handing the host element to yivi-core, with the comment "Small delay to ensure DOM element is ready". That fixed delay ran on every decrypt session even when the element was already mounted.

This replaces it with waitForElement, which resolves as soon as the target element is present in the DOM. When the element is already there it resolves on the same tick, so the session starts immediately. When it is not yet mounted, a MutationObserver watches for it, with a bounded 5 s fallback timeout so a never-appearing element can't hang the session. This mirrors the signing flow (src/signing/yivi.ts), which builds YiviCore with no delay at all.

Tests

Added a waitForElement regression suite to tests/decrypt-session.test.ts covering:

  • resolves immediately in a non-DOM environment;
  • resolves without any fixed delay when the element is already present (fails if a setTimeout is reintroduced on the ready path — verified against the old code);
  • resolves as soon as the element appears via the observer, not before;
  • falls back to the bounded timeout if the element never appears.

Full suite (npm test) and npm run typecheck pass.

retrieveUSKViaYivi always waited a hardcoded 500ms setTimeout before
handing the host element to yivi-core. Replace it with waitForElement,
which resolves as soon as the target element is in the DOM (mirroring the
signing flow, which builds YiviCore with no delay), watching via
MutationObserver with a bounded fallback timeout when the element is not
yet mounted.

Closes #110
@dobby-coder
dobby-coder Bot requested a review from rubenhensen July 23, 2026 17:16

@dobby-coder dobby-coder Bot left a comment

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

VERDICT: approve

Rules Dobby 2 — sign-off. No blocking issues found.

Scope check: the change is confined to the timing workaround. waitForElement replaces the hardcoded setTimeout(500) in retrieveUSKViaYivi, resolving on the same tick when the host element is already present (the common case) and watching via MutationObserver with a bounded 5 s fallback otherwise. element is the CSS selector string yivi-core already receives and that the signing flow passes to document.querySelector, so the readiness check is semantically correct and mirrors the no-delay signing path.

Verified:

  • Correctness: promise settles on every path, observer and timer are both cleaned up in finish(), no leak or hang. Confirmed by two independent checks.
  • Tests: four-case regression suite added, including a guard that fails if a fixed delay is reintroduced on the ready path. Full suite and typecheck pass.
  • Title is conventional (fix(yivi): ...), body has Closes #110, no vi.mock hoisting issue, no writing-rule violations.

One process item fixed during sign-off: reviewer @rubenhensen was assigned (PR had none).

Review Dobby 2 reported 0 findings; rule sweep added none. Approving and flipping out of draft.

@rubenhensen
rubenhensen merged commit 6cbc6cf into main Jul 24, 2026
5 checks passed
@rubenhensen
rubenhensen deleted the fix/decrypt-session-dom-readiness branch July 24, 2026 14:06
@github-actions

Copy link
Copy Markdown

🎉 This PR is included in version 2.3.3 🎉

The release is available on:

Your semantic-release bot 📦🚀

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Replace hardcoded 500ms delay before Yivi decrypt session with a readiness check

1 participant