This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // scheduler package API (the most important bits) | |
| type PriorityLevel = number; // currently it's just a number | |
| type SchedulerCallback = () => SchedulerCallback | void; // callback can return "continuation" callback | |
| type CallbackNode = unknown; // we don't need to know how callback node is represented internally | |
| // available priorities | |
| const ImmediatePriority: PriorityLevel; // must run immidiately | |
| const UserBlockingPriority: PriorityLevel; // will run in 250ms at the latest | |
| const NormalPriority: PriorityLevel; // will run in 5s at the latest | |
| const IdlePriority: PriorityLevel; // will run in 10s at the latest |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import { | |
| unstable_ImmediatePriority as ImmediatePriority, | |
| unstable_UserBlockingPriority as UserBlockingPriority, | |
| unstable_NormalPriority as NormalPriority, | |
| unstable_IdlePriority as IdlePriority, | |
| unstable_LowPriority as LowPriority, | |
| unstable_scheduleCallback as scheduleCallback, | |
| unstable_cancelCallback as cancelCallback, | |
| unstable_shouldYield as shouldYield, | |
| unstable_runWithPriority as runWithPriority, |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import { useCallback, useEffect, useRef, useState } from "react"; | |
| import { | |
| cancelCallback, | |
| scheduleCallback, | |
| runWithPriority, | |
| ImmediatePriority, | |
| IdlePriority, | |
| } from "./scheduler"; | |
| export function useTransitionEffect() { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| const [isPending, startTransitionEffect, stopTransitionEffect] = | |
| useTransitionEffect(); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| startTransitionEffect(function* () { | |
| for (let item of items) { | |
| doSomeComplexSideEffects(item); | |
| yield; | |
| } | |
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| startTransitionEffect(function* () { | |
| const cleanup = () => cleanupSideEffects(); | |
| for (let item of items) { | |
| doSomeComplexSideEffects(item); | |
| yield cleanup; | |
| } | |
| return cleanup; | |
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| useEffect(() => { | |
| startTransitionEffect(function* () { | |
| // effect | |
| }); | |
| return () => stopTransitionEffect(); | |
| }, []); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| function App() { | |
| const [isPending, startTransitionEffect, stopTransitionEffect] = | |
| useTransitionEffect(); | |
| function handleStartClick() { | |
| startTransitionEffect(function* () { | |
| // do stuff, for example render something on a canvas | |
| }); | |
| } | |
| function handleStopClick() { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import { shouldYield } from "./scheduler"; | |
| startTransitionEffect(function* () { | |
| for (let item of items) { | |
| doSomeComplexSideEffects(item); | |
| if (shouldYield()) { | |
| yield; | |
| } | |
| } | |
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import { runWithPriority, NormalPriority } from "./scheduler"; | |
| startTransitionEffect(function* () { | |
| for (let item of items) { | |
| runWithPriority(NormalPriority, () => { | |
| setCount((prevCount) => prevCount + 1); | |
| }); | |
| yield; | |
| } | |
| }); |