Skip to content

Instantly share code, notes, and snippets.

@dignifiedquire
Last active April 16, 2016 19:16
Show Gist options
  • Save dignifiedquire/2d81b85074f153c0f2c28694bdb7eed9 to your computer and use it in GitHub Desktop.
Save dignifiedquire/2d81b85074f153c0f2c28694bdb7eed9 to your computer and use it in GitHub Desktop.
'use strict'
const _ = require('highland')
// Fake dagservice get
const get = (hash, cb) => {
cb(null, {
type: 'file',
data: Math.random() + hash,
links: []
})
}
// Turn our callback function into a stream generator
const getStream = _.wrapCallback(get)
const highFileExporter = (node, name, dir) => {
let stream
const unmarshal = (data) => data
if (node.links.length === 0) {
stream = _(unmarshal(node.data))
} else {
stream = _(node.links)
.flatMap(getStream)
.map((res) => unmarshal(res.data))
}
return {
stream,
path: name,
dir
}
}
const res = highFileExporter({
links: ['hello', 'world']
}, 'highland.txt', 'awesome/me')
let i = 0
console.log('got result', res.path, res.dir)
res.stream.each((data) => {
console.log('got my data %s', ++i, data)
}).done(() => {})
❯ node highland.js
got result highland.txt awesome/me
got my data 1 0.7449878605548292hello
got my data 2 0.36780680948868394world
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment