Made @ Anvil Research
I’ve just started a new library RxMo and published its first working version. Currently has
Observable, Subject, pipe, of, map, first, concatAll, mergeMap.
Why
Business processes are asynchronous. Smart contracts in IC dealing with business logic could benefit a great deal from reactive programming which is especially good with complex async logic.
Orthogonal persistence & Heartbeat make it even more powerful. Reactively programmed business logic should be a significant differentiator for IC.
Besides business logic: If Ethereum programs collaboration is like a lego pyramid. IC programs collaboration is more like async hypergraph and can also benefit from reactive programming.
This pattern will also be an interesting combination Motoko | Jazz heartbeat
Notes: Testing with moc -r
could benefit from async delay mock or heartbeat so deploying won’t be needed to test asynchronous execution.
Example
The real-world examples will be coming soon, meanwhile:
pipe2(
of<Text>(["A","B","C"]),
mergeMap<Text,Text>( func(x) {
pipe2(
of<Nat>([1,2,3,4,5,6]),
map<Nat, Text>( func (i) {
x # debug_show(i);
})
);
}, 1)
).subscribe( {
next = func (v) {
Debug.print(v);
};
complete = func () {
Debug.print("complete");
}
});
Outputs:
A1 A2 A3 A4 A5 A6 B1 B2 B3 B4 B5 B6 C1 C2 C3 C4 C5 C6
To help you visualize reactive programming : RxMarbles: Interactive diagrams of Rx Observables