s-stream

Minimal streams

Usage no npm install needed!

<script type="module">
  import sStream from 'https://cdn.skypack.dev/s-stream';
</script>

README

s-stream

Basic use

As a getter/setter

var s = stream(5);
s(); // 5
s(3);
s(); // 3

Can be observed via .map

var s = stream();

s.map(function(value) {
    // called once with value 'foo'
    // called once more with value 'bar'
});

s('foo');
s('bar');

Map is called immediately if initialized with any value

var s = stream(123);
s.map(function(value) {
    // called with value 123
});

Including undefined

Promises

Streams wait for promise to resolve

var p = new Promise(function(resolve, reject) {
    setTimeout(function() {
        resolve('foo');
    }, 250);
});

var s = stream(p);

s.map(function(value) {
    // called after 250ms with value 'foo'
});

With error handling

var p = new Promise(function(resolve, reject) {
    setTimeout(function() {
        reject(new Error('failed'));
    }, 250);
});

var s = stream(p);

s.map(function(value) {
    // never called
});

s.catch(function(err) {
    // called after 250ms with Error('failed');
});

Preventing memory leaks

Listeners can be removed with .off

var s = stream();
var f = function() {};
s.map(f);
s.off(f);

Other information

stream.toString returns a string of form stream(contentType)

var s = stream(123);
s.toString(); // `stream(Number)`