Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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