Skip to content

Visualize Your Ideas With Code

refactor: introduce signal extensions (#649)

Jacobfae72b3729f2
diff --git a/packages/2d/src/components/CodeBlock.ts b/packages/2d/src/components/CodeBlock.ts
index 3396ee29..741579f9 100644
--- a/packages/2d/src/components/CodeBlock.ts
+++ b/packages/2d/src/components/CodeBlock.ts
@@ -260,8 +260,8 @@ export class CodeBlock extends Shape {
if (typeof code === 'function') throw new Error();
if (!CodeBlock.initialized()) return;
- const autoWidth = this.customWidth() === null;
- const autoHeight = this.customHeight() === null;
+ const autoWidth = this.width.isInitial();
+ const autoHeight = this.height.isInitial();
const fromSize = this.size();
const toSize = this.getTokensSize(parse(code, {codeStyle: this.theme()}));
@@ -277,20 +277,20 @@ export class CodeBlock extends Shape {
const remapped = clampRemap(beginning, ending, 0, 1, progress);
this.codeProgress(progress);
if (autoWidth) {
- this.customWidth(easeInOutSine(remapped, fromSize.x, toSize.x));
+ this.width(easeInOutSine(remapped, fromSize.x, toSize.x));
}
if (autoHeight) {
- this.customHeight(easeInOutSine(remapped, fromSize.y, toSize.y));
+ this.height(easeInOutSine(remapped, fromSize.y, toSize.y));
}
},
() => {
this.codeProgress(null);
this.diffed = null;
if (autoWidth) {
- this.customWidth(null);
+ this.width.reset();
}
if (autoHeight) {
- this.customHeight(null);
+ this.height.reset();
}
this.code(code);
},
diff --git a/packages/2d/src/components/Layout.ts b/packages/2d/src/components/Layout.ts
index 0fb1f3b6..3f713454 100644
--- a/packages/2d/src/components/Layout.ts
+++ b/packages/2d/src/components/Layout.ts
@@ -5,7 +5,6 @@ import {
defaultStyle,
getPropertyMeta,
initial,
- inspectable,
signal,
Vector2LengthSignal,
vector2Signal,
@@ -45,7 +44,7 @@ import {Node, NodeProps} from './Node';
import {drawLine, drawPivot} from '../utils';
import {spacingSignal} from '../decorators/spacingSignal';
import {
- isReactive,
+ modify,
Signal,
SignalValue,
SimpleSignal,
@@ -272,35 +271,26 @@ export class Layout extends Node {
@signal()
public declare readonly textAlign: SimpleSignal<CanvasTextAlign, this>;
- @cloneable(false)
- @inspectable(false)
- @signal()
- protected declare readonly customX: SimpleSignal<number, this>;
protected getX(): number {
if (this.isLayoutRoot()) {
- return this.customX();
+ return this.x.context.getter();
}
return this.computedPosition().x;
}
protected setX(value: SignalValue<number>) {
- this.customX(value);
+ this.x.context.setter(value);
}
- @cloneable(false)
- @inspectable(false)
- @signal()
- protected declare readonly customY: SimpleSignal<number, this>;
-
protected getY(): number {
if (this.isLayoutRoot()) {
- return this.customY();
+ return this.y.context.getter();
}
return this.computedPosition().y;
}
protected setY(value: SignalValue<number>) {
- this.customY(value);
+ this.y.context.setter(value);
}
/**
@@ -352,7 +342,6 @@ export class Layout extends Node {
* node.size.x(() => '50%');
* ```
*/
- @cloneable(false)
@initial({x: null, y: null})
@vector2Signal({x: 'width', y: 'height'})
public declare readonly size: Vector2LengthSignal<this>;
@@ -363,15 +352,11 @@ export class Layout extends Node {
return this.size.y;
}
- @inspectable(false)
- @signal()
- protected declare readonly customWidth: SimpleSignal<DesiredLength, this>;
-
protected getWidth(): number {
return this.computedSize().width;
}
protected setWidth(value: SignalValue<Length>) {
- this.customWidth(value);
+ this.width.context.setter(value);
}
@threadable()
@@ -407,15 +392,11 @@ export class Layout extends Node {
lock && this.releaseSize();
}
- @inspectable(false)
- @signal()
- protected declare readonly customHeight: SimpleSignal<DesiredLength, this>;
-
protected getHeight(): number {
return this.computedSize().height;
}
protected setHeight(value: SignalValue<Length>) {
- this.customHeight(value);
+ this.height.context.setter(value);
}
@threadable()
@@ -462,8 +443,8 @@ export class Layout extends Node {
@computed()
protected desiredSize(): SerializedVector2<DesiredLength> {
return {
- x: this.customWidth(),
- y: this.customHeight(),
+ x: this.width.context.getter(),
+ y: this.height.context.getter(),
};
}
@@ -684,7 +665,7 @@ export class Layout extends Node {
public override localToParent(): DOMMatrix {
const matrix = new DOMMatrix();
- matrix.translateSelf(this.position.x(), this.position.y());
+ matrix.translateSelf(this.x(), this.y());
matrix.rotateSelf(0, 0, this.rotation());
matrix.scaleSelf(this.scale.x(), this.scale.y());
@@ -1036,14 +1017,11 @@ function originSignal(origin: Origin): PropertyDecorator {
value: SignalValue<PossibleVector2>,
) {
this.position(
- isReactive(value)
- ? () =>
- this.getOriginDelta(origin)
- .transform(this.scalingRotationMatrix())
- .flipped.add(value())
- : this.getOriginDelta(origin)
- .transform(this.scalingRotationMatrix())
- .flipped.add(value),
+ modify(value, unwrapped =>
+ this.getOriginDelta(origin)
+ .transform(this.scalingRotationMatrix())
+ .flipped.add(unwrapped),
+ ),
);
return this;
};
diff --git a/packages/2d/src/components/Line.ts b/packages/2d/src/components/Line.ts
index 83bf31f4..13bc693c 100644
--- a/packages/2d/src/components/Line.ts
+++ b/packages/2d/src/components/Line.ts
@@ -1,7 +1,7 @@
import {
- isReactive,
SignalValue,
SimpleSignal,
+ unwrap,
} from '@motion-canvas/core/lib/signals';
import {BBox, PossibleVector2, Vector2} from '@motion-canvas/core/lib/types';
import {useLogger} from '@motion-canvas/core/lib/utils';
@@ -45,9 +45,7 @@ export class Line extends Curve {
protected childrenBBox() {
const custom = this.points();
const points = custom
- ? custom.map(
- signal => new Vector2(isReactive(signal) ? signal() : signal),
- )
+ ? custom.map(signal => new Vector2(unwrap(signal)))
: this.children()
.filter(child => !(child instanceof Layout) || child.isLayoutRoot())
.map(child => child.position());
@@ -59,9 +57,7 @@ export class Line extends Curve {
public parsedPoints(): Vector2[] {
const custom = this.points();
return custom
- ? custom.map(
- signal => new Vector2(isReactive(signal) ? signal() : signal),
- )
+ ? custom.map(signal => new Vector2(unwrap(signal)))
: this.children().map(child => child.position());
}
diff --git a/packages/2d/src/components/Node.ts b/packages/2d/src/components/Node.ts
index 34d9d036..61cde2d5 100644
--- a/packages/2d/src/components/Node.ts
+++ b/packages/2d/src/components/Node.ts
@@ -46,6 +46,8 @@ import {
SignalValue,
SimpleSignal,
isReactive,
+ modify,
+ unwrap,
} from '@motion-canvas/core/lib/signals';
export type NodeState = NodeProps & Record<string, any>;
@@ -113,6 +115,13 @@ export class Node implements Promisable<Node> {
@vector2Signal()
public declare readonly position: Vector2Signal<this>;
+ public get x() {
+ return this.position.x as SimpleSignal<number, this>;
+ }
+ public get y() {
+ return this.position.y as SimpleSignal<number, this>;
+ }
+
/**
* A helper signal for operating on the position in world space.
*
@@ -140,13 +149,11 @@ export class Node implements Promisable<Node> {
}
protected setAbsolutePosition(value: SignalValue<PossibleVector2>) {
- if (isReactive(value)) {
- this.position(() =>
- new Vector2(value()).transformAsPoint(this.worldToParent()),
- );
- } else {
- this.position(new Vector2(value).transformAsPoint(this.worldToParent()));
- }
+ this.position(
+ modify(value, unwrapped =>
+ new Vector2(unwrapped).transformAsPoint(this.worldToParent()),
+ ),
+ );
}
/**
@@ -177,11 +184,11 @@ export class Node implements Promisable<Node> {
}
protected setAbsoluteRotation(value: SignalValue<number>) {
- if (isReactive(value)) {
- this.rotation(() => transformAngle(value(), this.worldToParent()));
- } else {
- this.rotation(transformAngle(value, this.worldToParent()));
- }
+ this.rotation(
+ modify(value, unwrapped =>
+ transformAngle(unwrapped, this.worldToParent()),
+ ),
+ );
}
/**
@@ -246,11 +253,9 @@ export class Node implements Promisable<Node> {
}
protected setAbsoluteScale(value: SignalValue<PossibleVector2>) {
- if (isReactive(value)) {
- this.scale(() => this.getRelativeScale(new Vector2(value())));
- } else {
- this.scale(this.getRelativeScale(new Vector2(value)));
- }
+ this.scale(
+ modify(value, unwrapped => this.getRelativeScale(new Vector2(unwrapped))),
+ );
}
private getRelativeScale(scale: Vector2): Vector2 {
@@ -285,7 +290,7 @@ export class Node implements Promisable<Node> {
time: number,
timingFunction: TimingFunction,
) {
- const nextValue = isReactive(value) ? value() : value;
+ const nextValue = unwrap(value);
if (nextValue === 'source-over') {
yield* this.compositeOverride(1, time, timingFunction);
this.compositeOverride(0);
diff --git a/packages/2d/src/components/Spline.ts b/packages/2d/src/components/Spline.ts
index 7422e0ad..692224f8 100644
--- a/packages/2d/src/components/Spline.ts
+++ b/packages/2d/src/components/Spline.ts
@@ -1,7 +1,7 @@
import {
- isReactive,
SignalValue,
SimpleSignal,
+ unwrap,
} from '@motion-canvas/core/lib/signals';
import {
BBox,
@@ -140,7 +140,7 @@ export class Spline extends Curve {
if (points) {
return points.map(signal => {
- const point = new Vector2(isReactive(signal) ? signal() : signal);
+ const point = new Vector2(unwrap(signal));
return {
position: point,
diff --git a/packages/2d/src/decorators/compound.ts b/packages/2d/src/decorators/compound.ts
index 907430e0..f654ea51 100644
--- a/packages/2d/src/decorators/compound.ts
+++ b/packages/2d/src/decorators/compound.ts
@@ -1,9 +1,13 @@
import {useLogger} from '@motion-canvas/core/lib/utils';
import {getPropertyMetaOrCreate} from './signal';
import {addInitializer} from './initializers';
-import {deepLerp} from '@motion-canvas/core/lib/tweening';
-import {CompoundSignalContext} from '@motion-canvas/core/lib/signals';
-import {patchSignal} from '../utils/patchSignal';
+import {deepLerp, map} from '@motion-canvas/core/lib/tweening';
+import {
+ CompoundSignalContext,
+ SignalContext,
+} from '@motion-canvas/core/lib/signals';
+import {makeSignalExtensions} from '../utils/makeSignalExtensions';
+import {modify} from '@motion-canvas/core/lib/signals/utils';
/**
* Create a compound property decorator.
@@ -41,21 +45,27 @@ export function compound(entries: Record<string, string>): PropertyDecorator {
return;
}
+ const initial = meta.default;
+ const parser = meta.parser.bind(instance);
const signalContext = new CompoundSignalContext(
- Object.keys(entries),
- meta.parser,
- meta.default,
+ meta.compoundEntries.map(([key, property]) => {
+ const signal = new SignalContext(
+ modify(initial, value => parser(value)[key]),
+ <any>map,
+ instance,
+ undefined,
+ makeSignalExtensions(undefined, instance, property),
+ ).toSignal();
+ return [key, signal];
+ }),
+ parser,
+ initial,
meta.interpolationFunction ?? deepLerp,
instance,
+ makeSignalExtensions(meta, instance, <string>key),
);
- patchSignal(signalContext, meta, instance, <string>key);
- const signal = signalContext.toSignal();
- for (const [key, property] of meta.compoundEntries) {
- patchSignal(signal[key].context, undefined, instance, property);
- }
-
- instance[key] = signal;
+ instance[key] = signalContext.toSignal();
});
};
}
diff --git a/packages/2d/src/decorators/filtersSignal.ts b/packages/2d/src/decorators/filtersSignal.ts
index 013dc66b..64da9988 100644
--- a/packages/2d/src/decorators/filtersSignal.ts
+++ b/packages/2d/src/decorators/filtersSignal.ts
@@ -9,11 +9,11 @@ import {ThreadGenerator} from '@motion-canvas/core/lib/threading';
import {all} from '@motion-canvas/core/lib/flow';
import {addInitializer} from './initializers';
import {
- isReactive,
Signal,
SignalContext,
SignalValue,
SimpleSignal,
+ unwrap,
} from '@motion-canvas/core/lib/signals';
export type FiltersSignal<TOwner> = Signal<
@@ -68,13 +68,13 @@ export class FiltersSignalContext<TOwner> extends SignalContext<
}
}
- public override *doTween(
+ public override *tweener(
value: SignalValue<Filter[]>,
duration: number,
timingFunction: TimingFunction,
): ThreadGenerator {
const from = this.get();
- const to = isReactive(value) ? value() : value;
+ const to = unwrap(value);
if (areFiltersCompatible(from, to)) {
yield* all(
diff --git a/packages/2d/src/decorators/signal.ts b/packages/2d/src/decorators/signal.ts
index dafbe5e9..1e72a4fc 100644
--- a/packages/2d/src/decorators/signal.ts
+++ b/packages/2d/src/decorators/signal.ts
@@ -5,7 +5,7 @@ import {
} from '@motion-canvas/core/lib/tweening';
import {addInitializer, initialize} from './initializers';
import {capitalize, useLogger} from '@motion-canvas/core/lib/utils';
-import {patchSignal} from '../utils/patchSignal';
+import {makeSignalExtensions} from '../utils/makeSignalExtensions';
import {SignalContext} from '@motion-canvas/core/lib/signals';
export interface PropertyMetadata<T> {
@@ -121,8 +121,9 @@ export function signal<T>(): PropertyDecorator {
getDefault ?? meta.default,
meta.interpolationFunction ?? deepLerp,
instance,
+ meta.parser?.bind(instance),
+ makeSignalExtensions(meta, instance, <string>key),
);
- patchSignal(signal, meta, instance, <string>key);
instance[key] = signal.toSignal();
});
};
diff --git a/packages/2d/src/partials/Gradient.ts b/packages/2d/src/partials/Gradient.ts
index 90ce3d6b..0dcccbba 100644
--- a/packages/2d/src/partials/Gradient.ts
+++ b/packages/2d/src/partials/Gradient.ts
@@ -8,9 +8,9 @@ import {
Vector2Signal,
} from '@motion-canvas/core/lib/types';
import {
- isReactive,
SignalValue,
SimpleSignal,
+ unwrap,
} from '@motion-canvas/core/lib/signals';
export type GradientType = 'linear' | 'conic' | 'radial';
@@ -95,8 +95,8 @@ export class Gradient {
for (const {offset, color} of this.stops()) {
gradient.addColorStop(
- isReactive(offset) ? offset() : offset,
- new Color(isReactive(color) ? color() : color).serialize(),
+ unwrap(offset),
+ new Color(unwrap(color)).serialize(),
);
}
diff --git a/packages/2d/src/utils/patchSignal.ts b/packages/2d/src/utils/makeSignalExtensions.ts
similarity index 53%
rename from packages/2d/src/utils/patchSignal.ts
rename to packages/2d/src/utils/makeSignalExtensions.ts
index 53d0bb46..5ddd2750 100644
--- a/packages/2d/src/utils/patchSignal.ts
+++ b/packages/2d/src/utils/makeSignalExtensions.ts
@@ -1,32 +1,30 @@
import {capitalize} from '@motion-canvas/core/lib/utils';
-import {SignalContext} from '@motion-canvas/core/lib/signals';
+import {SignalExtensions} from '@motion-canvas/core/lib/signals';
import {PropertyMetadata} from '../decorators';
-export function patchSignal<TSetterValue, TValue extends TSetterValue>(
- signal: SignalContext<TSetterValue, TValue>,
+export function makeSignalExtensions<TSetterValue, TValue extends TSetterValue>(
meta: Partial<PropertyMetadata<TValue>> = {},
owner?: any,
name?: string,
) {
- if (meta.parser) {
- signal.setParser(meta.parser.bind(owner));
- }
+ const extensions: Partial<SignalExtensions<TSetterValue, TValue>> = {};
if (name && owner) {
const setter = meta.setter ?? owner?.[`set${capitalize(name)}`];
if (setter) {
- signal.set = (...args: any[]) => {
- setter.apply(owner, args);
- return owner;
- };
+ extensions.setter = setter.bind(owner);
}
+
const getter = meta.getter ?? owner?.[`get${capitalize(name)}`];
if (getter) {
- signal.get = getter.bind(owner);
+ extensions.getter = getter.bind(owner);
}
+
const tweener = meta.tweener ?? owner?.[`tween${capitalize(name)}`];
if (tweener) {
- signal.doTween = tweener.bind(owner);
+ extensions.tweener = tweener.bind(owner);
}
}
+
+ return extensions;
}
diff --git a/packages/core/src/signals/CompoundSignalContext.ts b/packages/core/src/signals/CompoundSignalContext.ts
index e336259a..e7dea904 100644
--- a/packages/core/src/signals/CompoundSignalContext.ts
+++ b/packages/core/src/signals/CompoundSignalContext.ts
@@ -1,13 +1,13 @@
import {InterpolationFunction, map} from '../tweening';
import {Signal, SignalContext} from './SignalContext';
-import {SignalValue} from './types';
-import {isReactive} from './isReactive';
+import {SignalExtensions, SignalValue} from './types';
+import {isReactive, modify} from './utils';
export type CompoundSignal<
TSetterValue,
TValue extends TSetterValue,
- TKeys extends keyof TValue,
- TOwner,
+ TKeys extends keyof TValue = keyof TValue,
+ TOwner = void,
TContext = CompoundSignalContext<TSetterValue, TValue, TKeys, TOwner>,
> = Signal<TSetterValue, TValue, TOwner, TContext> & {
[K in TKeys]: Signal<
@@ -22,29 +22,39 @@ export type CompoundSignal<
export class CompoundSignalContext<
TSetterValue,
TValue extends TSetterValue,
- TKeys extends keyof TValue,
+ TKeys extends keyof TValue = keyof TValue,
TOwner = void,
> extends SignalContext<TSetterValue, TValue, TOwner> {
public readonly signals: [keyof TValue, Signal<any, any, TOwner>][] = [];
public constructor(
- private readonly entries: TKeys[],
+ private readonly entries: (
+ | TKeys
+ | [keyof TValue, Signal<any, any, TOwner>]
+ )[],
parser: (value: TSetterValue) => TValue,
initial: SignalValue<TSetterValue>,
interpolation: InterpolationFunction<TValue>,
owner: TOwner = <TOwner>(<unknown>undefined),
+ extensions: Partial<SignalExtensions<TSetterValue, TValue>> = {},
) {
- super(undefined, interpolation, owner);
+ super(undefined, interpolation, owner, parser, extensions);
this.parser = parser;
- for (const key of entries) {
- const signal = new SignalContext(
- isReactive(initial)
- ? () => parser(initial())[key]
- : parser(initial)[key],
- <any>map,
- owner ?? this.invokable,
- ).toSignal();
+ for (const entry of entries) {
+ let key: keyof TValue;
+ let signal: Signal<any, any, TOwner>;
+ if (Array.isArray(entry)) {
+ [key, signal] = entry;
+ (signal.context as any).owner ??= this;
+ } else {
+ key = entry;
+ signal = new SignalContext(
+ modify(initial, value => parser(value)[entry]),
+ <any>map,
+ owner ?? this.invokable,
+ ).toSignal();
+ }
this.signals.push([key, signal]);
Object.defineProperty(this.invokable, key, {value: signal});
}
@@ -63,7 +73,7 @@ export class CompoundSignalContext<
return this.parser(value);
}
- public override get(): TValue {
+ public override getter(): TValue {
return this.parse(
<TSetterValue>(
Object.fromEntries(
@@ -73,7 +83,7 @@ export class CompoundSignalContext<
);
}
- public override set(value: SignalValue<TValue>): TOwner {
+ public override setter(value: SignalValue<TValue>): TOwner {
if (isReactive(value)) {
for (const [key, property] of this.signals) {
property(() => this.parser(value())[key]);
diff --git a/packages/core/src/signals/SignalContext.ts b/packages/core/src/signals/SignalContext.ts
index 769595ac..130b00ff 100644
--- a/packages/core/src/signals/SignalContext.ts
+++ b/packages/core/src/signals/SignalContext.ts
@@ -9,13 +9,14 @@ import {ThreadGenerator} from '../threading';
import {run} from '../flow';
import {DependencyContext} from './DependencyContext';
import {
+ SignalExtensions,
SignalGenerator,
SignalGetter,
SignalSetter,
SignalTween,
SignalValue,
} from './types';
-import {isReactive} from './isReactive';
+import {isReactive, unwrap} from './utils';
import {DEFAULT} from './symbols';
export type SimpleSignal<TValue, TReturn = void> = Signal<
@@ -55,14 +56,16 @@ export class SignalContext<
TValue extends TSetterValue = TSetterValue,
TOwner = void,
> extends DependencyContext<TOwner> {
+ protected extensions: SignalExtensions<TSetterValue, TValue>;
protected current: SignalValue<TSetterValue> | undefined;
protected last: TValue | undefined;
- protected parser: (value: TSetterValue) => TValue = value => <TValue>value;
public constructor(
private initial: SignalValue<TSetterValue> | undefined,
private readonly interpolation: InterpolationFunction<TValue>,
owner: TOwner = <TOwner>(<unknown>undefined),
+ protected parser: (value: TSetterValue) => TValue = value => <TValue>value,
+ extensions: Partial<SignalExtensions<TSetterValue, TValue>> = {},
) {
super(owner);
@@ -84,6 +87,13 @@ export class SignalContext<
this.last = this.parse(this.initial);
}
}
+
+ this.extensions = {
+ getter: this.getter.bind(this),
+ setter: this.setter.bind(this),
+ tweener: this.tweener.bind(this),
+ ...extensions,
+ };
}
public toSignal(): Signal<TSetterValue, TValue, TOwner> {
@@ -94,23 +104,12 @@ export class SignalContext<
return this.parser(value);
}
- protected wrap(value: SignalValue<TSetterValue>): SignalValue<TValue> {
- return isReactive(value) ? () => this.parse(value()) : this.parse(value);
- }
-
- public setInitial(value: SignalValue<TSetterValue>) {
- this.initial = value;
- }
-
- public setParser(value: (value: TSetterValue) => TValue) {
- this.parser = value;
- if (this.current !== undefined && !isReactive(this.current)) {
- this.last = this.parse(this.current);
- }
- this.markDirty();
+ public set(value: SignalValue<TSetterValue> | typeof DEFAULT): TOwner {
+ this.extensions.setter(value);
+ return this.owner;
}
- public set(value: SignalValue<TSetterValue> | typeof DEFAULT): TOwner {
+ public setter(value: SignalValue<TSetterValue> | typeof DEFAULT): TOwner {
if (value === DEFAULT) {
value = this.initial!;
}
@@ -131,6 +130,10 @@ export class SignalContext<
}
public get(): TValue {
+ return this.extensions.getter();
+ }
+
+ public getter(): TValue {
if (this.event.isRaised() && isReactive(this.current)) {
this.clearDependencies();
this.startCollecting();
@@ -211,8 +214,8 @@ export class SignalContext<
value = this.initial!;
}
- yield* this.doTween(
- this.parse(isReactive(value) ? value() : value),
+ yield* this.extensions.tweener(
+ value,
duration,
timingFunction,
interpolationFunction,
@@ -220,15 +223,21 @@ export class SignalContext<
this.set(value);
}
- public *doTween(
- value: TValue,
+ public *tweener(
+ value: SignalValue<TSetterValue>,
duration: number,
timingFunction: TimingFunction,
interpolationFunction: InterpolationFunction<TValue>,
): ThreadGenerator {
const from = this.get();
yield* tween(duration, v => {
- this.set(interpolationFunction(from, value, timingFunction(v)));
+ this.set(
+ interpolationFunction(
+ from,
+ this.parse(unwrap(value)),
+ timingFunction(v),
+ ),
+ );
});
}
diff --git a/packages/core/src/signals/index.ts b/packages/core/src/signals/index.ts
index ead236ae..cb79c190 100644
--- a/packages/core/src/signals/index.ts
+++ b/packages/core/src/signals/index.ts
@@ -9,7 +9,7 @@ export * from './ComputedContext';
export * from './createComputed';
export * from './createComputedAsync';
export * from './createSignal';
-export * from './isReactive';
+export * from './utils';
export * from './SignalContext';
export * from './DependencyContext';
export * from './types';
diff --git a/packages/core/src/signals/isReactive.ts b/packages/core/src/signals/isReactive.ts
deleted file mode 100644
index 0487007f..00000000
--- a/packages/core/src/signals/isReactive.ts
+++ /dev/null
@@ -1,5 +0,0 @@
-import {SignalValue} from './types';
-
-export function isReactive<T>(value: SignalValue<T>): value is () => T {
- return typeof value === 'function';
-}
diff --git a/packages/core/src/signals/types.ts b/packages/core/src/signals/types.ts
index 5535d350..3b4d17a5 100644
--- a/packages/core/src/signals/types.ts
+++ b/packages/core/src/signals/types.ts
@@ -26,3 +26,14 @@ export interface SignalTween<TSetterValue, TValue extends TSetterValue> {
interpolationFunction?: InterpolationFunction<TValue>,
): SignalGenerator<TSetterValue, TValue>;
}
+
+export interface SignalExtensions<TSetterValue, TValue extends TSetterValue> {
+ getter: SignalGetter<TValue>;
+ setter: SignalSetter<TSetterValue>;
+ tweener(
+ value: SignalValue<TSetterValue>,
+ time: number,
+ timingFunction?: TimingFunction,
+ interpolationFunction?: InterpolationFunction<TValue>,
+ ): ThreadGenerator;
+}
diff --git a/packages/core/src/signals/utils.ts b/packages/core/src/signals/utils.ts
new file mode 100644
index 00000000..ca0917e1
--- /dev/null
+++ b/packages/core/src/signals/utils.ts
@@ -0,0 +1,16 @@
+import {SignalValue} from './types';
+
+export function isReactive<T>(value: SignalValue<T>): value is () => T {
+ return typeof value === 'function';
+}
+
+export function modify<TFrom, TTo>(
+ value: SignalValue<TFrom>,
+ modification: (value: TFrom) => TTo,
+): SignalValue<TTo> {
+ return isReactive(value) ? () => modification(value()) : modification(value);
+}
+
+export function unwrap<T>(value: SignalValue<T>): T {
+ return isReactive(value) ? value() : value;
+}
diff --git a/packages/core/src/types/Color.ts b/packages/core/src/types/Color.ts
index 89a24837..151eb849 100644
--- a/packages/core/src/types/Color.ts
+++ b/packages/core/src/types/Color.ts
@@ -83,9 +83,12 @@ Color.createSignal = (
initial?: SignalValue<PossibleColor>,
interpolation: InterpolationFunction<Color> = Color.lerp,
): ColorSignal<void> => {
- const context = new SignalContext(initial, interpolation);
- context.setParser(value => new Color(value));
- return context.toSignal();
+ return new SignalContext(
+ initial,
+ interpolation,
+ undefined,
+ value => new Color(value),
+ ).toSignal();
};
Color.prototype.toSymbol = () => {