-
Notifications
You must be signed in to change notification settings - Fork 0
/
notifyPointOfSale.ts
42 lines (40 loc) · 1.61 KB
/
notifyPointOfSale.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
import {Cell, CellLoop, Stream, Unit} from "sodiumjs";
import {Sale} from "../lib";
import {End, Fuel, IFillInterface, Optional, Phase} from "../types";
import LifeCycle from "./lifeCycle";
export default function getNotifyPointOfSale(lc: LifeCycle,
sClearSale: Stream<Unit>, {cPrice, cLitersDelivered, cDollarsDelivered}
: IFillInterface) {
const cPhase = new CellLoop<Phase>();
const sStart: Stream<Fuel> = lc.sStart.gate(cPhase.map(p => p === Phase.IDLE));
const sEnd: Stream<End> = lc.sEnd.gate(cPhase.map(p => p === Phase.FILLING));
cPhase.loop(
sStart.map(u => Phase.FILLING)
.orElse(sEnd.map(u => Phase.POS))
.orElse(sClearSale.map(u => Phase.IDLE))
.hold(Phase.IDLE),
);
const cFuelFlowing: Cell<Optional<Fuel>> = sStart.map(f => f as Optional<Fuel>)
.orElse(sEnd.map(f => null))
.hold(null);
const cFillActive: Cell<Optional<Fuel>> = sStart.map(f => f as Optional<Fuel>)
.orElse(sClearSale.map(f => null))
.hold(null);
const sBeep = sClearSale;
const sSaleComplete = sEnd.snapshot1(
cFuelFlowing.lift4(cPrice, cDollarsDelivered, cLitersDelivered,
(fuelFlowing, price, dollarsDelivered, litersDelivered) => {
return fuelFlowing !== null
? new Sale(fuelFlowing, price, dollarsDelivered, litersDelivered)
: null;
}),
).filterNotNull() as Stream<Sale>;
return {
sStart,
cFillActive,
cFuelFlowing,
sEnd,
sBeep,
sSaleComplete,
};
}