| 64 | * If a parent finishes execution, all of its child threads are terminated. |
| 65 | */ |
| 66 | export class Thread { |
| 67 | public children: Thread[] = []; |
| 68 | /** |
| 69 | * The next value to be passed to the wrapped generator. |
| 70 | */ |
| 71 | public value: unknown; |
| 72 | |
| 73 | /** |
| 74 | * The current time of this thread. |
| 75 | * |
| 76 | * @remarks |
| 77 | * Used by {@link flow.waitFor} and other time-based functions to properly |
| 78 | * support durations shorter than one frame. |
| 79 | */ |
| 80 | public readonly time = createSignal(0); |
| 81 | |
| 82 | /** |
| 83 | * The fixed time of this thread. |
| 84 | * |
| 85 | * @remarks |
| 86 | * Fixed time is a multiple of the frame duration. It can be used to account |
| 87 | * for the difference between this thread's {@link time} and the time of the |
| 88 | * current animation frame. |
| 89 | */ |
| 90 | public get fixed() { |
| 91 | return this.fixedTime; |
| 92 | } |
| 93 | |
| 94 | /** |
| 95 | * Check if this thread or any of its ancestors has been canceled. |
| 96 | */ |
| 97 | public get canceled(): boolean { |
| 98 | return this.isCanceled || (this.parent?.canceled ?? false); |
| 99 | } |
| 100 | |
| 101 | public get paused(): boolean { |
| 102 | return this.isPaused || (this.parent?.paused ?? false); |
| 103 | } |
| 104 | |
| 105 | public parent: Thread | null = null; |
| 106 | private isCanceled = false; |
| 107 | private isPaused = false; |
| 108 | private fixedTime = 0; |
| 109 | private queue: ThreadGenerator[] = []; |
| 110 | |
| 111 | public constructor( |
| 112 | /** |
| 113 | * The generator wrapped by this thread. |
| 114 | */ |
| 115 | public readonly runner: ThreadGenerator & {task?: Thread}, |
| 116 | ) { |
| 117 | if (this.runner.task) { |
| 118 | useLogger().error({ |
| 119 | message: `The generator "${getTaskName( |
| 120 | this.runner, |
| 121 | )}" is already being executed by another thread.`, |
| 122 | remarks: reusedGenerator, |
| 123 | }); |
nothing calls this directly
no test coverage detected