Skip to content

Fix the timer plugin (init crash, missed load, unstyled)#5

Open
DmitrySharabin wants to merge 3 commits into
mainfrom
fix-timer-this-binding
Open

Fix the timer plugin (init crash, missed load, unstyled)#5
DmitrySharabin wants to merge 3 commits into
mainfrom
fix-timer-this-binding

Conversation

@DmitrySharabin

@DmitrySharabin DmitrySharabin commented Jun 10, 2026

Copy link
Copy Markdown
Member

The timer plugin never actually worked when loaded as an ES module. Three bugs:

1. Crash on init

init-start did this.duration = …, but the handler is a top-level arrow function, so this is the module's top-level value — undefined in ESM. It threw TypeError: Cannot set properties of undefined before the timer was created. (It worked only as a classic script, where top-level this === window.) Fixed with a local variable.

2. Countdown never started on late load

The countdown was started from addEventListener("load", …), but the plugin loads asynchronously and can finish after load already fired — then the listener never runs. Now it starts immediately when document.readyState === "complete", mirroring how autosize handles late loading.

3. Stylesheet never loaded

timer/plugin.css styles the #timer progress bar, but the plugin didn't export const hasCSS = true, so the core loader (which injects plugin.css only when module.hasCSS is truthy) skipped it — the timer rendered unstyled. Added the export.

Verified (Playwright)

No crash; the timer auto-starts even when it loads after load; the #timer bar gets its real styles from timer/plugin.css (the <link id="plugin-css-timer"> is injected, ::before "Progress" label, height: 16px, etc.); and end/overtime classes apply on schedule.

🤖 Generated with Claude Code

DmitrySharabin and others added 2 commits June 10, 2026 19:37
The init-start handler stored duration on `this`, but the handler is a
top-level arrow function, so `this` is the module's top-level value —
`undefined` in ESM. It threw `TypeError: Cannot set properties of
undefined` before the timer was ever created, so the timer never worked.

It only worked when plugins loaded as classic scripts (top-level `this`
=== `window`); they now load as modules. Use a local variable instead.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
The countdown started on the window `load` event, but the plugin loads
asynchronously and can finish after `load` has already fired — then the
listener never ran and the timer never started. Run the start logic
immediately when the document is already loaded, the way the autosize
plugin handles late loading.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
@DmitrySharabin DmitrySharabin changed the title Fix timer plugin crashing on init (this is undefined in ESM) Fix the timer plugin: init crash and missed load event Jun 10, 2026
timer/plugin.css styles the #timer progress bar, but the plugin never
exported `hasCSS`, so the core loader — which injects plugin.css only
when `module.hasCSS` is truthy — skipped it, and the timer rendered
unstyled. Export hasCSS = true, like the other CSS-bearing plugins.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
@DmitrySharabin DmitrySharabin changed the title Fix the timer plugin: init crash and missed load event Fix the timer plugin (init crash, missed load, unstyled) Jun 10, 2026
@DmitrySharabin DmitrySharabin deleted the fix-timer-this-binding branch June 10, 2026 19:40
@DmitrySharabin DmitrySharabin restored the fix-timer-this-binding branch June 10, 2026 19:45
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant