whscullin / apple2js

An Apple II emulator originally written in Javascript, now being converted to TypeScript
http://www.scullinsteel.com/apple2/
MIT License
438 stars 57 forks source link

Interruptable spawn #132

Closed iflan closed 2 years ago

iflan commented 2 years ago

Adds a spawn function to run asynchronous tasks. The task function passed to spawn can take an Interrupted argument, which is merely a method that returns true if the task should stop doing work. Likewise, spawn returns an Interrupt function that causes the Interrupted function to return true.

One use of spawn is to run asynchronous tasks from useEffect blocks. The returned Interrupt functions can serve as the useEffect cleanup function or be called from the cleanup function.

Note that there's nothing magical about Interrupt—the task must check Interrupted after every await to see if it has been interrupted.