Skip to content

Instantly share code, notes, and snippets.

@AdamSaleh
Created November 14, 2017 16:25
Show Gist options
  • Save AdamSaleh/4de5b8cc1e4ce8a0ae4c67d26904b8e7 to your computer and use it in GitHub Desktop.
Save AdamSaleh/4de5b8cc1e4ce8a0ae4c67d26904b8e7 to your computer and use it in GitHub Desktop.
purescriptStreamToAff
'use strict';
exports._collapseStream = function (stream) { // accepts a request
return function (onError, onSuccess) { // and callbacks
var buffer = '';
stream.on('data', (d) => { buffer = buffer + d; });
stream.on('err', (err) => onError(err));
stream.on('end', () => onSuccess(buffer));
// Return a canceler, which is just another Aff effect.
return function (cancelError, cancelerError, cancelerSuccess) {
stream.pause(); // cancel the request
cancelerSuccess(); // invoke the success callback for the canceler
};
};
};
foreign import _collapseStream :: forall w e. Stream.Readable w e -> EffFnAff e String
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment