Skip to content

Unwrapping a signal with lambda type #980

Answered by aarthificial
Splines asked this question in Q&A
Discussion options

You must be logged in to vote

The value of a signal cannot be a function for the reasons you've just discovered.
The easiest solution is to have a wrapper type that holds the function:

// define a custom type
export interface Sampler {
  sample(x: number): number;
}

export interface MathFunctionProps extends SplineProps {
  // use it in your definition
  func: SignalValue<Sampler>;
  numPoints?: SignalValue<number>;
}

// optionally define a helper function:
export function sampler(fn: (x: number) => number): Sampler {
  return {
    sample(x: number) {
      return fn(x);
    },
  };
}

// use that in JSX:
view.add(
  <MathFunction
    // with the helper function:
    func={sampler((x) => Math.sin(x / freq()) * 100 * 

Replies: 1 comment 1 reply

Comment options

You must be logged in to vote
1 reply
@Splines
Comment options

Answer selected by Splines
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants