Skip to content

Visualize Your Ideas With Code

fix: support nested threads (#84)

Jacob4a4a95f5891b
diff --git a/src/scenes/GeneratorScene.ts b/src/scenes/GeneratorScene.ts
index 0d596c5a..b8c449d5 100644
--- a/src/scenes/GeneratorScene.ts
+++ b/src/scenes/GeneratorScene.ts
@@ -4,7 +4,7 @@ import {TimeEvents} from './TimeEvents';
import {EventDispatcher, ValueDispatcher} from '../events';
import {Project} from '../Project';
import {decorate, threadable} from '../decorators';
-import {setProject, setScene} from '../utils';
+import {endScene, setProject, startScene} from '../utils';
import {CachedSceneData, Scene, SceneMetadata, SceneRenderEvent} from './Scene';
import {LifecycleEvents} from './LifecycleEvents';
import {Threadable} from './Threadable';
@@ -159,7 +159,7 @@ export abstract class GeneratorScene<T>
}
public async next() {
- setScene(this);
+ startScene(this);
setProject(this.project);
let result = this.runner.next();
this.update();
@@ -173,6 +173,7 @@ export abstract class GeneratorScene<T>
}
this.update();
}
+ endScene(this);
if (result.done) {
this.state = SceneState.Finished;
diff --git a/src/threading/Thread.ts b/src/threading/Thread.ts
index 9b089037..a1b6f033 100644
--- a/src/threading/Thread.ts
+++ b/src/threading/Thread.ts
@@ -1,6 +1,6 @@
import {GeneratorHelper} from '../helpers';
import {ThreadGenerator} from './ThreadGenerator';
-import {setThread, useProject} from '../utils';
+import {endThread, startThread, useProject} from '../utils';
/**
* A class representing an individual thread.
@@ -51,8 +51,9 @@ export class Thread {
* Progress the wrapped generator once.
*/
public next() {
- setThread(this);
+ startThread(this);
const result = this.runner.next(this.value);
+ endThread(this);
this.value = null;
return result;
}
diff --git a/src/utils/useScene.ts b/src/utils/useScene.ts
index 9f2b1b61..f1dca315 100644
--- a/src/utils/useScene.ts
+++ b/src/utils/useScene.ts
@@ -1,11 +1,20 @@
import type {Scene} from '../scenes';
-let currentScene: Scene = null;
+const sceneStack: Scene[] = [];
+/**
+ * Get a reference to the current scene.
+ */
export function useScene(): Scene {
- return currentScene;
+ return sceneStack.at(-1);
}
-export function setScene(scene: Scene) {
- currentScene = scene;
+export function startScene(scene: Scene) {
+ sceneStack.push(scene);
+}
+
+export function endScene(scene: Scene) {
+ if (sceneStack.pop() !== scene) {
+ throw new Error('startScene/endScene was called out of order');
+ }
}
diff --git a/src/utils/useThread.ts b/src/utils/useThread.ts
index 523f6b15..42acb4fa 100644
--- a/src/utils/useThread.ts
+++ b/src/utils/useThread.ts
@@ -1,14 +1,20 @@
import type {Thread} from '../threading';
-let currentThread: Thread;
+const threadStack: Thread[] = [];
/**
* Get a reference to the current thread.
*/
-export function useThread() {
- return currentThread;
+export function useThread(): Thread {
+ return threadStack.at(-1);
}
-export function setThread(thread: Thread) {
- currentThread = thread;
+export function startThread(thread: Thread) {
+ threadStack.push(thread);
+}
+
+export function endThread(thread: Thread) {
+ if (threadStack.pop() !== thread) {
+ throw new Error('startThread/endThread was called out of order');
+ }
}