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 sometimes never starts spinning: calling `updatePlaybackRate(0)` on the paused animation before `play()` can leave its start time far in the future. Setting `playbackRate` directly fixes it. ```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: Same here. ```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]
