Class Decider<C, S, E>

Decider is a datatype that represents the main decision-making algorithm. It has three generic parameters C, S, E , representing the type of the values that Decider may contain or use. Decider can be specialized for any type C or S or E because these types does not affect its behavior. Decider behaves the same for C=Int or C=YourCustomType, for example.

Decider is a pure domain component.

Param: decide

A function/lambda that takes command of type C and input state of type S as parameters, and returns/emits the list of output events E[]>

Param: evolve

A function/lambda that takes input state of type S and input event of type E as parameters, and returns the output/new state S

Param: initialState

A starting point / An initial state of type S

Author

Иван Дугалић / Ivan Dugalic /

Idugalic

Example

const decider: Decider<OddNumberCmd, number, OddNumberEvt> = new Decider<OddNumberCmd, number, OddNumberEvt>(
(c, _) => {
if (c instanceof AddOddNumberCmd) {
return [new OddNumberAddedEvt(c.value)];
} else if (c instanceof MultiplyOddNumberCmd) {
return [new OddNumberMultiplied(c.value)];
} else {
const _: never = c;
console.log('Never just happened in decide function: ' + _);
return [];
}
},
(s, e) => {
if (e instanceof OddNumberAddedEvt) {
return s + e.value;
} else if (e instanceof OddNumberMultiplied) {
return s * e.value;
} else {
const _: never = e;
console.log('Never just happened in evolve function: ' + _);
return s;
}
},
0
);

Type Parameters

  • C

    Command type

  • S

    State type

  • E

    Event type

Implements

Constructors

  • Type Parameters

    • C

    • S

    • E

    Parameters

    • decide: ((c, s) => readonly E[])
        • (c, s): readonly E[]
        • Parameters

          • c: C
          • s: S

          Returns readonly E[]

    • evolve: ((s, e) => S)
        • (s, e): S
        • Parameters

          • s: S
          • e: E

          Returns S

    • initialState: S

    Returns Decider<C, S, E>

Properties

decide: ((c, s) => readonly E[])

Type declaration

    • (c, s): readonly E[]
    • Parameters

      • c: C
      • s: S

      Returns readonly E[]

evolve: ((s, e) => S)

Type declaration

    • (s, e): S
    • Parameters

      • s: S
      • e: E

      Returns S

initialState: S

Methods

  • Combine Deciders into one Decider

    Type Parameters

    • C2

    • S2

    • E2

    Parameters

    Returns Decider<C | C2, readonly [S, S2], E | E2>

  • Dimap on E/Event parameter

    Type Parameters

    • En

      New Event

    Parameters

    • fl: ((en) => E)
        • (en): E
        • Parameters

          • en: En

          Returns E

    • fr: ((e) => En)
        • (e): En
        • Parameters

          • e: E

          Returns En

    Returns Decider<C, S, En>

  • Dimap on S/State parameter

    Type Parameters

    • Sn

      New State

    Parameters

    • fl: ((sn) => S)
        • (sn): S
        • Parameters

          • sn: Sn

          Returns S

    • fr: ((s) => Sn)
        • (s): Sn
        • Parameters

          • s: S

          Returns Sn

    Returns Decider<C, Sn, E>

  • Left map on C/Command parameter - Contravariant

    Type Parameters

    • Cn

      New Command

    Parameters

    • f: ((cn) => C)
        • (cn): C
        • Parameters

          • cn: Cn

          Returns C

    Returns Decider<Cn, S, E>

Generated using TypeDoc