Skip to content

Visualize Your Ideas With Code

feat(core): improve `SignalGenerator` chaining (#651)

Jacobde72f1f70edf
diff --git a/packages/core/src/signals/SignalContext.ts b/packages/core/src/signals/SignalContext.ts
index 130b00ff..f49b824a 100644
--- a/packages/core/src/signals/SignalContext.ts
+++ b/packages/core/src/signals/SignalContext.ts
@@ -6,7 +6,7 @@ import {
} from '../tweening';
import {errorToLog, useLogger} from '../utils';
import {ThreadGenerator} from '../threading';
-import {run} from '../flow';
+import {run, waitFor} from '../flow';
import {DependencyContext} from './DependencyContext';
import {
SignalExtensions,
@@ -167,41 +167,75 @@ export class SignalContext<
return this.set(value);
}
- return this.makeAnimate(timingFunction, interpolationFunction)(
- value,
- duration,
- );
+ const queue = this.createQueue(timingFunction, interpolationFunction);
+ return queue.to(value, duration);
}
- protected makeAnimate(
+ protected createQueue(
defaultTimingFunction: TimingFunction,
defaultInterpolationFunction: InterpolationFunction<TValue>,
- before?: ThreadGenerator,
) {
- const animate = (
+ const initial = this.get();
+ const queue: ThreadGenerator[] = [];
+
+ const task = run(function* animate() {
+ while (queue.length > 0) {
+ yield* queue.shift()!;
+ }
+ }) as SignalGenerator<TSetterValue, TValue>;
+
+ task.to = (
value: SignalValue<TSetterValue> | typeof DEFAULT,
duration: number,
timingFunction = defaultTimingFunction,
interpolationFunction = defaultInterpolationFunction,
) => {
- const tween = this.tween(
- value,
- duration,
- timingFunction,
- interpolationFunction,
- ) as SignalGenerator<TSetterValue, TValue>;
- let task = tween;
- if (before) {
- task = run(function* () {
- yield* before;
- yield* tween;
- }) as SignalGenerator<TSetterValue, TValue>;
- }
- task.to = this.makeAnimate(timingFunction, interpolationFunction, task);
+ defaultTimingFunction = timingFunction;
+ defaultInterpolationFunction = interpolationFunction;
+ queue.push(
+ this.tween(value, duration, timingFunction, interpolationFunction),
+ );
+ return task;
+ };
+
+ task.back = (
+ time: number,
+ timingFunction = defaultTimingFunction,
+ interpolationFunction = defaultInterpolationFunction,
+ ) => {
+ defaultTimingFunction = timingFunction;
+ defaultInterpolationFunction = interpolationFunction;
+ queue.push(
+ this.tween(
+ initial,
+ time,
+ defaultTimingFunction,
+ defaultInterpolationFunction,
+ ),
+ );
+ return task;
+ };
+
+ task.wait = (duration: number) => {
+ queue.push(waitFor(duration));
+ return task;
+ };
+
+ task.run = (generator: ThreadGenerator) => {
+ queue.push(generator);
+ return task;
+ };
+
+ task.do = (callback: () => void) => {
+ queue.push(
+ run(function* () {
+ callback();
+ }),
+ );
return task;
};
- return <SignalTween<TSetterValue, TValue>>animate;
+ return task;
}
protected *tween(
diff --git a/packages/core/src/signals/types.ts b/packages/core/src/signals/types.ts
index 3b4d17a5..868d6842 100644
--- a/packages/core/src/signals/types.ts
+++ b/packages/core/src/signals/types.ts
@@ -7,7 +7,40 @@ export type SignalGenerator<
TSetterValue,
TValue extends TSetterValue,
> = ThreadGenerator & {
+ /**
+ * Tween to the specified value.
+ */
to: SignalTween<TSetterValue, TValue>;
+ /**
+ * Tween back to the original value.
+ *
+ * @param time - The duration of the tween.
+ * @param timingFunction - The timing function of the tween.
+ * @param interpolationFunction - The interpolation function of the tween.
+ */
+ back: (
+ time: number,
+ timingFunction?: TimingFunction,
+ interpolationFunction?: InterpolationFunction<TValue>,
+ ) => SignalGenerator<TSetterValue, TValue>;
+ /**
+ * Wait for the specified duration.
+ *
+ * @param duration - The duration to wait.
+ */
+ wait: (duration: number) => SignalGenerator<TSetterValue, TValue>;
+ /**
+ * Run the given task.
+ *
+ * @param task - The generator to run.
+ */
+ run: (task: ThreadGenerator) => SignalGenerator<TSetterValue, TValue>;
+ /**
+ * Invoke the given callback.
+ *
+ * @param callback - The callback to invoke.
+ */
+ do: (callback: () => void) => SignalGenerator<TSetterValue, TValue>;
};
export interface SignalSetter<TValue, TOwner = void> {