choo121600 commented on code in PR #74917: URL: https://github.com/apache/airflow/pull/74917#discussion_r4597024226
########## airflow-core/src/airflow/ui/src/components/SmoothOperator/Record.tsx: ########## @@ -0,0 +1,267 @@ +/*! + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +import { useEffect, useId, useRef, useState } from "react"; + +import { Box, chakra } from "@chakra-ui/react"; + +import { AirflowPin } from "src/assets/AirflowPin"; + +import { + ARM_LEAD_IN, + ARM_REST, + COLORS, + COPY, + cue, + CUE_MS, + prefersReducedMotion, + SIDE_MS, + TABLET_PORTRAIT, + track, +} from "./stage"; + +// 33⅓ rpm is one turn every 1.8 s. The platter turns at speed for everyone: a record that is +// playing has to look like it is playing. Reduced motion drops the ambient movement around it. +const TURN_MS = 1800; +// The motor brings the platter up to speed in well under a second. +const SPIN_UP_MS = 700; +// When the opening is skipped, the arm still swings over, just faster. +const QUICK_CUE_MS = 700; + +// As large as the stage allows: what is left between the letterbox bars (22vh) after the title, +// the gap and the stage padding, capped by the column width. +const SIZE = { + base: "min(72vw, 300px)", + md: "min(36vw, calc(78dvh - 13.5rem))", + xl: "min(34vw, calc(78dvh - 16.5rem))", +}; + +type Props = { + readonly isPaused: boolean; + readonly isSpinning: boolean; + // "cueing" plays the lift–swing–lower move once; "down" and "rest" are where it ends up. + readonly needle: "cueing" | "down" | "rest"; +}; + +export const Record = ({ isPaused, isSpinning, needle }: Props) => { + // useId() can contain characters that break url(#…) references. + const pathId = `so${useId().replaceAll(/[^\w-]/gu, "")}`; + const discRef = useRef<HTMLDivElement>(null); + const spinRef = useRef<Animation | undefined>(undefined); + + useEffect(() => { + const disc = discRef.current; + + if (disc === null || typeof disc.animate !== "function") { + return undefined; + } + const spin = disc.animate([{ transform: "rotate(0deg)" }, { transform: "rotate(360deg)" }], { + duration: TURN_MS, + iterations: Infinity, + }); + + // cancel() rejects `finished`; nothing awaits it. + spin.finished.catch(() => undefined); + spin.pause(); + spinRef.current = spin; + + return () => { + spin.cancel(); + spinRef.current = undefined; + }; + }, []); + + // Once the motor starts it does not stop; ramp the rate up so the platter has inertia. + useEffect(() => { + const spin = spinRef.current; + + if (!isSpinning || spin === undefined) { + return undefined; + } + const start = performance.now(); + let frame = 0; + const step = (now: number) => { + const progress = Math.min(1, (now - start) / SPIN_UP_MS); + + spin.updatePlaybackRate(1 - (1 - progress) ** 3); + if (progress < 1) { + frame = requestAnimationFrame(step); + } + }; + + spin.updatePlaybackRate(0); Review Comment: **The record intermittently never turns** The record sometimes does not turn. With the scene open in Chromium, the spin reported `playState: "running"` but `currentTime: -68341` and `transform: none` while the needle was down and the video was playing; the start time had been resolved about a minute into the future. The order that produces it: the spin is created and `pause()`d (L80); when `isSpinning` turns true, this effect calls `updatePlaybackRate(0)` and the ramp keeps calling `updatePlaybackRate(r)` every frame, while the effect below calls `play()`. `updatePlaybackRate` applies asynchronously, and racing it with the pending `play()` can mis-resolve the start time. Replaying that call order on a standalone element, it failed 3 times in 20 (one stuck run was 402 s behind). Setting `playbackRate` directly, which applies synchronously and keeps the current time, turned 20 times out of 20, including when the Dag was paused before the spin started or mid-ramp. Toggling the scene's pause switch recovers a stuck record, because `pause()` + `play()` resolves the start time again. happy-dom has no Web Animations implementation, so the vitest suite cannot catch this. ```suggestion spin.playbackRate = 0; ``` ########## airflow-core/src/airflow/ui/src/components/SmoothOperator/Record.tsx: ########## @@ -0,0 +1,267 @@ +/*! + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +import { useEffect, useId, useRef, useState } from "react"; + +import { Box, chakra } from "@chakra-ui/react"; + +import { AirflowPin } from "src/assets/AirflowPin"; + +import { + ARM_LEAD_IN, + ARM_REST, + COLORS, + COPY, + cue, + CUE_MS, + prefersReducedMotion, + SIDE_MS, + TABLET_PORTRAIT, + track, +} from "./stage"; + +// 33⅓ rpm is one turn every 1.8 s. The platter turns at speed for everyone: a record that is +// playing has to look like it is playing. Reduced motion drops the ambient movement around it. +const TURN_MS = 1800; +// The motor brings the platter up to speed in well under a second. +const SPIN_UP_MS = 700; +// When the opening is skipped, the arm still swings over, just faster. +const QUICK_CUE_MS = 700; + +// As large as the stage allows: what is left between the letterbox bars (22vh) after the title, +// the gap and the stage padding, capped by the column width. +const SIZE = { + base: "min(72vw, 300px)", + md: "min(36vw, calc(78dvh - 13.5rem))", + xl: "min(34vw, calc(78dvh - 16.5rem))", +}; + +type Props = { + readonly isPaused: boolean; + readonly isSpinning: boolean; + // "cueing" plays the lift–swing–lower move once; "down" and "rest" are where it ends up. + readonly needle: "cueing" | "down" | "rest"; +}; + +export const Record = ({ isPaused, isSpinning, needle }: Props) => { + // useId() can contain characters that break url(#…) references. + const pathId = `so${useId().replaceAll(/[^\w-]/gu, "")}`; + const discRef = useRef<HTMLDivElement>(null); + const spinRef = useRef<Animation | undefined>(undefined); + + useEffect(() => { + const disc = discRef.current; + + if (disc === null || typeof disc.animate !== "function") { + return undefined; + } + const spin = disc.animate([{ transform: "rotate(0deg)" }, { transform: "rotate(360deg)" }], { + duration: TURN_MS, + iterations: Infinity, + }); + + // cancel() rejects `finished`; nothing awaits it. + spin.finished.catch(() => undefined); + spin.pause(); + spinRef.current = spin; + + return () => { + spin.cancel(); + spinRef.current = undefined; + }; + }, []); + + // Once the motor starts it does not stop; ramp the rate up so the platter has inertia. + useEffect(() => { + const spin = spinRef.current; + + if (!isSpinning || spin === undefined) { + return undefined; + } + const start = performance.now(); + let frame = 0; + const step = (now: number) => { + const progress = Math.min(1, (now - start) / SPIN_UP_MS); + + spin.updatePlaybackRate(1 - (1 - progress) ** 3); Review Comment: **Set the ramp's rate directly too** Same cause as the comment on L107: the per-frame ramp should set `playbackRate` directly, so each frame's rate applies at once rather than queueing another asynchronous update. ```suggestion spin.playbackRate = 1 - (1 - progress) ** 3; ``` -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
