| 54 | * Used for debugging purposes. |
| 55 | */ |
| 56 | export function* threads( |
| 57 | factory: ThreadsFactory, |
| 58 | callback?: ThreadsCallback, |
| 59 | ): ThreadGenerator { |
| 60 | const playback = usePlayback(); |
| 61 | const root = factory(); |
| 62 | setTaskName(root, 'root'); |
| 63 | const rootThread = new Thread(root); |
| 64 | callback?.(rootThread); |
| 65 | |
| 66 | let threads: Thread[] = [rootThread]; |
| 67 | while (threads.length > 0) { |
| 68 | const newThreads = []; |
| 69 | const queue = [...threads]; |
| 70 | const dt = playback.deltaTime; |
| 71 | |
| 72 | while (queue.length > 0) { |
| 73 | const thread = queue.pop(); |
| 74 | if (!thread || thread.canceled) { |
| 75 | continue; |
| 76 | } |
| 77 | |
| 78 | const result = thread.next(); |
| 79 | if (result.done) { |
| 80 | thread.cancel(); |
| 81 | continue; |
| 82 | } |
| 83 | |
| 84 | if (isThreadGenerator(result.value)) { |
| 85 | const child = new Thread(result.value); |
| 86 | thread.value = result.value; |
| 87 | thread.add(child); |
| 88 | |
| 89 | queue.push(thread); |
| 90 | queue.push(child); |
| 91 | } else if (result.value) { |
| 92 | thread.value = yield result.value; |
| 93 | queue.push(thread); |
| 94 | } else { |
| 95 | thread.update(dt); |
| 96 | thread.drain(task => { |
| 97 | const child = new Thread(task); |
| 98 | thread.add(child); |
| 99 | newThreads.unshift(child); |
| 100 | }); |
| 101 | |
| 102 | newThreads.unshift(thread); |
| 103 | } |
| 104 | } |
| 105 | |
| 106 | threads = newThreads.filter(thread => !thread.canceled); |
| 107 | if (threads.length > 0) yield; |
| 108 | } |
| 109 | } |