Last active
October 26, 2017 10:24
-
-
Save ivan-kleshnin/1509ba04644bf6632dfc9b3e84337053 to your computer and use it in GitHub Desktop.
Reactive middleware stack prototype
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import * as R from "ramda" | |
let Atom = R.curry((options, actions) => { | |
console.log(`@ Atom "${options.name || ""}" is called`) | |
return {$: "$"} | |
}) | |
let withLog = R.curry((options, Atom) => { | |
return (actions) => { | |
console.log(`@ Shell "withLog" is called`) | |
return Atom(actions) | |
} | |
}) | |
let withStorage = R.curry((options, Atom) => { | |
return (actions) => { | |
console.log(`@ Shell "withStorage" is called`) | |
return Atom(actions) | |
} | |
}) | |
let withHistory = R.curry((options, Atom) => { | |
return (actions) => { | |
console.log(`@ Shell "withHistory" is called`) | |
return Atom(actions) | |
} | |
}) | |
let actions = {foo: "FOO$", bar: "BAR$"} | |
let atomOptions = {name: "db"} | |
let historyOptions = {} | |
let logOptions = {} | |
let storageOptions = {} | |
// Middleware stack | |
let atom = R.pipe( | |
() => Atom(atomOptions), // () -> Atom1 (lowest level) | |
withLog(logOptions), // Atom1 -> Atom2 (mid level) | |
withStorage(storageOptions), // Atom2 -> Atom3 (mid level) | |
withHistory(historyOptions), // Atom3 -> Atom4 (highest level) | |
)()(actions) // Atom4(actions) -> atom | |
console.log(atom) | |
/* | |
@ Shell "withHistory" is called | |
@ Shell "withStorage" is called | |
@ Shell "withLog" is called | |
@ Atom "db" is called | |
{ '$': '$' } | |
*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment