An extendable base class for callable classes, designed to work nicely with Flow.
// @flow
declare class Callable<A, B> extends Function {
// See https://github.com/facebook/flow/issues/3084
// (A): B
$call: A => B;
constructor(A => B): this;
}
// @flow
import Callable from "callable-class";
class Composable<A, B> extends Callable<A, B> {
andThen(next: B => C): Composable<A, B> {
return new Composable(x => next(this(x)));
}
}
const foo = new Composable(x => [...x, 1])
.andThen(x => [...x, 2])
.andThen(x => [...x, 3]);
console.log(foo([])); // [1, 2, 3]
// @flow
import Callable from "callable-class";
class ActionCreator<Type, A, B> extends Callable<A, B> {
type: string;
constructor(type: Type, fn: A => B) {
super(fn);
this.type = type;
}
}
const foo = new ActionCreator("foo", x => x + 1);
console.log(foo.type); // "foo"
console.log(foo(0)); // 1