Skip to content

Instantly share code, notes, and snippets.

@dmamills
Created May 29, 2017 02:23
Show Gist options
  • Select an option

  • Save dmamills/3ce152e4bf5db2d65945e6636399a30d to your computer and use it in GitHub Desktop.

Select an option

Save dmamills/3ce152e4bf5db2d65945e6636399a30d to your computer and use it in GitHub Desktop.
JS Bin extremely simple pull-streams example // source http://jsbin.com/redodazigu
<!DOCTYPE html>
<html>
<head>
<meta name="description" content="extremely simple pull-streams example">
<meta charset="utf-8">
<meta name="viewport" content="width=device-width">
<title>JS Bin</title>
</head>
<body>
<script id="jsbin-javascript">
/*
Creates a basic values "pull-stream" it's considered pull because the data
is extracted from it, rather that a "push-stream" which guess what: PUSHES!
*/
function values(arr) {
var i =0;
return function(abort, cb) {
if(i+1 == arr.length) return cb(true);
cb(null, arr[i++]);
}
}
function count() {
var n = 0;
return function(abort, cb) {
if(abort) cb(abort);
cb(n++)
}
}
/**
this is considered a "sink" because it consumes a stream, it pulls from the stream.
*/
function log() {
return function(source) {
source(null, function next(err, val) {
if(err) return;
console.log(val);
source(null, next);
});
}
}
//Map transform over a stream.
function map(mapper) {
return function(source) {
return function(abort, cb) {
source(abort, function(end, data){
if(end) cb(end);
else cb(null, mapper(data));
});
}
}
}
/*
This composes a pull stream, essentially writing streams like this:
sink(through(source));
requires you to read it manga or RPN style. Why not:
pull(source, through, stream)
*/
function pull() {
//get all arguments
var args = [].slice.call(arguments);
//grab the first stream
var stream = args.shift();
//while we have anything left
while(args.length) {
//apply the next stream, and set it as the current stream
stream = args.shift()(stream);
}
}
//create a stream
var source = values([1,2,3,4,5,6]);
//A map transform to add 1 to each value
var through = map(function(v) {
return v+1;
})
//output sink
var sink = log();
pull(source, through, sink);
</script>
<script id="jsbin-source-javascript" type="text/javascript">/*
Creates a basic values "pull-stream" it's considered pull because the data
is extracted from it, rather that a "push-stream" which guess what: PUSHES!
*/
function values(arr) {
var i =0;
return function(abort, cb) {
if(i+1 == arr.length) return cb(true);
cb(null, arr[i++]);
}
}
function count() {
var n = 0;
return function(abort, cb) {
if(abort) cb(abort);
cb(n++)
}
}
/**
this is considered a "sink" because it consumes a stream, it pulls from the stream.
*/
function log() {
return function(source) {
source(null, function next(err, val) {
if(err) return;
console.log(val);
source(null, next);
});
}
}
//Map transform over a stream.
function map(mapper) {
return function(source) {
return function(abort, cb) {
source(abort, function(end, data){
if(end) cb(end);
else cb(null, mapper(data));
});
}
}
}
/*
This composes a pull stream, essentially writing streams like this:
sink(through(source));
requires you to read it manga or RPN style. Why not:
pull(source, through, stream)
*/
function pull() {
//get all arguments
var args = [].slice.call(arguments);
//grab the first stream
var stream = args.shift();
//while we have anything left
while(args.length) {
//apply the next stream, and set it as the current stream
stream = args.shift()(stream);
}
}
//create a stream
var source = values([1,2,3,4,5,6]);
//A map transform to add 1 to each value
var through = map(function(v) {
return v+1;
})
//output sink
var sink = log();
pull(source, through, sink);
</script></body>
</html>
/*
Creates a basic values "pull-stream" it's considered pull because the data
is extracted from it, rather that a "push-stream" which guess what: PUSHES!
*/
function values(arr) {
var i =0;
return function(abort, cb) {
if(i+1 == arr.length) return cb(true);
cb(null, arr[i++]);
}
}
function count() {
var n = 0;
return function(abort, cb) {
if(abort) cb(abort);
cb(n++)
}
}
/**
this is considered a "sink" because it consumes a stream, it pulls from the stream.
*/
function log() {
return function(source) {
source(null, function next(err, val) {
if(err) return;
console.log(val);
source(null, next);
});
}
}
//Map transform over a stream.
function map(mapper) {
return function(source) {
return function(abort, cb) {
source(abort, function(end, data){
if(end) cb(end);
else cb(null, mapper(data));
});
}
}
}
/*
This composes a pull stream, essentially writing streams like this:
sink(through(source));
requires you to read it manga or RPN style. Why not:
pull(source, through, stream)
*/
function pull() {
//get all arguments
var args = [].slice.call(arguments);
//grab the first stream
var stream = args.shift();
//while we have anything left
while(args.length) {
//apply the next stream, and set it as the current stream
stream = args.shift()(stream);
}
}
//create a stream
var source = values([1,2,3,4,5,6]);
//A map transform to add 1 to each value
var through = map(function(v) {
return v+1;
})
//output sink
var sink = log();
pull(source, through, sink);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment