Comparing version
{ | ||
"name": "F.js", | ||
"version": "0.5.0", | ||
"version": "0.5.1", | ||
"homepage": "https://github.com/colin-dumitru/F.js", | ||
@@ -5,0 +5,0 @@ "authors": [ |
@@ -1,4 +0,9 @@ | ||
## 0.5.0 (in development) | ||
## 0.5.1 | ||
* Fixed an issue where stream could not be used by multiple iterable chains. | ||
* Event streams now detach their handlers when they have no more listeners to prevent memory leaks. | ||
* Polyfill promises can now be chained through the "then" method. | ||
## 0.5.0 | ||
* The `pullStream` no longer requires a stream to pull, and inferes the value initially passed to the `F(...)` method. | ||
* Added `throttle` method on streams for limiting how often values can be pushed to a stream. | ||
* Added `bindPush` method on streams. |
@@ -1,1 +0,1 @@ | ||
!function(a){if(!a.Promise){var b=function(a){var b=this;this._value=void 0,this._resolved=!1,this._callbacks=[],a(function(a){b._value=a,b._resolved=!0;for(var c=0;c<b._callbacks.length;c++)b._callbacks[c](a)})};b.prototype.then=function(a){this._resolved?a(this._value):this._callbacks.push(a)},a.Promise=b}}(this),function(a){var b=function(){};b.and=function(){var a=arguments;return a&&Array.isArray(a[0])&&(a=a[0]),function(b){for(var c=0;c<a.length;c++)if(!a[c](b))return!1;return!0}},b.or=function(){var a=arguments;return a&&Array.isArray(a[0])&&(a=a[0]),function(b){for(var c=0;c<a.length;c++)if(a[c](b))return!0;return!1}},b.not=function(a){return function(){return!a.apply(this,arguments)}},b.alwaysTrue=function(){return!0},b.alwaysFalse=function(){return!1},b.isNull=function(a){return null===a},b.notNull=function(a){return null!==a},b.hasProperty=function(){var a=arguments;return function(b){for(var c=0;c<a.length;c++){if("undefined"==typeof b[a[c]])return!1;b=b[a[c]]}return!0}},b["in"]=function(a){return function(b){return-1!==a.indexOf(b)}},b.equalTo=function(a){return function(b){return b==a}},b.strongEqualTo=function(a){return function(b){return b===a}},b.instanceOf=function(a){return function(b){return b instanceof a}},b.on=function(){var a=arguments[arguments.length-1],b=arguments;return function(c){for(var d=0;d<b.length-1;d++){if("undefined"==typeof c[b[d]])return!1;c=c[b[d]]}return a(c)}},b.count=function(a){return function(){return a-->0?!0:!1}},b.limit=function(a){return function(){return--a>0?!0:!1}},a.P=b}(this),function(a){var b=a.P,c=function(a){return d(a,{root:a})},d=function(a,b){return a instanceof q?a:"function"==typeof a.next?new q(a,b):a instanceof Function?new q(new h(a),b):"number"==typeof a.length?new q(new f(a),b):new q(new g(a),b)},e=function(a){return"undefined"==typeof a.value},f=function(a){this._collection=a,this._index=-1};f.prototype.next=function(){return this._index++,{value:this._collection[this._index],done:this._index>=this._collection.length-1}};var g=function(a){this._keys=Object.keys(a),this._obj=a,this._index=-1};g.prototype.next=function(){var a=this._keys[++this._index],b={done:this._index>=this._keys.length};return b.done||(b.value=[a,this._obj[a]]),b};var h=function(a){this._index=-1,this._func=a};h.prototype.next=function(){var a=this._func(++this._index);return{value:a,done:"undefined"==typeof a}};var i=function(a,b){this._iter=a,this._func=b};i.prototype.next=function(){var a=this._iter.next();return e(a)?a:{value:this._func(a.value),done:a.done}};var j=function(a,b){this._iter=a,this._pred=b};j.prototype.next=function(){var a=this._iter.next();return e(a)||this._pred(a.value)||delete a.value,a};var k=function(a,b){this._iter=a,this._func=b};k.prototype.next=function(){var a=this._iter.next();return e(a)||this._func(a.value),a};var l=function(a,b){this._iter=a,this._pred=b,this._buffer=[]};l.prototype.next=function(){var a=this._iter.next();return e(a)||(this._buffer.push(a.value),this._pred(a.value)?a.value=this._buffer.splice(0):delete a.value),a};var m=function(a,b){this._iter=a,this._count=b};m.prototype.next=function(){var a=this._iter.next();return e(a)||--this._count<=0&&(a.done=!0),a};var n=function(a,b){this._iter=a,this._pred=b};n.prototype.next=function(){var a=this._iter.next();return e(a)||this._pred(a.value)||(delete a.value,a.done=!0),a};var o=function(a,b){this._liter=a,this._riter=b,this._lbuffer=[],this._rbuffer=[]};o.prototype._read=function(a,b){var d=a.next();return c.isEmptyValue(d)||b.push(d),d.done},o.prototype.next=function(){var a=this._read(this._liter,this._lbuffer),b=this._read(this._riter,this._rbuffer),c=a&&b;return this._lbuffer.length&&this._rbuffer.length?{value:[this._lbuffer.shift().value,this._rbuffer.shift().value],done:c}:{done:c}};var p=function(a){this._iterators=a.slice(1),this._activeIter=a.length?a[0]:c([])._iter};p.prototype.next=function(){for(var a;;){if(a=this._activeIter.next(),!e(a)||!this._iterators.length)break;this._activeIter=this._iterators[0],this._iterators=this._iterators.slice(1)}return a.done&&this._iterators.length&&(a.done=!1,this._activeIter=this._iterators[0],this._iterators=this._iterators.slice(1)),a};var q=function(a,b){if(this._iter=a,this._context=b,!b)throw"Must provide a context"};q.prototype.concat=function(){return d(new p([this._iter].concat(c(arguments).map(function(a){return c(a)._iter}).toArray())),this._context)},q.prototype.flatten=function(){return d(new p(this.map(function(a){return c(a)._iter}).toArray()),this._context)},q.prototype.map=function(a){return d(new i(this._iter,a),this._context)},q.prototype.filter=function(a){return d(new j(this._iter,a),this._context)},q.prototype.zip=function(a){return d(new o(this._iter,c(a)._iter),this._context)},q.prototype.toArray=function(){var a,b=[];do a=this._iter.next(),e(a)||b.push(a.value);while(!a.done);return b},q.prototype.toMap=function(){var a,b={};do a=this._iter.next(),e(a)||(b[a.value[0]]=a.value[1]);while(!a.done);return b},q.prototype.partition=function(a){var b,c=[[],[]];do b=this._iter.next(),e(b)||(a(b.value)?c[0].push(b.value):c[1].push(b.value));while(!b.done);return c},q.prototype.find=function(a){var b;do if(b=this._iter.next(),!e(b)&&a(b.value))return b.value;while(!b.done)},q.prototype.findIndex=function(a){var b,c=-1;do if(b=this._iter.next(),c++,!e(b)&&a(b.value))return c;while(!b.done);return-1},q.prototype.drop=function(a){return d(new j(this._iter,b.not(b.count(a))),this._context)},q.prototype.take=function(a){return d(new m(this._iter,a),this._context)},q.prototype.dropWhile=function(a){var b=!1;return d(new j(this._iter,function(c){return b?!0:a(c)?!1:(b=!0,!0)}),this._context)},q.prototype.takeWhile=function(a){return d(new n(this._iter,a),this._context)},q.prototype.fold=function(a,b){var c;do c=this._iter.next(),e(c)||(b="undefined"==typeof b?c.value:a(b,c.value));while(!c.done);return b},q.prototype.foreach=function(a){var b,c=0;do b=this._iter.next(),e(b)||a(b.value,c++,b.done);while(!b.done)},q.prototype.each=function(a){return d(new k(this._iter,a),this._context)},q.prototype.feedStream=function(a){return d(new k(this._iter,function(b){a.push(b)}),this._context)},q.prototype.reverse=function(){var a,b=[];do a=this._iter.next(),e(a)||b.unshift(a.value);while(!a.done);return d(b,this._context)},q.prototype.unique=function(){var a=[];return d(new j(this._iter,function(b){return-1===a.indexOf(b)?(a.push(b),!0):!1}),this._context)},q.prototype.property=function(){var a=arguments;return d(new i(this._iter,function(b){for(var c=0;c<a.length;c++){if("undefined"==typeof b[a[c]])return null;b=b[a[c]]}return b}),this._context)},q.prototype.keys=function(){arguments;return d(new i(this._iter,function(a){return a[0]}),this._context)},q.prototype.values=function(){arguments;return d(new i(this._iter,function(a){return a[1]}),this._context)},q.prototype.accumulateUntil=function(a){return d(new l(this._iter,a),this._context)},q.prototype.log=function(){return d(new k(this._iter,function(b){a.console&&console.log(b)}),this._context)},q.prototype.all=function(a){return d(this._iter,this._context).fold(function(b,c){return b&&a(c)},!0)},q.prototype.some=function(a){return d(this._iter,this._context).fold(function(b,c){return b||a(c)},!1)},q.prototype.none=function(a){return d(this._iter,this._context).fold(function(b,c){return b&&!a(c)},!0)},q.prototype.max=function(a){return a||(a=function(a,b){return a>b}),d(this._iter,this._context).fold(function(b,c){return a(b,c)?b:c})},q.prototype.min=function(a){return a||(a=function(a,b){return b>a}),d(this._iter,this._context).max(a)},c.Iterable=q,c.isEmptyValue=e,a.F=c}(this),function(a){var b=a.F,c=a.P,d=a.Promise;b.stream=function(a){return b.lastStream=new e(a)},b.eventStream=function(a,c){var d=b.stream(),e=function(a){d.push(a)};return"function"==typeof a.addEventListener?(a.addEventListener(c,e),d.done(function(){a.removeEventListener(c,e)})):"function"==typeof a.on&&(a.on(c,e),d.done(function(){a.off(c,e)})),d},b.estream=b.eventStream,b.multiplexStream=function(){var a,c=b.stream(),d=0,e=function(a){c.push(a)},f=function(){++d===a.length&&c.stop()};a=Array.isArray(arguments[0])?arguments[0]:arguments;for(var g=0;g<a.length;g++)a[g].then(e).done(f);return c},b.mplex=b.multiplexStream,b.intervalStream=function(a,c){var d=b.stream(),e=setInterval(function(){d.push(a)},c);return d.done(function(){clearInterval(e)}),d},b.timerStream=function(a,c){var d=b.stream(),e=setTimeout(function(){d.push(a),d.stop()},c);return d.done(function(){clearTimeout(e)}),d};var e=function(a){this._done=!1,this._eventListeners=[],this._doneListeners=[],this._size=a||1,this._buffer=[]};e.prototype.then=function(a){return this._eventListeners.push(a),this},e.prototype.done=function(a){return this._doneListeners.push(a),this},e.prototype.push=function(a){if(this._done)return this;this._buffer.push(a),this._buffer.length>this._size&&this._buffer.shift();for(var b=0;b<this._eventListeners.length;b++)this._eventListeners[b](a);return this},e.prototype.bindPush=function(){var a=this,b=this.push;return function(){b.apply(a,arguments)}},e.prototype.throttle=function(a){var b=this.push,c=this,d=0,e=0,f=[];return this.push=function(g){var h=Date.now();f.push(g),h-e>a?(b.call(c,f.splice(0)),e=h):d||(d=setTimeout(function(){b.call(c,f.splice(0)),e=Date.now(),d=0},a-(h-e)))},this},e.prototype.pushAll=function(a){if(!this._done){for(var b=0;b<a.length;b++)this.push(a[b]);return this}},e.prototype.next=function(){var a={value:this._buffer.shift(),done:this._done};return a},e.prototype.stop=function(){if(!this._done){this._done=!0;for(var a=0;a<this._doneListeners.length;a++)this._doneListeners[a]()}},b.Iterable.prototype.pullStream=function(a,f){a instanceof e||(f=a,a=this._context.root),f||(f=c.alwaysTrue);var g=this;return new d(function(c){var d=[];a.then(function(){var c=g._iter.next();b.isEmptyValue(c)||d.push(c.value),(!f(c.value)||c.done)&&a.stop()}),a.done(function(){c(d)})})}}(this),function(a){var b={F:a.F,P:a.P,Promise:a.Promise};"function"==typeof define&&define.amd?define([],function(){return b}):"object"==typeof exports&&(module.exports=b)}(this); | ||
!function(a){if(!a.Promise){var b=function(a){var b=this;this._value=void 0,this._resolved=!1,this._callbacks=[],a(function(a){b._value=a,b._resolved=!0;for(var c=0;c<b._callbacks.length;c++)b._callbacks[c](a)})};b.prototype.then=function(a){return this._resolved?a(this._value):this._callbacks.push(a),this},a.Promise=b}}(this),function(a){var b=function(){};b.and=function(){var a=arguments;return a&&Array.isArray(a[0])&&(a=a[0]),function(b){for(var c=0;c<a.length;c++)if(!a[c](b))return!1;return!0}},b.or=function(){var a=arguments;return a&&Array.isArray(a[0])&&(a=a[0]),function(b){for(var c=0;c<a.length;c++)if(a[c](b))return!0;return!1}},b.not=function(a){return function(){return!a.apply(this,arguments)}},b.alwaysTrue=function(){return!0},b.alwaysFalse=function(){return!1},b.isNull=function(a){return null===a},b.notNull=function(a){return null!==a},b.hasProperty=function(){var a=arguments;return function(b){for(var c=0;c<a.length;c++){if("undefined"==typeof b[a[c]])return!1;b=b[a[c]]}return!0}},b["in"]=function(a){return function(b){return-1!==a.indexOf(b)}},b.equalTo=function(a){return function(b){return b==a}},b.strongEqualTo=function(a){return function(b){return b===a}},b.instanceOf=function(a){return function(b){return b instanceof a}},b.on=function(){var a=arguments[arguments.length-1],b=arguments;return function(c){for(var d=0;d<b.length-1;d++){if("undefined"==typeof c[b[d]])return!1;c=c[b[d]]}return a(c)}},b.count=function(a){return function(){return a-->0?!0:!1}},b.limit=function(a){return function(){return--a>0?!0:!1}},a.P=b}(this),function(a){var b=a.P,c=function(a){var b={},c=d(a,b);return b.root=a,b.iterable=c,c},d=function(a,b){if(a instanceof q)return a;var c;return c=Array.isArray(a)?new f(a):"function"==typeof a["@@iterator"]?a["@@iterator"]():"function"==typeof a.next?a:a instanceof Function?new h(a):"number"==typeof a.length?new f(a):new g(a),new q(c,b)},e=function(a){return"undefined"==typeof a.value},f=function(a){this._collection=a,this._index=-1};f.prototype.next=function(){return this._index++,{value:this._collection[this._index],done:this._index>=this._collection.length-1}};var g=function(a){this._keys=Object.keys(a),this._obj=a,this._index=-1};g.prototype.next=function(){var a=this._keys[++this._index],b={done:this._index>=this._keys.length};return b.done||(b.value=[a,this._obj[a]]),b};var h=function(a){this._index=-1,this._func=a};h.prototype.next=function(){var a=this._func(++this._index);return{value:a,done:"undefined"==typeof a}};var i=function(a,b){this._iter=a,this._func=b};i.prototype.next=function(){var a=this._iter.next();return e(a)?a:{value:this._func(a.value),done:a.done}};var j=function(a,b){this._iter=a,this._pred=b};j.prototype.next=function(){var a=this._iter.next();return e(a)||this._pred(a.value)||delete a.value,a};var k=function(a,b){this._iter=a,this._func=b};k.prototype.next=function(){var a=this._iter.next();return e(a)||this._func(a.value),a};var l=function(a,b){this._iter=a,this._pred=b,this._buffer=[]};l.prototype.next=function(){var a=this._iter.next();return e(a)||(this._buffer.push(a.value),this._pred(a.value)?a.value=this._buffer.splice(0):delete a.value),a};var m=function(a,b){this._iter=a,this._count=b};m.prototype.next=function(){var a=this._iter.next();return e(a)||--this._count<=0&&(a.done=!0),a};var n=function(a,b){this._iter=a,this._pred=b};n.prototype.next=function(){var a=this._iter.next();return e(a)||this._pred(a.value)||(delete a.value,a.done=!0),a};var o=function(a,b){this._liter=a,this._riter=b,this._lbuffer=[],this._rbuffer=[]};o.prototype._read=function(a,b){var d=a.next();return c.isEmptyValue(d)||b.push(d),d.done},o.prototype.next=function(){var a=this._read(this._liter,this._lbuffer),b=this._read(this._riter,this._rbuffer),c=a&&b;return this._lbuffer.length&&this._rbuffer.length?{value:[this._lbuffer.shift().value,this._rbuffer.shift().value],done:c}:{done:c}};var p=function(a){this._iterators=a.slice(1),this._activeIter=a.length?a[0]:c([])._iter};p.prototype.next=function(){for(var a;;){if(a=this._activeIter.next(),!e(a)||!this._iterators.length)break;this._activeIter=this._iterators[0],this._iterators=this._iterators.slice(1)}return a.done&&this._iterators.length&&(a.done=!1,this._activeIter=this._iterators[0],this._iterators=this._iterators.slice(1)),a};var q=function(a,b){if(this._iter=a,this._context=b,!b)throw"Must provide a context"};q.prototype.concat=function(){return d(new p([this._iter].concat(c(arguments).map(function(a){return c(a)._iter}).toArray())),this._context)},q.prototype.flatten=function(){return d(new p(this.map(function(a){return c(a)._iter}).toArray()),this._context)},q.prototype.map=function(a){return d(new i(this._iter,a),this._context)},q.prototype.filter=function(a){return d(new j(this._iter,a),this._context)},q.prototype.zip=function(a){return d(new o(this._iter,c(a)._iter),this._context)},q.prototype.toArray=function(){var a,b=[];do a=this._iter.next(),e(a)||b.push(a.value);while(!a.done);return b},q.prototype.toMap=function(){var a,b={};do a=this._iter.next(),e(a)||(b[a.value[0]]=a.value[1]);while(!a.done);return b},q.prototype.partition=function(a){var b,c=[[],[]];do b=this._iter.next(),e(b)||(a(b.value)?c[0].push(b.value):c[1].push(b.value));while(!b.done);return c},q.prototype.find=function(a){var b;do if(b=this._iter.next(),!e(b)&&a(b.value))return b.value;while(!b.done)},q.prototype.findIndex=function(a){var b,c=-1;do if(b=this._iter.next(),c++,!e(b)&&a(b.value))return c;while(!b.done);return-1},q.prototype.drop=function(a){return d(new j(this._iter,b.not(b.count(a))),this._context)},q.prototype.take=function(a){return d(new m(this._iter,a),this._context)},q.prototype.dropWhile=function(a){var b=!1;return d(new j(this._iter,function(c){return b?!0:a(c)?!1:(b=!0,!0)}),this._context)},q.prototype.takeWhile=function(a){return d(new n(this._iter,a),this._context)},q.prototype.fold=function(a,b){var c;do c=this._iter.next(),e(c)||(b="undefined"==typeof b?c.value:a(b,c.value));while(!c.done);return b},q.prototype.foreach=function(a){var b,c=0;do b=this._iter.next(),e(b)||a(b.value,c++,b.done);while(!b.done)},q.prototype.each=function(a){return d(new k(this._iter,a),this._context)},q.prototype.feedStream=function(a){return d(new k(this._iter,function(b){a.push(b)}),this._context)},q.prototype.reverse=function(){var a,b=[];do a=this._iter.next(),e(a)||b.unshift(a.value);while(!a.done);return d(b,this._context)},q.prototype.unique=function(){var a=[];return d(new j(this._iter,function(b){return-1===a.indexOf(b)?(a.push(b),!0):!1}),this._context)},q.prototype.property=function(){var a=arguments;return d(new i(this._iter,function(b){for(var c=0;c<a.length;c++){if("undefined"==typeof b[a[c]])return null;b=b[a[c]]}return b}),this._context)},q.prototype.keys=function(){arguments;return d(new i(this._iter,function(a){return a[0]}),this._context)},q.prototype.values=function(){arguments;return d(new i(this._iter,function(a){return a[1]}),this._context)},q.prototype.accumulateUntil=function(a){return d(new l(this._iter,a),this._context)},q.prototype.log=function(){return d(new k(this._iter,function(b){a.console&&console.log(b)}),this._context)},q.prototype.all=function(a){return d(this._iter,this._context).fold(function(b,c){return b&&a(c)},!0)},q.prototype.some=function(a){return d(this._iter,this._context).fold(function(b,c){return b||a(c)},!1)},q.prototype.none=function(a){return d(this._iter,this._context).fold(function(b,c){return b&&!a(c)},!0)},q.prototype.max=function(a){return a||(a=function(a,b){return a>b}),d(this._iter,this._context).fold(function(b,c){return a(b,c)?b:c})},q.prototype.min=function(a){return a||(a=function(a,b){return b>a}),d(this._iter,this._context).max(a)},c.Iterable=q,c.isEmptyValue=e,a.F=c}(this),function(a){var b=a.F,c=a.P,d=a.Promise,e=function(a){var b=this;this._stream=a,this._buffer=[],this._done=!1,this._handler=function(a){b._buffer.push(a)},a.then(this._handler),a.done(function(){b._done=!0})};e.prototype.next=function(){return{value:this._buffer.shift(),done:this._done}},e.prototype.stop=function(){this._done=!0,this._stream.remove(this._handler)},b.stream=function(a){return b.lastStream=new f(a)},b.eventStream=function(a,c){function d(a){i.push(a),i._eventListeners.length||f()}function e(){j=!0,g.call(a,c,d)}function f(){j=!1,h.call(a,c,d)}var g,h,i=b.stream(),j=!1,k=i.then;return"function"==typeof a.addEventListener?(g=a.addEventListener,h=a.removeEventListener):"function"==typeof a.on&&(g=a.on,h=a.off),i.then=function(){j||e(),k.apply(i,arguments)},i.done(function(){f()}),i},b.estream=b.eventStream,b.multiplexStream=function(){var a,c=b.stream(),d=0,e=function(a){c.push(a)},f=function(){++d===a.length&&c.stop()};a=Array.isArray(arguments[0])?arguments[0]:arguments;for(var g=0;g<a.length;g++)a[g].then(e).done(f);return c},b.mplex=b.multiplexStream,b.intervalStream=function(a,c){var d=b.stream(),e=setInterval(function(){d.push(a)},c);return d.done(function(){clearInterval(e)}),d},b.timerStream=function(a,c){var d=b.stream(),e=setTimeout(function(){d.push(a),d.stop()},c);return d.done(function(){clearTimeout(e)}),d};var f=function(a){this._done=!1,this._eventListeners=[],this._doneListeners=[],this._size=a||1,this._buffer=[]};f.prototype.then=function(a){return this._eventListeners.push(a),this},f.prototype.remove=function(a){return this._eventListeners.splice(this._eventListeners.indexOf(a),1),this},f.prototype.done=function(a){return this._doneListeners.push(a),this},f.prototype.push=function(a){return this._done?this:(this._buffer.push(a),this._buffer.length>this._size&&this._buffer.shift(),this._notify(a),this)},f.prototype._notify=function(a){this._eventListeners=this._eventListeners.filter(function(b){return b(a)!==!1})},f.prototype.bindPush=function(){var a=this,b=this.push;return function(){b.apply(a,arguments)}},f.prototype.throttle=function(a){var b=this.push,c=this,d=0,e=0,f=[];return this.push=function(g){var h=Date.now();f.push(g),h-e>a?(b.call(c,f.splice(0)),e=h):d||(d=setTimeout(function(){b.call(c,f.splice(0)),e=Date.now(),d=0},a-(h-e)))},this},f.prototype.pushAll=function(a){if(!this._done){for(var b=0;b<a.length;b++)this.push(a[b]);return this}},f.prototype.next=function(){var a={value:this._buffer.shift(),done:this._done};return a},f.prototype.stop=function(){if(!this._done){this._done=!0;for(var a=0;a<this._doneListeners.length;a++)this._doneListeners[a]()}},f.prototype["@@iterator"]=function(){return new e(this)},b.Iterable.prototype.pullStream=function(a,e){a instanceof f||(e=a,a=this._context.root),e||(e=c.alwaysTrue);var g=this;return new d(function(c){var d=[];a.then(function(){var a=g._iter.next();return b.isEmptyValue(a)||d.push(a.value),!e(a.value)||a.done?(c(d),!1):void 0}),a.done(function(){c(d)})}).then(function(a){return"function"==typeof g._context.iterable._iter.stop&&g._context.iterable._iter.stop(),a})}}(this),function(a){var b={F:a.F,P:a.P,Promise:a.Promise};"function"==typeof define&&define.amd?define([],function(){return b}):"object"==typeof exports&&(module.exports=b)}(this); |
35
lib/F.js
@@ -14,5 +14,9 @@ /* global console: true */ | ||
var F = function(iterator) { | ||
return make(iterator, { | ||
root: iterator | ||
}); | ||
var context = {}, | ||
iterable = make(iterator, context); | ||
context.root = iterator; | ||
context.iterable = iterable; | ||
return iterable; | ||
}; | ||
@@ -32,12 +36,27 @@ | ||
return iterator; | ||
} | ||
var iter; | ||
if (Array.isArray(iterator)) { | ||
/* We need an explicit "isArray" check as functions have a "length" property */ | ||
iter = new ArrayIterator(iterator); | ||
} else if (typeof iterator["@@iterator"] === "function") { | ||
/* If the input is a ES6 generator, first create an iterator, and then an iterable wrapper */ | ||
iter = iterator["@@iterator"](); | ||
} else if (typeof iterator.next === "function") { | ||
/* If the input is a ES6 generator, just create an Iterable wrapper */ | ||
return new Iterable(iterator, context); | ||
/* If the input is a ES6 iteratoe, just create an Iterable wrapper */ | ||
iter = iterator; | ||
} else if (iterator instanceof Function) { | ||
return new Iterable(new FunctionGeneratorIterator(iterator), context); | ||
iter = new FunctionGeneratorIterator(iterator); | ||
} else if (typeof iterator.length === "number") { | ||
return new Iterable(new ArrayIterator(iterator), context); | ||
/* Array iterators have higher precedence than ES6 generatos as arrays are also | ||
generators, but they do not set the "done" property to true for the last element. | ||
This causes some issue with methods such as "foreach" which expects the last | ||
element to be "done" */ | ||
iter = new ArrayIterator(iterator); | ||
} else { | ||
return new Iterable(new ObjectIterator(iterator), context); | ||
iter = new ObjectIterator(iterator); | ||
} | ||
return new Iterable(iter, context); | ||
}; | ||
@@ -44,0 +63,0 @@ |
@@ -13,2 +13,35 @@ /* global console: true */ | ||
/** | ||
* A wrapper iterator over streams which buffer a stream output. | ||
* | ||
* @param stream - the stream to buffer values. | ||
*/ | ||
var StreamIterator = function(stream) { | ||
var us = this; | ||
this._stream = stream; | ||
this._buffer = []; | ||
this._done = false; | ||
this._handler = function(value) { | ||
us._buffer.push(value); | ||
}; | ||
stream.then(this._handler); | ||
stream.done(function() { | ||
us._done = true; | ||
}); | ||
}; | ||
StreamIterator.prototype.next = function() { | ||
return { | ||
value: this._buffer.shift(), | ||
done: this._done | ||
}; | ||
}; | ||
StreamIterator.prototype.stop = function() { | ||
this._done = true; | ||
this._stream.remove(this._handler); | ||
}; | ||
/** | ||
* Creates a new stream instance. The new instance will also be set on the | ||
@@ -34,25 +67,51 @@ * F.lastStream static property | ||
*/ | ||
F.eventStream = function(obj, event) { | ||
F.eventStream = function(obj, eventName) { | ||
var stream = F.stream(), | ||
handler = function(obj) { | ||
stream.push(obj); | ||
}; | ||
attach, | ||
detach, | ||
attached = false, | ||
originalThen = stream.then; | ||
if (typeof obj.addEventListener === "function") { | ||
/* Normal DOM elements */ | ||
obj.addEventListener(event, handler); | ||
stream.done(function() { | ||
obj.removeEventListener(event, handler); | ||
}); | ||
attach = obj.addEventListener; | ||
detach = obj.removeEventListener; | ||
} else if (typeof obj.on === "function") { | ||
/* jQuery event handling */ | ||
obj.on(event, handler); | ||
attach = obj.on; | ||
detach = obj.off; | ||
} | ||
stream.done(function() { | ||
obj.off(event, handler); | ||
}); | ||
function handler(event) { | ||
stream.push(event); | ||
if (!stream._eventListeners.length) { | ||
disable(); | ||
} | ||
} | ||
function enable() { | ||
attached = true; | ||
attach.call(obj, eventName, handler); | ||
} | ||
function disable() { | ||
attached = false; | ||
detach.call(obj, eventName, handler); | ||
} | ||
stream.then = function() { | ||
if (!attached) { | ||
enable(); | ||
} | ||
originalThen.apply(stream, arguments); | ||
}; | ||
stream.done(function() { | ||
disable(); | ||
}); | ||
return stream; | ||
}; | ||
/* Alias for event stream */ | ||
@@ -155,2 +214,7 @@ F.estream = F.eventStream; | ||
Stream.prototype.remove = function(callback) { | ||
this._eventListeners.splice(this._eventListeners.indexOf(callback), 1); | ||
return this; | ||
}; | ||
Stream.prototype.done = function(callback) { | ||
@@ -171,8 +235,13 @@ this._doneListeners.push(callback); | ||
for (var i = 0; i < this._eventListeners.length; i++) { | ||
this._eventListeners[i](value); | ||
} | ||
this._notify(value); | ||
return this; | ||
}; | ||
Stream.prototype._notify = function(value) { | ||
this._eventListeners = this._eventListeners.filter(function(handler) { | ||
return handler(value) !== false; | ||
}); | ||
}; | ||
Stream.prototype.bindPush = function() { | ||
@@ -250,2 +319,6 @@ var us = this, | ||
Stream.prototype["@@iterator"] = function() { | ||
return new StreamIterator(this); | ||
}; | ||
/** | ||
@@ -266,3 +339,2 @@ * Pulls values from the root stream down the iterable chain. You can also | ||
} | ||
if (!pred) { | ||
@@ -275,24 +347,34 @@ pred = P.alwaysTrue; | ||
return new Promise(function(resolve) { | ||
var values = []; | ||
var values = []; | ||
/* Each time the stream pushes an event, we must pull the iterable chain | ||
so the value is passed through. */ | ||
stream.then(function() { | ||
/* Pull values from the iterable chain */ | ||
var value = us._iter.next(); | ||
/* Each time the stream pushes an event, we must pull the iterable chain | ||
so the value is passed through. */ | ||
stream.then(function() { | ||
/* Pull values from the iterable chain */ | ||
var value = us._iter.next(); | ||
if (!F.isEmptyValue(value)) { | ||
values.push(value.value); | ||
} | ||
if (!F.isEmptyValue(value)) { | ||
values.push(value.value); | ||
} | ||
if (!pred(value.value) || value.done) { | ||
stream.stop(); | ||
if (!pred(value.value) || value.done) { | ||
resolve(values); | ||
/* Signal to the stream that the event listener should be removed */ | ||
return false; | ||
} | ||
}); | ||
stream.done(function() { | ||
resolve(values); | ||
}); | ||
}) | ||
.then(function(values) { | ||
/* Remove this iterator from the list of listeners on the stream to | ||
prevent memory leaks and slowness */ | ||
if (typeof us._context.iterable._iter.stop === "function") { | ||
us._context.iterable._iter.stop(); | ||
} | ||
return values; | ||
}); | ||
stream.done(function() { | ||
resolve(values); | ||
}); | ||
}); | ||
}; | ||
})(this); |
@@ -33,4 +33,5 @@ /** | ||
} | ||
return this; | ||
}; | ||
root.Promise = Promise; | ||
})(this); |
{ | ||
"name": "f-js", | ||
"version": "0.5.0", | ||
"version": "0.5.1", | ||
"description": "Dead simple library for functional and reactive programming", | ||
@@ -5,0 +5,0 @@ "main": "dist/F.min.js", |
@@ -44,3 +44,3 @@ # F.js | ||
```bash | ||
curl "https://codeload.github.com/colin-dumitru/F.js/zip/0.4.11" > F.js.zip | ||
curl "https://codeload.github.com/colin-dumitru/F.js/zip/0.5.0" > F.js.zip | ||
unzip F.js.zip | ||
@@ -51,3 +51,3 @@ ``` | ||
```bash | ||
<script src="F.js-0.4.11/dist/F.min.js"></script> | ||
<script src="F.js-0.5.0/dist/F.min.js"></script> | ||
``` | ||
@@ -141,3 +141,3 @@ | ||
.feedStream(wordStream) | ||
.pullStream(keyStream); | ||
.pullStream(); | ||
@@ -152,4 +152,4 @@ F(wordStream) | ||
}) | ||
.then(imageStream.push.bind(imageStream))) | ||
.pullStream(wordStream); | ||
.then(imageStream.bindPush())) | ||
.pullStream(); | ||
@@ -163,5 +163,5 @@ F(imageStream) | ||
.foreach(display)) | ||
.pullStream(imageStream); | ||
.pullStream(); | ||
``` | ||
Got you interested? Visit our [wiki pages](https://github.com/colin-dumitru/F.js/wiki) for more examples and info. |
@@ -136,2 +136,45 @@ if (typeof require !== 'undefined') { | ||
return promise; | ||
}, | ||
"Pull stream with multiple chains": function() { | ||
var stream = F.stream(), | ||
values = [], | ||
doneCalled = 0, | ||
onDone = function() { | ||
doneCalled++; | ||
if (doneCalled == 3) { | ||
equals(values, [1, 2, 3, 10, 20, 30, 20, 40, 60]); | ||
equals(doneCalled, 3); | ||
} | ||
}; | ||
var promise = F(stream) | ||
.each(function(x) { | ||
values.push(x); | ||
}) | ||
.pullStream() | ||
.then(onDone); | ||
var promise = F(stream) | ||
.each(function(x) { | ||
values.push(x * 2); | ||
}) | ||
.pullStream() | ||
.then(onDone); | ||
var promise = F(stream) | ||
.each(function(x) { | ||
values.push(x * 3); | ||
}) | ||
.pullStream() | ||
.then(onDone); | ||
stream.push(1); | ||
stream.push(10); | ||
stream.push(20); | ||
stream.stop(); | ||
return promise; | ||
} | ||
@@ -313,2 +356,9 @@ }); | ||
equals(values, [22, 33]) | ||
}) | ||
.then(function() { | ||
/* The handler is disabled when pushing a new values without any listeners attached */ | ||
mock.trigger("keydown", { | ||
keycode: 33 | ||
}); | ||
assert(!mock.listeners["keydown"]); | ||
}); | ||
@@ -323,4 +373,2 @@ | ||
assert(!mock.listeners["keydown"]) | ||
return promise; | ||
@@ -327,0 +375,0 @@ } |
@@ -1084,4 +1084,3 @@ if (typeof require !== 'undefined') { | ||
"With stream": function() { | ||
var | ||
stream = F.stream(), | ||
var stream = F.stream(), | ||
@@ -1088,0 +1087,0 @@ result = F(stream) |
URL strings
Supply chain riskPackage contains fragments of external URLs or IP addresses, which the package may be accessing at runtime.
Found 1 instance in 1 package
URL strings
Supply chain riskPackage contains fragments of external URLs or IP addresses, which the package may be accessing at runtime.
Found 1 instance in 1 package
108793
4.56%3603
3.56%