Skip to content

Visualize Your Ideas With Code

feat: threading

aarthificiale9f6b2ad0838
diff --git a/src/Project.ts b/src/Project.ts
index ea62f9c8..611f5262 100644
--- a/src/Project.ts
+++ b/src/Project.ts
@@ -12,6 +12,7 @@ import {
waitFor,
waitUntil,
sequence,
+ threads,
} from './animations';
Konva.autoDrawEnabled = false;
@@ -75,7 +76,10 @@ export class Project extends Stage {
this.scenes.forEach(scene => scene.layer.destroy());
this.scenes = [];
this.frame = 0;
- this.runner = this.runnerFactory(this);
+ this.runner = this.threads(join => {
+ this.join = join;
+ return this.runnerFactory(this);
+ });
}
public next(): boolean {
@@ -94,6 +98,8 @@ export class Project extends Stage {
return frames / this.framesPerSeconds;
}
+ public join: (...runners: Generator[]) => Generator;
+ public threads = threads;
public waitFor = waitFor;
public waitUntil = waitUntil;
public tween = tween;
diff --git a/src/animations/index.ts b/src/animations/index.ts
index 1ee7533d..ee9aa8ca 100644
--- a/src/animations/index.ts
+++ b/src/animations/index.ts
@@ -5,4 +5,5 @@ export * from './surfaceTransition';
export * from './tween';
export * from './TimeTween';
export * from './scheduling';
-export * from './sequence';
\ No newline at end of file
+export * from './sequence';
+export * from './threads';
\ No newline at end of file
diff --git a/src/animations/threads.ts b/src/animations/threads.ts
new file mode 100644
index 00000000..57f9805d
--- /dev/null
+++ b/src/animations/threads.ts
@@ -0,0 +1,29 @@
+export function* threads(
+ factory: (join: (...tasks: Generator[]) => Generator) => Generator,
+): Generator {
+ let runners: Generator[] = [];
+ const join = function* (...tasks: Generator[]): Generator {
+ while (tasks.find(runner => runners.includes(runner))) {
+ yield;
+ }
+ };
+ runners.push(factory(join));
+ while (runners.length > 0) {
+ const newRunners = [];
+ for (let i = 0; i < runners.length; i++) {
+ const runner = runners[i];
+ const result = runner.next();
+
+ if (result.value?.next) {
+ if (!result.done) {
+ runners.push(runner);
+ }
+ runners.push(result.value);
+ } else if (!result.done) {
+ newRunners.push(runner);
+ }
+ }
+ runners = newRunners;
+ if (runners.length > 0) yield;
+ }
+}