Comparing version 0.1.9 to 0.1.10
@@ -47,6 +47,6 @@ # Bacon.js API implementation status | ||
| `observable.onValue(f)` | :rocket: | `observable.onValue(f)` | | ||
| `observable.onError(f)` | :broken_heart: | | | ||
| `observable.onError(f)` | :rocket: | `observable.onError(f)` | | ||
| `observable.onEnd(f)` | :rocket: | `observable.onEnd(f)` | | ||
| `observable.map(f)` | :rocket: | `observable.map(f)` | | ||
| `stream.map(property) / property.sampledBy(stream)` | :broken_heart: | | | ||
| `stream.map(property) / property.sampledBy(stream)` | :rocket: | `property.sampledBy(stream)` | | ||
| `observable.mapError(f)` | :broken_heart: | | | ||
@@ -70,6 +70,6 @@ | `observable.errors()` | :broken_heart: | | | ||
| `observable.flatMap(f)` | :rocket: | `observable.flatMap(f)` | | ||
| `observable.flatMapLatest(f)` | :broken_heart: | | | ||
| `observable.flatMapLatest(f)` | :rocket: | `observable.flatMapLatest(f)` | | ||
| `observable.flatMapFirst(f)` | :broken_heart: | | | ||
| `observable.scan(seed, f)` | :rocket: | `observable.scan(seed, f)` | | ||
| `observable.fold(seed, f) / observable.reduce(seed, f)` | :broken_heart: | | | ||
| `observable.fold(seed, f) / observable.reduce(seed, f)` | :rocket: | `observable.reduce(seed, f)` | | ||
| `observable.diff(start, f)` | :rocket: | `observable.diff(start, f)` | | ||
@@ -99,3 +99,3 @@ | `observable.zip(other, f)` | :broken_heart: | | | ||
| `stream.concat(otherStream)` | :broken_heart: | | | ||
| `stream.merge(otherStream)` | :rocket: | `stream.merge(stream1[, stream2, ...])` / `stream.merge(streams)` | | ||
| `stream.merge(otherStream)` | :rocket: | `stream.merge(obs1, obs2, ...)` / `stream.merge(observables)` | | ||
| `stream.startWith(value)` | :broken_heart: | | | ||
@@ -124,5 +124,5 @@ | `stream.skipWhile(f)` | :rocket: | `stream.skipWhile(f)` | | ||
| `property.sample(interval)` | :broken_heart: | | | ||
| `property.sampledBy(stream)` | :broken_heart: | | | ||
| `property.sampledBy(property)` | :broken_heart: | | | ||
| `property.sampledBy(streamOrProperty, f)` | :broken_heart: | | | ||
| `property.sampledBy(stream)` | :rocket: | `property.sampledBy(stream)` | | ||
| `property.sampledBy(property)` | :rocket: | `property.sampledBy(property)` | | ||
| `property.sampledBy(streamOrProperty, f)` | :rocket: | `property.sampledBy(streamOrProperty, f)` | | ||
| `property.skipDuplicates(isEqual)` | :rocket: | `property.skipDuplicates(isEqual)` | | ||
@@ -140,7 +140,7 @@ | `property.changes()` | :rocket: | `property.changes()` | | ||
|:---|:---:|:---| | ||
| `Bacon.combineAsArray(streams)` | :rocket: | `Kefir.combine(streams)` | | ||
| `Bacon.combineAsArray(streams)` | :rocket: | `Kefir.combine(observables)` | | ||
| `Bacon.combineAsArray(s1, s2...)` | :broken_heart: | | | ||
| `Bacon.combineWith(f, stream1, stream2...)` | :rocket: | `Kefir.combine(observables, f)` | | ||
| `Bacon.combineTemplate(template)` | :broken_heart: | | | ||
| `Bacon.mergeAll(streams)` | :rocket: | `Kefir.merge(streams)` / `Kefir.merge(stream1[, stream2, ...])` | | ||
| `Bacon.mergeAll(streams)` | :rocket: | `Kefir.merge(observables)` / `Kefir.merge(obs1, obs2, ...)` | | ||
| `Bacon.zipAsArray(streams)` | :broken_heart: | | | ||
@@ -150,3 +150,3 @@ | `Bacon.zipAsArray(stream1, stream2...)` | :broken_heart: | | | ||
| `Bacon.zipWith(f, stream1, stream1...)` | :broken_heart: | | | ||
| `Bacon.onValues(a, b [, c...], f)` | :broken_heart: | | | ||
| `Bacon.onValues(a, b [, c...], f)` | :rocket: | Kefir.onValues(observables, f) | | ||
@@ -169,3 +169,3 @@ | ||
| `bus.end()` | :rocket: | `bus.end()` | | ||
| `bus.error(e)` | :broken_heart: | | | ||
| `bus.error(e)` | :rocket: | `bus.error(e)` | | ||
| `bus.plug(stream)` | :rocket: | `bus.plug(stream)` | | ||
@@ -177,3 +177,3 @@ | ||
:broken_heart: | ||
:bike: | ||
@@ -180,0 +180,0 @@ |
{ | ||
"name": "kefir", | ||
"version": "0.1.9", | ||
"version": "0.1.10", | ||
"homepage": "https://github.com/pozadi/kefir", | ||
@@ -5,0 +5,0 @@ "authors": [ |
@@ -1,2 +0,2 @@ | ||
/*! kefir - 0.1.9 | ||
/*! kefir - 0.1.10 | ||
* https://github.com/pozadi/kefir | ||
@@ -16,3 +16,3 @@ */ | ||
function toArray(arrayLike){ | ||
if (arrayLike instanceof Array) { | ||
if (isArray(arrayLike)) { | ||
return arrayLike; | ||
@@ -31,11 +31,10 @@ } else { | ||
function extend() { | ||
var objects = toArray(arguments); | ||
if (objects.length === 1) { | ||
return objects[0]; | ||
if (arguments.length === 1) { | ||
return arguments[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]; | ||
var result = arguments[0]; | ||
for (var i = 1; i < arguments.length; i++) { | ||
for (var prop in arguments[i]) { | ||
if(own(arguments[i], prop)) { | ||
result[prop] = arguments[i][prop]; | ||
} | ||
@@ -47,3 +46,3 @@ } | ||
function inherit(Child, Parent) { // (Child, Parent[, mixin1, mixin2, ...]) | ||
function inherit(Child, Parent/*[, mixin1, mixin2, ...]*/) { | ||
Child.prototype = createObj(Parent.prototype); | ||
@@ -67,3 +66,3 @@ Child.prototype.constructor = Child; | ||
function firstArrOrToArr(args) { | ||
if (Object.prototype.toString.call(args[0]) === '[object Array]') { | ||
if (isArray(args[0])) { | ||
return args[0]; | ||
@@ -85,5 +84,4 @@ } | ||
function callSubscriber(subscriber, moreArgs){ | ||
// subscriber = [ | ||
// eventName, | ||
function callFn(fnMeta, moreArgs){ | ||
// fnMeta = [ | ||
// fn, | ||
@@ -95,16 +93,66 @@ // context, | ||
// ] | ||
var fn = subscriber[1]; | ||
var context = subscriber[2]; | ||
var args = restArgs(subscriber, 3); | ||
var fn, context, args; | ||
if (isFn(fnMeta)) { | ||
fn = fnMeta; | ||
context = null; | ||
args = null; | ||
} else { | ||
fn = fnMeta[0]; | ||
context = fnMeta[1]; | ||
args = restArgs(fnMeta, 2, true); | ||
/*jshint eqnull:true */ | ||
if (!isFn(fn) && context != null) { | ||
fn = context[fn]; | ||
} | ||
} | ||
if (moreArgs){ | ||
args = args.concat(toArray(moreArgs)); | ||
if (args) { | ||
args = args.concat(toArray(moreArgs)); | ||
} else { | ||
args = moreArgs; | ||
} | ||
} | ||
return fn.apply(context, args); | ||
if (isFn(fn)) { | ||
return args ? fn.apply(context, args) : fn.call(context); | ||
} else { | ||
throw new Error('not a function ' + fn); | ||
} | ||
} | ||
function normFnMeta(fnMeta) { | ||
if (isArray(fnMeta) || isArguments(fnMeta)) { | ||
if (fnMeta.length === 1) { | ||
return fnMeta[0]; | ||
} | ||
if (fnMeta.length === 0) { | ||
return null; | ||
} | ||
} | ||
return fnMeta; | ||
} | ||
function isFn(fn) { | ||
return typeof fn === "function"; | ||
return typeof fn === 'function'; | ||
} | ||
function isEqualArrays(a, b){ | ||
function isUndefined(x) { | ||
return typeof x === 'undefined'; | ||
} | ||
function isArray(xs) { | ||
return Object.prototype.toString.call(xs) === '[object Array]'; | ||
} | ||
var isArguments = function(xs) { | ||
return Object.prototype.toString.call(xs) === '[object Arguments]'; | ||
} | ||
// For IE | ||
if (!isArguments(arguments)) { | ||
isArguments = function(obj) { | ||
return !!(obj && own(obj, 'callee')); | ||
} | ||
} | ||
function isEqualArrays(a, b) { | ||
/*jshint eqnull:true */ | ||
@@ -149,5 +197,14 @@ if (a == null && b == null) { | ||
// Example: | ||
// stream.__sendAny(Kefir.error('network error')) | ||
Kefir.Error = function(error) { | ||
this.error = error; | ||
} | ||
Kefir.error = function(error) { | ||
return new Kefir.Error(error); | ||
} | ||
// Observable | ||
@@ -182,9 +239,12 @@ | ||
if (!this.isEnded()) { | ||
var firstValueSubscriber = (type === 'value' && !this.__hasSubscribers('value')); | ||
var firstIn = ( | ||
(type === 'value' || type === 'error') && | ||
!(this.__hasSubscribers('value') || this.__hasSubscribers('error')) | ||
); | ||
this.__subscribers.push(arguments); | ||
if (firstValueSubscriber) { | ||
if (firstIn) { | ||
this.__onFirstIn(); | ||
} | ||
} else if (type === 'end') { | ||
callSubscriber(arguments); | ||
callFn(restArgs(arguments, 1)); | ||
} | ||
@@ -199,3 +259,6 @@ }, | ||
} | ||
if (type === 'value' && !this.__hasSubscribers('value')) { | ||
if ( | ||
(type === 'value' || type === 'error') && | ||
!(this.__hasSubscribers('value') || this.__hasSubscribers('error')) | ||
) { | ||
this.__onLastOut(); | ||
@@ -210,3 +273,3 @@ } | ||
if (subscriber && subscriber[0] === type) { | ||
var result = callSubscriber(subscriber, restArgs(arguments, 1)); | ||
var result = callFn(restArgs(subscriber, 1), restArgs(arguments, 1)); | ||
if (result === NO_MORE) { | ||
@@ -247,5 +310,11 @@ this.__off.apply(this, subscriber) | ||
this.__send('value', x); | ||
return this; | ||
}, | ||
__sendError: function(x){ | ||
this.__send('error', x); | ||
return this; | ||
}, | ||
__sendEnd: function(){ | ||
this.__send('end'); | ||
return this; | ||
}, | ||
@@ -259,5 +328,8 @@ __sendAny: function(x){ | ||
} | ||
} else if (x !== Kefir.NOTHING) { | ||
} else if (x instanceof Kefir.Error) { | ||
this.__sendError(x.error); | ||
} else if (x !== NOTHING) { | ||
this.__sendValue(x); | ||
} | ||
return this; | ||
}, | ||
@@ -268,11 +340,23 @@ | ||
this.__on.apply(this, ['value'].concat(toArray(arguments))); | ||
return this; | ||
}, | ||
offValue: function(){ | ||
this.__off.apply(this, ['value'].concat(toArray(arguments))); | ||
return this; | ||
}, | ||
onError: function(){ | ||
this.__on.apply(this, ['error'].concat(toArray(arguments))); | ||
return this; | ||
}, | ||
offError: function(){ | ||
this.__off.apply(this, ['error'].concat(toArray(arguments))); | ||
return this; | ||
}, | ||
onEnd: function(){ | ||
this.__on.apply(this, ['end'].concat(toArray(arguments))); | ||
return this; | ||
}, | ||
offEnd: function(){ | ||
this.__off.apply(this, ['end'].concat(toArray(arguments))); | ||
return this; | ||
}, | ||
@@ -282,3 +366,3 @@ | ||
onNewValue: function(){ | ||
this.onValue.apply(this, arguments); | ||
return this.onValue.apply(this, arguments); | ||
}, | ||
@@ -313,3 +397,3 @@ | ||
Observable.call(this, onFirstIn, onLastOut); | ||
this.__cached = (typeof initial !== "undefined") ? initial : Kefir.NOTHING; | ||
this.__cached = isUndefined(initial) ? NOTHING : initial; | ||
} | ||
@@ -321,6 +405,6 @@ | ||
hasCached: function(){ | ||
return this.__cached !== Kefir.NOTHING; | ||
hasValue: function(){ | ||
return this.__cached !== NOTHING; | ||
}, | ||
getCached: function(){ | ||
getValue: function(){ | ||
return this.__cached; | ||
@@ -337,8 +421,9 @@ }, | ||
this.__on.apply(this, ['value'].concat(toArray(arguments))); | ||
return this; | ||
}, | ||
onValue: function() { | ||
if ( this.hasCached() ) { | ||
callSubscriber(['value'].concat(toArray(arguments)), [this.__cached]); | ||
if ( this.hasValue() ) { | ||
callFn(arguments, [this.__cached]) | ||
} | ||
this.onNewValue.apply(this, arguments); | ||
return this.onNewValue.apply(this, arguments); | ||
} | ||
@@ -352,9 +437,14 @@ | ||
Observable.prototype.log = function(text) { | ||
if (!text) { | ||
text = this.toString(); | ||
var logHelper = function(name, type, x) { | ||
console.log(name, type, x); | ||
} | ||
Observable.prototype.log = function(name) { | ||
if (!name) { | ||
name = this.toString(); | ||
} | ||
function log(x){ console.log(text, x) } | ||
this.onValue(log); | ||
this.onEnd(function(){ log(END) }); | ||
this.onValue(logHelper, null, name, '<value>'); | ||
this.onError(logHelper, null, name, '<error>'); | ||
this.onEnd(logHelper, null, name, '<end>'); | ||
return this; | ||
} | ||
@@ -365,2 +455,3 @@ | ||
// Kefir.constant(x) | ||
// Kefir.fromArray(values) | ||
@@ -393,7 +484,9 @@ | ||
if (!this.isEnded()) { | ||
callSubscriber(['value'].concat(toArray(arguments)), [this.__value]); | ||
callFn(arguments, [this.__value]); | ||
this.__value = null; | ||
this.__sendEnd(); | ||
} | ||
} | ||
return this; | ||
}, | ||
onError: noop | ||
@@ -447,4 +540,4 @@ }) | ||
source.onEnd(this.__sendEnd, this); | ||
if (source instanceof Property && this instanceof Property && source.hasCached()) { | ||
this.__handle(source.getCached()); | ||
if (source instanceof Property && this instanceof Property && source.hasValue()) { | ||
this.__handle(source.getValue()); | ||
} | ||
@@ -456,6 +549,8 @@ }, | ||
__onFirstIn: function(){ | ||
this.__source.onNewValue(this.__handle, this); | ||
this.__source.onNewValue('__handle', this); | ||
this.__source.onError('__sendError', this); | ||
}, | ||
__onLastOut: function(){ | ||
this.__source.offValue(this.__handle, this); | ||
this.__source.offValue('__handle', this); | ||
this.__source.offError('__sendError', this); | ||
}, | ||
@@ -488,3 +583,3 @@ __clear: function(){ | ||
Property.prototype.toProperty = function(initial){ | ||
if (typeof initial === "undefined") { | ||
if (isUndefined(initial)) { | ||
return this | ||
@@ -502,15 +597,27 @@ } else { | ||
// property.changes() | ||
Kefir.ChangesStream = function ChangesStream(source){ | ||
Stream.call(this); | ||
// .scan(seed, fn) | ||
Kefir.ScanProperty = function ScanProperty(source, seed, fnMeta){ | ||
Property.call(this, null, null, seed); | ||
this.__fnMeta = normFnMeta(fnMeta); | ||
this.__Constructor(source); | ||
} | ||
inherit(Kefir.ChangesStream, Stream, WithSourceStreamMixin, { | ||
__ClassName: 'ChangesStream' | ||
inherit(Kefir.ScanProperty, Property, WithSourceStreamMixin, { | ||
__ClassName: 'ScanProperty', | ||
__handle: function(x){ | ||
this.__sendValue( callFn(this.__fnMeta, [this.getValue(), x]) ); | ||
}, | ||
__clear: function(){ | ||
WithSourceStreamMixin.__clear.call(this); | ||
this.__fnMeta = null; | ||
} | ||
}) | ||
Property.prototype.changes = function() { | ||
return new Kefir.ChangesStream(this); | ||
Observable.prototype.scan = function(seed/*fn[, context[, arg1, arg2, ...]]*/) { | ||
return new Kefir.ScanProperty(this, seed, restArgs(arguments, 1)); | ||
} | ||
@@ -521,27 +628,32 @@ | ||
// .reduce(seed, fn) | ||
// .scan(seed, fn) | ||
Kefir.ScanProperty = function ScanProperty(source, seed, fn){ | ||
Property.call(this, null, null, seed); | ||
this.__fn = fn; | ||
Kefir.ReducedProperty = function ReducedProperty(source, seed, fnMeta){ | ||
Property.call(this); | ||
this.__fnMeta = normFnMeta(fnMeta); | ||
this.__result = seed; | ||
source.onEnd('__sendResult', this); | ||
this.__Constructor(source); | ||
} | ||
inherit(Kefir.ScanProperty, Property, WithSourceStreamMixin, { | ||
inherit(Kefir.ReducedProperty, Property, WithSourceStreamMixin, { | ||
__ClassName: 'ScanProperty', | ||
__ClassName: 'ReducedProperty', | ||
__handle: function(x){ | ||
this.__sendValue( this.__fn(this.getCached(), x) ); | ||
this.__result = callFn(this.__fnMeta, [this.__result, x]); | ||
}, | ||
__sendResult: function(){ | ||
this.__sendValue(this.__result); | ||
}, | ||
__clear: function(){ | ||
WithSourceStreamMixin.__clear.call(this); | ||
this.__fn = null; | ||
this.__fnMeta = null; | ||
this.__result = null; | ||
} | ||
}) | ||
}); | ||
Observable.prototype.scan = function(seed, fn) { | ||
return new Kefir.ScanProperty(this, seed, fn); | ||
Observable.prototype.reduce = function(seed/*fn[, context[, arg1, arg2, ...]]*/) { | ||
return new Kefir.ReducedProperty(this, seed, restArgs(arguments, 1)); | ||
} | ||
@@ -555,4 +667,4 @@ | ||
var MapMixin = { | ||
__Constructor: function(source, mapFn){ | ||
if (source instanceof Property) { | ||
__Constructor: function(source, mapFnMeta){ | ||
if (this instanceof Property) { | ||
Property.call(this); | ||
@@ -562,11 +674,13 @@ } else { | ||
} | ||
this.__mapFn = mapFn; | ||
this.__mapFnMeta = normFnMeta(mapFnMeta); | ||
WithSourceStreamMixin.__Constructor.call(this, source); | ||
}, | ||
__handle: function(x){ | ||
this.__sendAny( this.__mapFn(x) ); | ||
this.__sendAny( | ||
this.__mapFnMeta ? callFn(this.__mapFnMeta, [x]) : x | ||
); | ||
}, | ||
__clear: function(){ | ||
WithSourceStreamMixin.__clear.call(this); | ||
this.__mapFn = null; | ||
this.__mapFnMeta = null; | ||
} | ||
@@ -592,8 +706,8 @@ } | ||
Stream.prototype.map = function(fn) { | ||
return new Kefir.MappedStream(this, fn); | ||
Stream.prototype.map = function(/*fn[, context[, arg1, arg2, ...]]*/) { | ||
return new Kefir.MappedStream(this, arguments); | ||
} | ||
Property.prototype.map = function(fn) { | ||
return new Kefir.MappedProperty(this, fn); | ||
Property.prototype.map = function(/*fn[, context[, arg1, arg2, ...]]*/) { | ||
return new Kefir.MappedProperty(this, arguments); | ||
} | ||
@@ -604,12 +718,25 @@ | ||
// property.changes() | ||
Property.prototype.changes = function() { | ||
return new Kefir.MappedStream(this); | ||
} | ||
// .diff(seed, fn) | ||
Observable.prototype.diff = function(prev, fn) { | ||
return this.map(function(x){ | ||
var result = fn(prev, x); | ||
prev = x; | ||
return result; | ||
}) | ||
var diffMapFn = function(x){ | ||
var result = callFn(this.fnMeta, [this.prev, x]); | ||
this.prev = x; | ||
return result; | ||
} | ||
Observable.prototype.diff = function(start/*fn[, context[, arg1, arg2, ...]]*/) { | ||
return this.map(diffMapFn, { | ||
prev: start, | ||
fnMeta: normFnMeta(restArgs(arguments, 1)) | ||
}); | ||
} | ||
@@ -619,45 +746,52 @@ | ||
// .filter(fn) | ||
Observable.prototype.filter = function(fn) { | ||
return this.map(function(x){ | ||
if (fn(x)) { | ||
return x; | ||
} else { | ||
return NOTHING; | ||
} | ||
}) | ||
var filterMapFn = function(filterFnMeta, x){ | ||
if (callFn(filterFnMeta, [x])) { | ||
return x; | ||
} else { | ||
return NOTHING; | ||
} | ||
} | ||
Observable.prototype.filter = function(/*fn[, context[, arg1, arg2, ...]]*/) { | ||
return this.map(filterMapFn, null, normFnMeta(arguments)); | ||
} | ||
// .takeWhile(fn) | ||
Observable.prototype.takeWhile = function(fn) { | ||
return this.map(function(x){ | ||
if (fn(x)) { | ||
return x; | ||
} else { | ||
return END; | ||
} | ||
}) | ||
var takeWhileMapFn = function(fnMeta, x) { | ||
if (callFn(fnMeta, [x])) { | ||
return x; | ||
} else { | ||
return END; | ||
} | ||
} | ||
Observable.prototype.takeWhile = function(/*fn[, context[, arg1, arg2, ...]]*/) { | ||
return this.map(takeWhileMapFn, null, normFnMeta(arguments)); | ||
} | ||
// .take(n) | ||
var takeMapFn = function(x) { | ||
if (this.n <= 0) { | ||
return END; | ||
} | ||
if (this.n === 1) { | ||
return Kefir.bunch(x, END); | ||
} | ||
this.n--; | ||
return x; | ||
} | ||
Observable.prototype.take = function(n) { | ||
return this.map(function(x){ | ||
if (n <= 0) { | ||
return END; | ||
} | ||
if (n === 1) { | ||
return Kefir.bunch(x, END); | ||
} | ||
n--; | ||
return x; | ||
}) | ||
return this.map(takeMapFn, {n: n}); | ||
} | ||
@@ -670,11 +804,13 @@ | ||
var skipMapFn = function(x) { | ||
if (this.n <= 0) { | ||
return x; | ||
} else { | ||
this.n--; | ||
return NOTHING; | ||
} | ||
} | ||
Observable.prototype.skip = function(n) { | ||
return this.map(function(x){ | ||
if (n <= 0) { | ||
return x; | ||
} else { | ||
n--; | ||
return Kefir.NOTHING; | ||
} | ||
}) | ||
return this.map(skipMapFn, {n: n}); | ||
} | ||
@@ -688,15 +824,16 @@ | ||
var skipDuplicatesMapFn = function(x){ | ||
var result; | ||
if (this.prev !== NOTHING && (this.fn ? this.fn(this.prev, x) : this.prev === x)) { | ||
result = NOTHING; | ||
} else { | ||
result = x; | ||
} | ||
this.hasPrev = true; | ||
this.prev = x; | ||
return result; | ||
} | ||
Observable.prototype.skipDuplicates = function(fn) { | ||
var prev, hasPrev = false; | ||
return this.map(function(x){ | ||
var result; | ||
if (hasPrev && (fn ? fn(prev, x) : prev === x)) { | ||
result = Kefir.NOTHING; | ||
} else { | ||
result = x; | ||
} | ||
hasPrev = true; | ||
prev = x; | ||
return result; | ||
}) | ||
return this.map(skipDuplicatesMapFn, {fn: fn, prev: NOTHING}); | ||
} | ||
@@ -710,17 +847,102 @@ | ||
Observable.prototype.skipWhile = function(fn) { | ||
var skip = true; | ||
return this.map(function(x){ | ||
if (skip && fn(x)) { | ||
return Kefir.NOTHING; | ||
var skipWhileMapFn = function(x){ | ||
if (this.skip && callFn(this.fnMeta, [x])) { | ||
return NOTHING; | ||
} else { | ||
this.skip = false; | ||
return x; | ||
} | ||
} | ||
Observable.prototype.skipWhile = function(/*fn[, context[, arg1, arg2, ...]]*/) { | ||
return this.map(skipWhileMapFn, {skip: true, fnMeta: normFnMeta(arguments)}); | ||
} | ||
// TODO | ||
// | ||
// observable.filter(property) | ||
// observable.takeWhile(property) | ||
// observable.skipWhile(property) | ||
// .sampledBy(observable, fn) | ||
var SampledByMixin = { | ||
__Constructor: function(main, sampler, fnMeta){ | ||
if (this instanceof Property) { | ||
Property.call(this); | ||
} else { | ||
skip = false; | ||
return x; | ||
Stream.call(this); | ||
} | ||
}) | ||
WithSourceStreamMixin.__Constructor.call(this, sampler); | ||
this.__lastValue = NOTHING; | ||
this.__fnMeta = normFnMeta(fnMeta); | ||
this.__mainStream = main; | ||
}, | ||
__handle: function(y){ | ||
if (this.__lastValue !== NOTHING) { | ||
var x = this.__lastValue; | ||
if (this.__fnMeta) { | ||
x = callFn(this.__fnMeta, [x, y]); | ||
} | ||
this.__sendValue(x); | ||
} | ||
}, | ||
__onFirstIn: function(){ | ||
WithSourceStreamMixin.__onFirstIn.call(this); | ||
this.__mainStream.onValue('__saveValue', this); | ||
this.__mainStream.onError('__sendError', this); | ||
}, | ||
__onLastOut: function(){ | ||
WithSourceStreamMixin.__onLastOut.call(this); | ||
this.__mainStream.offValue('__saveValue', this); | ||
this.__mainStream.offError('__sendError', this); | ||
}, | ||
__saveValue: function(x){ | ||
this.__lastValue = x; | ||
}, | ||
__clear: function(){ | ||
WithSourceStreamMixin.__clear.call(this); | ||
this.__lastValue = null; | ||
this.__fn = null; | ||
this.__mainStream = null; | ||
} | ||
} | ||
inheritMixin(SampledByMixin, WithSourceStreamMixin); | ||
Kefir.SampledByStream = function SampledByStream(){ | ||
this.__Constructor.apply(this, arguments); | ||
} | ||
inherit(Kefir.SampledByStream, Stream, SampledByMixin, { | ||
__ClassName: 'SampledByStream' | ||
}) | ||
Kefir.SampledByProperty = function SampledByProperty(){ | ||
this.__Constructor.apply(this, arguments); | ||
} | ||
inherit(Kefir.SampledByProperty, Property, SampledByMixin, { | ||
__ClassName: 'SampledByProperty' | ||
}) | ||
Observable.prototype.sampledBy = function(observable/*fn[, context[, arg1, arg2, ...]]*/) { | ||
if (observable instanceof Stream) { | ||
return new Kefir.SampledByStream(this, observable, restArgs(arguments, 1)); | ||
} else { | ||
return new Kefir.SampledByProperty(this, observable, restArgs(arguments, 1)); | ||
} | ||
} | ||
// TODO | ||
// | ||
// observable.flatMapLatest(f) | ||
// observable.flatMapFirst(f) | ||
@@ -733,4 +955,2 @@ // | ||
// stream.concat(otherStream) | ||
// | ||
// Kefir.onValues(a, b [, c...], f) | ||
@@ -756,5 +976,6 @@ | ||
if (this.__hasSubscribers('value')) { | ||
stream.onValue(this.__handlePlugged, this, i); | ||
stream.onValue('__handlePlugged', this, i); | ||
stream.onError('__sendError', this); | ||
} | ||
stream.onEnd(this.__unplugById, this, i); | ||
stream.onEnd('__unplugById', this, i); | ||
} | ||
@@ -767,4 +988,5 @@ }, | ||
this.__plugged[i] = null; | ||
stream.offValue(this.__handlePlugged, this, i); | ||
stream.onEnd(this.__unplugById, this, i); | ||
stream.offValue('__handlePlugged', this, i); | ||
stream.offError('__sendError', this); | ||
stream.offEnd('__unplugById', this, i); | ||
} | ||
@@ -786,3 +1008,4 @@ } | ||
if (stream) { | ||
stream.onValue(this.__handlePlugged, this, i); | ||
stream.onValue('__handlePlugged', this, i); | ||
stream.onError('__sendError', this); | ||
} | ||
@@ -795,3 +1018,4 @@ } | ||
if (stream) { | ||
stream.offValue(this.__handlePlugged, this, i); | ||
stream.offValue('__handlePlugged', this, i); | ||
stream.offError('__sendError', this); | ||
} | ||
@@ -831,11 +1055,19 @@ } | ||
this.__sendAny(x); | ||
return this; | ||
}, | ||
error: function(e){ | ||
this.__sendError(e); | ||
return this; | ||
}, | ||
plug: function(stream){ | ||
this.__plug(stream); | ||
return this; | ||
}, | ||
unplug: function(stream){ | ||
this.__unplug(stream); | ||
return this; | ||
}, | ||
end: function(){ | ||
this.__sendEnd(); | ||
return this; | ||
}, | ||
@@ -860,7 +1092,7 @@ __clear: function(){ | ||
Kefir.FlatMappedStream = function FlatMappedStream(sourceStream, mapFn){ | ||
Kefir.FlatMappedStream = function FlatMappedStream(sourceStream, mapFnMeta){ | ||
Stream.call(this); | ||
this.__initPluggable(); | ||
this.__sourceStream = sourceStream; | ||
this.__mapFn = mapFn; | ||
this.__mapFnMeta = normFnMeta(mapFnMeta); | ||
sourceStream.onEnd(this.__onSourceEnds, this); | ||
@@ -879,10 +1111,12 @@ } | ||
__plugResult: function(x){ | ||
this.__plug( this.__mapFn(x) ); | ||
this.__plug( callFn(this.__mapFnMeta, [x]) ); | ||
}, | ||
__onFirstIn: function(){ | ||
this.__sourceStream.onValue(this.__plugResult, this); | ||
this.__sourceStream.onValue('__plugResult', this); | ||
this.__sourceStream.onError('__sendError', this); | ||
PluggableMixin.__onFirstIn.call(this); | ||
}, | ||
__onLastOut: function(){ | ||
this.__sourceStream.offValue(this.__plugResult, this); | ||
this.__sourceStream.offValue('__plugResult', this); | ||
this.__sourceStream.offError('__sendError', this); | ||
PluggableMixin.__onLastOut.call(this); | ||
@@ -900,3 +1134,3 @@ }, | ||
this.__sourceStream = null; | ||
this.__mapFn = null; | ||
this.__mapFnMeta = null; | ||
} | ||
@@ -906,4 +1140,4 @@ | ||
Observable.prototype.flatMap = function(fn) { | ||
return new Kefir.FlatMappedStream(this, fn); | ||
Observable.prototype.flatMap = function(/*fn[, context[, arg1, arg2, ...]]*/) { | ||
return new Kefir.FlatMappedStream(this, arguments); | ||
}; | ||
@@ -914,6 +1148,28 @@ | ||
// FlatMapLatest | ||
Kefir.FlatMapLatestStream = function FlatMapLatestStream(){ | ||
Kefir.FlatMappedStream.apply(this, arguments); | ||
} | ||
inherit(Kefir.FlatMapLatestStream, Kefir.FlatMappedStream, { | ||
__ClassName: 'FlatMapLatestStream', | ||
__plugResult: function(x){ | ||
for (var i = 0; i < this.__plugged.length; i++) { | ||
this.__unplugById(i); | ||
} | ||
Kefir.FlatMappedStream.prototype.__plugResult.call(this, x); | ||
} | ||
}) | ||
Observable.prototype.flatMapLatest = function(/*fn[, context[, arg1, arg2, ...]]*/) { | ||
return new Kefir.FlatMapLatestStream(this, arguments); | ||
}; | ||
// Merge | ||
@@ -965,3 +1221,3 @@ | ||
Kefir.CombinedStream = function CombinedStream(sources, mapFn){ | ||
Kefir.CombinedStream = function CombinedStream(sources, mapFnMeta){ | ||
Stream.call(this); | ||
@@ -973,4 +1229,4 @@ this.__initPluggable(); | ||
this.__cachedValues = new Array(sources.length); | ||
this.__hasCached = new Array(sources.length); | ||
this.__mapFn = mapFn; | ||
this.__hasValue = new Array(sources.length); | ||
this.__mapFnMeta = normFnMeta(mapFnMeta); | ||
} | ||
@@ -989,7 +1245,7 @@ | ||
__handlePlugged: function(i, x) { | ||
this.__hasCached[i] = true; | ||
this.__hasValue[i] = true; | ||
this.__cachedValues[i] = x; | ||
if (this.__allCached()) { | ||
if (isFn(this.__mapFn)) { | ||
this.__sendAny(this.__mapFn.apply(null, this.__cachedValues)); | ||
if (this.__mapFnMeta) { | ||
this.__sendAny(callFn(this.__mapFnMeta, this.__cachedValues)); | ||
} else { | ||
@@ -1001,4 +1257,4 @@ this.__sendValue(this.__cachedValues.slice(0)); | ||
__allCached: function(){ | ||
for (var i = 0; i < this.__hasCached.length; i++) { | ||
if (!this.__hasCached[i]) { | ||
for (var i = 0; i < this.__hasValue.length; i++) { | ||
if (!this.__hasValue[i]) { | ||
return false; | ||
@@ -1013,4 +1269,4 @@ } | ||
this.__cachedValues = null; | ||
this.__hasCached = null; | ||
this.__mapFn = null; | ||
this.__hasValue = null; | ||
this.__mapFnMeta = null; | ||
} | ||
@@ -1020,13 +1276,25 @@ | ||
Kefir.combine = function(sources, mapFn) { | ||
return new Kefir.CombinedStream(sources, mapFn); | ||
Kefir.combine = function(sources/*, fn[, context[, arg1, arg2, ...]]*/) { | ||
return new Kefir.CombinedStream(sources, restArgs(arguments, 1)); | ||
} | ||
Observable.prototype.combine = function(sources, mapFn) { | ||
return Kefir.combine([this].concat(sources), mapFn); | ||
Observable.prototype.combine = function(sources/*, fn[, context[, arg1, arg2, ...]]*/) { | ||
return new Kefir.CombinedStream([this].concat(sources), restArgs(arguments, 1)); | ||
} | ||
// Kefir.onValues() | ||
Kefir.onValues = function(streams/*, fn[, context[, arg1, agr2, ...]]*/){ | ||
var fnMeta = normFnMeta(restArgs(arguments, 1)) | ||
return Kefir.combine(streams).onValue(callFn, null, fnMeta); | ||
} | ||
// FromPoll | ||
var FromPollStream = Kefir.FromPollStream = function FromPollStream(interval, sourceFn){ | ||
var FromPollStream = Kefir.FromPollStream = function FromPollStream(interval, sourceFnMeta){ | ||
Stream.call(this); | ||
@@ -1036,3 +1304,4 @@ this.__interval = interval; | ||
var _this = this; | ||
this.__bindedSend = function(){ _this.__sendAny(sourceFn()) } | ||
sourceFnMeta = normFnMeta(sourceFnMeta); | ||
this.__bindedSend = function(){ _this.__sendAny(callFn(sourceFnMeta)) } | ||
} | ||
@@ -1059,4 +1328,4 @@ | ||
Kefir.fromPoll = function(interval, fn){ | ||
return new FromPollStream(interval, fn); | ||
Kefir.fromPoll = function(interval/*, fn[, context[, arg1, arg2, ...]]*/){ | ||
return new FromPollStream(interval, restArgs(arguments, 1)); | ||
} | ||
@@ -1069,3 +1338,3 @@ | ||
Kefir.interval = function(interval, x){ | ||
return new FromPollStream(interval, function(){ return x }); | ||
return new FromPollStream(interval, [id, null, x]); | ||
} | ||
@@ -1077,13 +1346,14 @@ | ||
var sequentiallyHelperFn = function(){ | ||
if (this.xs.length === 0) { | ||
return END; | ||
} | ||
if (this.xs.length === 1){ | ||
return Kefir.bunch(this.xs[0], END); | ||
} | ||
return this.xs.shift(); | ||
} | ||
Kefir.sequentially = function(interval, xs){ | ||
xs = xs.slice(0); | ||
return new FromPollStream(interval, function(){ | ||
if (xs.length === 0) { | ||
return END; | ||
} | ||
if (xs.length === 1){ | ||
return Kefir.bunch(xs[0], END); | ||
} | ||
return xs.shift(); | ||
}); | ||
return new FromPollStream(interval, [sequentiallyHelperFn, {xs: xs.slice(0)}]); | ||
} | ||
@@ -1095,7 +1365,9 @@ | ||
var repeatedlyHelperFn = function(){ | ||
this.i = (this.i + 1) % this.xs.length; | ||
return this.xs[this.i]; | ||
} | ||
Kefir.repeatedly = function(interval, xs){ | ||
var i = -1; | ||
return new FromPollStream(interval, function(){ | ||
return xs[++i % xs.length]; | ||
}); | ||
return new FromPollStream(interval, [repeatedlyHelperFn, {i: -1, xs: xs}]); | ||
} | ||
@@ -1102,0 +1374,0 @@ |
@@ -1,4 +0,4 @@ | ||
/*! kefir - 0.1.9 | ||
/*! kefir - 0.1.10 | ||
* 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){return a instanceof Array?a: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){a.prototype=e(b.prototype),a.prototype.constructor=a;for(var c=2;c<arguments.length;c++)f(a.prototype,arguments[c]);return a}function h(a,b){for(var d in b)!c(b,d)||d in a||(a[d]=b[d]);return a}function i(a){return"[object Array]"===Object.prototype.toString.call(a[0])?a[0]:d(a)}function j(a,b,c){return a.length>b?Array.prototype.slice.call(a,b):c?null:[]}function k(a,b){var c=a[1],e=a[2],f=j(a,3);return b&&(f=f.concat(d(b))),c.apply(e,f)}function l(a){return"function"==typeof a}function m(a,b){if(null==a&&null==b)return!0;if(null==a||null==b)return!1;if(a.length!==b.length)return!1;for(var c=0;c<a.length;c++)if(a[c]!==b[c])return!1;return!0}var n={},o=n.NOTHING=["<nothing>"],p=n.END=["<end>"],q=n.NO_MORE=["<no more>"];n.BunchOfValues=function(a){this.values=a},n.bunch=function(){return new n.BunchOfValues(i(arguments))};var r=n.Observable=function(a,b){l(a)&&(this.__onFirstIn=a),l(b)&&(this.__onLastOut=b),this.__subscribers=[]};g(r,Object,{__ClassName:"Observable",toString:function(){return"["+this.__ClassName+(this.__objName?" | "+this.__objName:"")+"]"},__onFirstIn:b,__onLastOut:b,__on:function(a){if(this.isEnded())"end"===a&&k(arguments);else{var b="value"===a&&!this.__hasSubscribers("value");this.__subscribers.push(arguments),b&&this.__onFirstIn()}},__off:function(a){if(!this.isEnded()){for(var b=0;b<this.__subscribers.length;b++)m(this.__subscribers[b],arguments)&&(this.__subscribers[b]=null);"value"!==a||this.__hasSubscribers("value")||this.__onLastOut()}},__send:function(a){if(!this.isEnded()){for(var b=0;b<this.__subscribers.length;b++){var c=this.__subscribers[b];if(c&&c[0]===a){var d=k(c,j(arguments,1));d===q&&this.__off.apply(this,c)}}"end"===a&&this.__clear()}},__hasSubscribers:function(a){if(this.isEnded())return!1;for(var b=0;b<this.__subscribers.length;b++)if(this.__subscribers[b]&&this.__subscribers[b][0]===a)return!0;return!1},__clear:function(){this.__onLastOut(),c(this,"__onFirstIn")&&(this.__onFirstIn=null),c(this,"__onLastOut")&&(this.__onLastOut=null),this.__subscribers=null},__sendValue:function(a){this.__send("value",a)},__sendEnd:function(){this.__send("end")},__sendAny:function(a){if(a===p)this.__sendEnd();else if(a instanceof n.BunchOfValues)for(var b=0;b<a.values.length;b++)this.__sendAny(a.values[b]);else a!==n.NOTHING&&this.__sendValue(a)},onValue:function(){this.__on.apply(this,["value"].concat(d(arguments)))},offValue:function(){this.__off.apply(this,["value"].concat(d(arguments)))},onEnd:function(){this.__on.apply(this,["end"].concat(d(arguments)))},offEnd:function(){this.__off.apply(this,["end"].concat(d(arguments)))},onNewValue:function(){this.onValue.apply(this,arguments)},isEnded:function(){return!this.__subscribers}});var s=n.Stream=function(){r.apply(this,arguments)};g(s,r,{__ClassName:"Stream"});var t=n.Property=function(a,b,c){r.call(this,a,b),this.__cached="undefined"!=typeof c?c:n.NOTHING};g(t,r,{__ClassName:"Property",hasCached:function(){return this.__cached!==n.NOTHING},getCached:function(){return this.__cached},__sendValue:function(a){this.isEnded()||(this.__cached=a),r.prototype.__sendValue.call(this,a)},onNewValue:function(){this.__on.apply(this,["value"].concat(d(arguments)))},onValue:function(){this.hasCached()&&k(["value"].concat(d(arguments)),[this.__cached]),this.onNewValue.apply(this,arguments)}}),r.prototype.log=function(a){function b(b){console.log(a,b)}a||(a=this.toString()),this.onValue(b),this.onEnd(function(){b(p)})};var u=new s;u.__sendEnd(),u.__objName="Kefir.never()",n.never=function(){return u},n.OnceStream=function(a){s.call(this),this.__value=a},g(n.OnceStream,s,{__ClassName:"OnceStream",onValue:function(){this.isEnded()||(k(["value"].concat(d(arguments)),[this.__value]),this.__value=null,this.__sendEnd())}}),n.once=function(a){return new n.OnceStream(a)},n.FromBinderStream=function(a){s.call(this),this.__subscribe=a},g(n.FromBinderStream,s,{__ClassName:"FromBinderStream",__onFirstIn:function(){var a=this;this.__usubscriber=this.__subscribe(function(b){a.__sendAny(b)})},__onLastOut:function(){l(this.__usubscriber)&&this.__usubscriber(),this.__usubscriber=null},__clear:function(){s.prototype.__clear.call(this),this.__subscribe=null}}),n.fromBinder=function(a){return new n.FromBinderStream(a)};var v={__Constructor:function(a){this.__source=a,a.onEnd(this.__sendEnd,this),a instanceof t&&this instanceof t&&a.hasCached()&&this.__handle(a.getCached())},__handle:function(a){this.__sendAny(a)},__onFirstIn:function(){this.__source.onNewValue(this.__handle,this)},__onLastOut:function(){this.__source.offValue(this.__handle,this)},__clear:function(){r.prototype.__clear.call(this),this.__source=null}};n.PropertyFromStream=function(a,b){t.call(this,null,null,b),this.__Constructor(a)},g(n.PropertyFromStream,t,v,{__ClassName:"PropertyFromStream"}),s.prototype.toProperty=function(a){return new n.PropertyFromStream(this,a)},t.prototype.toProperty=function(a){if("undefined"==typeof a)return this;var b=new n.PropertyFromStream(this);return b.__sendValue(a),b},n.ChangesStream=function(a){s.call(this),this.__Constructor(a)},g(n.ChangesStream,s,v,{__ClassName:"ChangesStream"}),t.prototype.changes=function(){return new n.ChangesStream(this)},n.ScanProperty=function(a,b,c){t.call(this,null,null,b),this.__fn=c,this.__Constructor(a)},g(n.ScanProperty,t,v,{__ClassName:"ScanProperty",__handle:function(a){this.__sendValue(this.__fn(this.getCached(),a))},__clear:function(){v.__clear.call(this),this.__fn=null}}),r.prototype.scan=function(a,b){return new n.ScanProperty(this,a,b)};var w={__Constructor:function(a,b){a instanceof t?t.call(this):s.call(this),this.__mapFn=b,v.__Constructor.call(this,a)},__handle:function(a){this.__sendAny(this.__mapFn(a))},__clear:function(){v.__clear.call(this),this.__mapFn=null}};h(w,v),n.MappedStream=function(){this.__Constructor.apply(this,arguments)},g(n.MappedStream,s,w,{__ClassName:"MappedStream"}),n.MappedProperty=function(){this.__Constructor.apply(this,arguments)},g(n.MappedProperty,t,w,{__ClassName:"MappedProperty"}),s.prototype.map=function(a){return new n.MappedStream(this,a)},t.prototype.map=function(a){return new n.MappedProperty(this,a)},r.prototype.diff=function(a,b){return this.map(function(c){var d=b(a,c);return a=c,d})},r.prototype.filter=function(a){return this.map(function(b){return a(b)?b:o})},r.prototype.takeWhile=function(a){return this.map(function(b){return a(b)?b:p})},r.prototype.take=function(a){return this.map(function(b){return 0>=a?p:1===a?n.bunch(b,p):(a--,b)})},r.prototype.skip=function(a){return this.map(function(b){return 0>=a?b:(a--,n.NOTHING)})},r.prototype.skipDuplicates=function(a){var b,c=!1;return this.map(function(d){var e;return e=c&&(a?a(b,d):b===d)?n.NOTHING:d,c=!0,b=d,e})},r.prototype.skipWhile=function(a){var b=!0;return this.map(function(c){return b&&a(c)?n.NOTHING:(b=!1,c)})};var x={__initPluggable:function(){this.__plugged=[]},__clearPluggable:function(){this.__plugged=null},__handlePlugged:function(a,b){this.__sendAny(b)},__plug:function(a){if(!this.isEnded()){this.__plugged.push(a);var b=this.__plugged.length-1;this.__hasSubscribers("value")&&a.onValue(this.__handlePlugged,this,b),a.onEnd(this.__unplugById,this,b)}},__unplugById:function(a){if(!this.isEnded()){var b=this.__plugged[a];b&&(this.__plugged[a]=null,b.offValue(this.__handlePlugged,this,a),b.onEnd(this.__unplugById,this,a))}},__unplug:function(a){if(!this.isEnded())for(var b=0;b<this.__plugged.length;b++)this.__plugged[b]===a&&this.__unplugById(b)},__onFirstIn:function(){for(var a=0;a<this.__plugged.length;a++){var b=this.__plugged[a];b&&b.onValue(this.__handlePlugged,this,a)}},__onLastOut:function(){for(var a=0;a<this.__plugged.length;a++){var b=this.__plugged[a];b&&b.offValue(this.__handlePlugged,this,a)}},__hasNoPlugged:function(){if(this.isEnded())return!0;for(var a=0;a<this.__plugged.length;a++)if(this.__plugged[a])return!1;return!0}};n.Bus=function(){s.call(this),this.__initPluggable()},g(n.Bus,s,x,{__ClassName:"Bus",push:function(a){this.__sendAny(a)},plug:function(a){this.__plug(a)},unplug:function(a){this.__unplug(a)},end:function(){this.__sendEnd()},__clear:function(){s.prototype.__clear.call(this),this.__clearPluggable(),this.push=b}}),n.bus=function(){return new n.Bus},n.FlatMappedStream=function(a,b){s.call(this),this.__initPluggable(),this.__sourceStream=a,this.__mapFn=b,a.onEnd(this.__onSourceEnds,this)},g(n.FlatMappedStream,s,x,{__ClassName:"FlatMappedStream",__onSourceEnds:function(){this.__hasNoPlugged()&&this.__sendEnd()},__plugResult:function(a){this.__plug(this.__mapFn(a))},__onFirstIn:function(){this.__sourceStream.onValue(this.__plugResult,this),x.__onFirstIn.call(this)},__onLastOut:function(){this.__sourceStream.offValue(this.__plugResult,this),x.__onLastOut.call(this)},__unplugById:function(a){x.__unplugById.call(this,a),!this.isEnded()&&this.__hasNoPlugged()&&this.__sourceStream.isEnded()&&this.__sendEnd()},__clear:function(){s.prototype.__clear.call(this),this.__clearPluggable(),this.__sourceStream=null,this.__mapFn=null}}),r.prototype.flatMap=function(a){return new n.FlatMappedStream(this,a)},n.MergedStream=function(){s.call(this),this.__initPluggable();for(var a=i(arguments),b=0;b<a.length;b++)this.__plug(a[b])},g(n.MergedStream,s,x,{__ClassName:"MergedStream",__clear:function(){s.prototype.__clear.call(this),this.__clearPluggable()},__unplugById:function(a){x.__unplugById.call(this,a),this.__hasNoPlugged()&&this.__sendEnd()}}),n.merge=function(){return new n.MergedStream(i(arguments))},r.prototype.merge=function(){return n.merge([this].concat(i(arguments)))},n.CombinedStream=function(a,b){s.call(this),this.__initPluggable();for(var c=0;c<a.length;c++)this.__plug(a[c]);this.__cachedValues=new Array(a.length),this.__hasCached=new Array(a.length),this.__mapFn=b},g(n.CombinedStream,s,x,{__ClassName:"CombinedStream",__unplugById:function(a){x.__unplugById.call(this,a),this.__hasNoPlugged()&&this.__sendEnd()},__handlePlugged:function(a,b){this.__hasCached[a]=!0,this.__cachedValues[a]=b,this.__allCached()&&(l(this.__mapFn)?this.__sendAny(this.__mapFn.apply(null,this.__cachedValues)):this.__sendValue(this.__cachedValues.slice(0)))},__allCached:function(){for(var a=0;a<this.__hasCached.length;a++)if(!this.__hasCached[a])return!1;return!0},__clear:function(){s.prototype.__clear.call(this),this.__clearPluggable(),this.__cachedValues=null,this.__hasCached=null,this.__mapFn=null}}),n.combine=function(a,b){return new n.CombinedStream(a,b)},r.prototype.combine=function(a,b){return n.combine([this].concat(a),b)};var y=n.FromPollStream=function(a,b){s.call(this),this.__interval=a,this.__intervalId=null;var c=this;this.__bindedSend=function(){c.__sendAny(b())}};g(y,s,{__ClassName:"FromPollStream",__onFirstIn:function(){this.__intervalId=setInterval(this.__bindedSend,this.__interval)},__onLastOut:function(){null!==this.__intervalId&&(clearInterval(this.__intervalId),this.__intervalId=null)},__clear:function(){s.prototype.__clear.call(this),this.__bindedSend=null}}),n.fromPoll=function(a,b){return new y(a,b)},n.interval=function(a,b){return new y(a,function(){return b})},n.sequentially=function(a,b){return b=b.slice(0),new y(a,function(){return 0===b.length?p:1===b.length?n.bunch(b[0],p):b.shift()})},n.repeatedly=function(a,b){var c=-1;return new y(a,function(){return b[++c%b.length]})},"function"==typeof define&&define.amd?(define([],function(){return n}),a.Kefir=n):"object"==typeof module&&"object"==typeof exports?(module.exports=n,n.Kefir=n):a.Kefir=n}(this); | ||
!function(a){"use strict";function b(){}function c(a){return a}function d(a,b){return Object.prototype.hasOwnProperty.call(a,b)}function e(a){return p(a)?a:Array.prototype.slice.call(a)}function f(a){var b=function(){};return b.prototype=a,new b}function g(){if(1===arguments.length)return arguments[0];for(var a=arguments[0],b=1;b<arguments.length;b++)for(var c in arguments[b])d(arguments[b],c)&&(a[c]=arguments[b][c]);return a}function h(a,b){a.prototype=f(b.prototype),a.prototype.constructor=a;for(var c=2;c<arguments.length;c++)g(a.prototype,arguments[c]);return a}function i(a,b){for(var c in b)!d(b,c)||c in a||(a[c]=b[c]);return a}function j(a){return p(a[0])?a[0]:e(a)}function k(a,b,c){return a.length>b?Array.prototype.slice.call(a,b):c?null:[]}function l(a,b){var c,d,f;if(n(a)?(c=a,d=null,f=null):(c=a[0],d=a[1],f=k(a,2,!0),n(c)||null==d||(c=d[c])),b&&(f=f?f.concat(e(b)):b),n(c))return f?c.apply(d,f):c.call(d);throw new Error("not a function "+c)}function m(a){if(p(a)||r(a)){if(1===a.length)return a[0];if(0===a.length)return null}return a}function n(a){return"function"==typeof a}function o(a){return"undefined"==typeof a}function p(a){return"[object Array]"===Object.prototype.toString.call(a)}function q(a,b){if(null==a&&null==b)return!0;if(null==a||null==b)return!1;if(a.length!==b.length)return!1;for(var c=0;c<a.length;c++)if(a[c]!==b[c])return!1;return!0}var r=function(a){return"[object Arguments]"===Object.prototype.toString.call(a)};r(arguments)||(r=function(a){return!(!a||!d(a,"callee"))});var s={},t=s.NOTHING=["<nothing>"],u=s.END=["<end>"],v=s.NO_MORE=["<no more>"];s.BunchOfValues=function(a){this.values=a},s.bunch=function(){return new s.BunchOfValues(j(arguments))},s.Error=function(a){this.error=a},s.error=function(a){return new s.Error(a)};var w=s.Observable=function(a,b){n(a)&&(this.__onFirstIn=a),n(b)&&(this.__onLastOut=b),this.__subscribers=[]};h(w,Object,{__ClassName:"Observable",toString:function(){return"["+this.__ClassName+(this.__objName?" | "+this.__objName:"")+"]"},__onFirstIn:b,__onLastOut:b,__on:function(a){if(this.isEnded())"end"===a&&l(k(arguments,1));else{var b=("value"===a||"error"===a)&&!(this.__hasSubscribers("value")||this.__hasSubscribers("error"));this.__subscribers.push(arguments),b&&this.__onFirstIn()}},__off:function(a){if(!this.isEnded()){for(var b=0;b<this.__subscribers.length;b++)q(this.__subscribers[b],arguments)&&(this.__subscribers[b]=null);"value"!==a&&"error"!==a||this.__hasSubscribers("value")||this.__hasSubscribers("error")||this.__onLastOut()}},__send:function(a){if(!this.isEnded()){for(var b=0;b<this.__subscribers.length;b++){var c=this.__subscribers[b];if(c&&c[0]===a){var d=l(k(c,1),k(arguments,1));d===v&&this.__off.apply(this,c)}}"end"===a&&this.__clear()}},__hasSubscribers:function(a){if(this.isEnded())return!1;for(var b=0;b<this.__subscribers.length;b++)if(this.__subscribers[b]&&this.__subscribers[b][0]===a)return!0;return!1},__clear:function(){this.__onLastOut(),d(this,"__onFirstIn")&&(this.__onFirstIn=null),d(this,"__onLastOut")&&(this.__onLastOut=null),this.__subscribers=null},__sendValue:function(a){return this.__send("value",a),this},__sendError:function(a){return this.__send("error",a),this},__sendEnd:function(){return this.__send("end"),this},__sendAny:function(a){if(a===u)this.__sendEnd();else if(a instanceof s.BunchOfValues)for(var b=0;b<a.values.length;b++)this.__sendAny(a.values[b]);else a instanceof s.Error?this.__sendError(a.error):a!==t&&this.__sendValue(a);return this},onValue:function(){return this.__on.apply(this,["value"].concat(e(arguments))),this},offValue:function(){return this.__off.apply(this,["value"].concat(e(arguments))),this},onError:function(){return this.__on.apply(this,["error"].concat(e(arguments))),this},offError:function(){return this.__off.apply(this,["error"].concat(e(arguments))),this},onEnd:function(){return this.__on.apply(this,["end"].concat(e(arguments))),this},offEnd:function(){return this.__off.apply(this,["end"].concat(e(arguments))),this},onNewValue:function(){return this.onValue.apply(this,arguments)},isEnded:function(){return!this.__subscribers}});var x=s.Stream=function(){w.apply(this,arguments)};h(x,w,{__ClassName:"Stream"});var y=s.Property=function(a,b,c){w.call(this,a,b),this.__cached=o(c)?t:c};h(y,w,{__ClassName:"Property",hasValue:function(){return this.__cached!==t},getValue:function(){return this.__cached},__sendValue:function(a){this.isEnded()||(this.__cached=a),w.prototype.__sendValue.call(this,a)},onNewValue:function(){return this.__on.apply(this,["value"].concat(e(arguments))),this},onValue:function(){return this.hasValue()&&l(arguments,[this.__cached]),this.onNewValue.apply(this,arguments)}});var z=function(a,b,c){console.log(a,b,c)};w.prototype.log=function(a){return a||(a=this.toString()),this.onValue(z,null,a,"<value>"),this.onError(z,null,a,"<error>"),this.onEnd(z,null,a,"<end>"),this};var A=new x;A.__sendEnd(),A.__objName="Kefir.never()",s.never=function(){return A},s.OnceStream=function(a){x.call(this),this.__value=a},h(s.OnceStream,x,{__ClassName:"OnceStream",onValue:function(){return this.isEnded()||(l(arguments,[this.__value]),this.__value=null,this.__sendEnd()),this},onError:b}),s.once=function(a){return new s.OnceStream(a)},s.FromBinderStream=function(a){x.call(this),this.__subscribe=a},h(s.FromBinderStream,x,{__ClassName:"FromBinderStream",__onFirstIn:function(){var a=this;this.__usubscriber=this.__subscribe(function(b){a.__sendAny(b)})},__onLastOut:function(){n(this.__usubscriber)&&this.__usubscriber(),this.__usubscriber=null},__clear:function(){x.prototype.__clear.call(this),this.__subscribe=null}}),s.fromBinder=function(a){return new s.FromBinderStream(a)};var B={__Constructor:function(a){this.__source=a,a.onEnd(this.__sendEnd,this),a instanceof y&&this instanceof y&&a.hasValue()&&this.__handle(a.getValue())},__handle:function(a){this.__sendAny(a)},__onFirstIn:function(){this.__source.onNewValue("__handle",this),this.__source.onError("__sendError",this)},__onLastOut:function(){this.__source.offValue("__handle",this),this.__source.offError("__sendError",this)},__clear:function(){w.prototype.__clear.call(this),this.__source=null}};s.PropertyFromStream=function(a,b){y.call(this,null,null,b),this.__Constructor(a)},h(s.PropertyFromStream,y,B,{__ClassName:"PropertyFromStream"}),x.prototype.toProperty=function(a){return new s.PropertyFromStream(this,a)},y.prototype.toProperty=function(a){if(o(a))return this;var b=new s.PropertyFromStream(this);return b.__sendValue(a),b},s.ScanProperty=function(a,b,c){y.call(this,null,null,b),this.__fnMeta=m(c),this.__Constructor(a)},h(s.ScanProperty,y,B,{__ClassName:"ScanProperty",__handle:function(a){this.__sendValue(l(this.__fnMeta,[this.getValue(),a]))},__clear:function(){B.__clear.call(this),this.__fnMeta=null}}),w.prototype.scan=function(a){return new s.ScanProperty(this,a,k(arguments,1))},s.ReducedProperty=function(a,b,c){y.call(this),this.__fnMeta=m(c),this.__result=b,a.onEnd("__sendResult",this),this.__Constructor(a)},h(s.ReducedProperty,y,B,{__ClassName:"ReducedProperty",__handle:function(a){this.__result=l(this.__fnMeta,[this.__result,a])},__sendResult:function(){this.__sendValue(this.__result)},__clear:function(){B.__clear.call(this),this.__fnMeta=null,this.__result=null}}),w.prototype.reduce=function(a){return new s.ReducedProperty(this,a,k(arguments,1))};var C={__Constructor:function(a,b){this instanceof y?y.call(this):x.call(this),this.__mapFnMeta=m(b),B.__Constructor.call(this,a)},__handle:function(a){this.__sendAny(this.__mapFnMeta?l(this.__mapFnMeta,[a]):a)},__clear:function(){B.__clear.call(this),this.__mapFnMeta=null}};i(C,B),s.MappedStream=function(){this.__Constructor.apply(this,arguments)},h(s.MappedStream,x,C,{__ClassName:"MappedStream"}),s.MappedProperty=function(){this.__Constructor.apply(this,arguments)},h(s.MappedProperty,y,C,{__ClassName:"MappedProperty"}),x.prototype.map=function(){return new s.MappedStream(this,arguments)},y.prototype.map=function(){return new s.MappedProperty(this,arguments)},y.prototype.changes=function(){return new s.MappedStream(this)};var D=function(a){var b=l(this.fnMeta,[this.prev,a]);return this.prev=a,b};w.prototype.diff=function(a){return this.map(D,{prev:a,fnMeta:m(k(arguments,1))})};var E=function(a,b){return l(a,[b])?b:t};w.prototype.filter=function(){return this.map(E,null,m(arguments))};var F=function(a,b){return l(a,[b])?b:u};w.prototype.takeWhile=function(){return this.map(F,null,m(arguments))};var G=function(a){return this.n<=0?u:1===this.n?s.bunch(a,u):(this.n--,a)};w.prototype.take=function(a){return this.map(G,{n:a})};var H=function(a){return this.n<=0?a:(this.n--,t)};w.prototype.skip=function(a){return this.map(H,{n:a})};var I=function(a){var b;return b=this.prev!==t&&(this.fn?this.fn(this.prev,a):this.prev===a)?t:a,this.hasPrev=!0,this.prev=a,b};w.prototype.skipDuplicates=function(a){return this.map(I,{fn:a,prev:t})};var J=function(a){return this.skip&&l(this.fnMeta,[a])?t:(this.skip=!1,a)};w.prototype.skipWhile=function(){return this.map(J,{skip:!0,fnMeta:m(arguments)})};var K={__Constructor:function(a,b,c){this instanceof y?y.call(this):x.call(this),B.__Constructor.call(this,b),this.__lastValue=t,this.__fnMeta=m(c),this.__mainStream=a},__handle:function(a){if(this.__lastValue!==t){var b=this.__lastValue;this.__fnMeta&&(b=l(this.__fnMeta,[b,a])),this.__sendValue(b)}},__onFirstIn:function(){B.__onFirstIn.call(this),this.__mainStream.onValue("__saveValue",this),this.__mainStream.onError("__sendError",this)},__onLastOut:function(){B.__onLastOut.call(this),this.__mainStream.offValue("__saveValue",this),this.__mainStream.offError("__sendError",this)},__saveValue:function(a){this.__lastValue=a},__clear:function(){B.__clear.call(this),this.__lastValue=null,this.__fn=null,this.__mainStream=null}};i(K,B),s.SampledByStream=function(){this.__Constructor.apply(this,arguments)},h(s.SampledByStream,x,K,{__ClassName:"SampledByStream"}),s.SampledByProperty=function(){this.__Constructor.apply(this,arguments)},h(s.SampledByProperty,y,K,{__ClassName:"SampledByProperty"}),w.prototype.sampledBy=function(a){return a instanceof x?new s.SampledByStream(this,a,k(arguments,1)):new s.SampledByProperty(this,a,k(arguments,1))};var L={__initPluggable:function(){this.__plugged=[]},__clearPluggable:function(){this.__plugged=null},__handlePlugged:function(a,b){this.__sendAny(b)},__plug:function(a){if(!this.isEnded()){this.__plugged.push(a);var b=this.__plugged.length-1;this.__hasSubscribers("value")&&(a.onValue("__handlePlugged",this,b),a.onError("__sendError",this)),a.onEnd("__unplugById",this,b)}},__unplugById:function(a){if(!this.isEnded()){var b=this.__plugged[a];b&&(this.__plugged[a]=null,b.offValue("__handlePlugged",this,a),b.offError("__sendError",this),b.offEnd("__unplugById",this,a))}},__unplug:function(a){if(!this.isEnded())for(var b=0;b<this.__plugged.length;b++)this.__plugged[b]===a&&this.__unplugById(b)},__onFirstIn:function(){for(var a=0;a<this.__plugged.length;a++){var b=this.__plugged[a];b&&(b.onValue("__handlePlugged",this,a),b.onError("__sendError",this))}},__onLastOut:function(){for(var a=0;a<this.__plugged.length;a++){var b=this.__plugged[a];b&&(b.offValue("__handlePlugged",this,a),b.offError("__sendError",this))}},__hasNoPlugged:function(){if(this.isEnded())return!0;for(var a=0;a<this.__plugged.length;a++)if(this.__plugged[a])return!1;return!0}};s.Bus=function(){x.call(this),this.__initPluggable()},h(s.Bus,x,L,{__ClassName:"Bus",push:function(a){return this.__sendAny(a),this},error:function(a){return this.__sendError(a),this},plug:function(a){return this.__plug(a),this},unplug:function(a){return this.__unplug(a),this},end:function(){return this.__sendEnd(),this},__clear:function(){x.prototype.__clear.call(this),this.__clearPluggable(),this.push=b}}),s.bus=function(){return new s.Bus},s.FlatMappedStream=function(a,b){x.call(this),this.__initPluggable(),this.__sourceStream=a,this.__mapFnMeta=m(b),a.onEnd(this.__onSourceEnds,this)},h(s.FlatMappedStream,x,L,{__ClassName:"FlatMappedStream",__onSourceEnds:function(){this.__hasNoPlugged()&&this.__sendEnd()},__plugResult:function(a){this.__plug(l(this.__mapFnMeta,[a]))},__onFirstIn:function(){this.__sourceStream.onValue("__plugResult",this),this.__sourceStream.onError("__sendError",this),L.__onFirstIn.call(this)},__onLastOut:function(){this.__sourceStream.offValue("__plugResult",this),this.__sourceStream.offError("__sendError",this),L.__onLastOut.call(this)},__unplugById:function(a){L.__unplugById.call(this,a),!this.isEnded()&&this.__hasNoPlugged()&&this.__sourceStream.isEnded()&&this.__sendEnd()},__clear:function(){x.prototype.__clear.call(this),this.__clearPluggable(),this.__sourceStream=null,this.__mapFnMeta=null}}),w.prototype.flatMap=function(){return new s.FlatMappedStream(this,arguments)},s.FlatMapLatestStream=function(){s.FlatMappedStream.apply(this,arguments)},h(s.FlatMapLatestStream,s.FlatMappedStream,{__ClassName:"FlatMapLatestStream",__plugResult:function(a){for(var b=0;b<this.__plugged.length;b++)this.__unplugById(b);s.FlatMappedStream.prototype.__plugResult.call(this,a)}}),w.prototype.flatMapLatest=function(){return new s.FlatMapLatestStream(this,arguments)},s.MergedStream=function(){x.call(this),this.__initPluggable();for(var a=j(arguments),b=0;b<a.length;b++)this.__plug(a[b])},h(s.MergedStream,x,L,{__ClassName:"MergedStream",__clear:function(){x.prototype.__clear.call(this),this.__clearPluggable()},__unplugById:function(a){L.__unplugById.call(this,a),this.__hasNoPlugged()&&this.__sendEnd()}}),s.merge=function(){return new s.MergedStream(j(arguments))},w.prototype.merge=function(){return s.merge([this].concat(j(arguments)))},s.CombinedStream=function(a,b){x.call(this),this.__initPluggable();for(var c=0;c<a.length;c++)this.__plug(a[c]);this.__cachedValues=new Array(a.length),this.__hasValue=new Array(a.length),this.__mapFnMeta=m(b)},h(s.CombinedStream,x,L,{__ClassName:"CombinedStream",__unplugById:function(a){L.__unplugById.call(this,a),this.__hasNoPlugged()&&this.__sendEnd()},__handlePlugged:function(a,b){this.__hasValue[a]=!0,this.__cachedValues[a]=b,this.__allCached()&&(this.__mapFnMeta?this.__sendAny(l(this.__mapFnMeta,this.__cachedValues)):this.__sendValue(this.__cachedValues.slice(0)))},__allCached:function(){for(var a=0;a<this.__hasValue.length;a++)if(!this.__hasValue[a])return!1;return!0},__clear:function(){x.prototype.__clear.call(this),this.__clearPluggable(),this.__cachedValues=null,this.__hasValue=null,this.__mapFnMeta=null}}),s.combine=function(a){return new s.CombinedStream(a,k(arguments,1))},w.prototype.combine=function(a){return new s.CombinedStream([this].concat(a),k(arguments,1))},s.onValues=function(a){var b=m(k(arguments,1));return s.combine(a).onValue(l,null,b)};var M=s.FromPollStream=function(a,b){x.call(this),this.__interval=a,this.__intervalId=null;var c=this;b=m(b),this.__bindedSend=function(){c.__sendAny(l(b))}};h(M,x,{__ClassName:"FromPollStream",__onFirstIn:function(){this.__intervalId=setInterval(this.__bindedSend,this.__interval)},__onLastOut:function(){null!==this.__intervalId&&(clearInterval(this.__intervalId),this.__intervalId=null)},__clear:function(){x.prototype.__clear.call(this),this.__bindedSend=null}}),s.fromPoll=function(a){return new M(a,k(arguments,1))},s.interval=function(a,b){return new M(a,[c,null,b])};var N=function(){return 0===this.xs.length?u:1===this.xs.length?s.bunch(this.xs[0],u):this.xs.shift()};s.sequentially=function(a,b){return new M(a,[N,{xs:b.slice(0)}])};var O=function(){return this.i=(this.i+1)%this.xs.length,this.xs[this.i]};s.repeatedly=function(a,b){return new M(a,[O,{i:-1,xs:b}])},"function"==typeof define&&define.amd?(define([],function(){return s}),a.Kefir=s):"object"==typeof module&&"object"==typeof exports?(module.exports=s,s.Kefir=s):a.Kefir=s}(this); |
Generated by run: node --expose-gc test-memory-usage.js > memory-usage-results.txt | ||
Please don't take this results too serious, I am not 100% sure about correctness of this tests | ||
Please don't take these results too serious, I am not 100% sure about correctness of these tests | ||
Also these tests do not measure amount of temporary allocated | ||
memory that causes frequent garbage collection. | ||
very base stream | ||
Kefir: w/o subscr. 0.68 Kb, w/ subscr. +0.22 Kb | ||
Rx: w/o subscr. 0.25 Kb, w/ subscr. +0.43 Kb | ||
Bacon: w/o subscr. 3.04 Kb, w/ subscr. +0.92 Kb | ||
Kefir: w/o subscr. 0.68 KiB, w/ subscr. +0.23 KiB | ||
Rx: w/o subscr. 0.25 KiB, w/ subscr. +0.43 KiB | ||
Bacon: w/o subscr. 3.04 KiB, w/ subscr. +0.92 KiB | ||
new Bus() | ||
Kefir: w/o subscr. 0.10 Kb, w/ subscr. +0.22 Kb | ||
Bacon: w/o subscr. 3.72 Kb, w/ subscr. +0.76 Kb | ||
Kefir: w/o subscr. 0.10 KiB, w/ subscr. +0.22 KiB | ||
Bacon: w/o subscr. 3.72 KiB, w/ subscr. +0.76 KiB | ||
.once(1) | ||
Kefir: w/o subscr. 0.07 Kb, w/ subscr. +0.04 Kb | ||
Bacon: w/o subscr. 4.09 Kb, w/ subscr. +0.95 Kb | ||
Kefir: w/o subscr. 0.07 KiB, w/ subscr. +0.04 KiB | ||
Bacon: w/o subscr. 4.09 KiB, w/ subscr. +0.95 KiB | ||
.never() | ||
Kefir: w/o subscr. -0.00 Kb, w/ subscr. -0.01 Kb | ||
Rx: w/o subscr. 0.23 Kb, w/ subscr. +0.00 Kb | ||
Bacon: w/o subscr. 4.04 Kb, w/ subscr. +0.89 Kb | ||
Kefir: w/o subscr. -0.00 KiB, w/ subscr. -0.00 KiB | ||
Rx: w/o subscr. 0.23 KiB, w/ subscr. -0.00 KiB | ||
Bacon: w/o subscr. 4.03 KiB, w/ subscr. +0.89 KiB | ||
.toProperty(1) | ||
Kefir: w/o subscr. 0.97 Kb, w/ subscr. +0.29 Kb | ||
Rx: w/o subscr. 1.22 Kb, w/ subscr. +1.18 Kb | ||
Bacon: w/o subscr. 7.88 Kb, w/ subscr. +1.92 Kb | ||
Kefir: w/o subscr. 0.97 KiB, w/ subscr. +0.36 KiB | ||
Rx: w/o subscr. 1.22 KiB, w/ subscr. +1.18 KiB | ||
Bacon: w/o subscr. 7.88 KiB, w/ subscr. +1.92 KiB | ||
.toProperty(1).changes() | ||
Kefir: w/o subscr. 1.25 Kb, w/ subscr. +0.38 Kb | ||
Bacon: w/o subscr. 10.80 Kb, w/ subscr. +2.44 Kb | ||
Kefir: w/o subscr. 1.27 KiB, w/ subscr. +0.52 KiB | ||
Bacon: w/o subscr. 10.80 KiB, w/ subscr. +2.44 KiB | ||
.map(noop) | ||
Kefir: w/o subscr. 0.96 Kb, w/ subscr. +0.28 Kb | ||
Rx: w/o subscr. 0.52 Kb, w/ subscr. +1.52 Kb | ||
Bacon: w/o subscr. 8.29 Kb, w/ subscr. +1.71 Kb | ||
Kefir: w/o subscr. 0.96 KiB, w/ subscr. +0.35 KiB | ||
Rx: w/o subscr. 0.52 KiB, w/ subscr. +1.52 KiB | ||
Bacon: w/o subscr. 8.29 KiB, w/ subscr. +1.71 KiB | ||
.filter(noop) | ||
Kefir: w/o subscr. 1.11 Kb, w/ subscr. +0.28 Kb | ||
Rx: w/o subscr. 0.52 Kb, w/ subscr. +1.47 Kb | ||
Bacon: w/o subscr. 8.27 Kb, w/ subscr. +1.71 Kb | ||
Kefir: w/o subscr. 1.03 KiB, w/ subscr. +0.35 KiB | ||
Rx: w/o subscr. 0.52 KiB, w/ subscr. +1.47 KiB | ||
Bacon: w/o subscr. 8.27 KiB, w/ subscr. +1.71 KiB | ||
.skipDuplicates(noop) | ||
Kefir: w/o subscr. 1.06 KiB, w/ subscr. +0.41 KiB | ||
Rx: w/o subscr. 0.53 KiB, w/ subscr. +1.48 KiB | ||
Bacon: w/o subscr. 9.11 KiB, w/ subscr. +1.71 KiB | ||
.scan(0, noop) | ||
Kefir: w/o subscr. 0.96 Kb, w/ subscr. +0.34 Kb | ||
Rx: w/o subscr. 0.54 Kb, w/ subscr. +1.17 Kb | ||
Bacon: w/o subscr. 6.80 Kb, w/ subscr. +1.90 Kb | ||
Kefir: w/o subscr. 0.97 KiB, w/ subscr. +0.35 KiB | ||
Rx: w/o subscr. 0.54 KiB, w/ subscr. +1.17 KiB | ||
Bacon: w/o subscr. 6.80 KiB, w/ subscr. +1.90 KiB | ||
.sequentially(0, [1, 2]) | ||
Kefir: w/o subscr. 0.41 Kb, w/ subscr. +0.55 Kb | ||
Bacon: w/o subscr. 5.02 Kb, w/ subscr. +1.32 Kb | ||
Kefir: w/o subscr. 0.38 KiB, w/ subscr. +0.55 KiB | ||
Bacon: w/o subscr. 5.02 KiB, w/ subscr. +1.31 KiB | ||
.take(5) | ||
Kefir: w/o subscr. 1.11 Kb, w/ subscr. +0.28 Kb | ||
Rx: w/o subscr. 0.52 Kb, w/ subscr. +1.47 Kb | ||
Bacon: w/o subscr. 8.20 Kb, w/ subscr. +1.71 Kb | ||
Kefir: w/o subscr. 1.05 KiB, w/ subscr. +0.35 KiB | ||
Rx: w/o subscr. 0.52 KiB, w/ subscr. +1.55 KiB | ||
Bacon: w/o subscr. 8.20 KiB, w/ subscr. +1.71 KiB | ||
.flatMap(noop) | ||
Kefir: w/o subscr. 0.99 Kb, w/ subscr. +0.29 Kb | ||
Rx: w/o subscr. 0.94 Kb, w/ subscr. +2.59 Kb | ||
Bacon: w/o subscr. 5.91 Kb, w/ subscr. +2.71 Kb | ||
Kefir: w/o subscr. 1.00 KiB, w/ subscr. +0.36 KiB | ||
Rx: w/o subscr. 0.94 KiB, w/ subscr. +2.51 KiB | ||
Bacon: w/o subscr. 5.91 KiB, w/ subscr. +2.70 KiB | ||
.combine(stream, noop) | ||
Kefir: w/o subscr. 2.19 Kb, w/ subscr. +0.37 Kb | ||
Rx: w/o subscr. 0.82 Kb, w/ subscr. +2.93 Kb | ||
Bacon: w/o subscr. 20.98 Kb, w/ subscr. +5.01 Kb | ||
Kefir: w/o subscr. 2.18 KiB, w/ subscr. +0.51 KiB | ||
Rx: w/o subscr. 0.82 KiB, w/ subscr. +2.93 KiB | ||
Bacon: w/o subscr. 20.99 KiB, w/ subscr. +5.01 KiB | ||
.toProperty(1).sampledBy(stream, noop) | ||
Kefir: w/o subscr. 1.91 KiB, w/ subscr. +0.92 KiB | ||
Bacon: w/o subscr. 15.19 KiB, w/ subscr. +5.03 KiB | ||
.combineAsArray(stream1, stream2, stream3, stream4) | ||
Kefir: w/o subscr. 3.97 Kb, w/ subscr. +0.51 Kb | ||
Rx: w/o subscr. 1.27 Kb, w/ subscr. +3.88 Kb | ||
Bacon: w/o subscr. 21.46 Kb, w/ subscr. +6.71 Kb | ||
Kefir: w/o subscr. 3.97 KiB, w/ subscr. +0.79 KiB | ||
Rx: w/o subscr. 1.26 KiB, w/ subscr. +3.70 KiB | ||
Bacon: w/o subscr. 21.45 KiB, w/ subscr. +6.72 KiB | ||
.mergeAll(stream1, stream2, stream3, stream4) | ||
Kefir: w/o subscr. 3.75 Kb, w/ subscr. +0.51 Kb | ||
Rx: w/o subscr. 1.56 Kb, w/ subscr. +5.41 Kb | ||
Bacon: w/o subscr. 14.62 Kb, w/ subscr. +4.59 Kb | ||
Kefir: w/o subscr. 3.75 KiB, w/ subscr. +0.80 KiB | ||
Rx: w/o subscr. 1.56 KiB, w/ subscr. +5.67 KiB | ||
Bacon: w/o subscr. 14.62 KiB, w/ subscr. +4.59 KiB |
{ | ||
"name": "kefir", | ||
"version": "0.1.9", | ||
"version": "0.1.10", | ||
"description": "Bacon.js inspired FRP library with less memory consumption", | ||
@@ -34,2 +34,3 @@ "main": "dist/kefir.js", | ||
"coffee-script": "~1.7.1", | ||
"coffeeify": "^0.6.0", | ||
"grunt": "~0.4.4", | ||
@@ -36,0 +37,0 @@ "grunt-browserify": "~2.0.8", |
# Kefir | ||
http://pozadi.github.io/kefir | ||
Bacon.js inspired FRP library with [less memory consumption](https://github.com/pozadi/kefir/blob/master/memory-usage-results.txt) | ||
@@ -4,0 +6,0 @@ |
@@ -20,5 +20,14 @@ var Kefir = {}; | ||
// Example: | ||
// stream.__sendAny(Kefir.error('network error')) | ||
Kefir.Error = function(error) { | ||
this.error = error; | ||
} | ||
Kefir.error = function(error) { | ||
return new Kefir.Error(error); | ||
} | ||
// Observable | ||
@@ -53,9 +62,12 @@ | ||
if (!this.isEnded()) { | ||
var firstValueSubscriber = (type === 'value' && !this.__hasSubscribers('value')); | ||
var firstIn = ( | ||
(type === 'value' || type === 'error') && | ||
!(this.__hasSubscribers('value') || this.__hasSubscribers('error')) | ||
); | ||
this.__subscribers.push(arguments); | ||
if (firstValueSubscriber) { | ||
if (firstIn) { | ||
this.__onFirstIn(); | ||
} | ||
} else if (type === 'end') { | ||
callSubscriber(arguments); | ||
callFn(restArgs(arguments, 1)); | ||
} | ||
@@ -70,3 +82,6 @@ }, | ||
} | ||
if (type === 'value' && !this.__hasSubscribers('value')) { | ||
if ( | ||
(type === 'value' || type === 'error') && | ||
!(this.__hasSubscribers('value') || this.__hasSubscribers('error')) | ||
) { | ||
this.__onLastOut(); | ||
@@ -81,3 +96,3 @@ } | ||
if (subscriber && subscriber[0] === type) { | ||
var result = callSubscriber(subscriber, restArgs(arguments, 1)); | ||
var result = callFn(restArgs(subscriber, 1), restArgs(arguments, 1)); | ||
if (result === NO_MORE) { | ||
@@ -118,5 +133,11 @@ this.__off.apply(this, subscriber) | ||
this.__send('value', x); | ||
return this; | ||
}, | ||
__sendError: function(x){ | ||
this.__send('error', x); | ||
return this; | ||
}, | ||
__sendEnd: function(){ | ||
this.__send('end'); | ||
return this; | ||
}, | ||
@@ -130,5 +151,8 @@ __sendAny: function(x){ | ||
} | ||
} else if (x !== Kefir.NOTHING) { | ||
} else if (x instanceof Kefir.Error) { | ||
this.__sendError(x.error); | ||
} else if (x !== NOTHING) { | ||
this.__sendValue(x); | ||
} | ||
return this; | ||
}, | ||
@@ -139,11 +163,23 @@ | ||
this.__on.apply(this, ['value'].concat(toArray(arguments))); | ||
return this; | ||
}, | ||
offValue: function(){ | ||
this.__off.apply(this, ['value'].concat(toArray(arguments))); | ||
return this; | ||
}, | ||
onError: function(){ | ||
this.__on.apply(this, ['error'].concat(toArray(arguments))); | ||
return this; | ||
}, | ||
offError: function(){ | ||
this.__off.apply(this, ['error'].concat(toArray(arguments))); | ||
return this; | ||
}, | ||
onEnd: function(){ | ||
this.__on.apply(this, ['end'].concat(toArray(arguments))); | ||
return this; | ||
}, | ||
offEnd: function(){ | ||
this.__off.apply(this, ['end'].concat(toArray(arguments))); | ||
return this; | ||
}, | ||
@@ -153,3 +189,3 @@ | ||
onNewValue: function(){ | ||
this.onValue.apply(this, arguments); | ||
return this.onValue.apply(this, arguments); | ||
}, | ||
@@ -184,3 +220,3 @@ | ||
Observable.call(this, onFirstIn, onLastOut); | ||
this.__cached = (typeof initial !== "undefined") ? initial : Kefir.NOTHING; | ||
this.__cached = isUndefined(initial) ? NOTHING : initial; | ||
} | ||
@@ -192,6 +228,6 @@ | ||
hasCached: function(){ | ||
return this.__cached !== Kefir.NOTHING; | ||
hasValue: function(){ | ||
return this.__cached !== NOTHING; | ||
}, | ||
getCached: function(){ | ||
getValue: function(){ | ||
return this.__cached; | ||
@@ -208,8 +244,9 @@ }, | ||
this.__on.apply(this, ['value'].concat(toArray(arguments))); | ||
return this; | ||
}, | ||
onValue: function() { | ||
if ( this.hasCached() ) { | ||
callSubscriber(['value'].concat(toArray(arguments)), [this.__cached]); | ||
if ( this.hasValue() ) { | ||
callFn(arguments, [this.__cached]) | ||
} | ||
this.onNewValue.apply(this, arguments); | ||
return this.onNewValue.apply(this, arguments); | ||
} | ||
@@ -223,9 +260,14 @@ | ||
Observable.prototype.log = function(text) { | ||
if (!text) { | ||
text = this.toString(); | ||
var logHelper = function(name, type, x) { | ||
console.log(name, type, x); | ||
} | ||
Observable.prototype.log = function(name) { | ||
if (!name) { | ||
name = this.toString(); | ||
} | ||
function log(x){ console.log(text, x) } | ||
this.onValue(log); | ||
this.onEnd(function(){ log(END) }); | ||
this.onValue(logHelper, null, name, '<value>'); | ||
this.onError(logHelper, null, name, '<error>'); | ||
this.onEnd(logHelper, null, name, '<end>'); | ||
return this; | ||
} |
// TODO | ||
// | ||
// observable.flatMapLatest(f) | ||
// observable.flatMapFirst(f) | ||
@@ -11,4 +10,2 @@ // | ||
// stream.concat(otherStream) | ||
// | ||
// Kefir.onValues(a, b [, c...], f) | ||
@@ -34,5 +31,6 @@ | ||
if (this.__hasSubscribers('value')) { | ||
stream.onValue(this.__handlePlugged, this, i); | ||
stream.onValue('__handlePlugged', this, i); | ||
stream.onError('__sendError', this); | ||
} | ||
stream.onEnd(this.__unplugById, this, i); | ||
stream.onEnd('__unplugById', this, i); | ||
} | ||
@@ -45,4 +43,5 @@ }, | ||
this.__plugged[i] = null; | ||
stream.offValue(this.__handlePlugged, this, i); | ||
stream.onEnd(this.__unplugById, this, i); | ||
stream.offValue('__handlePlugged', this, i); | ||
stream.offError('__sendError', this); | ||
stream.offEnd('__unplugById', this, i); | ||
} | ||
@@ -64,3 +63,4 @@ } | ||
if (stream) { | ||
stream.onValue(this.__handlePlugged, this, i); | ||
stream.onValue('__handlePlugged', this, i); | ||
stream.onError('__sendError', this); | ||
} | ||
@@ -73,3 +73,4 @@ } | ||
if (stream) { | ||
stream.offValue(this.__handlePlugged, this, i); | ||
stream.offValue('__handlePlugged', this, i); | ||
stream.offError('__sendError', this); | ||
} | ||
@@ -109,11 +110,19 @@ } | ||
this.__sendAny(x); | ||
return this; | ||
}, | ||
error: function(e){ | ||
this.__sendError(e); | ||
return this; | ||
}, | ||
plug: function(stream){ | ||
this.__plug(stream); | ||
return this; | ||
}, | ||
unplug: function(stream){ | ||
this.__unplug(stream); | ||
return this; | ||
}, | ||
end: function(){ | ||
this.__sendEnd(); | ||
return this; | ||
}, | ||
@@ -138,7 +147,7 @@ __clear: function(){ | ||
Kefir.FlatMappedStream = function FlatMappedStream(sourceStream, mapFn){ | ||
Kefir.FlatMappedStream = function FlatMappedStream(sourceStream, mapFnMeta){ | ||
Stream.call(this); | ||
this.__initPluggable(); | ||
this.__sourceStream = sourceStream; | ||
this.__mapFn = mapFn; | ||
this.__mapFnMeta = normFnMeta(mapFnMeta); | ||
sourceStream.onEnd(this.__onSourceEnds, this); | ||
@@ -157,10 +166,12 @@ } | ||
__plugResult: function(x){ | ||
this.__plug( this.__mapFn(x) ); | ||
this.__plug( callFn(this.__mapFnMeta, [x]) ); | ||
}, | ||
__onFirstIn: function(){ | ||
this.__sourceStream.onValue(this.__plugResult, this); | ||
this.__sourceStream.onValue('__plugResult', this); | ||
this.__sourceStream.onError('__sendError', this); | ||
PluggableMixin.__onFirstIn.call(this); | ||
}, | ||
__onLastOut: function(){ | ||
this.__sourceStream.offValue(this.__plugResult, this); | ||
this.__sourceStream.offValue('__plugResult', this); | ||
this.__sourceStream.offError('__sendError', this); | ||
PluggableMixin.__onLastOut.call(this); | ||
@@ -178,3 +189,3 @@ }, | ||
this.__sourceStream = null; | ||
this.__mapFn = null; | ||
this.__mapFnMeta = null; | ||
} | ||
@@ -184,4 +195,4 @@ | ||
Observable.prototype.flatMap = function(fn) { | ||
return new Kefir.FlatMappedStream(this, fn); | ||
Observable.prototype.flatMap = function(/*fn[, context[, arg1, arg2, ...]]*/) { | ||
return new Kefir.FlatMappedStream(this, arguments); | ||
}; | ||
@@ -192,6 +203,28 @@ | ||
// FlatMapLatest | ||
Kefir.FlatMapLatestStream = function FlatMapLatestStream(){ | ||
Kefir.FlatMappedStream.apply(this, arguments); | ||
} | ||
inherit(Kefir.FlatMapLatestStream, Kefir.FlatMappedStream, { | ||
__ClassName: 'FlatMapLatestStream', | ||
__plugResult: function(x){ | ||
for (var i = 0; i < this.__plugged.length; i++) { | ||
this.__unplugById(i); | ||
} | ||
Kefir.FlatMappedStream.prototype.__plugResult.call(this, x); | ||
} | ||
}) | ||
Observable.prototype.flatMapLatest = function(/*fn[, context[, arg1, arg2, ...]]*/) { | ||
return new Kefir.FlatMapLatestStream(this, arguments); | ||
}; | ||
// Merge | ||
@@ -243,3 +276,3 @@ | ||
Kefir.CombinedStream = function CombinedStream(sources, mapFn){ | ||
Kefir.CombinedStream = function CombinedStream(sources, mapFnMeta){ | ||
Stream.call(this); | ||
@@ -251,4 +284,4 @@ this.__initPluggable(); | ||
this.__cachedValues = new Array(sources.length); | ||
this.__hasCached = new Array(sources.length); | ||
this.__mapFn = mapFn; | ||
this.__hasValue = new Array(sources.length); | ||
this.__mapFnMeta = normFnMeta(mapFnMeta); | ||
} | ||
@@ -267,7 +300,7 @@ | ||
__handlePlugged: function(i, x) { | ||
this.__hasCached[i] = true; | ||
this.__hasValue[i] = true; | ||
this.__cachedValues[i] = x; | ||
if (this.__allCached()) { | ||
if (isFn(this.__mapFn)) { | ||
this.__sendAny(this.__mapFn.apply(null, this.__cachedValues)); | ||
if (this.__mapFnMeta) { | ||
this.__sendAny(callFn(this.__mapFnMeta, this.__cachedValues)); | ||
} else { | ||
@@ -279,4 +312,4 @@ this.__sendValue(this.__cachedValues.slice(0)); | ||
__allCached: function(){ | ||
for (var i = 0; i < this.__hasCached.length; i++) { | ||
if (!this.__hasCached[i]) { | ||
for (var i = 0; i < this.__hasValue.length; i++) { | ||
if (!this.__hasValue[i]) { | ||
return false; | ||
@@ -291,4 +324,4 @@ } | ||
this.__cachedValues = null; | ||
this.__hasCached = null; | ||
this.__mapFn = null; | ||
this.__hasValue = null; | ||
this.__mapFnMeta = null; | ||
} | ||
@@ -298,8 +331,20 @@ | ||
Kefir.combine = function(sources, mapFn) { | ||
return new Kefir.CombinedStream(sources, mapFn); | ||
Kefir.combine = function(sources/*, fn[, context[, arg1, arg2, ...]]*/) { | ||
return new Kefir.CombinedStream(sources, restArgs(arguments, 1)); | ||
} | ||
Observable.prototype.combine = function(sources, mapFn) { | ||
return Kefir.combine([this].concat(sources), mapFn); | ||
Observable.prototype.combine = function(sources/*, fn[, context[, arg1, arg2, ...]]*/) { | ||
return new Kefir.CombinedStream([this].concat(sources), restArgs(arguments, 1)); | ||
} | ||
// Kefir.onValues() | ||
Kefir.onValues = function(streams/*, fn[, context[, arg1, agr2, ...]]*/){ | ||
var fnMeta = normFnMeta(restArgs(arguments, 1)) | ||
return Kefir.combine(streams).onValue(callFn, null, fnMeta); | ||
} |
@@ -5,4 +5,4 @@ var WithSourceStreamMixin = { | ||
source.onEnd(this.__sendEnd, this); | ||
if (source instanceof Property && this instanceof Property && source.hasCached()) { | ||
this.__handle(source.getCached()); | ||
if (source instanceof Property && this instanceof Property && source.hasValue()) { | ||
this.__handle(source.getValue()); | ||
} | ||
@@ -14,6 +14,8 @@ }, | ||
__onFirstIn: function(){ | ||
this.__source.onNewValue(this.__handle, this); | ||
this.__source.onNewValue('__handle', this); | ||
this.__source.onError('__sendError', this); | ||
}, | ||
__onLastOut: function(){ | ||
this.__source.offValue(this.__handle, this); | ||
this.__source.offValue('__handle', this); | ||
this.__source.offError('__sendError', this); | ||
}, | ||
@@ -46,3 +48,3 @@ __clear: function(){ | ||
Property.prototype.toProperty = function(initial){ | ||
if (typeof initial === "undefined") { | ||
if (isUndefined(initial)) { | ||
return this | ||
@@ -60,15 +62,27 @@ } else { | ||
// property.changes() | ||
Kefir.ChangesStream = function ChangesStream(source){ | ||
Stream.call(this); | ||
// .scan(seed, fn) | ||
Kefir.ScanProperty = function ScanProperty(source, seed, fnMeta){ | ||
Property.call(this, null, null, seed); | ||
this.__fnMeta = normFnMeta(fnMeta); | ||
this.__Constructor(source); | ||
} | ||
inherit(Kefir.ChangesStream, Stream, WithSourceStreamMixin, { | ||
__ClassName: 'ChangesStream' | ||
inherit(Kefir.ScanProperty, Property, WithSourceStreamMixin, { | ||
__ClassName: 'ScanProperty', | ||
__handle: function(x){ | ||
this.__sendValue( callFn(this.__fnMeta, [this.getValue(), x]) ); | ||
}, | ||
__clear: function(){ | ||
WithSourceStreamMixin.__clear.call(this); | ||
this.__fnMeta = null; | ||
} | ||
}) | ||
Property.prototype.changes = function() { | ||
return new Kefir.ChangesStream(this); | ||
Observable.prototype.scan = function(seed/*fn[, context[, arg1, arg2, ...]]*/) { | ||
return new Kefir.ScanProperty(this, seed, restArgs(arguments, 1)); | ||
} | ||
@@ -79,27 +93,32 @@ | ||
// .reduce(seed, fn) | ||
// .scan(seed, fn) | ||
Kefir.ScanProperty = function ScanProperty(source, seed, fn){ | ||
Property.call(this, null, null, seed); | ||
this.__fn = fn; | ||
Kefir.ReducedProperty = function ReducedProperty(source, seed, fnMeta){ | ||
Property.call(this); | ||
this.__fnMeta = normFnMeta(fnMeta); | ||
this.__result = seed; | ||
source.onEnd('__sendResult', this); | ||
this.__Constructor(source); | ||
} | ||
inherit(Kefir.ScanProperty, Property, WithSourceStreamMixin, { | ||
inherit(Kefir.ReducedProperty, Property, WithSourceStreamMixin, { | ||
__ClassName: 'ScanProperty', | ||
__ClassName: 'ReducedProperty', | ||
__handle: function(x){ | ||
this.__sendValue( this.__fn(this.getCached(), x) ); | ||
this.__result = callFn(this.__fnMeta, [this.__result, x]); | ||
}, | ||
__sendResult: function(){ | ||
this.__sendValue(this.__result); | ||
}, | ||
__clear: function(){ | ||
WithSourceStreamMixin.__clear.call(this); | ||
this.__fn = null; | ||
this.__fnMeta = null; | ||
this.__result = null; | ||
} | ||
}) | ||
}); | ||
Observable.prototype.scan = function(seed, fn) { | ||
return new Kefir.ScanProperty(this, seed, fn); | ||
Observable.prototype.reduce = function(seed/*fn[, context[, arg1, arg2, ...]]*/) { | ||
return new Kefir.ReducedProperty(this, seed, restArgs(arguments, 1)); | ||
} | ||
@@ -113,4 +132,4 @@ | ||
var MapMixin = { | ||
__Constructor: function(source, mapFn){ | ||
if (source instanceof Property) { | ||
__Constructor: function(source, mapFnMeta){ | ||
if (this instanceof Property) { | ||
Property.call(this); | ||
@@ -120,11 +139,13 @@ } else { | ||
} | ||
this.__mapFn = mapFn; | ||
this.__mapFnMeta = normFnMeta(mapFnMeta); | ||
WithSourceStreamMixin.__Constructor.call(this, source); | ||
}, | ||
__handle: function(x){ | ||
this.__sendAny( this.__mapFn(x) ); | ||
this.__sendAny( | ||
this.__mapFnMeta ? callFn(this.__mapFnMeta, [x]) : x | ||
); | ||
}, | ||
__clear: function(){ | ||
WithSourceStreamMixin.__clear.call(this); | ||
this.__mapFn = null; | ||
this.__mapFnMeta = null; | ||
} | ||
@@ -150,8 +171,8 @@ } | ||
Stream.prototype.map = function(fn) { | ||
return new Kefir.MappedStream(this, fn); | ||
Stream.prototype.map = function(/*fn[, context[, arg1, arg2, ...]]*/) { | ||
return new Kefir.MappedStream(this, arguments); | ||
} | ||
Property.prototype.map = function(fn) { | ||
return new Kefir.MappedProperty(this, fn); | ||
Property.prototype.map = function(/*fn[, context[, arg1, arg2, ...]]*/) { | ||
return new Kefir.MappedProperty(this, arguments); | ||
} | ||
@@ -162,12 +183,25 @@ | ||
// property.changes() | ||
Property.prototype.changes = function() { | ||
return new Kefir.MappedStream(this); | ||
} | ||
// .diff(seed, fn) | ||
Observable.prototype.diff = function(prev, fn) { | ||
return this.map(function(x){ | ||
var result = fn(prev, x); | ||
prev = x; | ||
return result; | ||
}) | ||
var diffMapFn = function(x){ | ||
var result = callFn(this.fnMeta, [this.prev, x]); | ||
this.prev = x; | ||
return result; | ||
} | ||
Observable.prototype.diff = function(start/*fn[, context[, arg1, arg2, ...]]*/) { | ||
return this.map(diffMapFn, { | ||
prev: start, | ||
fnMeta: normFnMeta(restArgs(arguments, 1)) | ||
}); | ||
} | ||
@@ -177,45 +211,52 @@ | ||
// .filter(fn) | ||
Observable.prototype.filter = function(fn) { | ||
return this.map(function(x){ | ||
if (fn(x)) { | ||
return x; | ||
} else { | ||
return NOTHING; | ||
} | ||
}) | ||
var filterMapFn = function(filterFnMeta, x){ | ||
if (callFn(filterFnMeta, [x])) { | ||
return x; | ||
} else { | ||
return NOTHING; | ||
} | ||
} | ||
Observable.prototype.filter = function(/*fn[, context[, arg1, arg2, ...]]*/) { | ||
return this.map(filterMapFn, null, normFnMeta(arguments)); | ||
} | ||
// .takeWhile(fn) | ||
Observable.prototype.takeWhile = function(fn) { | ||
return this.map(function(x){ | ||
if (fn(x)) { | ||
return x; | ||
} else { | ||
return END; | ||
} | ||
}) | ||
var takeWhileMapFn = function(fnMeta, x) { | ||
if (callFn(fnMeta, [x])) { | ||
return x; | ||
} else { | ||
return END; | ||
} | ||
} | ||
Observable.prototype.takeWhile = function(/*fn[, context[, arg1, arg2, ...]]*/) { | ||
return this.map(takeWhileMapFn, null, normFnMeta(arguments)); | ||
} | ||
// .take(n) | ||
var takeMapFn = function(x) { | ||
if (this.n <= 0) { | ||
return END; | ||
} | ||
if (this.n === 1) { | ||
return Kefir.bunch(x, END); | ||
} | ||
this.n--; | ||
return x; | ||
} | ||
Observable.prototype.take = function(n) { | ||
return this.map(function(x){ | ||
if (n <= 0) { | ||
return END; | ||
} | ||
if (n === 1) { | ||
return Kefir.bunch(x, END); | ||
} | ||
n--; | ||
return x; | ||
}) | ||
return this.map(takeMapFn, {n: n}); | ||
} | ||
@@ -228,11 +269,13 @@ | ||
var skipMapFn = function(x) { | ||
if (this.n <= 0) { | ||
return x; | ||
} else { | ||
this.n--; | ||
return NOTHING; | ||
} | ||
} | ||
Observable.prototype.skip = function(n) { | ||
return this.map(function(x){ | ||
if (n <= 0) { | ||
return x; | ||
} else { | ||
n--; | ||
return Kefir.NOTHING; | ||
} | ||
}) | ||
return this.map(skipMapFn, {n: n}); | ||
} | ||
@@ -246,15 +289,16 @@ | ||
var skipDuplicatesMapFn = function(x){ | ||
var result; | ||
if (this.prev !== NOTHING && (this.fn ? this.fn(this.prev, x) : this.prev === x)) { | ||
result = NOTHING; | ||
} else { | ||
result = x; | ||
} | ||
this.hasPrev = true; | ||
this.prev = x; | ||
return result; | ||
} | ||
Observable.prototype.skipDuplicates = function(fn) { | ||
var prev, hasPrev = false; | ||
return this.map(function(x){ | ||
var result; | ||
if (hasPrev && (fn ? fn(prev, x) : prev === x)) { | ||
result = Kefir.NOTHING; | ||
} else { | ||
result = x; | ||
} | ||
hasPrev = true; | ||
prev = x; | ||
return result; | ||
}) | ||
return this.map(skipDuplicatesMapFn, {fn: fn, prev: NOTHING}); | ||
} | ||
@@ -268,12 +312,19 @@ | ||
Observable.prototype.skipWhile = function(fn) { | ||
var skip = true; | ||
return this.map(function(x){ | ||
if (skip && fn(x)) { | ||
return Kefir.NOTHING; | ||
} else { | ||
skip = false; | ||
return x; | ||
} | ||
}) | ||
var skipWhileMapFn = function(x){ | ||
if (this.skip && callFn(this.fnMeta, [x])) { | ||
return NOTHING; | ||
} else { | ||
this.skip = false; | ||
return x; | ||
} | ||
} | ||
Observable.prototype.skipWhile = function(/*fn[, context[, arg1, arg2, ...]]*/) { | ||
return this.map(skipWhileMapFn, {skip: true, fnMeta: normFnMeta(arguments)}); | ||
} | ||
// FromPoll | ||
var FromPollStream = Kefir.FromPollStream = function FromPollStream(interval, sourceFn){ | ||
var FromPollStream = Kefir.FromPollStream = function FromPollStream(interval, sourceFnMeta){ | ||
Stream.call(this); | ||
@@ -8,3 +8,4 @@ this.__interval = interval; | ||
var _this = this; | ||
this.__bindedSend = function(){ _this.__sendAny(sourceFn()) } | ||
sourceFnMeta = normFnMeta(sourceFnMeta); | ||
this.__bindedSend = function(){ _this.__sendAny(callFn(sourceFnMeta)) } | ||
} | ||
@@ -31,4 +32,4 @@ | ||
Kefir.fromPoll = function(interval, fn){ | ||
return new FromPollStream(interval, fn); | ||
Kefir.fromPoll = function(interval/*, fn[, context[, arg1, arg2, ...]]*/){ | ||
return new FromPollStream(interval, restArgs(arguments, 1)); | ||
} | ||
@@ -41,3 +42,3 @@ | ||
Kefir.interval = function(interval, x){ | ||
return new FromPollStream(interval, function(){ return x }); | ||
return new FromPollStream(interval, [id, null, x]); | ||
} | ||
@@ -49,13 +50,14 @@ | ||
var sequentiallyHelperFn = function(){ | ||
if (this.xs.length === 0) { | ||
return END; | ||
} | ||
if (this.xs.length === 1){ | ||
return Kefir.bunch(this.xs[0], END); | ||
} | ||
return this.xs.shift(); | ||
} | ||
Kefir.sequentially = function(interval, xs){ | ||
xs = xs.slice(0); | ||
return new FromPollStream(interval, function(){ | ||
if (xs.length === 0) { | ||
return END; | ||
} | ||
if (xs.length === 1){ | ||
return Kefir.bunch(xs[0], END); | ||
} | ||
return xs.shift(); | ||
}); | ||
return new FromPollStream(interval, [sequentiallyHelperFn, {xs: xs.slice(0)}]); | ||
} | ||
@@ -67,7 +69,9 @@ | ||
var repeatedlyHelperFn = function(){ | ||
this.i = (this.i + 1) % this.xs.length; | ||
return this.xs[this.i]; | ||
} | ||
Kefir.repeatedly = function(interval, xs){ | ||
var i = -1; | ||
return new FromPollStream(interval, function(){ | ||
return xs[++i % xs.length]; | ||
}); | ||
return new FromPollStream(interval, [repeatedlyHelperFn, {i: -1, xs: xs}]); | ||
} |
// TODO | ||
// | ||
// Kefir.constant(x) | ||
// Kefir.fromArray(values) | ||
@@ -31,7 +32,9 @@ | ||
if (!this.isEnded()) { | ||
callSubscriber(['value'].concat(toArray(arguments)), [this.__value]); | ||
callFn(arguments, [this.__value]); | ||
this.__value = null; | ||
this.__sendEnd(); | ||
} | ||
} | ||
return this; | ||
}, | ||
onError: noop | ||
@@ -38,0 +41,0 @@ }) |
@@ -10,3 +10,3 @@ function noop(){} | ||
function toArray(arrayLike){ | ||
if (arrayLike instanceof Array) { | ||
if (isArray(arrayLike)) { | ||
return arrayLike; | ||
@@ -25,11 +25,10 @@ } else { | ||
function extend() { | ||
var objects = toArray(arguments); | ||
if (objects.length === 1) { | ||
return objects[0]; | ||
if (arguments.length === 1) { | ||
return arguments[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]; | ||
var result = arguments[0]; | ||
for (var i = 1; i < arguments.length; i++) { | ||
for (var prop in arguments[i]) { | ||
if(own(arguments[i], prop)) { | ||
result[prop] = arguments[i][prop]; | ||
} | ||
@@ -41,3 +40,3 @@ } | ||
function inherit(Child, Parent) { // (Child, Parent[, mixin1, mixin2, ...]) | ||
function inherit(Child, Parent/*[, mixin1, mixin2, ...]*/) { | ||
Child.prototype = createObj(Parent.prototype); | ||
@@ -61,3 +60,3 @@ Child.prototype.constructor = Child; | ||
function firstArrOrToArr(args) { | ||
if (Object.prototype.toString.call(args[0]) === '[object Array]') { | ||
if (isArray(args[0])) { | ||
return args[0]; | ||
@@ -79,5 +78,4 @@ } | ||
function callSubscriber(subscriber, moreArgs){ | ||
// subscriber = [ | ||
// eventName, | ||
function callFn(fnMeta, moreArgs){ | ||
// fnMeta = [ | ||
// fn, | ||
@@ -89,16 +87,66 @@ // context, | ||
// ] | ||
var fn = subscriber[1]; | ||
var context = subscriber[2]; | ||
var args = restArgs(subscriber, 3); | ||
var fn, context, args; | ||
if (isFn(fnMeta)) { | ||
fn = fnMeta; | ||
context = null; | ||
args = null; | ||
} else { | ||
fn = fnMeta[0]; | ||
context = fnMeta[1]; | ||
args = restArgs(fnMeta, 2, true); | ||
/*jshint eqnull:true */ | ||
if (!isFn(fn) && context != null) { | ||
fn = context[fn]; | ||
} | ||
} | ||
if (moreArgs){ | ||
args = args.concat(toArray(moreArgs)); | ||
if (args) { | ||
args = args.concat(toArray(moreArgs)); | ||
} else { | ||
args = moreArgs; | ||
} | ||
} | ||
return fn.apply(context, args); | ||
if (isFn(fn)) { | ||
return args ? fn.apply(context, args) : fn.call(context); | ||
} else { | ||
throw new Error('not a function ' + fn); | ||
} | ||
} | ||
function normFnMeta(fnMeta) { | ||
if (isArray(fnMeta) || isArguments(fnMeta)) { | ||
if (fnMeta.length === 1) { | ||
return fnMeta[0]; | ||
} | ||
if (fnMeta.length === 0) { | ||
return null; | ||
} | ||
} | ||
return fnMeta; | ||
} | ||
function isFn(fn) { | ||
return typeof fn === "function"; | ||
return typeof fn === 'function'; | ||
} | ||
function isEqualArrays(a, b){ | ||
function isUndefined(x) { | ||
return typeof x === 'undefined'; | ||
} | ||
function isArray(xs) { | ||
return Object.prototype.toString.call(xs) === '[object Array]'; | ||
} | ||
var isArguments = function(xs) { | ||
return Object.prototype.toString.call(xs) === '[object Arguments]'; | ||
} | ||
// For IE | ||
if (!isArguments(arguments)) { | ||
isArguments = function(obj) { | ||
return !!(obj && own(obj, 'callee')); | ||
} | ||
} | ||
function isEqualArrays(a, b) { | ||
/*jshint eqnull:true */ | ||
@@ -105,0 +153,0 @@ if (a == null && b == null) { |
@@ -1,4 +0,2 @@ | ||
// I am not sure this tests are correct | ||
// | ||
// Run: node --expose-gc test-memory-usage.js | ||
// node --expose-gc test-memory-usage.js | ||
@@ -13,7 +11,7 @@ | ||
function toMb(x) { | ||
return ( x / 1024 / 1024 ).toFixed(2) + ' Mb'; | ||
return ( x / 1024 / 1024 ).toFixed(2) + ' MiB'; | ||
} | ||
function toKb(x) { | ||
return ( x / 1024 ).toFixed(2) + ' Kb'; | ||
return ( x / 1024 ).toFixed(2) + ' KiB'; | ||
} | ||
@@ -125,3 +123,5 @@ | ||
console.log('Generated by run: node --expose-gc test-memory-usage.js > memory-usage-results.txt') | ||
console.log('Please don\'t take this results too serious, I am not 100% sure about correctness of this tests') | ||
console.log('Please don\'t take these results too serious, I am not 100% sure about correctness of these tests') | ||
console.log('Also these tests do not measure amount of temporary allocated') | ||
console.log('memory that causes frequent garbage collection.') | ||
console.log('') | ||
@@ -248,2 +248,19 @@ | ||
title('.skipDuplicates(noop)') | ||
createNObservable('Kefir', 700, function(){ | ||
return baseKefir().skipDuplicates(noop); | ||
}) | ||
createNObservable('Rx', 700, function(){ | ||
return baseRx().distinctUntilChanged(noop); | ||
}) | ||
createNObservable('Bacon', 700, function(){ | ||
return baseBacon().skipDuplicates(noop); | ||
}) | ||
title('.scan(0, noop)') | ||
@@ -331,2 +348,16 @@ | ||
title('.toProperty(1).sampledBy(stream, noop)') | ||
createNObservable('Kefir', 700, function(){ | ||
return baseKefir().toProperty(1).sampledBy(baseKefir(), noop); | ||
}) | ||
createNObservable('Bacon', 700, function(){ | ||
return baseBacon().toProperty(1).sampledBy(baseBacon(), noop); | ||
}) | ||
title('.combineAsArray(stream1, stream2, stream3, stream4)') | ||
@@ -333,0 +364,0 @@ |
@@ -28,2 +28,19 @@ var Kefir = require('../../dist/kefir.js'); | ||
it(".error()", function() { | ||
var bus = new Kefir.Bus(); | ||
var result = helpers.getOutputAndErrors(bus); | ||
bus.push(1); | ||
bus.push(Kefir.error('e1')); | ||
bus.error('e2'); | ||
expect(result).toEqual({ended: false, xs: [1], errors: ['e1', 'e2']}); | ||
}); | ||
it(".plug()", function() { | ||
@@ -30,0 +47,0 @@ |
@@ -177,2 +177,21 @@ var Kefir = require('../../dist/kefir.js'); | ||
it("errors", function(){ | ||
var stream1 = new Kefir.Stream(); | ||
var stream2 = new Kefir.Stream(); | ||
var combined = stream1.combine([stream2], function(a, b) { return a + b }); | ||
var result = helpers.getOutputAndErrors(combined); | ||
stream1.__sendError('e1'); | ||
stream2.__sendError('e2'); | ||
stream1.__sendError('e3'); | ||
stream2.__sendError('e4'); | ||
expect(result).toEqual({ended: false, xs: [], errors: ['e1', 'e2', 'e3', 'e4']}); | ||
}) | ||
}); |
@@ -39,4 +39,4 @@ var Kefir = require('../../dist/kefir.js'); | ||
expect(diffs).toEqual(jasmine.any(Kefir.Property)); | ||
expect(diffs.hasCached()).toBe(true); | ||
expect(diffs.getCached()).toBe(1); | ||
expect(diffs.hasValue()).toBe(true); | ||
expect(diffs.getValue()).toBe(1); | ||
@@ -64,4 +64,4 @@ var result = helpers.getOutput(diffs); | ||
expect(diffs).toEqual(jasmine.any(Kefir.Property)); | ||
expect(diffs.hasCached()).toBe(false); | ||
expect(diffs.getCached()).toBe(Kefir.NOTHING); | ||
expect(diffs.hasValue()).toBe(false); | ||
expect(diffs.getValue()).toBe(Kefir.NOTHING); | ||
@@ -68,0 +68,0 @@ var result = helpers.getOutput(diffs); |
@@ -41,4 +41,4 @@ var Kefir = require('../../dist/kefir.js'); | ||
expect(filtered).toEqual(jasmine.any(Kefir.Property)); | ||
expect(filtered.hasCached()).toBe(true); | ||
expect(filtered.getCached()).toBe(6); | ||
expect(filtered.hasValue()).toBe(true); | ||
expect(filtered.getValue()).toBe(6); | ||
@@ -68,4 +68,4 @@ var result = helpers.getOutput(filtered); | ||
expect(filtered).toEqual(jasmine.any(Kefir.Property)); | ||
expect(filtered.hasCached()).toBe(false); | ||
expect(filtered.getCached()).toBe(Kefir.NOTHING); | ||
expect(filtered.hasValue()).toBe(false); | ||
expect(filtered.getValue()).toBe(Kefir.NOTHING); | ||
@@ -72,0 +72,0 @@ var result = helpers.getOutput(filtered); |
@@ -106,2 +106,31 @@ var Kefir = require('../../dist/kefir.js'); | ||
it("errors", function(){ | ||
var stream = new Kefir.Stream(); | ||
var childStreams = [ | ||
new Kefir.Stream(), | ||
new Kefir.Stream(), | ||
new Kefir.Stream() | ||
] | ||
var mapped = stream.flatMap(function(x){ | ||
return childStreams[x]; | ||
}); | ||
var result = helpers.getOutputAndErrors(mapped); | ||
stream.__sendValue(2); | ||
childStreams[0].__sendError('e0 - not delivered'); | ||
childStreams[2].__sendError('e1'); | ||
stream.__sendValue(0); | ||
childStreams[0].__sendError('e2'); | ||
stream.__sendError('e3'); | ||
expect(result).toEqual({ended: false, xs: [], errors: ['e1', 'e2', 'e3']}); | ||
}) | ||
}); |
@@ -37,4 +37,4 @@ var Kefir = require('../../dist/kefir.js'); | ||
expect(mapped).toEqual(jasmine.any(Kefir.Property)); | ||
expect(mapped.hasCached()).toBe(true); | ||
expect(mapped.getCached()).toBe(10); | ||
expect(mapped.hasValue()).toBe(true); | ||
expect(mapped.getValue()).toBe(10); | ||
@@ -87,2 +87,23 @@ var result = helpers.getOutput(mapped); | ||
it(".map() and errors", function(){ | ||
var stream = new Kefir.Stream(); | ||
var mapped = stream.map(x2); | ||
var result = helpers.getOutputAndErrors(mapped); | ||
stream.__sendValue(1); | ||
stream.__sendError('e1'); | ||
stream.__sendAny(Kefir.error('e2')); | ||
expect(result).toEqual({ | ||
ended: false, | ||
xs: [2], | ||
errors: ['e1', 'e2'] | ||
}); | ||
}); | ||
}); |
@@ -62,2 +62,19 @@ var Kefir = require('../../dist/kefir.js'); | ||
it("errors", function(){ | ||
var stream1 = new Kefir.Stream(); | ||
var stream2 = new Kefir.Stream(); | ||
var merged = stream1.merge(stream2); | ||
var result = helpers.getOutputAndErrors(merged); | ||
stream1.__sendError('e1'); | ||
stream2.__sendError('e2'); | ||
stream1.__sendError('e3'); | ||
stream2.__sendError('e4'); | ||
expect(result).toEqual({ended: false, xs: [], errors: ['e1', 'e2', 'e3', 'e4']}); | ||
}) | ||
}); |
@@ -141,2 +141,29 @@ var Kefir = require('../../dist/kefir.js'); | ||
it("subscribers with string as fn and context", function(){ | ||
var log = []; | ||
var obs = new Kefir.Observable(); | ||
var subscriber = function(){ | ||
log.push( [this.name].concat([].slice.call(arguments)) ); | ||
} | ||
var context = { | ||
foo: subscriber, | ||
name: "bar" | ||
}; | ||
obs.onValue("foo", context, 1, 2); | ||
obs.__sendValue(1); | ||
obs.__sendValue(2); | ||
expect(log).toEqual([ | ||
["bar", 1, 2, 1], | ||
["bar", 1, 2, 2] | ||
]); | ||
}); | ||
it("send after end", function(){ | ||
@@ -159,6 +186,26 @@ | ||
it("_sendAny", function(){ | ||
it("errors", function(){ | ||
var obs = new Kefir.Observable(); | ||
var result = helpers.getOutputAndErrors(obs); | ||
obs.__sendValue(1); | ||
obs.__sendError('e1'); | ||
obs.__sendAny(Kefir.error('e2')); | ||
expect(result).toEqual({ | ||
ended: false, | ||
xs: [1], | ||
errors: ['e1', 'e2'] | ||
}); | ||
}); | ||
it("__sendAny", function(){ | ||
var obs = new Kefir.Observable(); | ||
var result = helpers.getOutput(obs); | ||
@@ -165,0 +212,0 @@ |
@@ -8,17 +8,17 @@ var Kefir = require('../../dist/kefir.js'); | ||
it("hasCached, getCached", function(){ | ||
it("hasValue, getValue", function(){ | ||
var prop = new Kefir.Property(); | ||
expect(prop.hasCached()).toBe(false); | ||
expect(prop.getCached()).toBe(Kefir.NOTHING); | ||
expect(prop.hasValue()).toBe(false); | ||
expect(prop.getValue()).toBe(Kefir.NOTHING); | ||
prop.__sendValue(1) | ||
expect(prop.hasCached()).toBe(true); | ||
expect(prop.getCached()).toBe(1); | ||
expect(prop.hasValue()).toBe(true); | ||
expect(prop.getValue()).toBe(1); | ||
prop = new Kefir.Property(null, null, 2); | ||
expect(prop.hasCached()).toBe(true); | ||
expect(prop.getCached()).toBe(2); | ||
expect(prop.hasValue()).toBe(true); | ||
expect(prop.getValue()).toBe(2); | ||
@@ -71,4 +71,4 @@ }); | ||
expect(prop.hasCached()).toBe(false); | ||
expect(prop.getCached()).toBe(Kefir.NOTHING); | ||
expect(prop.hasValue()).toBe(false); | ||
expect(prop.getValue()).toBe(Kefir.NOTHING); | ||
expect(log).toEqual([]); | ||
@@ -78,4 +78,4 @@ | ||
expect(prop.hasCached()).toBe(true); | ||
expect(prop.getCached()).toBe(1); | ||
expect(prop.hasValue()).toBe(true); | ||
expect(prop.getValue()).toBe(1); | ||
expect(log).toEqual([1]); | ||
@@ -85,4 +85,4 @@ | ||
expect(prop.hasCached()).toBe(true); | ||
expect(prop.getCached()).toBe(2); | ||
expect(prop.hasValue()).toBe(true); | ||
expect(prop.getValue()).toBe(2); | ||
expect(log).toEqual([1, 2]); | ||
@@ -93,4 +93,4 @@ | ||
expect(prop.isEnded()).toBe(true); | ||
expect(prop.hasCached()).toBe(true); | ||
expect(prop.getCached()).toBe(2); | ||
expect(prop.hasValue()).toBe(true); | ||
expect(prop.getValue()).toBe(2); | ||
@@ -102,4 +102,4 @@ | ||
expect(prop2.hasCached()).toBe(true); | ||
expect(prop2.getCached()).toBe(5); | ||
expect(prop2.hasValue()).toBe(true); | ||
expect(prop2.getValue()).toBe(5); | ||
@@ -121,4 +121,4 @@ }); | ||
expect(prop2.hasCached()).toBe(true); | ||
expect(prop2.getCached()).toBe(5); | ||
expect(prop2.hasValue()).toBe(true); | ||
expect(prop2.getValue()).toBe(5); | ||
@@ -128,3 +128,26 @@ }); | ||
it("stream.toProperty() and errors", function(){ | ||
var stream = new Kefir.Stream(); | ||
var prop = stream.toProperty(); | ||
expect(stream.__hasSubscribers('error')).toBe(false); | ||
var result = helpers.getOutputAndErrors(prop); | ||
expect(stream.__hasSubscribers('error')).toBe(true); | ||
stream.__sendValue(1); | ||
stream.__sendError('e1'); | ||
stream.__sendAny(Kefir.error('e2')); | ||
expect(result).toEqual({ | ||
ended: false, | ||
xs: [1], | ||
errors: ['e1', 'e2'] | ||
}) | ||
}); | ||
it("property.changes()", function(){ | ||
@@ -152,2 +175,25 @@ | ||
it("property.changes() and errors", function(){ | ||
var prop = new Kefir.Property(null, null, 'foo'); | ||
var changesStream = prop.changes(); | ||
expect(changesStream).toEqual(jasmine.any(Kefir.Stream)); | ||
var result = helpers.getOutputAndErrors(changesStream); | ||
prop.__sendValue(1); | ||
prop.__sendError('e1'); | ||
prop.__sendAny(Kefir.error('e2')); | ||
expect(result).toEqual({ | ||
ended: false, | ||
xs: [1], | ||
errors: ['e1', 'e2'] | ||
}) | ||
}); | ||
}); |
@@ -18,4 +18,4 @@ var Kefir = require('../../dist/kefir.js'); | ||
expect(scanned).toEqual(jasmine.any(Kefir.Property)); | ||
expect(scanned.hasCached()).toBe(true); | ||
expect(scanned.getCached()).toBe(0); | ||
expect(scanned.hasValue()).toBe(true); | ||
expect(scanned.getValue()).toBe(0); | ||
@@ -42,4 +42,4 @@ var result = helpers.getOutput(scanned); | ||
expect(scanned).toEqual(jasmine.any(Kefir.Property)); | ||
expect(scanned.hasCached()).toBe(true); | ||
expect(scanned.getCached()).toBe(11); | ||
expect(scanned.hasValue()).toBe(true); | ||
expect(scanned.getValue()).toBe(11); | ||
@@ -67,4 +67,4 @@ var result = helpers.getOutput(scanned); | ||
expect(scanned).toEqual(jasmine.any(Kefir.Property)); | ||
expect(scanned.hasCached()).toBe(true); | ||
expect(scanned.getCached()).toBe(5); | ||
expect(scanned.hasValue()).toBe(true); | ||
expect(scanned.getValue()).toBe(5); | ||
@@ -86,2 +86,23 @@ var result = helpers.getOutput(scanned); | ||
it(".scan() and errors", function(){ | ||
var stream = new Kefir.Stream(); | ||
var scanned = stream.scan(0, sum); | ||
var result = helpers.getOutputAndErrors(scanned); | ||
stream.__sendValue(1); | ||
stream.__sendError('e1'); | ||
stream.__sendAny(Kefir.error('e2')); | ||
expect(result).toEqual({ | ||
ended: false, | ||
xs: [0, 1], | ||
errors: ['e1', 'e2'] | ||
}); | ||
}); | ||
}); |
@@ -43,4 +43,4 @@ var Kefir = require('../../dist/kefir.js'); | ||
expect(mapped).toEqual(jasmine.any(Kefir.Property)); | ||
expect(mapped.hasCached()).toBe(true); | ||
expect(mapped.getCached()).toBe(5); | ||
expect(mapped.hasValue()).toBe(true); | ||
expect(mapped.getValue()).toBe(5); | ||
@@ -77,4 +77,4 @@ var result = helpers.getOutput(mapped); | ||
expect(mapped).toEqual(jasmine.any(Kefir.Property)); | ||
expect(mapped.hasCached()).toBe(false); | ||
expect(mapped.getCached()).toBe(Kefir.NOTHING); | ||
expect(mapped.hasValue()).toBe(false); | ||
expect(mapped.getValue()).toBe(Kefir.NOTHING); | ||
@@ -140,4 +140,4 @@ var result = helpers.getOutput(mapped); | ||
expect(mapped).toEqual(jasmine.any(Kefir.Property)); | ||
expect(mapped.hasCached()).toBe(true); | ||
expect(mapped.getCached()).toBe(5); | ||
expect(mapped.hasValue()).toBe(true); | ||
expect(mapped.getValue()).toBe(5); | ||
@@ -144,0 +144,0 @@ var result = helpers.getOutput(mapped); |
@@ -40,4 +40,4 @@ var Kefir = require('../../dist/kefir.js'); | ||
expect(mapped).toEqual(jasmine.any(Kefir.Property)); | ||
expect(mapped.hasCached()).toBe(false); | ||
expect(mapped.getCached()).toBe(Kefir.NOTHING); | ||
expect(mapped.hasValue()).toBe(false); | ||
expect(mapped.getValue()).toBe(Kefir.NOTHING); | ||
@@ -66,4 +66,4 @@ var result = helpers.getOutput(mapped); | ||
expect(mapped).toEqual(jasmine.any(Kefir.Property)); | ||
expect(mapped.hasCached()).toBe(true); | ||
expect(mapped.getCached()).toBe(5); | ||
expect(mapped.hasValue()).toBe(true); | ||
expect(mapped.getValue()).toBe(5); | ||
@@ -70,0 +70,0 @@ var result = helpers.getOutput(mapped); |
@@ -16,1 +16,21 @@ var Kefir = require('../dist/kefir.js'); | ||
} | ||
exports.getOutputAndErrors = function(stream) { | ||
var result = { | ||
xs: [], | ||
errors: [], | ||
ended: false | ||
}; | ||
stream.onValue(function(x){ | ||
result.xs.push(x); | ||
}); | ||
stream.onError(function(e){ | ||
result.errors.push(e); | ||
}); | ||
stream.onEnd(function(){ | ||
result.ended = true; | ||
}) | ||
return result; | ||
} |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is too big to display
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
5597537
262
93429
26
17