Socket
Socket
Sign inDemoInstall

kefir

Package Overview
Dependencies
Maintainers
1
Versions
86
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

kefir - npm Package Compare versions

Comparing version 0.1.2 to 0.1.4

bower.json

508

dist/kefir.js

@@ -1,2 +0,2 @@

/*! kefir - 0.1.2
/*! kefir - 0.1.4
* https://github.com/pozadi/kefir

@@ -16,4 +16,2 @@ */

var Kefir = {};

@@ -26,2 +24,3 @@

// Utils

@@ -35,2 +34,6 @@

function toArray(arrayLike){
return Array.prototype.slice.call(arrayLike);
}
function createObj(proto) {

@@ -42,8 +45,16 @@ var F = function(){};

function extend(to, from) {
for (var prop in from) {
if(own(from, prop)) {
to[prop] = from[prop];
function extend() {
var objects = toArray(arguments);
if (objects.length === 1) {
return objects[0];
}
var result = objects.shift();
for (var i = 0; i < objects.length; i++) {
for (var prop in objects[i]) {
if(own(objects[i], prop)) {
result[prop] = objects[i][prop];
}
}
}
return result;
}

@@ -91,6 +102,14 @@

}
return Array.prototype.slice.call(args);
return toArray(args);
}
function assert(condition, message) {
if (!condition) {
throw new Error(message);
}
}
function isFn(fn) {
return typeof fn === "function";
}

@@ -101,2 +120,82 @@

// Callbacks
var Callbacks = Kefir.Callbacks = inherit(function Callbacks(){
this.__subscribers = null;
this.__contexts = null;
}, Object, {
add: function(fn, context){
if (this.__subscribers === null) {
this.__subscribers = [];
this.__contexts = [];
}
this.__subscribers.push(fn);
this.__contexts.push(context);
},
remove: function(fn, context){
if (this.isEmpty()) {return}
for (var i = 0; i < this.__subscribers.length; i++) {
if (this.__subscribers[i] === fn && this.__contexts[i] === context) {
this.__subscribers[i] = null;
this.__contexts[i] = null;
}
}
if (isAllDead(this.__subscribers)){
this.__subscribers = null;
this.__contexts = null;
}
},
isEmpty: function(){
return this.__subscribers === null;
},
hasOne: function(){
return !this.isEmpty() && this.__subscribers.length === 1;
},
send: function(x){
if (this.isEmpty()) {return}
for (var i = 0, l = this.__subscribers.length; i < l; i++) {
var callback = this.__subscribers[i];
var context = this.__contexts[i];
if (isFn(callback)) {
if(Kefir.NO_MORE === callback.call(context, x)) {
this.remove(callback, context);
}
}
}
}
});
// Helper mixins
var withHandlerMixin = {
__Constructor: function(source) {
this.__source = source;
source.onEnd(this.__sendEnd, this);
if (source instanceof Property && this instanceof Property && source.hasCached()) {
this.__handle(source.getCached());
}
},
__handle: function(x){
this._send(x);
},
__onFirstIn: function(){
this.__source.onChanges(this.__handle, this);
},
__onLastOut: function(){
this.__source.off(this.__handle, this);
},
__end: function(){
this.__source = null;
}
}
// Base Stream class

@@ -107,37 +206,34 @@

// __onFirstIn, __onLastOut can also be added to prototype of child classes
if (typeof onFirstIn === "function") {
if (isFn(onFirstIn)) {
this.__onFirstIn = onFirstIn;
}
if (typeof onFirstIn === "function") {
if (isFn(onLastOut)) {
this.__onLastOut = onLastOut;
}
this.__subscribers = [];
this.__contexts = [];
this.__endSubscribers = [];
this.__endContexts = [];
this.__subscribers = new Callbacks;
this.__endSubscribers = new Callbacks;
}, Object, {
_send: function(value) {
_send: function(x) {
if (!this.isEnded()) {
if (value === Kefir.END) {
if (x === Kefir.END) {
this.__end();
} else {
for (var i = 0; i < this.__subscribers.length; i++) {
var callback = this.__subscribers[i];
var context = this.__contexts[i];
if (typeof callback === "function") {
if(Kefir.NO_MORE === callback.call(context, value)) {
this.off(callback, context);
}
}
}
this.__deliver(x);
}
}
},
__deliver: function(x){
if (!this.__subscribers.isEmpty()) {
this.__subscribers.send(x);
if (this.__subscribers.isEmpty()) {
this.__onLastOut();
}
}
},
on: function(callback, context) {
if (!this.isEnded()) {
this.__subscribers.push(callback);
this.__contexts.push(context);
if (this.__subscribers.length === 1) {
this.__subscribers.add(callback, context);
if (this.__subscribers.hasOne()) {
this.__onFirstIn();

@@ -147,13 +243,9 @@ }

},
onChanges: function(callback, context){
this.on(callback, context);
},
off: function(callback, context) {
if (!this.isEnded()) {
for (var i = 0; i < this.__subscribers.length; i++) {
if (this.__subscribers[i] === callback && this.__contexts[i] === context) {
this.__subscribers[i] = null;
this.__contexts[i] = null;
}
}
if (isAllDead(this.__subscribers)) {
this.__subscribers = [];
this.__contexts = [];
this.__subscribers.remove(callback, context);
if (this.__subscribers.isEmpty()) {
this.__onLastOut();

@@ -165,6 +257,5 @@ }

if (this.isEnded()) {
callback();
callback.call(context);
} else {
this.__endSubscribers.push(callback);
this.__endContexts.push(context);
this.__endSubscribers.add(callback, context);
}

@@ -174,8 +265,3 @@ },

if (!this.isEnded()){
for (var i = 0; i < this.__endSubscribers.length; i++) {
if (this.__endSubscribers[i] === callback && this.__endContexts[i] === context) {
this.__endSubscribers[i] = null;
this.__endContexts[i] = null;
}
}
this.__endSubscribers.remove(callback, context);
}

@@ -187,3 +273,3 @@ },

hasSubscribers: function(){
return !this.isEnded() && this.__subscribers.length > 0;
return !this.isEnded() && !this.__subscribers.isEmpty();
},

@@ -198,2 +284,3 @@ __onFirstIn: noop,

this.__onLastOut();
this.__endSubscribers.send();
if (own(this, '__onFirstIn')) {

@@ -206,10 +293,3 @@ this.__onFirstIn = null;

this.__subscribers = null;
this.__contexts = null;
for (var i = 0; i < this.__endSubscribers.length; i++) {
if (typeof this.__endSubscribers[i] === "function") {
this.__endSubscribers[i].call(this.__endContexts[i]);
}
}
this.__endSubscribers = null;
this.__endContexts = null;
}

@@ -238,3 +318,3 @@ }

var OnceStream = Kefir.OnceStream = inherit(function OnceStream(value){
Kefir.OnceStream = inherit(function OnceStream(value){
Stream.call(this);

@@ -252,4 +332,4 @@ this.__value = value;

Kefir.once = function(value) {
return new OnceStream(value);
Kefir.once = function(x) {
return new Kefir.OnceStream(x);
}

@@ -262,6 +342,6 @@

var Property = Kefir.Property = inherit(function Property(onFirstIn, onLastOut, initialValue){
var Property = Kefir.Property = inherit(function Property(onFirstIn, onLastOut, initial){
Stream.call(this, onFirstIn, onLastOut);
this.__hasCached = (typeof initialValue !== "undefined");
this.__cached = initialValue;
this.__hasCached = (typeof initial !== "undefined");
this.__cached = initial;
}, Stream, {

@@ -278,8 +358,21 @@

},
_send: function(value) {
_send: function(x) {
if (!this.isEnded()){
this.__hasCached = true;
this.__cached = value;
this.__cached = x;
}
Stream.prototype._send.call(this, value);
Stream.prototype._send.call(this, x);
},
toProperty: function(initial){
assert(
typeof initial === "undefined",
"can't convert Property to Property with new initial value"
)
return this;
},
hasCached: function(){
return this.__hasCached;
},
getCached: function(){
return this.__cached;
}

@@ -289,27 +382,17 @@

var PropertyFromStream = Kefir.PropertyFromStream = inherit(function PropertyFromStream(sourceStream, initialValue){
Property.call(this, null, null, initialValue);
this.__sourceStream = sourceStream;
sourceStream.onEnd(this.__sendEnd, this);
}, Property, {
Kefir.PropertyFromStream = inherit(function PropertyFromStream(source, initial){
Property.call(this, null, null, initial);
this.__Constructor.call(this, source);
}, Property, extend({}, withHandlerMixin, {
__onFirstIn: function(){
this.__sourceStream.on(this._send, this);
},
__onLastOut: function(){
this.__sourceStream.off(this._send, this);
},
__end: function(){
Stream.prototype.__end.call(this);
this.__sourceStream = null;
Property.prototype.__end.call(this);
withHandlerMixin.__end.call(this);
}
})
}))
Kefir.toProperty = function(sourceStream, initialValue){
return new PropertyFromStream(sourceStream, initialValue);
Stream.prototype.toProperty = function(initial){
return new Kefir.PropertyFromStream(this, initial);
}
Stream.prototype.toProperty = function(initialValue){
return Kefir.toProperty(this, initialValue);
}

@@ -321,28 +404,16 @@

var PropertyChangesStream = Kefir.PropertyChangesStream = inherit(function PropertyChangesStream(property){
Kefir.ChangesStream = inherit(function ChangesStream(source){
Stream.call(this);
this.__sourceProperty = property;
var _this = this;
property.onEnd(function(){ _this._send(Kefir.END) })
}, Stream, {
this.__Constructor.call(this, source);
}, Stream, extend({}, withHandlerMixin, {
__onFirstIn: function(){
this.__sourceProperty.onChanges(this._send, this);
},
__onLastOut: function(){
this.__sourceProperty.off(this._send, this);
},
__end: function(){
Stream.prototype.__end.call(this);
this.__sourceProperty = null;
withHandlerMixin.__end.call(this);
}
})
}))
Kefir.changes = function(property){
return new PropertyChangesStream(property);
}
Property.prototype.changes = function() {
return Kefir.changes(this);
return new Kefir.ChangesStream(this);
};

@@ -356,22 +427,22 @@

var FromBinderStream = Kefir.FromBinderStream = inherit(function FromBinderStream(generator){
Kefir.FromBinderStream = inherit(function FromBinderStream(subscribe){
Stream.call(this);
this.__generator = generator;
var _this = this;
this.__deliver = function(x){ _this._send(x) }
this.__subscribe = subscribe;
}, Stream, {
__onFirstIn: function(){
this.__generatorUsubscriber = this.__generator(this.__deliver);
var _this = this;
this.__usubscriber = this.__subscribe(function(x){
_this._send(x);
});
},
__onLastOut: function(){
if (typeof this.__generatorUsubscriber === "function") {
this.__generatorUsubscriber();
if (isFn(this.__usubscriber)) {
this.__usubscriber();
}
this.__generatorUsubscriber = null;
this.__usubscriber = null;
},
__end: function(){
Stream.prototype.__end.call(this);
this.__generator = null;
this.__deliver = null;
this.__subscribe = null;
}

@@ -381,4 +452,4 @@

Kefir.fromBinder = function(generator){
return new FromBinderStream(generator);
Kefir.fromBinder = function(subscribe){
return new Kefir.FromBinderStream(subscribe);
}

@@ -395,3 +466,3 @@

var Bus = Kefir.Bus = inherit(function Bus(){
Kefir.Bus = inherit(function Bus(){
Stream.call(this);

@@ -441,5 +512,9 @@ this.__plugged = [];

Kefir.bus = function(){
return new Kefir.Bus;
}
// FromPoll

@@ -452,7 +527,7 @@

var _this = this;
this.__deliver = function(){ _this._send(sourceFn()) }
this.__send = function(){ _this._send(sourceFn()) }
}, Stream, {
__onFirstIn: function(){
this.__intervalId = setInterval(this.__deliver, this.__interval);
this.__intervalId = setInterval(this.__send, this.__interval);
},

@@ -467,3 +542,3 @@ __onLastOut: function(){

Stream.prototype.__end.call(this);
this.__deliver = null;
this.__send = null;
}

@@ -481,4 +556,4 @@

Kefir.interval = function(interval, value){
return new FromPollStream(interval, function(){ return value });
Kefir.interval = function(interval, x){
return new FromPollStream(interval, function(){ return x });
}

@@ -517,32 +592,38 @@

var MappedStream = Kefir.MappedStream = inherit(function MappedStream(sourceStream, mapFn){
Stream.call(this)
this.__sourceStream = sourceStream;
this.__mapFn = mapFn;
sourceStream.onEnd(this.__sendEnd, this);
}, Stream, {
__mapAndSend: function(x){
var mapMixin = extend({}, withHandlerMixin, {
__Constructor: function(source, mapFn){
if (source instanceof Property) {
Property.call(this);
} else {
Stream.call(this);
}
this.__mapFn = mapFn;
withHandlerMixin.__Constructor.call(this, source);
},
__handle: function(x){
this._send( this.__mapFn(x) );
},
__onFirstIn: function(){
this.__sourceStream.on(this.__mapAndSend, this);
},
__onLastOut: function(){
this.__sourceStream.off(this.__mapAndSend, this);
},
__end: function(){
Stream.prototype.__end.call(this);
this.__sourceStream = null;
withHandlerMixin.__end.call(this);
this.__mapFn = null;
}
});
Kefir.map = function(stream, mapFn) {
return new MappedStream(stream, mapFn);
}
Kefir.MappedStream = inherit(
function MappedStream(){this.__Constructor.apply(this, arguments)},
Stream, mapMixin
);
Kefir.MappedProperty = inherit(
function MappedProperty(){this.__Constructor.apply(this, arguments)},
Property, mapMixin
);
Stream.prototype.map = function(fn) {
return Kefir.map(this, fn);
if (this instanceof Property) {
return new Kefir.MappedProperty(this, fn);
} else {
return new Kefir.MappedStream(this, fn);
}
};

@@ -554,9 +635,8 @@

// Filter
var FilteredStream = Kefir.FilteredStream = inherit(function FilteredStream(sourceStream, filterFn){
MappedStream.call(this, sourceStream, filterFn);
}, MappedStream, {
__mapAndSend: function(x){
var filterMixin = extend({}, mapMixin, {
__handle: function(x){
if (this.__mapFn(x)) {

@@ -566,11 +646,20 @@ this._send(x);

}
});
Kefir.filter = function(stream, filterFn) {
return new FilteredStream(stream, filterFn);
}
Kefir.FilteredStream = inherit(
function FilteredStream(){this.__Constructor.apply(this, arguments)},
Stream, filterMixin
);
Kefir.FilteredProperty = inherit(
function FilteredProperty(){this.__Constructor.apply(this, arguments)},
Property, filterMixin
);
Stream.prototype.filter = function(fn) {
return Kefir.filter(this, fn);
if (this instanceof Property) {
return new Kefir.FilteredProperty(this, fn);
} else {
return new Kefir.FilteredStream(this, fn);
}
};

@@ -581,9 +670,7 @@

// TakeWhile
var TakeWhileStream = Kefir.TakeWhileStream = inherit(function TakeWhileStream(sourceStream, filterFn){
MappedStream.call(this, sourceStream, filterFn);
}, MappedStream, {
__mapAndSend: function(x){
var takeWhileMixin = extend({}, mapMixin, {
__handle: function(x){
if (this.__mapFn(x)) {

@@ -595,11 +682,20 @@ this._send(x);

}
});
Kefir.takeWhile = function(stream, filterFn) {
return new TakeWhileStream(stream, filterFn);
}
Kefir.TakeWhileStream = inherit(
function TakeWhileStream(){this.__Constructor.apply(this, arguments)},
Stream, takeWhileMixin
);
Kefir.TakeWhileProperty = inherit(
function TakeWhileProperty(){this.__Constructor.apply(this, arguments)},
Property, takeWhileMixin
);
Stream.prototype.takeWhile = function(fn) {
return Kefir.takeWhile(this, fn);
if (this instanceof Property) {
return new Kefir.TakeWhileProperty(this, fn);
} else {
return new Kefir.TakeWhileStream(this, fn);
}
};

@@ -612,10 +708,6 @@

Kefir.take = function(stream, n) {
return new TakeWhileStream(stream, function(){
Stream.prototype.take = function(n) {
return this.takeWhile(function(){
return n-- > 0;
});
}
Stream.prototype.take = function(n) {
return Kefir.take(this, n);
})
};

@@ -630,3 +722,3 @@

var FlatMappedStream = Kefir.FlatMappedStream = inherit(function FlatMappedStream(sourceStream, mapFn){
Kefir.FlatMappedStream = inherit(function FlatMappedStream(sourceStream, mapFn){
Stream.call(this)

@@ -677,8 +769,4 @@ this.__sourceStream = sourceStream;

Kefir.flatMap = function(stream, mapFn) {
return new FlatMappedStream(stream, mapFn);
}
Stream.prototype.flatMap = function(fn) {
return Kefir.flatMap(this, fn);
return new Kefir.FlatMappedStream(this, fn);
};

@@ -695,8 +783,8 @@

var MergedStream = Kefir.MergedStream = inherit(function MergedStream(){
Kefir.MergedStream = inherit(function MergedStream(){
Stream.call(this)
this.__sourceStreams = firstArrOrToArr(arguments);
for (var i = 0; i < this.__sourceStreams.length; i++) {
this.__sourceStreams[i].onEnd(
this.__unplugFor(this.__sourceStreams[i])
this.__sources = firstArrOrToArr(arguments);
for (var i = 0; i < this.__sources.length; i++) {
this.__sources[i].onEnd(
this.__unplugFor(this.__sources[i])
);

@@ -707,9 +795,9 @@ }

__onFirstIn: function(){
for (var i = 0; i < this.__sourceStreams.length; i++) {
this.__sourceStreams[i].on(this._send, this);
for (var i = 0; i < this.__sources.length; i++) {
this.__sources[i].on(this._send, this);
}
},
__onLastOut: function(){
for (var i = 0; i < this.__sourceStreams.length; i++) {
this.__sourceStreams[i].off(this._send, this);
for (var i = 0; i < this.__sources.length; i++) {
this.__sources[i].off(this._send, this);
}

@@ -719,4 +807,4 @@ },

stream.off(this._send, this);
removeFromArray(this.__sourceStreams, stream);
if (this.__sourceStreams.length === 0) {
removeFromArray(this.__sources, stream);
if (this.__sources.length === 0) {
this._send(Kefir.END);

@@ -731,3 +819,3 @@ }

Stream.prototype.__end.call(this);
this.__sourceStreams = null;
this.__sources = null;
}

@@ -738,3 +826,3 @@

Kefir.merge = function() {
return new MergedStream(firstArrOrToArr(arguments));
return new Kefir.MergedStream(firstArrOrToArr(arguments));
}

@@ -756,14 +844,14 @@

var CombinedStream = Kefir.CombinedStream = inherit(function CombinedStream(sourceStreams, mapFn){
Kefir.CombinedStream = inherit(function CombinedStream(sources, mapFn){
Stream.call(this)
this.__sourceStreams = sourceStreams;
this.__cachedValues = new Array(sourceStreams.length);
this.__hasCached = new Array(sourceStreams.length);
this.__receiveFns = new Array(sourceStreams.length);
this.__sources = sources;
this.__cachedValues = new Array(sources.length);
this.__hasCached = new Array(sources.length);
this.__receiveFns = new Array(sources.length);
this.__mapFn = mapFn;
for (var i = 0; i < this.__sourceStreams.length; i++) {
for (var i = 0; i < this.__sources.length; i++) {
this.__receiveFns[i] = this.__receiveFor(i);
this.__sourceStreams[i].onEnd( this.__unplugFor(i) );
this.__sources[i].onEnd( this.__unplugFor(i) );
}

@@ -774,5 +862,5 @@

__onFirstIn: function(){
for (var i = 0; i < this.__sourceStreams.length; i++) {
if (this.__sourceStreams[i]) {
this.__sourceStreams[i].on(this.__receiveFns[i]);
for (var i = 0; i < this.__sources.length; i++) {
if (this.__sources[i]) {
this.__sources[i].on(this.__receiveFns[i]);
}

@@ -782,5 +870,5 @@ }

__onLastOut: function(){
for (var i = 0; i < this.__sourceStreams.length; i++) {
if (this.__sourceStreams[i]) {
this.__sourceStreams[i].off(this.__receiveFns[i]);
for (var i = 0; i < this.__sources.length; i++) {
if (this.__sources[i]) {
this.__sources[i].off(this.__receiveFns[i]);
}

@@ -790,6 +878,6 @@ }

__unplug: function(i){
this.__sourceStreams[i].off(this.__receiveFns[i]);
this.__sourceStreams[i] = null
this.__sources[i].off(this.__receiveFns[i]);
this.__sources[i] = null
this.__receiveFns[i] = null
if (isAllDead(this.__sourceStreams)) {
if (isAllDead(this.__sources)) {
this._send(Kefir.END);

@@ -802,7 +890,7 @@ }

},
__receive: function(i, value) {
__receive: function(i, x) {
this.__hasCached[i] = true;
this.__cachedValues[i] = value;
this.__cachedValues[i] = x;
if (this.__allCached()) {
if (typeof this.__mapFn === "function") {
if (isFn(this.__mapFn)) {
this._send(this.__mapFn.apply(null, this.__cachedValues));

@@ -816,4 +904,4 @@ } else {

var _this = this;
return function(value){
_this.__receive(i, value);
return function(x){
_this.__receive(i, x);
}

@@ -831,3 +919,3 @@ },

Stream.prototype.__end.call(this);
this.__sourceStreams = null;
this.__sources = null;
this.__cachedValues = null;

@@ -841,8 +929,8 @@ this.__hasCached = null;

Kefir.combine = function(streams, mapFn) {
return new CombinedStream(streams, mapFn);
Kefir.combine = function(sources, mapFn) {
return new Kefir.CombinedStream(sources, mapFn);
}
Stream.prototype.combine = function(streams, mapFn) {
return Kefir.combine([this].concat(streams), mapFn);
Stream.prototype.combine = function(sources, mapFn) {
return Kefir.combine([this].concat(sources), mapFn);
}

@@ -849,0 +937,0 @@

@@ -1,4 +0,4 @@

/*! kefir - 0.1.2
/*! kefir - 0.1.4
* https://github.com/pozadi/kefir
*/
!function(a){"use strict";function b(){}function c(a,b){return Object.prototype.hasOwnProperty.call(a,b)}function d(a){var b=function(){};return b.prototype=a,new b}function e(a,b){for(var d in b)c(b,d)&&(a[d]=b[d])}function f(a,b,c){return a.prototype=d(b.prototype),a.prototype.constructor=a,c&&e(a.prototype,c),a}function g(a,b){for(var c=0;c<a.length;)a[c]===b?a.splice(c,1):c++}function h(a){for(var b=0;b<a.length;b++)if(a[b])return!1;return!0}function i(a){return"[object Array]"===Object.prototype.toString.call(a[0])?a[0]:Array.prototype.slice.call(a)}var j={};j.END=["<end>"],j.NO_MORE=["<no more>"];var k=j.Stream=f(function(a,b){"function"==typeof a&&(this.__onFirstIn=a),"function"==typeof a&&(this.__onLastOut=b),this.__subscribers=[],this.__contexts=[],this.__endSubscribers=[],this.__endContexts=[]},Object,{_send:function(a){if(!this.isEnded())if(a===j.END)this.__end();else for(var b=0;b<this.__subscribers.length;b++){var c=this.__subscribers[b],d=this.__contexts[b];"function"==typeof c&&j.NO_MORE===c.call(d,a)&&this.off(c,d)}},on:function(a,b){this.isEnded()||(this.__subscribers.push(a),this.__contexts.push(b),1===this.__subscribers.length&&this.__onFirstIn())},off:function(a,b){if(!this.isEnded()){for(var c=0;c<this.__subscribers.length;c++)this.__subscribers[c]===a&&this.__contexts[c]===b&&(this.__subscribers[c]=null,this.__contexts[c]=null);h(this.__subscribers)&&(this.__subscribers=[],this.__contexts=[],this.__onLastOut())}},onEnd:function(a,b){this.isEnded()?a():(this.__endSubscribers.push(a),this.__endContexts.push(b))},offEnd:function(a,b){if(!this.isEnded())for(var c=0;c<this.__endSubscribers.length;c++)this.__endSubscribers[c]===a&&this.__endContexts[c]===b&&(this.__endSubscribers[c]=null,this.__endContexts[c]=null)},isEnded:function(){return null===this.__subscribers},hasSubscribers:function(){return!this.isEnded()&&this.__subscribers.length>0},__onFirstIn:b,__onLastOut:b,__sendEnd:function(){this._send(j.END)},__end:function(){if(!this.isEnded()){this.__onLastOut(),c(this,"__onFirstIn")&&(this.__onFirstIn=null),c(this,"__onLastOut")&&(this.__onLastOut=null),this.__subscribers=null,this.__contexts=null;for(var a=0;a<this.__endSubscribers.length;a++)"function"==typeof this.__endSubscribers[a]&&this.__endSubscribers[a].call(this.__endContexts[a]);this.__endSubscribers=null,this.__endContexts=null}}}),l=d(k.prototype);l.__subscribers=null,j.never=function(){return l};var m=j.OnceStream=f(function(a){k.call(this),this.__value=a},k,{__onFirstIn:function(){this._send(this.__value),this.__value=null,this._send(j.END)}});j.once=function(a){return new m(a)};var n=j.Property=f(function(a,b,c){k.call(this,a,b),this.__hasCached="undefined"!=typeof c,this.__cached=c},k,{onChanges:function(a,b){k.prototype.on.call(this,a,b)},on:function(a,b){this.__hasCached&&a(this.__cached),this.onChanges(a,b)},_send:function(a){this.isEnded()||(this.__hasCached=!0,this.__cached=a),k.prototype._send.call(this,a)}}),o=j.PropertyFromStream=f(function(a,b){n.call(this,null,null,b),this.__sourceStream=a,a.onEnd(this.__sendEnd,this)},n,{__onFirstIn:function(){this.__sourceStream.on(this._send,this)},__onLastOut:function(){this.__sourceStream.off(this._send,this)},__end:function(){k.prototype.__end.call(this),this.__sourceStream=null}});j.toProperty=function(a,b){return new o(a,b)},k.prototype.toProperty=function(a){return j.toProperty(this,a)};var p=j.PropertyChangesStream=f(function(a){k.call(this),this.__sourceProperty=a;var b=this;a.onEnd(function(){b._send(j.END)})},k,{__onFirstIn:function(){this.__sourceProperty.onChanges(this._send,this)},__onLastOut:function(){this.__sourceProperty.off(this._send,this)},__end:function(){k.prototype.__end.call(this),this.__sourceProperty=null}});j.changes=function(a){return new p(a)},n.prototype.changes=function(){return j.changes(this)};var q=j.FromBinderStream=f(function(a){k.call(this),this.__generator=a;var b=this;this.__deliver=function(a){b._send(a)}},k,{__onFirstIn:function(){this.__generatorUsubscriber=this.__generator(this.__deliver)},__onLastOut:function(){"function"==typeof this.__generatorUsubscriber&&this.__generatorUsubscriber(),this.__generatorUsubscriber=null},__end:function(){k.prototype.__end.call(this),this.__generator=null,this.__deliver=null}});j.fromBinder=function(a){return new q(a)};var r=(j.Bus=f(function(){k.call(this),this.__plugged=[]},k,{push:function(a){this._send(a)},plug:function(a){if(!this.isEnded()){this.__plugged.push(a),this.hasSubscribers()&&a.on(this._send,this);var b=this;a.onEnd(function(){b.unplug(a)})}},unplug:function(a){this.isEnded()||(a.off(this._send,this),g(this.__plugged,a))},end:function(){this._send(j.END)},__onFirstIn:function(){for(var a=0;a<this.__plugged.length;a++)this.__plugged[a].on(this._send,this)},__onLastOut:function(){for(var a=0;a<this.__plugged.length;a++)this.__plugged[a].off(this._send,this)},__end:function(){k.prototype.__end.call(this),this.__plugged=null,this.push=b}}),j.FromPollStream=f(function(a,b){k.call(this),this.__interval=a,this.__intervalId=null;var c=this;this.__deliver=function(){c._send(b())}},k,{__onFirstIn:function(){this.__intervalId=setInterval(this.__deliver,this.__interval)},__onLastOut:function(){null!==this.__intervalId&&(clearInterval(this.__intervalId),this.__intervalId=null)},__end:function(){k.prototype.__end.call(this),this.__deliver=null}}));j.fromPoll=function(a,b){return new r(a,b)},j.interval=function(a,b){return new r(a,function(){return b})},j.sequentially=function(a,b){return b=b.slice(0),new r(a,function(){return 0===b.length?j.END:b.shift()})},j.repeatedly=function(a,b){var c=-1;return new r(a,function(){return b[++c%b.length]})};var s=j.MappedStream=f(function(a,b){k.call(this),this.__sourceStream=a,this.__mapFn=b,a.onEnd(this.__sendEnd,this)},k,{__mapAndSend:function(a){this._send(this.__mapFn(a))},__onFirstIn:function(){this.__sourceStream.on(this.__mapAndSend,this)},__onLastOut:function(){this.__sourceStream.off(this.__mapAndSend,this)},__end:function(){k.prototype.__end.call(this),this.__sourceStream=null,this.__mapFn=null}});j.map=function(a,b){return new s(a,b)},k.prototype.map=function(a){return j.map(this,a)};var t=j.FilteredStream=f(function(a,b){s.call(this,a,b)},s,{__mapAndSend:function(a){this.__mapFn(a)&&this._send(a)}});j.filter=function(a,b){return new t(a,b)},k.prototype.filter=function(a){return j.filter(this,a)};var u=j.TakeWhileStream=f(function(a,b){s.call(this,a,b)},s,{__mapAndSend:function(a){this._send(this.__mapFn(a)?a:j.END)}});j.takeWhile=function(a,b){return new u(a,b)},k.prototype.takeWhile=function(a){return j.takeWhile(this,a)},j.take=function(a,b){return new u(a,function(){return b-->0})},k.prototype.take=function(a){return j.take(this,a)};var v=j.FlatMappedStream=f(function(a,b){k.call(this),this.__sourceStream=a,this.__plugged=[],this.__mapFn=b,a.onEnd(this.__sendEnd,this)},k,{__plugResult:function(a){this.__plug(this.__mapFn(a))},__onFirstIn:function(){this.__sourceStream.on(this.__plugResult,this);for(var a=0;a<this.__plugged.length;a++)this.__plugged[a].on(this._send,this)},__onLastOut:function(){this.__sourceStream.off(this.__plugResult,this);for(var a=0;a<this.__plugged.length;a++)this.__plugged[a].off(this._send,this)},__plug:function(a){this.__plugged.push(a),this.hasSubscribers()&&a.on(this._send,this);var b=this;a.onEnd(function(){b.__unplug(a)})},__unplug:function(a){this.isEnded()||(a.off(this._send,this),g(this.__plugged,a))},__end:function(){k.prototype.__end.call(this),this.__sourceStream=null,this.__mapFn=null,this.__plugged=null}});j.flatMap=function(a,b){return new v(a,b)},k.prototype.flatMap=function(a){return j.flatMap(this,a)};var w=j.MergedStream=f(function(){k.call(this),this.__sourceStreams=i(arguments);for(var a=0;a<this.__sourceStreams.length;a++)this.__sourceStreams[a].onEnd(this.__unplugFor(this.__sourceStreams[a]))},k,{__onFirstIn:function(){for(var a=0;a<this.__sourceStreams.length;a++)this.__sourceStreams[a].on(this._send,this)},__onLastOut:function(){for(var a=0;a<this.__sourceStreams.length;a++)this.__sourceStreams[a].off(this._send,this)},__unplug:function(a){a.off(this._send,this),g(this.__sourceStreams,a),0===this.__sourceStreams.length&&this._send(j.END)},__unplugFor:function(a){var b=this;return function(){b.__unplug(a)}},__end:function(){k.prototype.__end.call(this),this.__sourceStreams=null}});j.merge=function(){return new w(i(arguments))},k.prototype.merge=function(){return j.merge([this].concat(i(arguments)))};var x=j.CombinedStream=f(function(a,b){k.call(this),this.__sourceStreams=a,this.__cachedValues=new Array(a.length),this.__hasCached=new Array(a.length),this.__receiveFns=new Array(a.length),this.__mapFn=b;for(var c=0;c<this.__sourceStreams.length;c++)this.__receiveFns[c]=this.__receiveFor(c),this.__sourceStreams[c].onEnd(this.__unplugFor(c))},k,{__onFirstIn:function(){for(var a=0;a<this.__sourceStreams.length;a++)this.__sourceStreams[a]&&this.__sourceStreams[a].on(this.__receiveFns[a])},__onLastOut:function(){for(var a=0;a<this.__sourceStreams.length;a++)this.__sourceStreams[a]&&this.__sourceStreams[a].off(this.__receiveFns[a])},__unplug:function(a){this.__sourceStreams[a].off(this.__receiveFns[a]),this.__sourceStreams[a]=null,this.__receiveFns[a]=null,h(this.__sourceStreams)&&this._send(j.END)},__unplugFor:function(a){var b=this;return function(){b.__unplug(a)}},__receive:function(a,b){this.__hasCached[a]=!0,this.__cachedValues[a]=b,this.__allCached()&&this._send("function"==typeof this.__mapFn?this.__mapFn.apply(null,this.__cachedValues):this.__cachedValues.slice(0))},__receiveFor:function(a){var b=this;return function(c){b.__receive(a,c)}},__allCached:function(){for(var a=0;a<this.__hasCached.length;a++)if(!this.__hasCached[a])return!1;return!0},__end:function(){k.prototype.__end.call(this),this.__sourceStreams=null,this.__cachedValues=null,this.__hasCached=null,this.__receiveFns=null,this.__mapFn=null}});j.combine=function(a,b){return new x(a,b)},k.prototype.combine=function(a,b){return j.combine([this].concat(a),b)},k.prototype.log=function(a){function b(b){a?console.log(a,b):console.log(b)}this.on(b),this.onEnd(function(){b(j.END)})},"function"==typeof define&&define.amd?(define([],function(){return j}),a.Kefir=j):"object"==typeof module&&"object"==typeof exports?(module.exports=j,j.Kefir=j):a.Kefir=j}(this);
!function(a){"use strict";function b(){}function c(a,b){return Object.prototype.hasOwnProperty.call(a,b)}function d(a){return Array.prototype.slice.call(a)}function e(a){var b=function(){};return b.prototype=a,new b}function f(){var a=d(arguments);if(1===a.length)return a[0];for(var b=a.shift(),e=0;e<a.length;e++)for(var f in a[e])c(a[e],f)&&(b[f]=a[e][f]);return b}function g(a,b,c){return a.prototype=e(b.prototype),a.prototype.constructor=a,c&&f(a.prototype,c),a}function h(a,b){for(var c=0;c<a.length;)a[c]===b?a.splice(c,1):c++}function i(a){for(var b=0;b<a.length;b++)if(a[b])return!1;return!0}function j(a){return"[object Array]"===Object.prototype.toString.call(a[0])?a[0]:d(a)}function k(a,b){if(!a)throw new Error(b)}function l(a){return"function"==typeof a}var m={};m.END=["<end>"],m.NO_MORE=["<no more>"];var n=m.Callbacks=g(function(){this.__subscribers=null,this.__contexts=null},Object,{add:function(a,b){null===this.__subscribers&&(this.__subscribers=[],this.__contexts=[]),this.__subscribers.push(a),this.__contexts.push(b)},remove:function(a,b){if(!this.isEmpty()){for(var c=0;c<this.__subscribers.length;c++)this.__subscribers[c]===a&&this.__contexts[c]===b&&(this.__subscribers[c]=null,this.__contexts[c]=null);i(this.__subscribers)&&(this.__subscribers=null,this.__contexts=null)}},isEmpty:function(){return null===this.__subscribers},hasOne:function(){return!this.isEmpty()&&1===this.__subscribers.length},send:function(a){if(!this.isEmpty())for(var b=0,c=this.__subscribers.length;c>b;b++){var d=this.__subscribers[b],e=this.__contexts[b];l(d)&&m.NO_MORE===d.call(e,a)&&this.remove(d,e)}}}),o={__Constructor:function(a){this.__source=a,a.onEnd(this.__sendEnd,this),a instanceof r&&this instanceof r&&a.hasCached()&&this.__handle(a.getCached())},__handle:function(a){this._send(a)},__onFirstIn:function(){this.__source.onChanges(this.__handle,this)},__onLastOut:function(){this.__source.off(this.__handle,this)},__end:function(){this.__source=null}},p=m.Stream=g(function(a,b){l(a)&&(this.__onFirstIn=a),l(b)&&(this.__onLastOut=b),this.__subscribers=new n,this.__endSubscribers=new n},Object,{_send:function(a){this.isEnded()||(a===m.END?this.__end():this.__deliver(a))},__deliver:function(a){this.__subscribers.isEmpty()||(this.__subscribers.send(a),this.__subscribers.isEmpty()&&this.__onLastOut())},on:function(a,b){this.isEnded()||(this.__subscribers.add(a,b),this.__subscribers.hasOne()&&this.__onFirstIn())},onChanges:function(a,b){this.on(a,b)},off:function(a,b){this.isEnded()||(this.__subscribers.remove(a,b),this.__subscribers.isEmpty()&&this.__onLastOut())},onEnd:function(a,b){this.isEnded()?a.call(b):this.__endSubscribers.add(a,b)},offEnd:function(a,b){this.isEnded()||this.__endSubscribers.remove(a,b)},isEnded:function(){return null===this.__subscribers},hasSubscribers:function(){return!this.isEnded()&&!this.__subscribers.isEmpty()},__onFirstIn:b,__onLastOut:b,__sendEnd:function(){this._send(m.END)},__end:function(){this.isEnded()||(this.__onLastOut(),this.__endSubscribers.send(),c(this,"__onFirstIn")&&(this.__onFirstIn=null),c(this,"__onLastOut")&&(this.__onLastOut=null),this.__subscribers=null,this.__endSubscribers=null)}}),q=e(p.prototype);q.__subscribers=null,m.never=function(){return q},m.OnceStream=g(function(a){p.call(this),this.__value=a},p,{__onFirstIn:function(){this._send(this.__value),this.__value=null,this._send(m.END)}}),m.once=function(a){return new m.OnceStream(a)};var r=m.Property=g(function(a,b,c){p.call(this,a,b),this.__hasCached="undefined"!=typeof c,this.__cached=c},p,{onChanges:function(a,b){p.prototype.on.call(this,a,b)},on:function(a,b){this.__hasCached&&a(this.__cached),this.onChanges(a,b)},_send:function(a){this.isEnded()||(this.__hasCached=!0,this.__cached=a),p.prototype._send.call(this,a)},toProperty:function(a){return k("undefined"==typeof a,"can't convert Property to Property with new initial value"),this},hasCached:function(){return this.__hasCached},getCached:function(){return this.__cached}});m.PropertyFromStream=g(function(a,b){r.call(this,null,null,b),this.__Constructor.call(this,a)},r,f({},o,{__end:function(){r.prototype.__end.call(this),o.__end.call(this)}})),p.prototype.toProperty=function(a){return new m.PropertyFromStream(this,a)},m.ChangesStream=g(function(a){p.call(this),this.__Constructor.call(this,a)},p,f({},o,{__end:function(){p.prototype.__end.call(this),o.__end.call(this)}})),r.prototype.changes=function(){return new m.ChangesStream(this)},m.FromBinderStream=g(function(a){p.call(this),this.__subscribe=a},p,{__onFirstIn:function(){var a=this;this.__usubscriber=this.__subscribe(function(b){a._send(b)})},__onLastOut:function(){l(this.__usubscriber)&&this.__usubscriber(),this.__usubscriber=null},__end:function(){p.prototype.__end.call(this),this.__subscribe=null}}),m.fromBinder=function(a){return new m.FromBinderStream(a)},m.Bus=g(function(){p.call(this),this.__plugged=[]},p,{push:function(a){this._send(a)},plug:function(a){if(!this.isEnded()){this.__plugged.push(a),this.hasSubscribers()&&a.on(this._send,this);var b=this;a.onEnd(function(){b.unplug(a)})}},unplug:function(a){this.isEnded()||(a.off(this._send,this),h(this.__plugged,a))},end:function(){this._send(m.END)},__onFirstIn:function(){for(var a=0;a<this.__plugged.length;a++)this.__plugged[a].on(this._send,this)},__onLastOut:function(){for(var a=0;a<this.__plugged.length;a++)this.__plugged[a].off(this._send,this)},__end:function(){p.prototype.__end.call(this),this.__plugged=null,this.push=b}}),m.bus=function(){return new m.Bus};var s=m.FromPollStream=g(function(a,b){p.call(this),this.__interval=a,this.__intervalId=null;var c=this;this.__send=function(){c._send(b())}},p,{__onFirstIn:function(){this.__intervalId=setInterval(this.__send,this.__interval)},__onLastOut:function(){null!==this.__intervalId&&(clearInterval(this.__intervalId),this.__intervalId=null)},__end:function(){p.prototype.__end.call(this),this.__send=null}});m.fromPoll=function(a,b){return new s(a,b)},m.interval=function(a,b){return new s(a,function(){return b})},m.sequentially=function(a,b){return b=b.slice(0),new s(a,function(){return 0===b.length?m.END:b.shift()})},m.repeatedly=function(a,b){var c=-1;return new s(a,function(){return b[++c%b.length]})};var t=f({},o,{__Constructor:function(a,b){a instanceof r?r.call(this):p.call(this),this.__mapFn=b,o.__Constructor.call(this,a)},__handle:function(a){this._send(this.__mapFn(a))},__end:function(){p.prototype.__end.call(this),o.__end.call(this),this.__mapFn=null}});m.MappedStream=g(function(){this.__Constructor.apply(this,arguments)},p,t),m.MappedProperty=g(function(){this.__Constructor.apply(this,arguments)},r,t),p.prototype.map=function(a){return this instanceof r?new m.MappedProperty(this,a):new m.MappedStream(this,a)};var u=f({},t,{__handle:function(a){this.__mapFn(a)&&this._send(a)}});m.FilteredStream=g(function(){this.__Constructor.apply(this,arguments)},p,u),m.FilteredProperty=g(function(){this.__Constructor.apply(this,arguments)},r,u),p.prototype.filter=function(a){return this instanceof r?new m.FilteredProperty(this,a):new m.FilteredStream(this,a)};var v=f({},t,{__handle:function(a){this._send(this.__mapFn(a)?a:m.END)}});m.TakeWhileStream=g(function(){this.__Constructor.apply(this,arguments)},p,v),m.TakeWhileProperty=g(function(){this.__Constructor.apply(this,arguments)},r,v),p.prototype.takeWhile=function(a){return this instanceof r?new m.TakeWhileProperty(this,a):new m.TakeWhileStream(this,a)},p.prototype.take=function(a){return this.takeWhile(function(){return a-->0})},m.FlatMappedStream=g(function(a,b){p.call(this),this.__sourceStream=a,this.__plugged=[],this.__mapFn=b,a.onEnd(this.__sendEnd,this)},p,{__plugResult:function(a){this.__plug(this.__mapFn(a))},__onFirstIn:function(){this.__sourceStream.on(this.__plugResult,this);for(var a=0;a<this.__plugged.length;a++)this.__plugged[a].on(this._send,this)},__onLastOut:function(){this.__sourceStream.off(this.__plugResult,this);for(var a=0;a<this.__plugged.length;a++)this.__plugged[a].off(this._send,this)},__plug:function(a){this.__plugged.push(a),this.hasSubscribers()&&a.on(this._send,this);var b=this;a.onEnd(function(){b.__unplug(a)})},__unplug:function(a){this.isEnded()||(a.off(this._send,this),h(this.__plugged,a))},__end:function(){p.prototype.__end.call(this),this.__sourceStream=null,this.__mapFn=null,this.__plugged=null}}),p.prototype.flatMap=function(a){return new m.FlatMappedStream(this,a)},m.MergedStream=g(function(){p.call(this),this.__sources=j(arguments);for(var a=0;a<this.__sources.length;a++)this.__sources[a].onEnd(this.__unplugFor(this.__sources[a]))},p,{__onFirstIn:function(){for(var a=0;a<this.__sources.length;a++)this.__sources[a].on(this._send,this)},__onLastOut:function(){for(var a=0;a<this.__sources.length;a++)this.__sources[a].off(this._send,this)},__unplug:function(a){a.off(this._send,this),h(this.__sources,a),0===this.__sources.length&&this._send(m.END)},__unplugFor:function(a){var b=this;return function(){b.__unplug(a)}},__end:function(){p.prototype.__end.call(this),this.__sources=null}}),m.merge=function(){return new m.MergedStream(j(arguments))},p.prototype.merge=function(){return m.merge([this].concat(j(arguments)))},m.CombinedStream=g(function(a,b){p.call(this),this.__sources=a,this.__cachedValues=new Array(a.length),this.__hasCached=new Array(a.length),this.__receiveFns=new Array(a.length),this.__mapFn=b;for(var c=0;c<this.__sources.length;c++)this.__receiveFns[c]=this.__receiveFor(c),this.__sources[c].onEnd(this.__unplugFor(c))},p,{__onFirstIn:function(){for(var a=0;a<this.__sources.length;a++)this.__sources[a]&&this.__sources[a].on(this.__receiveFns[a])},__onLastOut:function(){for(var a=0;a<this.__sources.length;a++)this.__sources[a]&&this.__sources[a].off(this.__receiveFns[a])},__unplug:function(a){this.__sources[a].off(this.__receiveFns[a]),this.__sources[a]=null,this.__receiveFns[a]=null,i(this.__sources)&&this._send(m.END)},__unplugFor:function(a){var b=this;return function(){b.__unplug(a)}},__receive:function(a,b){this.__hasCached[a]=!0,this.__cachedValues[a]=b,this.__allCached()&&this._send(l(this.__mapFn)?this.__mapFn.apply(null,this.__cachedValues):this.__cachedValues.slice(0))},__receiveFor:function(a){var b=this;return function(c){b.__receive(a,c)}},__allCached:function(){for(var a=0;a<this.__hasCached.length;a++)if(!this.__hasCached[a])return!1;return!0},__end:function(){p.prototype.__end.call(this),this.__sources=null,this.__cachedValues=null,this.__hasCached=null,this.__receiveFns=null,this.__mapFn=null}}),m.combine=function(a,b){return new m.CombinedStream(a,b)},p.prototype.combine=function(a,b){return m.combine([this].concat(a),b)},p.prototype.log=function(a){function b(b){a?console.log(a,b):console.log(b)}this.on(b),this.onEnd(function(){b(m.END)})},"function"==typeof define&&define.amd?(define([],function(){return m}),a.Kefir=m):"object"==typeof module&&"object"==typeof exports?(module.exports=m,m.Kefir=m):a.Kefir=m}(this);

@@ -14,4 +14,2 @@ (function e(t,n,r){function s(o,u){if(!n[o]){if(!t[o]){var a=typeof require=="function"&&require;if(!u&&a)return a(o,!0);if(i)return i(o,!0);throw new Error("Cannot find module '"+o+"'")}var f=n[o]={exports:{}};t[o][0].call(f.exports,function(e){var n=t[o][1][e];return s(n?n:e)},f,f.exports,e,t,n,r)}return n[o].exports}var i=typeof require=="function"&&require;for(var o=0;o<r.length;o++)s(r[o]);return s})({1:[function(require,module,exports){

var Kefir = {};

@@ -24,2 +22,3 @@

// Utils

@@ -33,2 +32,6 @@

function toArray(arrayLike){
return Array.prototype.slice.call(arrayLike);
}
function createObj(proto) {

@@ -40,8 +43,16 @@ var F = function(){};

function extend(to, from) {
for (var prop in from) {
if(own(from, prop)) {
to[prop] = from[prop];
function extend() {
var objects = toArray(arguments);
if (objects.length === 1) {
return objects[0];
}
var result = objects.shift();
for (var i = 0; i < objects.length; i++) {
for (var prop in objects[i]) {
if(own(objects[i], prop)) {
result[prop] = objects[i][prop];
}
}
}
return result;
}

@@ -89,6 +100,14 @@

}
return Array.prototype.slice.call(args);
return toArray(args);
}
function assert(condition, message) {
if (!condition) {
throw new Error(message);
}
}
function isFn(fn) {
return typeof fn === "function";
}

@@ -99,2 +118,82 @@

// Callbacks
var Callbacks = Kefir.Callbacks = inherit(function Callbacks(){
this.__subscribers = null;
this.__contexts = null;
}, Object, {
add: function(fn, context){
if (this.__subscribers === null) {
this.__subscribers = [];
this.__contexts = [];
}
this.__subscribers.push(fn);
this.__contexts.push(context);
},
remove: function(fn, context){
if (this.isEmpty()) {return}
for (var i = 0; i < this.__subscribers.length; i++) {
if (this.__subscribers[i] === fn && this.__contexts[i] === context) {
this.__subscribers[i] = null;
this.__contexts[i] = null;
}
}
if (isAllDead(this.__subscribers)){
this.__subscribers = null;
this.__contexts = null;
}
},
isEmpty: function(){
return this.__subscribers === null;
},
hasOne: function(){
return !this.isEmpty() && this.__subscribers.length === 1;
},
send: function(x){
if (this.isEmpty()) {return}
for (var i = 0, l = this.__subscribers.length; i < l; i++) {
var callback = this.__subscribers[i];
var context = this.__contexts[i];
if (isFn(callback)) {
if(Kefir.NO_MORE === callback.call(context, x)) {
this.remove(callback, context);
}
}
}
}
});
// Helper mixins
var withHandlerMixin = {
__Constructor: function(source) {
this.__source = source;
source.onEnd(this.__sendEnd, this);
if (source instanceof Property && this instanceof Property && source.hasCached()) {
this.__handle(source.getCached());
}
},
__handle: function(x){
this._send(x);
},
__onFirstIn: function(){
this.__source.onChanges(this.__handle, this);
},
__onLastOut: function(){
this.__source.off(this.__handle, this);
},
__end: function(){
this.__source = null;
}
}
// Base Stream class

@@ -105,37 +204,34 @@

// __onFirstIn, __onLastOut can also be added to prototype of child classes
if (typeof onFirstIn === "function") {
if (isFn(onFirstIn)) {
this.__onFirstIn = onFirstIn;
}
if (typeof onFirstIn === "function") {
if (isFn(onLastOut)) {
this.__onLastOut = onLastOut;
}
this.__subscribers = [];
this.__contexts = [];
this.__endSubscribers = [];
this.__endContexts = [];
this.__subscribers = new Callbacks;
this.__endSubscribers = new Callbacks;
}, Object, {
_send: function(value) {
_send: function(x) {
if (!this.isEnded()) {
if (value === Kefir.END) {
if (x === Kefir.END) {
this.__end();
} else {
for (var i = 0; i < this.__subscribers.length; i++) {
var callback = this.__subscribers[i];
var context = this.__contexts[i];
if (typeof callback === "function") {
if(Kefir.NO_MORE === callback.call(context, value)) {
this.off(callback, context);
}
}
}
this.__deliver(x);
}
}
},
__deliver: function(x){
if (!this.__subscribers.isEmpty()) {
this.__subscribers.send(x);
if (this.__subscribers.isEmpty()) {
this.__onLastOut();
}
}
},
on: function(callback, context) {
if (!this.isEnded()) {
this.__subscribers.push(callback);
this.__contexts.push(context);
if (this.__subscribers.length === 1) {
this.__subscribers.add(callback, context);
if (this.__subscribers.hasOne()) {
this.__onFirstIn();

@@ -145,13 +241,9 @@ }

},
onChanges: function(callback, context){
this.on(callback, context);
},
off: function(callback, context) {
if (!this.isEnded()) {
for (var i = 0; i < this.__subscribers.length; i++) {
if (this.__subscribers[i] === callback && this.__contexts[i] === context) {
this.__subscribers[i] = null;
this.__contexts[i] = null;
}
}
if (isAllDead(this.__subscribers)) {
this.__subscribers = [];
this.__contexts = [];
this.__subscribers.remove(callback, context);
if (this.__subscribers.isEmpty()) {
this.__onLastOut();

@@ -163,6 +255,5 @@ }

if (this.isEnded()) {
callback();
callback.call(context);
} else {
this.__endSubscribers.push(callback);
this.__endContexts.push(context);
this.__endSubscribers.add(callback, context);
}

@@ -172,8 +263,3 @@ },

if (!this.isEnded()){
for (var i = 0; i < this.__endSubscribers.length; i++) {
if (this.__endSubscribers[i] === callback && this.__endContexts[i] === context) {
this.__endSubscribers[i] = null;
this.__endContexts[i] = null;
}
}
this.__endSubscribers.remove(callback, context);
}

@@ -185,3 +271,3 @@ },

hasSubscribers: function(){
return !this.isEnded() && this.__subscribers.length > 0;
return !this.isEnded() && !this.__subscribers.isEmpty();
},

@@ -196,2 +282,3 @@ __onFirstIn: noop,

this.__onLastOut();
this.__endSubscribers.send();
if (own(this, '__onFirstIn')) {

@@ -204,10 +291,3 @@ this.__onFirstIn = null;

this.__subscribers = null;
this.__contexts = null;
for (var i = 0; i < this.__endSubscribers.length; i++) {
if (typeof this.__endSubscribers[i] === "function") {
this.__endSubscribers[i].call(this.__endContexts[i]);
}
}
this.__endSubscribers = null;
this.__endContexts = null;
}

@@ -236,3 +316,3 @@ }

var OnceStream = Kefir.OnceStream = inherit(function OnceStream(value){
Kefir.OnceStream = inherit(function OnceStream(value){
Stream.call(this);

@@ -250,4 +330,4 @@ this.__value = value;

Kefir.once = function(value) {
return new OnceStream(value);
Kefir.once = function(x) {
return new Kefir.OnceStream(x);
}

@@ -260,6 +340,6 @@

var Property = Kefir.Property = inherit(function Property(onFirstIn, onLastOut, initialValue){
var Property = Kefir.Property = inherit(function Property(onFirstIn, onLastOut, initial){
Stream.call(this, onFirstIn, onLastOut);
this.__hasCached = (typeof initialValue !== "undefined");
this.__cached = initialValue;
this.__hasCached = (typeof initial !== "undefined");
this.__cached = initial;
}, Stream, {

@@ -276,8 +356,21 @@

},
_send: function(value) {
_send: function(x) {
if (!this.isEnded()){
this.__hasCached = true;
this.__cached = value;
this.__cached = x;
}
Stream.prototype._send.call(this, value);
Stream.prototype._send.call(this, x);
},
toProperty: function(initial){
assert(
typeof initial === "undefined",
"can't convert Property to Property with new initial value"
)
return this;
},
hasCached: function(){
return this.__hasCached;
},
getCached: function(){
return this.__cached;
}

@@ -287,27 +380,17 @@

var PropertyFromStream = Kefir.PropertyFromStream = inherit(function PropertyFromStream(sourceStream, initialValue){
Property.call(this, null, null, initialValue);
this.__sourceStream = sourceStream;
sourceStream.onEnd(this.__sendEnd, this);
}, Property, {
Kefir.PropertyFromStream = inherit(function PropertyFromStream(source, initial){
Property.call(this, null, null, initial);
this.__Constructor.call(this, source);
}, Property, extend({}, withHandlerMixin, {
__onFirstIn: function(){
this.__sourceStream.on(this._send, this);
},
__onLastOut: function(){
this.__sourceStream.off(this._send, this);
},
__end: function(){
Stream.prototype.__end.call(this);
this.__sourceStream = null;
Property.prototype.__end.call(this);
withHandlerMixin.__end.call(this);
}
})
}))
Kefir.toProperty = function(sourceStream, initialValue){
return new PropertyFromStream(sourceStream, initialValue);
Stream.prototype.toProperty = function(initial){
return new Kefir.PropertyFromStream(this, initial);
}
Stream.prototype.toProperty = function(initialValue){
return Kefir.toProperty(this, initialValue);
}

@@ -319,28 +402,16 @@

var PropertyChangesStream = Kefir.PropertyChangesStream = inherit(function PropertyChangesStream(property){
Kefir.ChangesStream = inherit(function ChangesStream(source){
Stream.call(this);
this.__sourceProperty = property;
var _this = this;
property.onEnd(function(){ _this._send(Kefir.END) })
}, Stream, {
this.__Constructor.call(this, source);
}, Stream, extend({}, withHandlerMixin, {
__onFirstIn: function(){
this.__sourceProperty.onChanges(this._send, this);
},
__onLastOut: function(){
this.__sourceProperty.off(this._send, this);
},
__end: function(){
Stream.prototype.__end.call(this);
this.__sourceProperty = null;
withHandlerMixin.__end.call(this);
}
})
}))
Kefir.changes = function(property){
return new PropertyChangesStream(property);
}
Property.prototype.changes = function() {
return Kefir.changes(this);
return new Kefir.ChangesStream(this);
};

@@ -354,22 +425,22 @@

var FromBinderStream = Kefir.FromBinderStream = inherit(function FromBinderStream(generator){
Kefir.FromBinderStream = inherit(function FromBinderStream(subscribe){
Stream.call(this);
this.__generator = generator;
var _this = this;
this.__deliver = function(x){ _this._send(x) }
this.__subscribe = subscribe;
}, Stream, {
__onFirstIn: function(){
this.__generatorUsubscriber = this.__generator(this.__deliver);
var _this = this;
this.__usubscriber = this.__subscribe(function(x){
_this._send(x);
});
},
__onLastOut: function(){
if (typeof this.__generatorUsubscriber === "function") {
this.__generatorUsubscriber();
if (isFn(this.__usubscriber)) {
this.__usubscriber();
}
this.__generatorUsubscriber = null;
this.__usubscriber = null;
},
__end: function(){
Stream.prototype.__end.call(this);
this.__generator = null;
this.__deliver = null;
this.__subscribe = null;
}

@@ -379,4 +450,4 @@

Kefir.fromBinder = function(generator){
return new FromBinderStream(generator);
Kefir.fromBinder = function(subscribe){
return new Kefir.FromBinderStream(subscribe);
}

@@ -393,3 +464,3 @@

var Bus = Kefir.Bus = inherit(function Bus(){
Kefir.Bus = inherit(function Bus(){
Stream.call(this);

@@ -439,5 +510,9 @@ this.__plugged = [];

Kefir.bus = function(){
return new Kefir.Bus;
}
// FromPoll

@@ -450,7 +525,7 @@

var _this = this;
this.__deliver = function(){ _this._send(sourceFn()) }
this.__send = function(){ _this._send(sourceFn()) }
}, Stream, {
__onFirstIn: function(){
this.__intervalId = setInterval(this.__deliver, this.__interval);
this.__intervalId = setInterval(this.__send, this.__interval);
},

@@ -465,3 +540,3 @@ __onLastOut: function(){

Stream.prototype.__end.call(this);
this.__deliver = null;
this.__send = null;
}

@@ -479,4 +554,4 @@

Kefir.interval = function(interval, value){
return new FromPollStream(interval, function(){ return value });
Kefir.interval = function(interval, x){
return new FromPollStream(interval, function(){ return x });
}

@@ -515,32 +590,38 @@

var MappedStream = Kefir.MappedStream = inherit(function MappedStream(sourceStream, mapFn){
Stream.call(this)
this.__sourceStream = sourceStream;
this.__mapFn = mapFn;
sourceStream.onEnd(this.__sendEnd, this);
}, Stream, {
__mapAndSend: function(x){
var mapMixin = extend({}, withHandlerMixin, {
__Constructor: function(source, mapFn){
if (source instanceof Property) {
Property.call(this);
} else {
Stream.call(this);
}
this.__mapFn = mapFn;
withHandlerMixin.__Constructor.call(this, source);
},
__handle: function(x){
this._send( this.__mapFn(x) );
},
__onFirstIn: function(){
this.__sourceStream.on(this.__mapAndSend, this);
},
__onLastOut: function(){
this.__sourceStream.off(this.__mapAndSend, this);
},
__end: function(){
Stream.prototype.__end.call(this);
this.__sourceStream = null;
withHandlerMixin.__end.call(this);
this.__mapFn = null;
}
});
Kefir.map = function(stream, mapFn) {
return new MappedStream(stream, mapFn);
}
Kefir.MappedStream = inherit(
function MappedStream(){this.__Constructor.apply(this, arguments)},
Stream, mapMixin
);
Kefir.MappedProperty = inherit(
function MappedProperty(){this.__Constructor.apply(this, arguments)},
Property, mapMixin
);
Stream.prototype.map = function(fn) {
return Kefir.map(this, fn);
if (this instanceof Property) {
return new Kefir.MappedProperty(this, fn);
} else {
return new Kefir.MappedStream(this, fn);
}
};

@@ -552,9 +633,8 @@

// Filter
var FilteredStream = Kefir.FilteredStream = inherit(function FilteredStream(sourceStream, filterFn){
MappedStream.call(this, sourceStream, filterFn);
}, MappedStream, {
__mapAndSend: function(x){
var filterMixin = extend({}, mapMixin, {
__handle: function(x){
if (this.__mapFn(x)) {

@@ -564,11 +644,20 @@ this._send(x);

}
});
Kefir.filter = function(stream, filterFn) {
return new FilteredStream(stream, filterFn);
}
Kefir.FilteredStream = inherit(
function FilteredStream(){this.__Constructor.apply(this, arguments)},
Stream, filterMixin
);
Kefir.FilteredProperty = inherit(
function FilteredProperty(){this.__Constructor.apply(this, arguments)},
Property, filterMixin
);
Stream.prototype.filter = function(fn) {
return Kefir.filter(this, fn);
if (this instanceof Property) {
return new Kefir.FilteredProperty(this, fn);
} else {
return new Kefir.FilteredStream(this, fn);
}
};

@@ -579,9 +668,7 @@

// TakeWhile
var TakeWhileStream = Kefir.TakeWhileStream = inherit(function TakeWhileStream(sourceStream, filterFn){
MappedStream.call(this, sourceStream, filterFn);
}, MappedStream, {
__mapAndSend: function(x){
var takeWhileMixin = extend({}, mapMixin, {
__handle: function(x){
if (this.__mapFn(x)) {

@@ -593,11 +680,20 @@ this._send(x);

}
});
Kefir.takeWhile = function(stream, filterFn) {
return new TakeWhileStream(stream, filterFn);
}
Kefir.TakeWhileStream = inherit(
function TakeWhileStream(){this.__Constructor.apply(this, arguments)},
Stream, takeWhileMixin
);
Kefir.TakeWhileProperty = inherit(
function TakeWhileProperty(){this.__Constructor.apply(this, arguments)},
Property, takeWhileMixin
);
Stream.prototype.takeWhile = function(fn) {
return Kefir.takeWhile(this, fn);
if (this instanceof Property) {
return new Kefir.TakeWhileProperty(this, fn);
} else {
return new Kefir.TakeWhileStream(this, fn);
}
};

@@ -610,10 +706,6 @@

Kefir.take = function(stream, n) {
return new TakeWhileStream(stream, function(){
Stream.prototype.take = function(n) {
return this.takeWhile(function(){
return n-- > 0;
});
}
Stream.prototype.take = function(n) {
return Kefir.take(this, n);
})
};

@@ -628,3 +720,3 @@

var FlatMappedStream = Kefir.FlatMappedStream = inherit(function FlatMappedStream(sourceStream, mapFn){
Kefir.FlatMappedStream = inherit(function FlatMappedStream(sourceStream, mapFn){
Stream.call(this)

@@ -675,8 +767,4 @@ this.__sourceStream = sourceStream;

Kefir.flatMap = function(stream, mapFn) {
return new FlatMappedStream(stream, mapFn);
}
Stream.prototype.flatMap = function(fn) {
return Kefir.flatMap(this, fn);
return new Kefir.FlatMappedStream(this, fn);
};

@@ -693,8 +781,8 @@

var MergedStream = Kefir.MergedStream = inherit(function MergedStream(){
Kefir.MergedStream = inherit(function MergedStream(){
Stream.call(this)
this.__sourceStreams = firstArrOrToArr(arguments);
for (var i = 0; i < this.__sourceStreams.length; i++) {
this.__sourceStreams[i].onEnd(
this.__unplugFor(this.__sourceStreams[i])
this.__sources = firstArrOrToArr(arguments);
for (var i = 0; i < this.__sources.length; i++) {
this.__sources[i].onEnd(
this.__unplugFor(this.__sources[i])
);

@@ -705,9 +793,9 @@ }

__onFirstIn: function(){
for (var i = 0; i < this.__sourceStreams.length; i++) {
this.__sourceStreams[i].on(this._send, this);
for (var i = 0; i < this.__sources.length; i++) {
this.__sources[i].on(this._send, this);
}
},
__onLastOut: function(){
for (var i = 0; i < this.__sourceStreams.length; i++) {
this.__sourceStreams[i].off(this._send, this);
for (var i = 0; i < this.__sources.length; i++) {
this.__sources[i].off(this._send, this);
}

@@ -717,4 +805,4 @@ },

stream.off(this._send, this);
removeFromArray(this.__sourceStreams, stream);
if (this.__sourceStreams.length === 0) {
removeFromArray(this.__sources, stream);
if (this.__sources.length === 0) {
this._send(Kefir.END);

@@ -729,3 +817,3 @@ }

Stream.prototype.__end.call(this);
this.__sourceStreams = null;
this.__sources = null;
}

@@ -736,3 +824,3 @@

Kefir.merge = function() {
return new MergedStream(firstArrOrToArr(arguments));
return new Kefir.MergedStream(firstArrOrToArr(arguments));
}

@@ -754,14 +842,14 @@

var CombinedStream = Kefir.CombinedStream = inherit(function CombinedStream(sourceStreams, mapFn){
Kefir.CombinedStream = inherit(function CombinedStream(sources, mapFn){
Stream.call(this)
this.__sourceStreams = sourceStreams;
this.__cachedValues = new Array(sourceStreams.length);
this.__hasCached = new Array(sourceStreams.length);
this.__receiveFns = new Array(sourceStreams.length);
this.__sources = sources;
this.__cachedValues = new Array(sources.length);
this.__hasCached = new Array(sources.length);
this.__receiveFns = new Array(sources.length);
this.__mapFn = mapFn;
for (var i = 0; i < this.__sourceStreams.length; i++) {
for (var i = 0; i < this.__sources.length; i++) {
this.__receiveFns[i] = this.__receiveFor(i);
this.__sourceStreams[i].onEnd( this.__unplugFor(i) );
this.__sources[i].onEnd( this.__unplugFor(i) );
}

@@ -772,5 +860,5 @@

__onFirstIn: function(){
for (var i = 0; i < this.__sourceStreams.length; i++) {
if (this.__sourceStreams[i]) {
this.__sourceStreams[i].on(this.__receiveFns[i]);
for (var i = 0; i < this.__sources.length; i++) {
if (this.__sources[i]) {
this.__sources[i].on(this.__receiveFns[i]);
}

@@ -780,5 +868,5 @@ }

__onLastOut: function(){
for (var i = 0; i < this.__sourceStreams.length; i++) {
if (this.__sourceStreams[i]) {
this.__sourceStreams[i].off(this.__receiveFns[i]);
for (var i = 0; i < this.__sources.length; i++) {
if (this.__sources[i]) {
this.__sources[i].off(this.__receiveFns[i]);
}

@@ -788,6 +876,6 @@ }

__unplug: function(i){
this.__sourceStreams[i].off(this.__receiveFns[i]);
this.__sourceStreams[i] = null
this.__sources[i].off(this.__receiveFns[i]);
this.__sources[i] = null
this.__receiveFns[i] = null
if (isAllDead(this.__sourceStreams)) {
if (isAllDead(this.__sources)) {
this._send(Kefir.END);

@@ -800,7 +888,7 @@ }

},
__receive: function(i, value) {
__receive: function(i, x) {
this.__hasCached[i] = true;
this.__cachedValues[i] = value;
this.__cachedValues[i] = x;
if (this.__allCached()) {
if (typeof this.__mapFn === "function") {
if (isFn(this.__mapFn)) {
this._send(this.__mapFn.apply(null, this.__cachedValues));

@@ -814,4 +902,4 @@ } else {

var _this = this;
return function(value){
_this.__receive(i, value);
return function(x){
_this.__receive(i, x);
}

@@ -829,3 +917,3 @@ },

Stream.prototype.__end.call(this);
this.__sourceStreams = null;
this.__sources = null;
this.__cachedValues = null;

@@ -839,8 +927,8 @@ this.__hasCached = null;

Kefir.combine = function(streams, mapFn) {
return new CombinedStream(streams, mapFn);
Kefir.combine = function(sources, mapFn) {
return new Kefir.CombinedStream(sources, mapFn);
}
Stream.prototype.combine = function(streams, mapFn) {
return Kefir.combine([this].concat(streams), mapFn);
Stream.prototype.combine = function(sources, mapFn) {
return Kefir.combine([this].concat(sources), mapFn);
}

@@ -1196,2 +1284,43 @@

it("works with properties", function(done){
var property = helpers.sampleStream([1, 2, 3, 4, Kefir.END]).toProperty(6);
var filtered = property.filter(function(x){
return x % 2 === 0;
})
expect(filtered instanceof Kefir.Property).toBe(true);
expect(filtered.getCached()).toBe(6);
helpers.captureOutput(filtered, function(values){
expect(values).toEqual([6, 2, 4]);
done();
});
}, 100);
it("works with properties 2", function(done){
var property = helpers.sampleStream([1, 2, 3, 4, Kefir.END]).toProperty(5);
var filtered = property.filter(function(x){
return x % 2 === 0;
})
expect(filtered instanceof Kefir.Property).toBe(true);
expect(filtered.hasCached()).toBe(false);
helpers.captureOutput(filtered, function(values){
expect(values).toEqual([2, 4]);
done();
});
}, 100);
});

@@ -1326,2 +1455,21 @@

it("produce Property from Property", function(done){
var property = helpers.sampleStream([1, 2, Kefir.END]).toProperty(5);
var mapped = property.map(function(x){
return x*2;
})
expect(mapped instanceof Kefir.Property).toBe(true);
expect(mapped.getCached()).toBe(10);
helpers.captureOutput(mapped, function(values){
expect(values).toEqual([10, 2, 4]);
done();
});
}, 100);
it("with temporary all unsubscribed", function(done){

@@ -1552,2 +1700,17 @@

it("property.toProperty()", function() {
var bus = new Kefir.Bus;
var property = bus.toProperty(1);
expect(property.toProperty()).toBe(property);
expect(function(){
property.toProperty(2);
}).toThrow();
});
});

@@ -1554,0 +1717,0 @@

{
"name": "kefir",
"version": "0.1.2",
"version": "0.1.4",
"description": "Bacon.js inspired FRP library with less memory consumption",

@@ -32,16 +32,19 @@ "main": "dist/kefir.js",

"devDependencies": {
"jasmine-node": "~1.14.3",
"browserify": "~3.46.0",
"coffee-script": "~1.7.1",
"grunt": "~0.4.4",
"load-grunt-tasks": "~0.4.0",
"browserify": "~3.46.0",
"grunt-browserify": "~2.0.8",
"grunt-cli": "~0.1.13",
"grunt-contrib-clean": "~0.5.0",
"grunt-contrib-concat": "~0.4.0",
"grunt-contrib-jasmine": "~0.6.4",
"grunt-contrib-clean": "~0.5.0",
"grunt-contrib-jshint": "~0.10.0",
"grunt-contrib-uglify": "~0.4.0",
"grunt-contrib-concat": "~0.4.0",
"grunt-contrib-watch": "~0.6.1",
"grunt-contrib-jshint": "~0.10.0",
"grunt-jasmine-node": "~0.2.1",
"grunt-cli": "~0.1.13"
"grunt-release": "^0.7.0",
"jasmine-node": "~1.14.3",
"load-grunt-tasks": "~0.4.0",
"semver": "^2.3.0",
"shelljs": "^0.3.0"
},

@@ -48,0 +51,0 @@ "testling": {

@@ -13,4 +13,2 @@ (function(global){

var Kefir = {};

@@ -23,2 +21,3 @@

// Utils

@@ -32,2 +31,6 @@

function toArray(arrayLike){
return Array.prototype.slice.call(arrayLike);
}
function createObj(proto) {

@@ -39,8 +42,16 @@ var F = function(){};

function extend(to, from) {
for (var prop in from) {
if(own(from, prop)) {
to[prop] = from[prop];
function extend() {
var objects = toArray(arguments);
if (objects.length === 1) {
return objects[0];
}
var result = objects.shift();
for (var i = 0; i < objects.length; i++) {
for (var prop in objects[i]) {
if(own(objects[i], prop)) {
result[prop] = objects[i][prop];
}
}
}
return result;
}

@@ -88,6 +99,14 @@

}
return Array.prototype.slice.call(args);
return toArray(args);
}
function assert(condition, message) {
if (!condition) {
throw new Error(message);
}
}
function isFn(fn) {
return typeof fn === "function";
}

@@ -98,2 +117,82 @@

// Callbacks
var Callbacks = Kefir.Callbacks = inherit(function Callbacks(){
this.__subscribers = null;
this.__contexts = null;
}, Object, {
add: function(fn, context){
if (this.__subscribers === null) {
this.__subscribers = [];
this.__contexts = [];
}
this.__subscribers.push(fn);
this.__contexts.push(context);
},
remove: function(fn, context){
if (this.isEmpty()) {return}
for (var i = 0; i < this.__subscribers.length; i++) {
if (this.__subscribers[i] === fn && this.__contexts[i] === context) {
this.__subscribers[i] = null;
this.__contexts[i] = null;
}
}
if (isAllDead(this.__subscribers)){
this.__subscribers = null;
this.__contexts = null;
}
},
isEmpty: function(){
return this.__subscribers === null;
},
hasOne: function(){
return !this.isEmpty() && this.__subscribers.length === 1;
},
send: function(x){
if (this.isEmpty()) {return}
for (var i = 0, l = this.__subscribers.length; i < l; i++) {
var callback = this.__subscribers[i];
var context = this.__contexts[i];
if (isFn(callback)) {
if(Kefir.NO_MORE === callback.call(context, x)) {
this.remove(callback, context);
}
}
}
}
});
// Helper mixins
var withHandlerMixin = {
__Constructor: function(source) {
this.__source = source;
source.onEnd(this.__sendEnd, this);
if (source instanceof Property && this instanceof Property && source.hasCached()) {
this.__handle(source.getCached());
}
},
__handle: function(x){
this._send(x);
},
__onFirstIn: function(){
this.__source.onChanges(this.__handle, this);
},
__onLastOut: function(){
this.__source.off(this.__handle, this);
},
__end: function(){
this.__source = null;
}
}
// Base Stream class

@@ -104,37 +203,34 @@

// __onFirstIn, __onLastOut can also be added to prototype of child classes
if (typeof onFirstIn === "function") {
if (isFn(onFirstIn)) {
this.__onFirstIn = onFirstIn;
}
if (typeof onFirstIn === "function") {
if (isFn(onLastOut)) {
this.__onLastOut = onLastOut;
}
this.__subscribers = [];
this.__contexts = [];
this.__endSubscribers = [];
this.__endContexts = [];
this.__subscribers = new Callbacks;
this.__endSubscribers = new Callbacks;
}, Object, {
_send: function(value) {
_send: function(x) {
if (!this.isEnded()) {
if (value === Kefir.END) {
if (x === Kefir.END) {
this.__end();
} else {
for (var i = 0; i < this.__subscribers.length; i++) {
var callback = this.__subscribers[i];
var context = this.__contexts[i];
if (typeof callback === "function") {
if(Kefir.NO_MORE === callback.call(context, value)) {
this.off(callback, context);
}
}
}
this.__deliver(x);
}
}
},
__deliver: function(x){
if (!this.__subscribers.isEmpty()) {
this.__subscribers.send(x);
if (this.__subscribers.isEmpty()) {
this.__onLastOut();
}
}
},
on: function(callback, context) {
if (!this.isEnded()) {
this.__subscribers.push(callback);
this.__contexts.push(context);
if (this.__subscribers.length === 1) {
this.__subscribers.add(callback, context);
if (this.__subscribers.hasOne()) {
this.__onFirstIn();

@@ -144,13 +240,9 @@ }

},
onChanges: function(callback, context){
this.on(callback, context);
},
off: function(callback, context) {
if (!this.isEnded()) {
for (var i = 0; i < this.__subscribers.length; i++) {
if (this.__subscribers[i] === callback && this.__contexts[i] === context) {
this.__subscribers[i] = null;
this.__contexts[i] = null;
}
}
if (isAllDead(this.__subscribers)) {
this.__subscribers = [];
this.__contexts = [];
this.__subscribers.remove(callback, context);
if (this.__subscribers.isEmpty()) {
this.__onLastOut();

@@ -162,6 +254,5 @@ }

if (this.isEnded()) {
callback();
callback.call(context);
} else {
this.__endSubscribers.push(callback);
this.__endContexts.push(context);
this.__endSubscribers.add(callback, context);
}

@@ -171,8 +262,3 @@ },

if (!this.isEnded()){
for (var i = 0; i < this.__endSubscribers.length; i++) {
if (this.__endSubscribers[i] === callback && this.__endContexts[i] === context) {
this.__endSubscribers[i] = null;
this.__endContexts[i] = null;
}
}
this.__endSubscribers.remove(callback, context);
}

@@ -184,3 +270,3 @@ },

hasSubscribers: function(){
return !this.isEnded() && this.__subscribers.length > 0;
return !this.isEnded() && !this.__subscribers.isEmpty();
},

@@ -195,2 +281,3 @@ __onFirstIn: noop,

this.__onLastOut();
this.__endSubscribers.send();
if (own(this, '__onFirstIn')) {

@@ -203,10 +290,3 @@ this.__onFirstIn = null;

this.__subscribers = null;
this.__contexts = null;
for (var i = 0; i < this.__endSubscribers.length; i++) {
if (typeof this.__endSubscribers[i] === "function") {
this.__endSubscribers[i].call(this.__endContexts[i]);
}
}
this.__endSubscribers = null;
this.__endContexts = null;
}

@@ -235,3 +315,3 @@ }

var OnceStream = Kefir.OnceStream = inherit(function OnceStream(value){
Kefir.OnceStream = inherit(function OnceStream(value){
Stream.call(this);

@@ -249,4 +329,4 @@ this.__value = value;

Kefir.once = function(value) {
return new OnceStream(value);
Kefir.once = function(x) {
return new Kefir.OnceStream(x);
}

@@ -259,6 +339,6 @@

var Property = Kefir.Property = inherit(function Property(onFirstIn, onLastOut, initialValue){
var Property = Kefir.Property = inherit(function Property(onFirstIn, onLastOut, initial){
Stream.call(this, onFirstIn, onLastOut);
this.__hasCached = (typeof initialValue !== "undefined");
this.__cached = initialValue;
this.__hasCached = (typeof initial !== "undefined");
this.__cached = initial;
}, Stream, {

@@ -275,8 +355,21 @@

},
_send: function(value) {
_send: function(x) {
if (!this.isEnded()){
this.__hasCached = true;
this.__cached = value;
this.__cached = x;
}
Stream.prototype._send.call(this, value);
Stream.prototype._send.call(this, x);
},
toProperty: function(initial){
assert(
typeof initial === "undefined",
"can't convert Property to Property with new initial value"
)
return this;
},
hasCached: function(){
return this.__hasCached;
},
getCached: function(){
return this.__cached;
}

@@ -286,27 +379,17 @@

var PropertyFromStream = Kefir.PropertyFromStream = inherit(function PropertyFromStream(sourceStream, initialValue){
Property.call(this, null, null, initialValue);
this.__sourceStream = sourceStream;
sourceStream.onEnd(this.__sendEnd, this);
}, Property, {
Kefir.PropertyFromStream = inherit(function PropertyFromStream(source, initial){
Property.call(this, null, null, initial);
this.__Constructor.call(this, source);
}, Property, extend({}, withHandlerMixin, {
__onFirstIn: function(){
this.__sourceStream.on(this._send, this);
},
__onLastOut: function(){
this.__sourceStream.off(this._send, this);
},
__end: function(){
Stream.prototype.__end.call(this);
this.__sourceStream = null;
Property.prototype.__end.call(this);
withHandlerMixin.__end.call(this);
}
})
}))
Kefir.toProperty = function(sourceStream, initialValue){
return new PropertyFromStream(sourceStream, initialValue);
Stream.prototype.toProperty = function(initial){
return new Kefir.PropertyFromStream(this, initial);
}
Stream.prototype.toProperty = function(initialValue){
return Kefir.toProperty(this, initialValue);
}

@@ -318,28 +401,16 @@

var PropertyChangesStream = Kefir.PropertyChangesStream = inherit(function PropertyChangesStream(property){
Kefir.ChangesStream = inherit(function ChangesStream(source){
Stream.call(this);
this.__sourceProperty = property;
var _this = this;
property.onEnd(function(){ _this._send(Kefir.END) })
}, Stream, {
this.__Constructor.call(this, source);
}, Stream, extend({}, withHandlerMixin, {
__onFirstIn: function(){
this.__sourceProperty.onChanges(this._send, this);
},
__onLastOut: function(){
this.__sourceProperty.off(this._send, this);
},
__end: function(){
Stream.prototype.__end.call(this);
this.__sourceProperty = null;
withHandlerMixin.__end.call(this);
}
})
}))
Kefir.changes = function(property){
return new PropertyChangesStream(property);
}
Property.prototype.changes = function() {
return Kefir.changes(this);
return new Kefir.ChangesStream(this);
};

@@ -353,22 +424,22 @@

var FromBinderStream = Kefir.FromBinderStream = inherit(function FromBinderStream(generator){
Kefir.FromBinderStream = inherit(function FromBinderStream(subscribe){
Stream.call(this);
this.__generator = generator;
var _this = this;
this.__deliver = function(x){ _this._send(x) }
this.__subscribe = subscribe;
}, Stream, {
__onFirstIn: function(){
this.__generatorUsubscriber = this.__generator(this.__deliver);
var _this = this;
this.__usubscriber = this.__subscribe(function(x){
_this._send(x);
});
},
__onLastOut: function(){
if (typeof this.__generatorUsubscriber === "function") {
this.__generatorUsubscriber();
if (isFn(this.__usubscriber)) {
this.__usubscriber();
}
this.__generatorUsubscriber = null;
this.__usubscriber = null;
},
__end: function(){
Stream.prototype.__end.call(this);
this.__generator = null;
this.__deliver = null;
this.__subscribe = null;
}

@@ -378,4 +449,4 @@

Kefir.fromBinder = function(generator){
return new FromBinderStream(generator);
Kefir.fromBinder = function(subscribe){
return new Kefir.FromBinderStream(subscribe);
}

@@ -392,3 +463,3 @@

var Bus = Kefir.Bus = inherit(function Bus(){
Kefir.Bus = inherit(function Bus(){
Stream.call(this);

@@ -438,5 +509,9 @@ this.__plugged = [];

Kefir.bus = function(){
return new Kefir.Bus;
}
// FromPoll

@@ -449,7 +524,7 @@

var _this = this;
this.__deliver = function(){ _this._send(sourceFn()) }
this.__send = function(){ _this._send(sourceFn()) }
}, Stream, {
__onFirstIn: function(){
this.__intervalId = setInterval(this.__deliver, this.__interval);
this.__intervalId = setInterval(this.__send, this.__interval);
},

@@ -464,3 +539,3 @@ __onLastOut: function(){

Stream.prototype.__end.call(this);
this.__deliver = null;
this.__send = null;
}

@@ -478,4 +553,4 @@

Kefir.interval = function(interval, value){
return new FromPollStream(interval, function(){ return value });
Kefir.interval = function(interval, x){
return new FromPollStream(interval, function(){ return x });
}

@@ -514,32 +589,38 @@

var MappedStream = Kefir.MappedStream = inherit(function MappedStream(sourceStream, mapFn){
Stream.call(this)
this.__sourceStream = sourceStream;
this.__mapFn = mapFn;
sourceStream.onEnd(this.__sendEnd, this);
}, Stream, {
__mapAndSend: function(x){
var mapMixin = extend({}, withHandlerMixin, {
__Constructor: function(source, mapFn){
if (source instanceof Property) {
Property.call(this);
} else {
Stream.call(this);
}
this.__mapFn = mapFn;
withHandlerMixin.__Constructor.call(this, source);
},
__handle: function(x){
this._send( this.__mapFn(x) );
},
__onFirstIn: function(){
this.__sourceStream.on(this.__mapAndSend, this);
},
__onLastOut: function(){
this.__sourceStream.off(this.__mapAndSend, this);
},
__end: function(){
Stream.prototype.__end.call(this);
this.__sourceStream = null;
withHandlerMixin.__end.call(this);
this.__mapFn = null;
}
});
Kefir.map = function(stream, mapFn) {
return new MappedStream(stream, mapFn);
}
Kefir.MappedStream = inherit(
function MappedStream(){this.__Constructor.apply(this, arguments)},
Stream, mapMixin
);
Kefir.MappedProperty = inherit(
function MappedProperty(){this.__Constructor.apply(this, arguments)},
Property, mapMixin
);
Stream.prototype.map = function(fn) {
return Kefir.map(this, fn);
if (this instanceof Property) {
return new Kefir.MappedProperty(this, fn);
} else {
return new Kefir.MappedStream(this, fn);
}
};

@@ -551,9 +632,8 @@

// Filter
var FilteredStream = Kefir.FilteredStream = inherit(function FilteredStream(sourceStream, filterFn){
MappedStream.call(this, sourceStream, filterFn);
}, MappedStream, {
__mapAndSend: function(x){
var filterMixin = extend({}, mapMixin, {
__handle: function(x){
if (this.__mapFn(x)) {

@@ -563,11 +643,20 @@ this._send(x);

}
});
Kefir.filter = function(stream, filterFn) {
return new FilteredStream(stream, filterFn);
}
Kefir.FilteredStream = inherit(
function FilteredStream(){this.__Constructor.apply(this, arguments)},
Stream, filterMixin
);
Kefir.FilteredProperty = inherit(
function FilteredProperty(){this.__Constructor.apply(this, arguments)},
Property, filterMixin
);
Stream.prototype.filter = function(fn) {
return Kefir.filter(this, fn);
if (this instanceof Property) {
return new Kefir.FilteredProperty(this, fn);
} else {
return new Kefir.FilteredStream(this, fn);
}
};

@@ -578,9 +667,7 @@

// TakeWhile
var TakeWhileStream = Kefir.TakeWhileStream = inherit(function TakeWhileStream(sourceStream, filterFn){
MappedStream.call(this, sourceStream, filterFn);
}, MappedStream, {
__mapAndSend: function(x){
var takeWhileMixin = extend({}, mapMixin, {
__handle: function(x){
if (this.__mapFn(x)) {

@@ -592,11 +679,20 @@ this._send(x);

}
});
Kefir.takeWhile = function(stream, filterFn) {
return new TakeWhileStream(stream, filterFn);
}
Kefir.TakeWhileStream = inherit(
function TakeWhileStream(){this.__Constructor.apply(this, arguments)},
Stream, takeWhileMixin
);
Kefir.TakeWhileProperty = inherit(
function TakeWhileProperty(){this.__Constructor.apply(this, arguments)},
Property, takeWhileMixin
);
Stream.prototype.takeWhile = function(fn) {
return Kefir.takeWhile(this, fn);
if (this instanceof Property) {
return new Kefir.TakeWhileProperty(this, fn);
} else {
return new Kefir.TakeWhileStream(this, fn);
}
};

@@ -609,10 +705,6 @@

Kefir.take = function(stream, n) {
return new TakeWhileStream(stream, function(){
Stream.prototype.take = function(n) {
return this.takeWhile(function(){
return n-- > 0;
});
}
Stream.prototype.take = function(n) {
return Kefir.take(this, n);
})
};

@@ -627,3 +719,3 @@

var FlatMappedStream = Kefir.FlatMappedStream = inherit(function FlatMappedStream(sourceStream, mapFn){
Kefir.FlatMappedStream = inherit(function FlatMappedStream(sourceStream, mapFn){
Stream.call(this)

@@ -674,8 +766,4 @@ this.__sourceStream = sourceStream;

Kefir.flatMap = function(stream, mapFn) {
return new FlatMappedStream(stream, mapFn);
}
Stream.prototype.flatMap = function(fn) {
return Kefir.flatMap(this, fn);
return new Kefir.FlatMappedStream(this, fn);
};

@@ -692,8 +780,8 @@

var MergedStream = Kefir.MergedStream = inherit(function MergedStream(){
Kefir.MergedStream = inherit(function MergedStream(){
Stream.call(this)
this.__sourceStreams = firstArrOrToArr(arguments);
for (var i = 0; i < this.__sourceStreams.length; i++) {
this.__sourceStreams[i].onEnd(
this.__unplugFor(this.__sourceStreams[i])
this.__sources = firstArrOrToArr(arguments);
for (var i = 0; i < this.__sources.length; i++) {
this.__sources[i].onEnd(
this.__unplugFor(this.__sources[i])
);

@@ -704,9 +792,9 @@ }

__onFirstIn: function(){
for (var i = 0; i < this.__sourceStreams.length; i++) {
this.__sourceStreams[i].on(this._send, this);
for (var i = 0; i < this.__sources.length; i++) {
this.__sources[i].on(this._send, this);
}
},
__onLastOut: function(){
for (var i = 0; i < this.__sourceStreams.length; i++) {
this.__sourceStreams[i].off(this._send, this);
for (var i = 0; i < this.__sources.length; i++) {
this.__sources[i].off(this._send, this);
}

@@ -716,4 +804,4 @@ },

stream.off(this._send, this);
removeFromArray(this.__sourceStreams, stream);
if (this.__sourceStreams.length === 0) {
removeFromArray(this.__sources, stream);
if (this.__sources.length === 0) {
this._send(Kefir.END);

@@ -728,3 +816,3 @@ }

Stream.prototype.__end.call(this);
this.__sourceStreams = null;
this.__sources = null;
}

@@ -735,3 +823,3 @@

Kefir.merge = function() {
return new MergedStream(firstArrOrToArr(arguments));
return new Kefir.MergedStream(firstArrOrToArr(arguments));
}

@@ -753,14 +841,14 @@

var CombinedStream = Kefir.CombinedStream = inherit(function CombinedStream(sourceStreams, mapFn){
Kefir.CombinedStream = inherit(function CombinedStream(sources, mapFn){
Stream.call(this)
this.__sourceStreams = sourceStreams;
this.__cachedValues = new Array(sourceStreams.length);
this.__hasCached = new Array(sourceStreams.length);
this.__receiveFns = new Array(sourceStreams.length);
this.__sources = sources;
this.__cachedValues = new Array(sources.length);
this.__hasCached = new Array(sources.length);
this.__receiveFns = new Array(sources.length);
this.__mapFn = mapFn;
for (var i = 0; i < this.__sourceStreams.length; i++) {
for (var i = 0; i < this.__sources.length; i++) {
this.__receiveFns[i] = this.__receiveFor(i);
this.__sourceStreams[i].onEnd( this.__unplugFor(i) );
this.__sources[i].onEnd( this.__unplugFor(i) );
}

@@ -771,5 +859,5 @@

__onFirstIn: function(){
for (var i = 0; i < this.__sourceStreams.length; i++) {
if (this.__sourceStreams[i]) {
this.__sourceStreams[i].on(this.__receiveFns[i]);
for (var i = 0; i < this.__sources.length; i++) {
if (this.__sources[i]) {
this.__sources[i].on(this.__receiveFns[i]);
}

@@ -779,5 +867,5 @@ }

__onLastOut: function(){
for (var i = 0; i < this.__sourceStreams.length; i++) {
if (this.__sourceStreams[i]) {
this.__sourceStreams[i].off(this.__receiveFns[i]);
for (var i = 0; i < this.__sources.length; i++) {
if (this.__sources[i]) {
this.__sources[i].off(this.__receiveFns[i]);
}

@@ -787,6 +875,6 @@ }

__unplug: function(i){
this.__sourceStreams[i].off(this.__receiveFns[i]);
this.__sourceStreams[i] = null
this.__sources[i].off(this.__receiveFns[i]);
this.__sources[i] = null
this.__receiveFns[i] = null
if (isAllDead(this.__sourceStreams)) {
if (isAllDead(this.__sources)) {
this._send(Kefir.END);

@@ -799,7 +887,7 @@ }

},
__receive: function(i, value) {
__receive: function(i, x) {
this.__hasCached[i] = true;
this.__cachedValues[i] = value;
this.__cachedValues[i] = x;
if (this.__allCached()) {
if (typeof this.__mapFn === "function") {
if (isFn(this.__mapFn)) {
this._send(this.__mapFn.apply(null, this.__cachedValues));

@@ -813,4 +901,4 @@ } else {

var _this = this;
return function(value){
_this.__receive(i, value);
return function(x){
_this.__receive(i, x);
}

@@ -828,3 +916,3 @@ },

Stream.prototype.__end.call(this);
this.__sourceStreams = null;
this.__sources = null;
this.__cachedValues = null;

@@ -838,8 +926,8 @@ this.__hasCached = null;

Kefir.combine = function(streams, mapFn) {
return new CombinedStream(streams, mapFn);
Kefir.combine = function(sources, mapFn) {
return new Kefir.CombinedStream(sources, mapFn);
}
Stream.prototype.combine = function(streams, mapFn) {
return Kefir.combine([this].concat(streams), mapFn);
Stream.prototype.combine = function(sources, mapFn) {
return Kefir.combine([this].concat(sources), mapFn);
}

@@ -846,0 +934,0 @@

@@ -23,2 +23,43 @@ var Kefir = require('../../src/kefir.js');

it("works with properties", function(done){
var property = helpers.sampleStream([1, 2, 3, 4, Kefir.END]).toProperty(6);
var filtered = property.filter(function(x){
return x % 2 === 0;
})
expect(filtered instanceof Kefir.Property).toBe(true);
expect(filtered.getCached()).toBe(6);
helpers.captureOutput(filtered, function(values){
expect(values).toEqual([6, 2, 4]);
done();
});
}, 100);
it("works with properties 2", function(done){
var property = helpers.sampleStream([1, 2, 3, 4, Kefir.END]).toProperty(5);
var filtered = property.filter(function(x){
return x % 2 === 0;
})
expect(filtered instanceof Kefir.Property).toBe(true);
expect(filtered.hasCached()).toBe(false);
helpers.captureOutput(filtered, function(values){
expect(values).toEqual([2, 4]);
done();
});
}, 100);
});

@@ -23,2 +23,21 @@ var Kefir = require('../../src/kefir.js');

it("produce Property from Property", function(done){
var property = helpers.sampleStream([1, 2, Kefir.END]).toProperty(5);
var mapped = property.map(function(x){
return x*2;
})
expect(mapped instanceof Kefir.Property).toBe(true);
expect(mapped.getCached()).toBe(10);
helpers.captureOutput(mapped, function(values){
expect(values).toEqual([10, 2, 4]);
done();
});
}, 100);
it("with temporary all unsubscribed", function(done){

@@ -25,0 +44,0 @@

@@ -85,2 +85,17 @@ var Kefir = require('../../src/kefir.js');

it("property.toProperty()", function() {
var bus = new Kefir.Bus;
var property = bus.toProperty(1);
expect(property.toProperty()).toBe(property);
expect(function(){
property.toProperty(2);
}).toThrow();
});
});

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is too big to display

Sorry, the diff of this file is not supported yet

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc