Skip to content

Instantly share code, notes, and snippets.

@ivan-kleshnin
Last active October 26, 2017 10:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ivan-kleshnin/1509ba04644bf6632dfc9b3e84337053 to your computer and use it in GitHub Desktop.
Save ivan-kleshnin/1509ba04644bf6632dfc9b3e84337053 to your computer and use it in GitHub Desktop.
Reactive middleware stack prototype
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