diff --git a/dist/xstream.js b/dist/xstream.js index 721a345..40f33d9 100644 --- a/dist/xstream.js +++ b/dist/xstream.js @@ -940,6 +940,9 @@ var Stream = (function () { } } }; + Stream.prototype.ctor = function () { + return this instanceof MemoryStream ? MemoryStream : Stream; + }; Stream.create = function (producer) { if (producer) { @@ -1008,19 +1011,23 @@ var Stream = (function () { } return new Stream(new MergeProducer(streams)); }; - - Stream.prototype.map = function (project) { + Stream.prototype._map = function (project) { var p = this._prod; + var ctor = this.ctor(); if (p instanceof FilterOperator) { - return new Stream(new FilterMapOperator(p.passes, project, p.ins)); + return new ctor(new FilterMapOperator(p.passes, project, p.ins)); } if (p instanceof FilterMapOperator) { - return new Stream(new FilterMapOperator(p.passes, compose2(project, p.project), p.ins)); + return new ctor(new FilterMapOperator(p.passes, compose2(project, p.project), p.ins)); } if (p instanceof MapOperator) { - return new Stream(new MapOperator(compose2(project, p.project), p.ins)); + return new ctor(new MapOperator(compose2(project, p.project), p.ins)); } - return new Stream(new MapOperator(project, this)); + return new ctor(new MapOperator(project, this)); + }; + + Stream.prototype.map = function (project) { + return this._map(project); }; Stream.prototype.mapTo = function (projectedValue) { @@ -1039,7 +1046,7 @@ var Stream = (function () { }; Stream.prototype.take = function (amount) { - return new Stream(new TakeOperator(amount, this)); + return new (this.ctor())(new TakeOperator(amount, this)); }; Stream.prototype.drop = function (amount) { @@ -1051,19 +1058,19 @@ var Stream = (function () { }; Stream.prototype.startWith = function (initial) { - return new Stream(new StartWithOperator(this, initial)); + return new MemoryStream(new StartWithOperator(this, initial)); }; Stream.prototype.endWhen = function (other) { - return new Stream(new EndWhenOperator(other, this)); + return new (this.ctor())(new EndWhenOperator(other, this)); }; Stream.prototype.fold = function (accumulate, seed) { - return new Stream(new FoldOperator(accumulate, seed, this)); + return new MemoryStream(new FoldOperator(accumulate, seed, this)); }; Stream.prototype.replaceError = function (replace) { - return new Stream(new ReplaceErrorOperator(replace, this)); + return new (this.ctor())(new ReplaceErrorOperator(replace, this)); }; Stream.prototype.flatten = function () { @@ -1090,7 +1097,7 @@ var Stream = (function () { }; Stream.prototype.debug = function (labelOrSpy) { - return new Stream(new DebugOperator(labelOrSpy, this)); + return new (this.ctor())(new DebugOperator(labelOrSpy, this)); }; Stream.prototype.shamefullySendNext = function (value) { @@ -1134,6 +1141,9 @@ var MimicStream = (function (_super) { }; MimicStream.prototype.imitate = function (other) { + if (other instanceof MemoryStream) { + throw new Error('bad'); + } this._target = other; }; return MimicStream; @@ -1160,6 +1170,24 @@ var MemoryStream = (function (_super) { this._has = false; _super.prototype._x.call(this); }; + MemoryStream.prototype.map = function (project) { + return this._map(project); + }; + MemoryStream.prototype.mapTo = function (projectedValue) { + return _super.prototype.mapTo.call(this, projectedValue); + }; + MemoryStream.prototype.take = function (amount) { + return _super.prototype.take.call(this, amount); + }; + MemoryStream.prototype.endWhen = function (other) { + return _super.prototype.endWhen.call(this, other); + }; + MemoryStream.prototype.replaceError = function (replace) { + return _super.prototype.replaceError.call(this, replace); + }; + MemoryStream.prototype.debug = function (labelOrSpy) { + return _super.prototype.debug.call(this, labelOrSpy); + }; return MemoryStream; }(Stream)); exports.MemoryStream = MemoryStream; diff --git a/dist/xstream.min.js b/dist/xstream.min.js index 8b8e8aa..238e8bd 100644 --- a/dist/xstream.min.js +++ b/dist/xstream.min.js @@ -1 +1 @@ -(function(f){if(typeof exports==="object"&&typeof module!=="undefined"){module.exports=f()}else if(typeof define==="function"&&define.amd){define([],f)}else{var g;if(typeof window!=="undefined"){g=window}else if(typeof global!=="undefined"){g=global}else if(typeof self!=="undefined"){g=self}else{g=this}g.xstream=f()}})(function(){var define,module,exports;return function e(t,n,r){function s(o,u){if(!n[o]){if(!t[o]){var a=typeof require=="function"&&require;if(!u&&a)return a(o,!0);if(i)return i(o,!0);var f=new Error("Cannot find module '"+o+"'");throw f.code="MODULE_NOT_FOUND",f}var l=n[o]={exports:{}};t[o][0].call(l.exports,function(e){var n=t[o][1][e];return s(n?n:e)},l,l.exports,e,t,n,r)}return n[o].exports}var i=typeof require=="function"&&require;for(var o=0;o=this.max)u._n(t)};DropOperator.prototype._e=function(err){var u=this.out;if(!u)return;u._e(err)};DropOperator.prototype._c=function(){var u=this.out;if(!u)return;u._c()};return DropOperator}();exports.DropOperator=DropOperator;var OtherIL=function(){function OtherIL(out,op){this.out=out;this.op=op}OtherIL.prototype._n=function(t){this.op.end()};OtherIL.prototype._e=function(err){this.out._e(err)};OtherIL.prototype._c=function(){this.op.end()};return OtherIL}();var EndWhenOperator=function(){function EndWhenOperator(o,ins){this.o=o;this.ins=ins;this.type="endWhen";this.out=null;this.oil=exports.emptyListener}EndWhenOperator.prototype._start=function(out){this.out=out;this.o._add(this.oil=new OtherIL(out,this));this.ins._add(this)};EndWhenOperator.prototype._stop=function(){this.ins._remove(this);this.o._remove(this.oil);this.out=null;this.oil=null};EndWhenOperator.prototype.end=function(){var u=this.out;if(!u)return;u._c()};EndWhenOperator.prototype._n=function(t){var u=this.out;if(!u)return;u._n(t)};EndWhenOperator.prototype._e=function(err){var u=this.out;if(!u)return;u._e(err)};EndWhenOperator.prototype._c=function(){this.end()};return EndWhenOperator}();exports.EndWhenOperator=EndWhenOperator;var FilterOperator=function(){function FilterOperator(passes,ins){this.passes=passes;this.ins=ins;this.type="filter";this.out=null}FilterOperator.prototype._start=function(out){this.out=out;this.ins._add(this)};FilterOperator.prototype._stop=function(){this.ins._remove(this);this.out=null};FilterOperator.prototype._n=function(t){var u=this.out;if(!u)return;try{if(this.passes(t))u._n(t)}catch(e){u._e(e)}};FilterOperator.prototype._e=function(err){var u=this.out;if(!u)return;u._e(err)};FilterOperator.prototype._c=function(){var u=this.out;if(!u)return;u._c()};return FilterOperator}();exports.FilterOperator=FilterOperator;var FlattenListener=function(){function FlattenListener(out,op){this.out=out;this.op=op}FlattenListener.prototype._n=function(t){this.out._n(t)};FlattenListener.prototype._e=function(err){this.out._e(err)};FlattenListener.prototype._c=function(){this.op.inner=null;this.op.less()};return FlattenListener}();var FlattenOperator=function(){function FlattenOperator(ins){this.ins=ins;this.type="flatten";this.inner=null;this.il=null;this.open=true;this.out=null}FlattenOperator.prototype._start=function(out){this.out=out;this.ins._add(this)};FlattenOperator.prototype._stop=function(){this.ins._remove(this);this.inner=null;this.il=null;this.open=true;this.out=null};FlattenOperator.prototype.less=function(){var u=this.out;if(!u)return;if(!this.open&&!this.inner)u._c()};FlattenOperator.prototype._n=function(s){var u=this.out;if(!u)return;var _a=this,inner=_a.inner,il=_a.il;if(inner&&il)inner._remove(il);(this.inner=s)._add(this.il=new FlattenListener(u,this))};FlattenOperator.prototype._e=function(err){var u=this.out;if(!u)return;u._e(err)};FlattenOperator.prototype._c=function(){this.open=false;this.less()};return FlattenOperator}();exports.FlattenOperator=FlattenOperator;var FoldOperator=function(){function FoldOperator(f,seed,ins){this.f=f;this.seed=seed;this.ins=ins;this.type="fold";this.out=null;this.acc=seed}FoldOperator.prototype._start=function(out){this.out=out;out._n(this.acc);this.ins._add(this)};FoldOperator.prototype._stop=function(){this.ins._remove(this);this.out=null;this.acc=this.seed};FoldOperator.prototype._n=function(t){var u=this.out;if(!u)return;try{u._n(this.acc=this.f(this.acc,t))}catch(e){u._e(e)}};FoldOperator.prototype._e=function(err){var u=this.out;if(!u)return;u._e(err)};FoldOperator.prototype._c=function(){var u=this.out;if(!u)return;u._c()};return FoldOperator}();exports.FoldOperator=FoldOperator;var LastOperator=function(){function LastOperator(ins){this.ins=ins;this.type="last";this.out=null;this.has=false;this.val=empty}LastOperator.prototype._start=function(out){this.out=out;this.ins._add(this)};LastOperator.prototype._stop=function(){this.ins._remove(this);this.out=null;this.has=false;this.val=empty};LastOperator.prototype._n=function(t){this.has=true;this.val=t};LastOperator.prototype._e=function(err){var u=this.out;if(!u)return;u._e(err)};LastOperator.prototype._c=function(){var u=this.out;if(!u)return;if(this.has){u._n(this.val);u._c()}else{u._e("TODO show proper error")}};return LastOperator}();exports.LastOperator=LastOperator;var MapFlattenInner=function(){function MapFlattenInner(out,op){this.out=out;this.op=op}MapFlattenInner.prototype._n=function(r){this.out._n(r)};MapFlattenInner.prototype._e=function(err){this.out._e(err)};MapFlattenInner.prototype._c=function(){this.op.inner=null;this.op.less()};return MapFlattenInner}();var MapFlattenOperator=function(){function MapFlattenOperator(mapOp){this.mapOp=mapOp;this.inner=null;this.il=null;this.open=true;this.out=null;this.type=mapOp.type+"+flatten";this.ins=mapOp.ins}MapFlattenOperator.prototype._start=function(out){this.out=out;this.mapOp.ins._add(this)};MapFlattenOperator.prototype._stop=function(){this.mapOp.ins._remove(this);this.inner=null;this.il=null;this.open=true;this.out=null};MapFlattenOperator.prototype.less=function(){if(!this.open&&!this.inner){var u=this.out;if(!u)return;u._c()}};MapFlattenOperator.prototype._n=function(v){var u=this.out;if(!u)return;var _a=this,inner=_a.inner,il=_a.il;if(inner&&il)inner._remove(il);try{(this.inner=this.mapOp.project(v))._add(this.il=new MapFlattenInner(u,this))}catch(e){u._e(e)}};MapFlattenOperator.prototype._e=function(err){var u=this.out;if(!u)return;u._e(err)};MapFlattenOperator.prototype._c=function(){this.open=false;this.less()};return MapFlattenOperator}();exports.MapFlattenOperator=MapFlattenOperator;var MapOperator=function(){function MapOperator(project,ins){this.project=project;this.ins=ins;this.type="map";this.out=null}MapOperator.prototype._start=function(out){this.out=out;this.ins._add(this)};MapOperator.prototype._stop=function(){this.ins._remove(this);this.out=null};MapOperator.prototype._n=function(t){var u=this.out;if(!u)return;try{u._n(this.project(t))}catch(e){u._e(e)}};MapOperator.prototype._e=function(err){var u=this.out;if(!u)return;u._e(err)};MapOperator.prototype._c=function(){var u=this.out;if(!u)return;u._c()};return MapOperator}();exports.MapOperator=MapOperator;var FilterMapOperator=function(_super){__extends(FilterMapOperator,_super);function FilterMapOperator(passes,project,ins){_super.call(this,project,ins);this.passes=passes;this.type="filter+map"}FilterMapOperator.prototype._n=function(v){if(this.passes(v)){_super.prototype._n.call(this,v)}};return FilterMapOperator}(MapOperator);exports.FilterMapOperator=FilterMapOperator;var ReplaceErrorOperator=function(){function ReplaceErrorOperator(fn,ins){this.fn=fn;this.ins=ins;this.type="replaceError";this.out=empty}ReplaceErrorOperator.prototype._start=function(out){this.out=out;this.ins._add(this)};ReplaceErrorOperator.prototype._stop=function(){this.ins._remove(this);this.out=null};ReplaceErrorOperator.prototype._n=function(t){var u=this.out;if(!u)return;u._n(t)};ReplaceErrorOperator.prototype._e=function(err){var u=this.out;if(!u)return;try{this.ins._remove(this);(this.ins=this.fn(err))._add(this)}catch(e){u._e(e)}};ReplaceErrorOperator.prototype._c=function(){var u=this.out;if(!u)return;u._c()};return ReplaceErrorOperator}();exports.ReplaceErrorOperator=ReplaceErrorOperator;var StartWithOperator=function(){function StartWithOperator(ins,value){this.ins=ins;this.value=value;this.type="startWith";this.out=exports.emptyListener}StartWithOperator.prototype._start=function(out){this.out=out;this.out._n(this.value);this.ins._add(out)};StartWithOperator.prototype._stop=function(){this.ins._remove(this.out);this.out=null};return StartWithOperator}();exports.StartWithOperator=StartWithOperator;var TakeOperator=function(){function TakeOperator(max,ins){this.max=max;this.ins=ins;this.type="take";this.out=null;this.taken=0}TakeOperator.prototype._start=function(out){this.out=out;this.ins._add(this)};TakeOperator.prototype._stop=function(){this.ins._remove(this);this.out=null;this.taken=0};TakeOperator.prototype._n=function(t){var u=this.out;if(!u)return;if(this.taken++-1){a.splice(i,1);var p_1=this._prod;if(p_1&&a.length<=0){this._stopID=setTimeout(function(){return p_1._stop()})}}};Stream.create=function(producer){if(producer){if(typeof producer.start!=="function"||typeof producer.stop!=="function"){throw new Error("producer requires both start and stop functions")}internalizeProducer(producer)}return new Stream(producer)};Stream.createWithMemory=function(producer){if(producer){internalizeProducer(producer)}return new MemoryStream(producer)};Stream.createMimic=function(){return new MimicStream};Stream.never=function(){return new Stream({_start:noop,_stop:noop})};Stream.empty=function(){return new Stream({_start:function(il){il._c()},_stop:noop})};Stream.throw=function(error){return new Stream({_start:function(il){il._e(error)},_stop:noop})};Stream.of=function(){var items=[];for(var _i=0;_i=this.max)u._n(t)};DropOperator.prototype._e=function(err){var u=this.out;if(!u)return;u._e(err)};DropOperator.prototype._c=function(){var u=this.out;if(!u)return;u._c()};return DropOperator}();exports.DropOperator=DropOperator;var OtherIL=function(){function OtherIL(out,op){this.out=out;this.op=op}OtherIL.prototype._n=function(t){this.op.end()};OtherIL.prototype._e=function(err){this.out._e(err)};OtherIL.prototype._c=function(){this.op.end()};return OtherIL}();var EndWhenOperator=function(){function EndWhenOperator(o,ins){this.o=o;this.ins=ins;this.type="endWhen";this.out=null;this.oil=exports.emptyListener}EndWhenOperator.prototype._start=function(out){this.out=out;this.o._add(this.oil=new OtherIL(out,this));this.ins._add(this)};EndWhenOperator.prototype._stop=function(){this.ins._remove(this);this.o._remove(this.oil);this.out=null;this.oil=null};EndWhenOperator.prototype.end=function(){var u=this.out;if(!u)return;u._c()};EndWhenOperator.prototype._n=function(t){var u=this.out;if(!u)return;u._n(t)};EndWhenOperator.prototype._e=function(err){var u=this.out;if(!u)return;u._e(err)};EndWhenOperator.prototype._c=function(){this.end()};return EndWhenOperator}();exports.EndWhenOperator=EndWhenOperator;var FilterOperator=function(){function FilterOperator(passes,ins){this.passes=passes;this.ins=ins;this.type="filter";this.out=null}FilterOperator.prototype._start=function(out){this.out=out;this.ins._add(this)};FilterOperator.prototype._stop=function(){this.ins._remove(this);this.out=null};FilterOperator.prototype._n=function(t){var u=this.out;if(!u)return;try{if(this.passes(t))u._n(t)}catch(e){u._e(e)}};FilterOperator.prototype._e=function(err){var u=this.out;if(!u)return;u._e(err)};FilterOperator.prototype._c=function(){var u=this.out;if(!u)return;u._c()};return FilterOperator}();exports.FilterOperator=FilterOperator;var FlattenListener=function(){function FlattenListener(out,op){this.out=out;this.op=op}FlattenListener.prototype._n=function(t){this.out._n(t)};FlattenListener.prototype._e=function(err){this.out._e(err)};FlattenListener.prototype._c=function(){this.op.inner=null;this.op.less()};return FlattenListener}();var FlattenOperator=function(){function FlattenOperator(ins){this.ins=ins;this.type="flatten";this.inner=null;this.il=null;this.open=true;this.out=null}FlattenOperator.prototype._start=function(out){this.out=out;this.ins._add(this)};FlattenOperator.prototype._stop=function(){this.ins._remove(this);this.inner=null;this.il=null;this.open=true;this.out=null};FlattenOperator.prototype.less=function(){var u=this.out;if(!u)return;if(!this.open&&!this.inner)u._c()};FlattenOperator.prototype._n=function(s){var u=this.out;if(!u)return;var _a=this,inner=_a.inner,il=_a.il;if(inner&&il)inner._remove(il);(this.inner=s)._add(this.il=new FlattenListener(u,this))};FlattenOperator.prototype._e=function(err){var u=this.out;if(!u)return;u._e(err)};FlattenOperator.prototype._c=function(){this.open=false;this.less()};return FlattenOperator}();exports.FlattenOperator=FlattenOperator;var FoldOperator=function(){function FoldOperator(f,seed,ins){this.f=f;this.seed=seed;this.ins=ins;this.type="fold";this.out=null;this.acc=seed}FoldOperator.prototype._start=function(out){this.out=out;out._n(this.acc);this.ins._add(this)};FoldOperator.prototype._stop=function(){this.ins._remove(this);this.out=null;this.acc=this.seed};FoldOperator.prototype._n=function(t){var u=this.out;if(!u)return;try{u._n(this.acc=this.f(this.acc,t))}catch(e){u._e(e)}};FoldOperator.prototype._e=function(err){var u=this.out;if(!u)return;u._e(err)};FoldOperator.prototype._c=function(){var u=this.out;if(!u)return;u._c()};return FoldOperator}();exports.FoldOperator=FoldOperator;var LastOperator=function(){function LastOperator(ins){this.ins=ins;this.type="last";this.out=null;this.has=false;this.val=empty}LastOperator.prototype._start=function(out){this.out=out;this.ins._add(this)};LastOperator.prototype._stop=function(){this.ins._remove(this);this.out=null;this.has=false;this.val=empty};LastOperator.prototype._n=function(t){this.has=true;this.val=t};LastOperator.prototype._e=function(err){var u=this.out;if(!u)return;u._e(err)};LastOperator.prototype._c=function(){var u=this.out;if(!u)return;if(this.has){u._n(this.val);u._c()}else{u._e("TODO show proper error")}};return LastOperator}();exports.LastOperator=LastOperator;var MapFlattenInner=function(){function MapFlattenInner(out,op){this.out=out;this.op=op}MapFlattenInner.prototype._n=function(r){this.out._n(r)};MapFlattenInner.prototype._e=function(err){this.out._e(err)};MapFlattenInner.prototype._c=function(){this.op.inner=null;this.op.less()};return MapFlattenInner}();var MapFlattenOperator=function(){function MapFlattenOperator(mapOp){this.mapOp=mapOp;this.inner=null;this.il=null;this.open=true;this.out=null;this.type=mapOp.type+"+flatten";this.ins=mapOp.ins}MapFlattenOperator.prototype._start=function(out){this.out=out;this.mapOp.ins._add(this)};MapFlattenOperator.prototype._stop=function(){this.mapOp.ins._remove(this);this.inner=null;this.il=null;this.open=true;this.out=null};MapFlattenOperator.prototype.less=function(){if(!this.open&&!this.inner){var u=this.out;if(!u)return;u._c()}};MapFlattenOperator.prototype._n=function(v){var u=this.out;if(!u)return;var _a=this,inner=_a.inner,il=_a.il;if(inner&&il)inner._remove(il);try{(this.inner=this.mapOp.project(v))._add(this.il=new MapFlattenInner(u,this))}catch(e){u._e(e)}};MapFlattenOperator.prototype._e=function(err){var u=this.out;if(!u)return;u._e(err)};MapFlattenOperator.prototype._c=function(){this.open=false;this.less()};return MapFlattenOperator}();exports.MapFlattenOperator=MapFlattenOperator;var MapOperator=function(){function MapOperator(project,ins){this.project=project;this.ins=ins;this.type="map";this.out=null}MapOperator.prototype._start=function(out){this.out=out;this.ins._add(this)};MapOperator.prototype._stop=function(){this.ins._remove(this);this.out=null};MapOperator.prototype._n=function(t){var u=this.out;if(!u)return;try{u._n(this.project(t))}catch(e){u._e(e)}};MapOperator.prototype._e=function(err){var u=this.out;if(!u)return;u._e(err)};MapOperator.prototype._c=function(){var u=this.out;if(!u)return;u._c()};return MapOperator}();exports.MapOperator=MapOperator;var FilterMapOperator=function(_super){__extends(FilterMapOperator,_super);function FilterMapOperator(passes,project,ins){_super.call(this,project,ins);this.passes=passes;this.type="filter+map"}FilterMapOperator.prototype._n=function(v){if(this.passes(v)){_super.prototype._n.call(this,v)}};return FilterMapOperator}(MapOperator);exports.FilterMapOperator=FilterMapOperator;var ReplaceErrorOperator=function(){function ReplaceErrorOperator(fn,ins){this.fn=fn;this.ins=ins;this.type="replaceError";this.out=empty}ReplaceErrorOperator.prototype._start=function(out){this.out=out;this.ins._add(this)};ReplaceErrorOperator.prototype._stop=function(){this.ins._remove(this);this.out=null};ReplaceErrorOperator.prototype._n=function(t){var u=this.out;if(!u)return;u._n(t)};ReplaceErrorOperator.prototype._e=function(err){var u=this.out;if(!u)return;try{this.ins._remove(this);(this.ins=this.fn(err))._add(this)}catch(e){u._e(e)}};ReplaceErrorOperator.prototype._c=function(){var u=this.out;if(!u)return;u._c()};return ReplaceErrorOperator}();exports.ReplaceErrorOperator=ReplaceErrorOperator;var StartWithOperator=function(){function StartWithOperator(ins,value){this.ins=ins;this.value=value;this.type="startWith";this.out=exports.emptyListener}StartWithOperator.prototype._start=function(out){this.out=out;this.out._n(this.value);this.ins._add(out)};StartWithOperator.prototype._stop=function(){this.ins._remove(this.out);this.out=null};return StartWithOperator}();exports.StartWithOperator=StartWithOperator;var TakeOperator=function(){function TakeOperator(max,ins){this.max=max;this.ins=ins;this.type="take";this.out=null;this.taken=0}TakeOperator.prototype._start=function(out){this.out=out;this.ins._add(this)};TakeOperator.prototype._stop=function(){this.ins._remove(this);this.out=null;this.taken=0};TakeOperator.prototype._n=function(t){var u=this.out;if(!u)return;if(this.taken++-1){a.splice(i,1);var p_1=this._prod;if(p_1&&a.length<=0){this._stopID=setTimeout(function(){return p_1._stop()})}}};Stream.prototype.ctor=function(){return this instanceof MemoryStream?MemoryStream:Stream};Stream.create=function(producer){if(producer){if(typeof producer.start!=="function"||typeof producer.stop!=="function"){throw new Error("producer requires both start and stop functions")}internalizeProducer(producer)}return new Stream(producer)};Stream.createWithMemory=function(producer){if(producer){internalizeProducer(producer)}return new MemoryStream(producer)};Stream.createMimic=function(){return new MimicStream};Stream.never=function(){return new Stream({_start:noop,_stop:noop})};Stream.empty=function(){return new Stream({_start:function(il){il._c()},_stop:noop})};Stream.throw=function(error){return new Stream({_start:function(il){il._e(error)},_stop:noop})};Stream.of=function(){var items=[];for(var _i=0;_i implements InternalListener { } } + private ctor(): typeof Stream { + return this instanceof MemoryStream ? MemoryStream : Stream; + } + /** * Creates a new Stream given a Producer. * @@ -1295,46 +1299,51 @@ export class Stream implements InternalListener { return new Stream(new CombineProducer(project, streams)); }; - /** - * Transforms each event from the input Stream through a `project` function, - * to get a Stream that emits those transformed events. - * - * Marble diagram: - * - * ```text - * --1---3--5-----7------ - * map(i => i * 10) - * --10--30-50----70----- - * ``` - * - * @param {Function} project A function of type `(t: T) => U` that takes event - * `t` of type `T` from the input Stream and produces an event of type `U`, to - * be emitted on the output Stream. - * @return {Stream} - */ - map(project: (t: T) => U): Stream { + protected _map(project: (t: T) => U): Stream | MemoryStream { const p = this._prod; + const ctor = this.ctor(); if (p instanceof FilterOperator) { - return new Stream(new FilterMapOperator( + return new ctor(new FilterMapOperator( (> p).passes, project, (> p).ins )); } if (p instanceof FilterMapOperator) { - return new Stream(new FilterMapOperator( + return new ctor(new FilterMapOperator( (> p).passes, compose2(project, (> p).project), (> p).ins )); } if (p instanceof MapOperator) { - return new Stream(new MapOperator( + return new ctor(new MapOperator( compose2(project, (> p).project), (> p).ins )); } - return new Stream(new MapOperator(project, this)); + return new ctor(new MapOperator(project, this)); + } + + /** + * Transforms each event from the input Stream through a `project` function, + * to get a Stream that emits those transformed events. + * + * Marble diagram: + * + * ```text + * --1---3--5-----7------ + * map(i => i * 10) + * --10--30-50----70----- + * ``` + * + * @param {Function} project A function of type `(t: T) => U` that takes event + * `t` of type `T` from the input Stream and produces an event of type `U`, to + * be emitted on the output Stream. + * @return {Stream} + */ + map(project: (t: T) => U): Stream { + return this._map(project); } /** @@ -1408,7 +1417,7 @@ export class Stream implements InternalListener { * @return {Stream} */ take(amount: number): Stream { - return new Stream(new TakeOperator(amount, this)); + return new (this.ctor())(new TakeOperator(amount, this)); } /** @@ -1465,8 +1474,8 @@ export class Stream implements InternalListener { * @param initial The value or event to prepend. * @return {Stream} */ - startWith(initial: T): Stream { - return new Stream(new StartWithOperator(this, initial)); + startWith(initial: T): MemoryStream { + return new MemoryStream(new StartWithOperator(this, initial)); } /** @@ -1489,7 +1498,7 @@ export class Stream implements InternalListener { * @return {Stream} */ endWhen(other: Stream): Stream { - return new Stream(new EndWhenOperator(other, this)); + return new (this.ctor())(new EndWhenOperator(other, this)); } /** @@ -1520,8 +1529,8 @@ export class Stream implements InternalListener { * @param seed The initial accumulated value, of type `R`. * @return {Stream} */ - fold(accumulate: (acc: R, t: T) => R, seed: R): Stream { - return new Stream(new FoldOperator(accumulate, seed, this)); + fold(accumulate: (acc: R, t: T) => R, seed: R): MemoryStream { + return new MemoryStream(new FoldOperator(accumulate, seed, this)); } /** @@ -1548,7 +1557,7 @@ export class Stream implements InternalListener { * @return {Stream} */ replaceError(replace: (err: any) => Stream): Stream { - return new Stream(new ReplaceErrorOperator(replace, this)); + return new (this.ctor())(new ReplaceErrorOperator(replace, this)); } /** @@ -1700,7 +1709,7 @@ export class Stream implements InternalListener { * @return {Stream} */ debug(labelOrSpy?: string | ((t: T) => void)): Stream { - return new Stream(new DebugOperator(labelOrSpy, this)); + return new (this.ctor())(new DebugOperator(labelOrSpy, this)); } /** @@ -1770,9 +1779,15 @@ export class MimicStream extends Stream { * the current stream, making it re-emit whatever events are emitted by the * given `other` stream. * - * @param {Stream} other The stream to imitate on the current one. + * @param {Stream} other The stream to imitate on the current one. Must not be + * a MemoryStream. */ imitate(other: Stream): void { + if (other instanceof MemoryStream) { + throw new Error('A MemoryStream was given to imitate(), but it only ' + + 'supports a Stream. Read more about this restriction here: ' + + 'https://github.com/staltz/xstream#faq'); + } this._target = other; } } @@ -1799,6 +1814,30 @@ export class MemoryStream extends Stream { this._has = false; super._x(); } + + map(project: (t: T) => U): MemoryStream { + return > this._map(project); + } + + mapTo(projectedValue: U): MemoryStream { + return > super.mapTo(projectedValue); + } + + take(amount: number): MemoryStream { + return > super.take(amount); + } + + endWhen(other: Stream): MemoryStream { + return > super.endWhen(other); + } + + replaceError(replace: (err: any) => Stream): MemoryStream { + return > super.replaceError(replace); + } + + debug(labelOrSpy?: string | ((t: T) => void)): MemoryStream { + return > super.debug(labelOrSpy); + } } export default Stream; diff --git a/tests/extra/concat.ts b/tests/extra/concat.ts index bf6dcf4..a64fd90 100644 --- a/tests/extra/concat.ts +++ b/tests/extra/concat.ts @@ -1,3 +1,5 @@ +/// +/// import xs from '../../src/index'; import concat from '../../src/extra/concat'; import * as assert from 'assert'; diff --git a/tests/extra/debounce.ts b/tests/extra/debounce.ts index 4592f25..093dcca 100644 --- a/tests/extra/debounce.ts +++ b/tests/extra/debounce.ts @@ -1,3 +1,5 @@ +/// +/// import xs, {Listener, Producer} from '../../src/index'; import debounce from '../../src/extra/debounce'; import * as assert from 'assert'; diff --git a/tests/extra/delay.ts b/tests/extra/delay.ts index c2afc8f..f60f5ba 100644 --- a/tests/extra/delay.ts +++ b/tests/extra/delay.ts @@ -1,3 +1,5 @@ +/// +/// import xs from '../../src/index'; import delay from '../../src/extra/delay'; import * as assert from 'assert'; diff --git a/tests/extra/dropRepeats.ts b/tests/extra/dropRepeats.ts index 53283b8..77172ad 100644 --- a/tests/extra/dropRepeats.ts +++ b/tests/extra/dropRepeats.ts @@ -1,3 +1,5 @@ +/// +/// import xs, {Stream} from '../../src/index'; import dropRepeats from '../../src/extra/dropRepeats'; import * as assert from 'assert'; diff --git a/tests/extra/dropUntil.ts b/tests/extra/dropUntil.ts index 43ea387..6bc1845 100644 --- a/tests/extra/dropUntil.ts +++ b/tests/extra/dropUntil.ts @@ -1,3 +1,5 @@ +/// +/// import xs from '../../src/index'; import dropUntil from '../../src/extra/dropUntil'; import delay from '../../src/extra/delay'; diff --git a/tests/extra/flattenConcurrently.ts b/tests/extra/flattenConcurrently.ts index deeada5..e318ecc 100644 --- a/tests/extra/flattenConcurrently.ts +++ b/tests/extra/flattenConcurrently.ts @@ -1,3 +1,5 @@ +/// +/// import xs, {Stream, Listener} from '../../src/index'; import flattenConcurrently from '../../src/extra/flattenConcurrently'; import * as assert from 'assert'; diff --git a/tests/extra/flattenSequentially.ts b/tests/extra/flattenSequentially.ts index 62fed64..f4b65d0 100644 --- a/tests/extra/flattenSequentially.ts +++ b/tests/extra/flattenSequentially.ts @@ -1,3 +1,5 @@ +/// +/// import xs from '../../src/index'; import flattenSequentially from '../../src/extra/flattenSequentially'; import * as assert from 'assert'; diff --git a/tests/extra/fromDiagram.ts b/tests/extra/fromDiagram.ts index fffbf93..fb8aeec 100644 --- a/tests/extra/fromDiagram.ts +++ b/tests/extra/fromDiagram.ts @@ -1,3 +1,5 @@ +/// +/// import xs from '../../src/index'; import fromDiagram from '../../src/extra/fromDiagram'; import * as assert from 'assert'; diff --git a/tests/extra/fromEvent.ts b/tests/extra/fromEvent.ts index f88b606..7f57f40 100644 --- a/tests/extra/fromEvent.ts +++ b/tests/extra/fromEvent.ts @@ -1,3 +1,5 @@ +/// +/// import xs from '../../src/index'; import fromEvent from '../../src/extra/fromEvent'; import * as assert from 'assert'; diff --git a/tests/extra/pairwise.ts b/tests/extra/pairwise.ts index 6a6a40b..1422197 100644 --- a/tests/extra/pairwise.ts +++ b/tests/extra/pairwise.ts @@ -1,3 +1,5 @@ +/// +/// import xs from '../../src/index'; import pairwise from '../../src/extra/pairwise'; import * as assert from 'assert'; diff --git a/tests/extra/split.ts b/tests/extra/split.ts index 2f7232b..0d07fa9 100644 --- a/tests/extra/split.ts +++ b/tests/extra/split.ts @@ -1,3 +1,5 @@ +/// +/// import xs, {Stream} from '../../src/index'; import split from '../../src/extra/split'; import concat from '../../src/extra/concat'; diff --git a/tests/factory/combine.ts b/tests/factory/combine.ts index 724dd71..49a841b 100644 --- a/tests/factory/combine.ts +++ b/tests/factory/combine.ts @@ -1,3 +1,5 @@ +/// +/// import xs, {Stream} from '../../src/index'; import * as assert from 'assert'; diff --git a/tests/factory/empty.ts b/tests/factory/empty.ts index 49098a2..5046d58 100644 --- a/tests/factory/empty.ts +++ b/tests/factory/empty.ts @@ -1,3 +1,5 @@ +/// +/// import xs from '../../src/index'; describe('xs.empty()', function() { diff --git a/tests/factory/fromArray.ts b/tests/factory/fromArray.ts index 95bea90..ea9c737 100644 --- a/tests/factory/fromArray.ts +++ b/tests/factory/fromArray.ts @@ -1,3 +1,5 @@ +/// +/// import xs from '../../src/index'; import * as assert from 'assert'; diff --git a/tests/factory/fromPromise.ts b/tests/factory/fromPromise.ts index 8499153..2e7298d 100644 --- a/tests/factory/fromPromise.ts +++ b/tests/factory/fromPromise.ts @@ -1,3 +1,5 @@ +/// +/// import {Promise} from 'es6-promise'; import xs from '../../src/index'; import * as assert from 'assert'; diff --git a/tests/factory/merge.ts b/tests/factory/merge.ts index b0e9071..2fb430b 100644 --- a/tests/factory/merge.ts +++ b/tests/factory/merge.ts @@ -1,3 +1,5 @@ +/// +/// import xs from '../../src/index'; import * as assert from 'assert'; diff --git a/tests/factory/never.ts b/tests/factory/never.ts index ec3b50a..8b233c3 100644 --- a/tests/factory/never.ts +++ b/tests/factory/never.ts @@ -1,3 +1,5 @@ +/// +/// import xs from '../../src/index'; describe('xs.never()', () => { diff --git a/tests/factory/of.ts b/tests/factory/of.ts index c89031d..e819100 100644 --- a/tests/factory/of.ts +++ b/tests/factory/of.ts @@ -1,3 +1,5 @@ +/// +/// import xs from '../../src/index'; import * as assert from 'assert'; diff --git a/tests/factory/throw.ts b/tests/factory/throw.ts index 0733104..c652a09 100644 --- a/tests/factory/throw.ts +++ b/tests/factory/throw.ts @@ -1,3 +1,5 @@ +/// +/// import xs from '../../src/index'; import * as assert from 'assert'; diff --git a/tests/memoryStream.ts b/tests/memoryStream.ts index 80d47af..38ae6fc 100644 --- a/tests/memoryStream.ts +++ b/tests/memoryStream.ts @@ -1,3 +1,5 @@ +/// +/// import xs, {Producer, Listener, Stream} from '../src/index'; import * as assert from 'assert'; diff --git a/tests/mimicStream.ts b/tests/mimicStream.ts index 24e29ce..723db81 100644 --- a/tests/mimicStream.ts +++ b/tests/mimicStream.ts @@ -1,3 +1,5 @@ +/// +/// import xs, {Producer, Listener, Stream, MimicStream} from '../src/index'; import delay from '../src/extra/delay'; import * as assert from 'assert'; diff --git a/tests/operator/combine.ts b/tests/operator/combine.ts index 9d53b1f..324d54c 100644 --- a/tests/operator/combine.ts +++ b/tests/operator/combine.ts @@ -1,4 +1,6 @@ -import xs from '../../src/index'; +/// +/// +import xs, {Stream} from '../../src/index'; import * as assert from 'assert'; describe('Stream.prototype.combine', () => { @@ -54,4 +56,20 @@ describe('Stream.prototype.combine', () => { }, }); }); + + it('should return a Stream when combining a MemoryStream with a Stream', (done) => { + const input1 = xs.periodic(50).take(4).remember(); + const input2 = xs.periodic(80).take(3); + const stream: Stream = input1.combine((x, y) => x + y, input2); + assert.strictEqual(stream instanceof Stream, true); + done(); + }); + + it('should return a Stream when combining a MemoryStream with a MemoryStream', (done) => { + const input1 = xs.periodic(50).take(4).remember(); + const input2 = xs.periodic(80).take(3).remember(); + const stream: Stream = input1.combine((x, y) => x + y, input2); + assert.strictEqual(stream instanceof Stream, true); + done(); + }); }); diff --git a/tests/operator/debug.ts b/tests/operator/debug.ts index d798945..46e65d9 100644 --- a/tests/operator/debug.ts +++ b/tests/operator/debug.ts @@ -1,4 +1,6 @@ -import xs from '../../src/index'; +/// +/// +import xs, {Stream, MemoryStream} from '../../src/index'; import * as assert from 'assert'; describe('Stream.prototype.debug', () => { @@ -21,6 +23,22 @@ describe('Stream.prototype.debug', () => { stream.addListener(listener); }); + it('should return a Stream if input stream is a Stream', (done) => { + const input = xs.of(1, 2, 3); + assert.strictEqual(input instanceof Stream, true); + const stream: Stream = input.debug('stream'); + assert.strictEqual(stream instanceof Stream, true); + done(); + }); + + it('should return a MemoryStream if input stream is a MemoryStream', (done) => { + const input = xs.of(1, 2, 3).remember(); + assert.strictEqual(input instanceof MemoryStream, true); + const stream: MemoryStream = input.debug('stream'); + assert.strictEqual(stream instanceof MemoryStream, true); + done(); + }); + it('should propagate user mistakes in spy as errors', (done) => { const source = xs.periodic(30).take(1); const stream = source.debug( diff --git a/tests/operator/drop.ts b/tests/operator/drop.ts index 000ea26..1cdd7ec 100644 --- a/tests/operator/drop.ts +++ b/tests/operator/drop.ts @@ -1,4 +1,6 @@ -import xs from '../../src/index'; +/// +/// +import xs, {Stream, MemoryStream} from '../../src/index'; import * as assert from 'assert'; describe('Stream.prototype.drop', () => { @@ -18,4 +20,20 @@ describe('Stream.prototype.drop', () => { }; stream.addListener(listener); }); + + it('should return a Stream if input stream is a Stream', (done) => { + const input = xs.of(1, 2, 3); + assert.strictEqual(input instanceof Stream, true); + const stream: Stream = input.drop(1); + assert.strictEqual(stream instanceof Stream, true); + done(); + }); + + it('should return a Stream if input stream is a MemoryStream', (done) => { + const input = xs.of(1, 2, 3).remember(); + assert.strictEqual(input instanceof MemoryStream, true); + const stream: Stream = input.drop(1); + assert.strictEqual(stream instanceof Stream, true); + done(); + }); }); diff --git a/tests/operator/endWhen.ts b/tests/operator/endWhen.ts index 7bb9270..45abf16 100644 --- a/tests/operator/endWhen.ts +++ b/tests/operator/endWhen.ts @@ -1,4 +1,6 @@ -import xs from '../../src/index'; +/// +/// +import xs, {Stream, MemoryStream} from '../../src/index'; import delay from '../../src/extra/delay'; import * as assert from 'assert'; @@ -38,4 +40,20 @@ describe('Stream.prototype.endWhen', () => { }, }); }); + + it('should return a Stream if input stream is a Stream', (done) => { + const input = xs.of(1, 2, 3); + assert.strictEqual(input instanceof Stream, true); + const stream: Stream = input.endWhen(xs.never()); + assert.strictEqual(stream instanceof Stream, true); + done(); + }); + + it('should return a MemoryStream if input stream is a MemoryStream', (done) => { + const input = xs.of(1, 2, 3).remember(); + assert.strictEqual(input instanceof MemoryStream, true); + const stream: MemoryStream = input.endWhen(xs.never()); + assert.strictEqual(stream instanceof MemoryStream, true); + done(); + }); }); diff --git a/tests/operator/filter.ts b/tests/operator/filter.ts index d190ccd..1e7019a 100644 --- a/tests/operator/filter.ts +++ b/tests/operator/filter.ts @@ -1,4 +1,6 @@ -import xs from '../../src/index'; +/// +/// +import xs, {Stream, MemoryStream} from '../../src/index'; import * as assert from 'assert'; describe('Stream.prototype.filter', () => { @@ -98,4 +100,20 @@ describe('Stream.prototype.filter', () => { assert.strictEqual(stream['_prod']['type'], 'filter+mapTo'); done(); }); + + it('should return a Stream if input stream is a Stream', (done) => { + const input = xs.of(1, 2, 3); + assert.strictEqual(input instanceof Stream, true); + const stream: Stream = input.filter(x => x % 2 === 0); + assert.strictEqual(stream instanceof Stream, true); + done(); + }); + + it('should return a Stream if input stream is a MemoryStream', (done) => { + const input = xs.of(1, 2, 3).remember(); + assert.strictEqual(input instanceof MemoryStream, true); + const stream: Stream = input.filter(x => x % 2 === 0); + assert.strictEqual(stream instanceof Stream, true); + done(); + }); }); diff --git a/tests/operator/flatten.ts b/tests/operator/flatten.ts index 6c52235..26c1eae 100644 --- a/tests/operator/flatten.ts +++ b/tests/operator/flatten.ts @@ -1,3 +1,5 @@ +/// +/// import xs, {Stream, Listener} from '../../src/index'; import * as assert from 'assert'; diff --git a/tests/operator/fold.ts b/tests/operator/fold.ts index 4c5ca71..2b529dc 100644 --- a/tests/operator/fold.ts +++ b/tests/operator/fold.ts @@ -1,4 +1,6 @@ -import xs from '../../src/index'; +/// +/// +import xs, {Stream, MemoryStream} from '../../src/index'; import * as assert from 'assert'; describe('Stream.prototype.fold', () => { @@ -41,4 +43,20 @@ describe('Stream.prototype.fold', () => { }, }); }); + + it('should return a MemoryStream if input stream is a Stream', (done) => { + const input = xs.of(1, 2, 3); + assert.strictEqual(input instanceof Stream, true); + const stream: MemoryStream = input.fold((acc, x) => acc + x, 0); + assert.strictEqual(stream instanceof MemoryStream, true); + done(); + }); + + it('should return a MemoryStream if input stream is a MemoryStream', (done) => { + const input = xs.of(1, 2, 3).remember(); + assert.strictEqual(input instanceof MemoryStream, true); + const stream: MemoryStream = input.fold((acc, x) => acc + x, 0); + assert.strictEqual(stream instanceof MemoryStream, true); + done();; + }) }); diff --git a/tests/operator/imitate.ts b/tests/operator/imitate.ts index 65691e0..96c6c20 100644 --- a/tests/operator/imitate.ts +++ b/tests/operator/imitate.ts @@ -1,4 +1,6 @@ -import xs from '../../src/index'; +/// +/// +import xs, {Stream, MemoryStream} from '../../src/index'; import * as assert from 'assert'; describe('MimicStream.prototype.imitate', () => { @@ -37,23 +39,13 @@ describe('MimicStream.prototype.imitate', () => { proxyStream.imitate(stream); }); -// it('should link the given stream to the mimic stream', (done) => { -// const stream = xs.periodic(50).take(3); -// const proxyStream = xs.createMimic(); -// proxyStream.imitate(stream); - -// const expected = [0, 1, 2]; -// setTimeout(() => { -// proxyStream.addListener({ -// next: (x: number) => { -// assert.equal(x, expected.shift()); -// }, -// error: (err: any) => done(err), -// complete: () => { -// assert.equal(expected.length, 0); -// done(); -// }, -// }); -// }, 125); -// }) + it('should throw an error when given a MemoryStream', (done) => { + const stream = xs.periodic(50).take(3).remember(); + assert.strictEqual(stream instanceof MemoryStream, true); + const proxyStream = xs.createMimic(); + assert.throws(() => { + proxyStream.imitate(stream); + }); + done(); + }); }); diff --git a/tests/operator/last.ts b/tests/operator/last.ts index 172858b..1cd096c 100644 --- a/tests/operator/last.ts +++ b/tests/operator/last.ts @@ -1,4 +1,6 @@ -import xs from '../../src/index'; +/// +/// +import xs, {Stream, MemoryStream} from '../../src/index'; import * as assert from 'assert'; describe('Stream.prototype.last', () => { @@ -18,4 +20,20 @@ describe('Stream.prototype.last', () => { }; stream.addListener(listener); }); + + it('should return a Stream if input stream is a Stream', (done) => { + const input = xs.of(1, 2, 3); + assert.strictEqual(input instanceof Stream, true); + const stream: Stream = input.last(); + assert.strictEqual(stream instanceof Stream, true); + done(); + }); + + it('should return a Stream if input stream is a MemoryStream', (done) => { + const input = xs.of(1, 2, 3).remember(); + assert.strictEqual(input instanceof MemoryStream, true); + const stream: Stream = input.last(); + assert.strictEqual(stream instanceof Stream, true); + done(); + }); }); diff --git a/tests/operator/map.ts b/tests/operator/map.ts index f4bd9c6..fa875bf 100644 --- a/tests/operator/map.ts +++ b/tests/operator/map.ts @@ -1,4 +1,6 @@ -import xs from '../../src/index'; +/// +/// +import xs, {Stream, MemoryStream} from '../../src/index'; import * as assert from 'assert'; describe('Stream.prototype.map', () => { @@ -57,6 +59,22 @@ describe('Stream.prototype.map', () => { done(); }); + it('should return a Stream if input stream is a Stream', (done) => { + const input = xs.of(1, 2, 3); + assert.strictEqual(input instanceof Stream, true); + const stream: Stream = input.map(x => x * 10); + assert.strictEqual(stream instanceof Stream, true); + done(); + }); + + it('should return a MemoryStream if input stream is a MemoryStream', (done) => { + const input = xs.of(1, 2, 3).remember(); + assert.strictEqual(input instanceof MemoryStream, true); + const stream = input.map(x => x * 10); + assert.strictEqual(stream instanceof MemoryStream, true); + done(); + }); + it('should clean up Operator producer when failed', (done) => { const stream = xs.of('a', 'b', 3).map(i => i.toUpperCase()); const expected = ['A', 'B']; diff --git a/tests/operator/mapTo.ts b/tests/operator/mapTo.ts index 04fc73a..10c2b3a 100644 --- a/tests/operator/mapTo.ts +++ b/tests/operator/mapTo.ts @@ -1,4 +1,6 @@ -import xs from '../../src/index'; +/// +/// +import xs, {Stream, MemoryStream} from '../../src/index'; import * as assert from 'assert'; describe('Stream.prototype.mapTo', () => { @@ -18,6 +20,22 @@ describe('Stream.prototype.mapTo', () => { }; stream.addListener(listener); }); + + it('should return a Stream if input stream is a Stream', (done) => { + const input = xs.of(1, 2, 3); + assert.strictEqual(input instanceof Stream, true); + const stream: Stream = input.mapTo(10); + assert.strictEqual(stream instanceof Stream, true); + done(); + }); + + it('should return a MemoryStream if input stream is a MemoryStream', (done) => { + const input = xs.of(1, 2, 3).remember(); + assert.strictEqual(input instanceof MemoryStream, true); + const stream: MemoryStream = input.mapTo(10); + assert.strictEqual(stream instanceof MemoryStream, true); + done(); + }); it('should have \'type\' metadata on the operator producer', (done) => { const stream = xs.periodic(100).mapTo(10); diff --git a/tests/operator/merge.ts b/tests/operator/merge.ts index 56850bf..711afd7 100644 --- a/tests/operator/merge.ts +++ b/tests/operator/merge.ts @@ -1,4 +1,6 @@ -import xs from '../../src/index'; +/// +/// +import xs, {Stream, MemoryStream} from '../../src/index'; import * as assert from 'assert'; describe('Stream.prototype.merge', () => { @@ -34,4 +36,20 @@ describe('Stream.prototype.merge', () => { }, }); }); + + it('should return a Stream when merging a MemoryStream with a Stream', (done) => { + const input1 = xs.periodic(50).take(4).remember(); + const input2 = xs.periodic(80).take(3); + const stream: Stream = input1.merge(input2); + assert.strictEqual(stream instanceof Stream, true); + done(); + }); + + it('should return a Stream when merging a MemoryStream with a MemoryStream', (done) => { + const input1 = xs.periodic(50).take(4).remember(); + const input2 = xs.periodic(80).take(3).remember(); + const stream: Stream = input1.merge(input2); + assert.strictEqual(stream instanceof Stream, true); + done(); + }); }); diff --git a/tests/operator/remember.ts b/tests/operator/remember.ts index c9ec83e..487d70f 100644 --- a/tests/operator/remember.ts +++ b/tests/operator/remember.ts @@ -1,4 +1,6 @@ -import xs from '../../src/index'; +/// +/// +import xs, {Stream, MemoryStream} from '../../src/index'; import * as assert from 'assert'; function noop() {}; @@ -91,4 +93,20 @@ describe('Stream.prototype.remember', () => { }, }); }); + + it('should return a MemoryStream if input stream is a Stream', (done) => { + const input = xs.of(1, 2, 3); + assert.strictEqual(input instanceof Stream, true); + const stream: MemoryStream = input.remember(); + assert.strictEqual(stream instanceof MemoryStream, true); + done(); + }) + + it('should return a MemoryStream if input stream is a MemoryStream', (done) => { + const input = xs.createWithMemory(); + assert.strictEqual(input instanceof MemoryStream, true); + const stream: MemoryStream = input.remember(); + assert.strictEqual(stream instanceof MemoryStream, true); + done(); + }); }); diff --git a/tests/operator/replaceError.ts b/tests/operator/replaceError.ts index f85f343..e0d2877 100644 --- a/tests/operator/replaceError.ts +++ b/tests/operator/replaceError.ts @@ -1,4 +1,6 @@ -import xs, {Producer} from '../../src/index'; +/// +/// +import xs, {Stream, MemoryStream, Producer} from '../../src/index'; import * as assert from 'assert'; describe('Stream.prototype.replaceError', () => { @@ -127,4 +129,20 @@ describe('Stream.prototype.replaceError', () => { }, }); }); + + it('should return a Stream if input stream is a Stream', (done) => { + const input = xs.of(1, 2, 3); + assert.strictEqual(input instanceof Stream, true); + const stream: Stream = input.replaceError(err => xs.never()); + assert.strictEqual(stream instanceof Stream, true); + done(); + }); + + it('should return a MemoryStream if input stream is a MemoryStream', (done) => { + const input = xs.of(1, 2, 3).remember(); + assert.strictEqual(input instanceof MemoryStream, true); + const stream: MemoryStream = input.replaceError(err => xs.never()); + assert.strictEqual(stream instanceof MemoryStream, true); + done(); + }); }); diff --git a/tests/operator/startWith.ts b/tests/operator/startWith.ts index 690015d..2a98c51 100644 --- a/tests/operator/startWith.ts +++ b/tests/operator/startWith.ts @@ -1,4 +1,6 @@ -import xs from '../../src/index'; +/// +/// +import xs, {MemoryStream} from '../../src/index'; import * as assert from 'assert'; describe('Stream.prototype.startWith', () => { @@ -13,4 +15,10 @@ describe('Stream.prototype.startWith', () => { complete: done }); }); + + it('should return a MemoryStream', (done) => { + const stream = xs.of(100).startWith(1); + assert.strictEqual(stream instanceof MemoryStream, true); + done(); + }); }); diff --git a/tests/operator/take.ts b/tests/operator/take.ts index b00a040..e6a91cf 100644 --- a/tests/operator/take.ts +++ b/tests/operator/take.ts @@ -1,4 +1,6 @@ -import xs from '../../src/index'; +/// +/// +import xs, {Stream, MemoryStream} from '../../src/index'; import * as assert from 'assert'; describe('Stream.prototype.take', () => { @@ -19,6 +21,22 @@ describe('Stream.prototype.take', () => { stream.addListener(listener); }); + it('should return a Stream if input stream is a tream', (done) => { + const input = xs.of(1, 2, 3); + assert.strictEqual(input instanceof Stream, true); + const stream: Stream = input.take(2); + assert.strictEqual(stream instanceof Stream, true); + done(); + }); + + it('should return a MemoryStream if input stream is a MemoryStream', (done) => { + const input = xs.of(1, 2, 3).remember(); + assert.strictEqual(input instanceof MemoryStream, true); + const stream: MemoryStream = input.take(2); + assert.strictEqual(stream instanceof MemoryStream, true); + done(); + }); + it('should not break sibling listeners when TakeOperator tears down', (done) => { const source = xs.periodic(50); const streamA = source.take(3); diff --git a/tests/stream.ts b/tests/stream.ts index 99e61d0..13a203c 100644 --- a/tests/stream.ts +++ b/tests/stream.ts @@ -1,3 +1,5 @@ +/// +/// import xs, {Producer, Listener, Stream} from '../src/index'; import * as assert from 'assert';