Last active
April 22, 2017 08:45
-
-
Save FeliciousX/14d7d32a20794daee89b2d8dd52fd6fe to your computer and use it in GitHub Desktop.
CycleJS Sink helper function
This file contains 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
/** | |
pretty straightforward implementatation. | |
( Stream -> Stream ) -> string -> HOC | |
HOC is a higher order component where the Signature is always | |
(sources -> sinks) -> sources -> sinks | |
**/ | |
function transformSink( fn, key ) { | |
return ( Component ) => ( sources ) => { | |
const sinks = Component( sources ) | |
return { | |
...sinks, | |
[key]: sinks[key].map( fn ) | |
} | |
} | |
} | |
/** | |
now to transform multiple sinks | |
gets a key to fn pair dictionary object, | |
reduce over all keys and transform sinks. | |
To maintain purity, clone sinks. | |
e.g. transformDict = { 'HTTP' : stream => stream.map( /../ ) } | |
Transform Object -> HOC | |
**/ | |
function transformSinks( transformDict ) { | |
return ( Component ) => ( sources ) => Object.keys( transformDict ) | |
.reduce((sinks, key) => ({ | |
...sinks, | |
[key]: sinks[key].map( transformDict[key] ) | |
}) | |
, Component( sources )) | |
} | |
/** | |
gets carried away with Ramda? lol | |
**/ | |
// import R from 'ramda' | |
function v2_transformSinks( transformObj ) { | |
const transformSink = ( sinks, [ key, fn ] ) => ({ | |
...sinks, | |
[key] : sinks[key].map( fn ) | |
}) | |
return ( Component ) => ( sources ) => R.compose( | |
R.reduce( transformSink, Component( sources )), | |
R.toPairs | |
)( transformObj ) | |
} | |
/** | |
How would `v2_transformSinks` be refactored? | |
Or how better would you refactor `transformSinks`? | |
Right now it has become quite a mess. Perhaps I've gone too far? | |
Also still learning Hindley Milner notation.. help correct my mistake please :P | |
**/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment