fix: the result preview and the robots.txt tester survive a dropped first action (CR-04)

Both elements sent their first summer-action from connectedCallback and
kept it in flight forever when the host dropped it (the v0.5.1 widget host
appends the element before it keeps a reference). A shared ActionQueue no
longer trusts that an emitted action was taken: one the host did not mark
busy synchronously is retried once on the next task, a second drop shows
an error, and a disconnect forgets the request in flight. The first
request is sent on a microtask. Element tests drive a fake host that
mirrors WidgetField's contract, including the append-first ordering, and
the robots.txt tester gets its own tests.
This commit is contained in:
2026-10-27 15:24:28 +01:00
parent 2d81c261b5
commit 3ecbd1f711
8 changed files with 529 additions and 52 deletions

View File

@@ -1,4 +1,5 @@
import { el, emit, fmt, icon, jsonAttr, labelsFor, uid, uiLocaleOf, type Labels } from './labels'
import { ActionQueue } from './actions'
import { el, fmt, icon, jsonAttr, labelsFor, uid, uiLocaleOf, type Labels } from './labels'
export const ROBOTSTESTER_TAG = 'golem15-seo-robotstester'
@@ -49,8 +50,7 @@ export class SeoRobotsTesterElement extends HTMLElement {
private context: ContextData | null = null
private result: TestData | null = null
private error = ''
private inflight: Payload | null = null
private queued: Payload | null = null
private readonly actions = new ActionQueue<Payload>(this, (payload) => this.failed(payload))
private connected = false
private input: HTMLInputElement | null = null
private select: HTMLSelectElement | null = null
@@ -62,7 +62,7 @@ export class SeoRobotsTesterElement extends HTMLElement {
private readonly onResult = (ev: Event): void => {
const detail = (ev as CustomEvent).detail as { data?: unknown } | undefined
const data = detail?.data as ContextData | TestData | undefined
this.inflight = null
this.actions.settle()
if (data && data.op === 'context') {
this.context = data
this.build(true)
@@ -71,19 +71,25 @@ export class SeoRobotsTesterElement extends HTMLElement {
this.error = ''
this.renderResult()
}
this.flush()
}
connectedCallback(): void {
this.connected = true
this.addEventListener('summer-result', this.onResult)
this.build(false)
this.request({ op: 'context' })
// Deferred: the host may wire the element only after connecting it.
queueMicrotask(() => {
if (this.connected && !this.context) {
this.request({ op: 'context' })
}
})
}
disconnectedCallback(): void {
this.connected = false
this.removeEventListener('summer-result', this.onResult)
// A result that arrives while disconnected is lost; start over on reconnect.
this.actions.reset()
}
attributeChangedCallback(name: string, _old: string | null, value: string | null): void {
@@ -91,10 +97,10 @@ export class SeoRobotsTesterElement extends HTMLElement {
return
}
if (name === 'busy' && value === null) {
this.flush()
this.actions.flush()
}
if (name === 'state' && value === 'error') {
this.inflight = null
this.actions.settle()
}
if (name === 'ui-locale') {
this.build(true)
@@ -108,21 +114,16 @@ export class SeoRobotsTesterElement extends HTMLElement {
}
private request(payload: Payload): void {
if (this.hasAttribute('busy') || this.inflight) {
this.queued = payload
return
}
this.inflight = payload
emit(this, 'summer-action', { payload })
this.actions.request(payload)
}
private flush(): void {
if (this.hasAttribute('busy') || this.inflight || !this.queued) {
return
/** A request the host never took: the tester keeps the default crawlers, a test says it failed. */
private failed(payload: Payload): void {
if (payload.op === 'test') {
this.error = this.labels().testFailed
this.result = null
this.renderResult()
}
const next = this.queued
this.queued = null
this.request(next)
}
private build(force: boolean): void {