Skip to content

Instantly share code, notes, and snippets.

@Raynos
Last active December 27, 2015 05:09
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 Raynos/7272324 to your computer and use it in GitHub Desktop.
Save Raynos/7272324 to your computer and use it in GitHub Desktop.
// { DATA, END, READ_ERROR, ERROR } + { PAUSE, RESUME, ABORT, ERROR, FINISH }
// producer
var producer = {
read: function (cb) {
// cb(null, DATA)
// cb(null, undefined | Stream.END_TOKEN)
// cb(READ_ERROR, undefined | Stream.END_TOKEN)
},
abort: function (message) {
// consumer specific abort implementation
},
// this ERROR is an outside of read() ERROR
onError: function (listener) {}
}
// consumer
function consumer(producer) {
// PAUSE is do not call .read()
// RESUME is call .read() one at a time sequentially
// ABORT is call .abort(optionalMessage)
// return FINI & ERROR
return {
// FINISH
onFinish: function (listener) {},
// ERROR
onError: function (listener) {}
}
}
// however there is a third thing in play here other then consumer & producer
// It's the author & user of the streams
// if consumer or producer creates an error then it needs to be handled
var result = consumer(producer)
producer.onError(function (err) {
})
result.onError(function (err) {
})
result.onFinish(function () {
// consumer is done.
// continue doing stuff
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment