Last active
April 16, 2016 19:16
-
-
Save dignifiedquire/2d81b85074f153c0f2c28694bdb7eed9 to your computer and use it in GitHub Desktop.
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
'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(() => {}) |
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
❯ 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