(params?: {
resolveStartSeconds?: (element: Element) => number;
})
| 2 | import { swallow } from "../diagnostics"; |
| 3 | |
| 4 | export function createCssAdapter(params?: { |
| 5 | resolveStartSeconds?: (element: Element) => number; |
| 6 | }): RuntimeDeterministicAdapter { |
| 7 | let entries: Array<{ |
| 8 | el: HTMLElement; |
| 9 | baseDelay: string; |
| 10 | basePlayState: string; |
| 11 | animations: Animation[]; |
| 12 | }> = []; |
| 13 | |
| 14 | const getAnimationsForElement = (el: HTMLElement): Animation[] => { |
| 15 | if (typeof el.getAnimations !== "function") return []; |
| 16 | try { |
| 17 | return el.getAnimations(); |
| 18 | } catch { |
| 19 | return []; |
| 20 | } |
| 21 | }; |
| 22 | |
| 23 | const resolveEntryStartSeconds = (el: HTMLElement): number => |
| 24 | params?.resolveStartSeconds |
| 25 | ? params.resolveStartSeconds(el) |
| 26 | : Number.parseFloat(el.getAttribute("data-start") ?? "0") || 0; |
| 27 | |
| 28 | /** |
| 29 | * End time (seconds, relative to composition start) for one WAAPI |
| 30 | * animation handle. `endSeconds` is set only when the timing is readable |
| 31 | * AND finite; `unbounded` is true when a timing was read but its endTime |
| 32 | * is Infinity/NaN (an infinite iteration count the caller can't |
| 33 | * auto-infer a duration from) — distinct from "no timing available at |
| 34 | * all" (both fields absent), which callers should simply skip. |
| 35 | */ |
| 36 | const inferAnimationEndSeconds = ( |
| 37 | animation: Animation, |
| 38 | startSeconds: number, |
| 39 | ): { endSeconds?: number; unbounded?: true } => { |
| 40 | let timing: { endTime?: number | string } | null = null; |
| 41 | try { |
| 42 | timing = animation.effect?.getComputedTiming?.() ?? null; |
| 43 | } catch (err) { |
| 44 | swallow("runtime.adapters.css.site5", err); |
| 45 | } |
| 46 | if (!timing) return {}; |
| 47 | const endTimeMs = Number(timing.endTime); |
| 48 | if (!Number.isFinite(endTimeMs)) return { unbounded: true }; |
| 49 | return { endSeconds: startSeconds + endTimeMs / 1000 }; |
| 50 | }; |
| 51 | |
| 52 | const seekAnimations = (animations: Animation[], timeMs: number) => { |
| 53 | for (const animation of animations) { |
| 54 | try { |
| 55 | animation.currentTime = timeMs; |
| 56 | } catch (err) { |
| 57 | // ignore animations that reject currentTime writes |
| 58 | swallow("runtime.adapters.css.site1", err); |
| 59 | } |
| 60 | try { |
| 61 | animation.pause(); |
no test coverage detected