Skip to content

sleexyz/callable

Repository files navigation

Callable

NPM

CircleCI

An extendable base class for callable classes, designed to work nicely with Flow.

Type definition:

// @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;
}

Examples:

Extend Callable with custom methods

// @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]

Extend Callable with custom fields and constructor

// @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

Releases

No releases published

Packages

No packages published