Comparing version 1.2.3 to 1.2.4
@@ -11,2 +11,3 @@ export declare class Subscription { | ||
export interface Subscriber<T = any> { | ||
name?: string; | ||
next?: OnNext<T>; | ||
@@ -27,2 +28,5 @@ error?: OnError; | ||
} | ||
export interface SubscribeFunction<T> { | ||
(subscriber: OnNext<T> | Subscriber<T>): Subscription; | ||
} | ||
export {}; |
import { OperationResult, OperatorFunction } from "./operator"; | ||
import { OnNext, Subscriber, Subscription } from "./subscription"; | ||
import { SubscribeFunction } from "./subscription"; | ||
export declare namespace Types { | ||
interface Observable<T = never> { | ||
pipe<U = any>(...operations: OperatorFunction<T, OperationResult<U>>[]): Observable<U>; | ||
subscribe(subscriber: OnNext<T> | Subscriber<T>): Subscription; | ||
subscribe: SubscribeFunction<T>; | ||
} | ||
@@ -16,2 +16,4 @@ interface MutableObservable<T = never> extends Observable<T> { | ||
} | ||
interface PromiseObservable<T = never> extends Observable<T> { | ||
} | ||
} | ||
@@ -21,1 +23,2 @@ export declare type Observable<T> = Types.Observable<T>; | ||
export declare type ObservableFork<T> = Types.ObservableFork<T>; | ||
export declare type PromiseObservable<T> = Types.PromiseObservable<T>; |
@@ -7,3 +7,3 @@ import { OperatorFunction, OperationResult } from "../models/operator"; | ||
constructor(...initialSequence: T[]); | ||
close(): void; | ||
close(): this; | ||
/** | ||
@@ -10,0 +10,0 @@ * @param operations |
@@ -16,2 +16,7 @@ var __extends = (this && this.__extends) || (function () { | ||
})(); | ||
var __spreadArray = (this && this.__spreadArray) || function (to, from) { | ||
for (var i = 0, il = from.length, j = to.length; i < il; i++, j++) | ||
to[j] = from[i]; | ||
return to; | ||
}; | ||
import { OperationResult, OperationResultFlag, } from "../models/operator"; | ||
@@ -33,10 +38,11 @@ import { Observable } from "./observable"; | ||
if (this._isComplete) { | ||
return; | ||
return this; | ||
} | ||
this._isComplete = true; | ||
this._subscribers.forEach(function (s) { | ||
if (s.complete) { | ||
s.complete(); | ||
} | ||
this._subscribers | ||
.filter(function (s) { return s.complete; }) | ||
.forEach(function (s) { | ||
s.complete(); | ||
}); | ||
return this; | ||
}; | ||
@@ -55,3 +61,5 @@ /** | ||
} | ||
var newSequence = this._buildNewSequence(this._innerSequence.filter(function (event) { return !event.isOperationError(); }).map(function (event) { return event.value; }), operations).filter(function (event) { return !event.isMustStop(); }); | ||
var newSequence = this._buildNewSequence(this._innerSequence | ||
.filter(function (event) { return !event.isOperationError(); }) | ||
.map(function (event) { return event.value; }), __spreadArray(__spreadArray([], operations), this._preProcessOperations)).filter(function (event) { return !event.isMustStop(); }); | ||
var idxError = newSequence.findIndex(function (opRes) { return opRes.isOperationError(); }); | ||
@@ -65,3 +73,4 @@ if (idxError > -1) { | ||
} | ||
return this.next.apply(this, (this._innerSequence = newSequence).map(function (event) { return event.value; })); | ||
this._triggerExecution(this._innerSequence = newSequence, this._subscribers); | ||
return this; | ||
}; | ||
@@ -68,0 +77,0 @@ MutableObservable.prototype.next = function () { |
@@ -9,3 +9,3 @@ import { Observable } from "./observable"; | ||
private operators; | ||
constructor(sourceObs$: Observable<T>, ...operators: OperatorFunction<T, OperationResult<any>>[]); | ||
constructor(sourceObs$: Types.Observable<T>, ...operators: OperatorFunction<T, OperationResult<any>>[]); | ||
subscribe(subscriber: Subscriber<T> | OnNext<T>): Subscription; | ||
@@ -12,0 +12,0 @@ close(): void; |
@@ -18,3 +18,3 @@ var __extends = (this && this.__extends) || (function () { | ||
import { isFunction } from "../helpers/function.helper"; | ||
import { OperationResult, OperationResultFlag } from "../models/operator"; | ||
import { OperationResult, OperationResultFlag, } from "../models/operator"; | ||
import { isInstanceOfSubscriber, subscriberOf, Subscription, } from "../models/subscription"; | ||
@@ -35,13 +35,14 @@ var ObservableFork = /** @class */ (function (_super) { | ||
_this.sourceObs$.subscribe({ | ||
name: 'subscriber fork constructor', | ||
next: function (value) { | ||
_this._subscribers.forEach(function (s) { | ||
if (s.next) { | ||
var result = _this._executeOperations(value, operators); | ||
if (!result.isFilterNotMatched() && !result.isMustStop()) { | ||
return s.next(result.value); | ||
} | ||
if (result.isMustStop()) { | ||
_this.close(); | ||
} | ||
_this._subscribers | ||
.filter(function (s) { return s.next; }) | ||
.forEach(function (s) { | ||
var result = _this._executeOperations(value, operators); | ||
if (!result.isFilterNotMatched() && !result.isMustStop()) { | ||
return s.next(result.value); | ||
} | ||
if (result.isMustStop()) { | ||
_this.close(); | ||
} | ||
}); | ||
@@ -51,3 +52,5 @@ }, | ||
_this._error = err; | ||
_this._subscribers.forEach(function (s) { return s.error && s.error(err); }); | ||
_this._subscribers | ||
.filter(function (s) { return s.error; }) | ||
.forEach(function (s) { return s.error(err); }); | ||
}, | ||
@@ -57,3 +60,5 @@ complete: function () { | ||
_this.unsubscribe(); | ||
_this._subscribers.forEach(function (s) { return s.complete && s.complete(); }); | ||
_this._subscribers | ||
.filter(function (s) { return s.complete; }) | ||
.forEach(function (s) { return s.complete(); }); | ||
}, | ||
@@ -76,2 +81,5 @@ }); | ||
try { | ||
if (sourceSequence[i].isOperationError()) { | ||
throw sourceSequence[i].error; | ||
} | ||
newSequence.push(this._executeOperations(sourceSequence[i].value, this.operators)); | ||
@@ -78,0 +86,0 @@ } |
import { OperationResult, OperatorFunction } from "../../models/operator"; | ||
export declare const tap: <T = any>(f: OperatorFunction<T, boolean>) => OperatorFunction<T, OperationResult<T>>; | ||
export declare const tap: <T = any>(f: OperatorFunction<T, void>) => OperatorFunction<T, OperationResult<T>>; |
export * from './from'; | ||
export * from './mutable'; | ||
export * from './of'; | ||
export * from './on-event'; | ||
export * from './pre-process'; | ||
export * from './fork'; |
export * from './from'; | ||
export * from './mutable'; | ||
export * from './of'; | ||
export * from './on-event'; | ||
export * from './pre-process'; | ||
export * from './fork'; |
export * from './creation'; | ||
export * from './functions'; | ||
export { http } from './http'; |
export * from './creation'; | ||
export * from './functions'; | ||
/* istanbul ignore next */ | ||
export { http } from './http'; |
@@ -1,1 +0,1 @@ | ||
var t={d:(r,e)=>{for(var n in e)t.o(e,n)&&!t.o(r,n)&&Object.defineProperty(r,n,{enumerable:!0,get:e[n]})},o:(t,r)=>Object.prototype.hasOwnProperty.call(t,r)},r={};t.d(r,{gC:()=>i,hX:()=>o,rM:()=>F,Dp:()=>g,KJ:()=>u,UI:()=>a,Nl:()=>O,hL:()=>j,of:()=>x,Vl:()=>E,IH:()=>s,d_:()=>N,wt:()=>p,qn:()=>k,bw:()=>f,C4:()=>c});var e,n=function(){function t(t,r,e){this._value=t,this._flag=r,this._error=e}return Object.defineProperty(t.prototype,"value",{get:function(){return this._value},enumerable:!1,configurable:!0}),Object.defineProperty(t.prototype,"flag",{get:function(){return this._flag},enumerable:!1,configurable:!0}),Object.defineProperty(t.prototype,"error",{get:function(){return this._error},enumerable:!1,configurable:!0}),t.prototype.isUnwrapSwitch=function(){return this._flag===e.UnwrapSwitch},t.prototype.isMustStop=function(){return this._flag===e.MustStop},t.prototype.isFilterNotMatched=function(){return this._flag===e.FilterNotMatched},t.prototype.isOperationError=function(){return this._flag===e.OperationError},t}();!function(t){t.UnwrapSwitch="UnwrapSwitch",t.MustStop="MustStop",t.FilterNotMatched="FilterNotMatched",t.OperationError="OperationError"}(e||(e={}));var o=function(t){return function(r){return new n(r,t(r)?void 0:e.FilterNotMatched)}},i=function(t){var r=!1;return function(i){return new n(i,(r=r||o(t)(i).isFilterNotMatched())?e.MustStop:void 0)}},u=function(t,r,e){return function(n){var i=o(t)(n),u=i.isFilterNotMatched()?null!=e?e:[]:r,s=u.shift();return s?u.reduce((function(t,r){return r(t.value)}),s(n)):i}},s=function(t){var r=!1;return function(o){return r?new n(o,e.MustStop):!t||t(o)?(r=!0,new n(o)):new n(o,e.FilterNotMatched)}},c=function(t){var r=!1;return function(i){return new n(i,(r=r||!o(t)(i).isFilterNotMatched())?e.MustStop:void 0)}},a=function(t){return function(r){return new n(t(r))}};function p(t){return function(r){return new n(t(r),e.UnwrapSwitch)}}var f=function(t){return function(r){return t(r),new n(r)}};function l(t){return"function"==typeof t}function h(){for(var t=[],r=0;r<arguments.length;r++)t[r]=arguments[r];return t.filter((function(t){return l(t)}))}var b=function(){function t(t){this._unsubscribeCallback=t}return t.prototype.unsubscribe=function(){this._unsubscribeCallback&&this._unsubscribeCallback()},t}();function v(t){return!l(t)&&function(t){for(var r=[],e=1;e<arguments.length;e++)r[e-1]=arguments[e];return r.some((function(r){return void 0!==t[r]&&null!==t[r]&&l(t[r])}))}(t,"next","error","complete")}function _(t,r,e){var n={next:t,error:r,complete:e};if(!v(n))throw new Error("Please provide functions for next, error and complete");return n}new b;var w,y=function(){function t(){for(var t=[],r=0;r<arguments.length;r++)t[r]=arguments[r];this._subscribers=[],this._isComplete=!0,this._innerSequence=t.map((function(t){return new n(t)}))}return t.prototype.pipe=function(){for(var r=[],o=0;o<arguments.length;o++)r[o]=arguments[o];for(var i=new t,u=[],s=this._innerSequence,c=0,a=s.length;c<a&&!s[c].isMustStop();c++)try{var p=this._executeOperations(s[c].value,r);p.isFilterNotMatched()||u.push(p)}catch(t){u.push(new n(s[c].value,e.OperationError,t)),c=a}return i._innerSequence=u,i},t.prototype.subscribe=function(t){var r=this;if(!l(t)&&!v(t))throw new Error("Please provide either a function or a Subscriber");var e=v(t)?t:_(t);return this._subscribers.push(e),this.executeSubscriber(e,this._innerSequence),new b((function(){return r._subscribers=r._subscribers.filter((function(r){return r!==t}))}))},t.prototype.executeSubscriber=function(t,r){for(var e=function(e,o){var i=r[e];return i.isOperationError()?(n._error=i.error,(t.error||function(){throw i.error})(i.error),"break"):i.isFilterNotMatched()?"continue":i.isMustStop()?"break":void(t.next&&t.next(i.value))},n=this,o=0,i=r.length;o<i&&"break"!==e(o);o++);this._isComplete&&t.complete&&t.complete()},t.prototype._computeValue=function(t){for(var r,o=[],i=1;i<arguments.length;i++)o[i-1]=arguments[i];for(var u=new n(t),s=0;s<o.length;s++)switch((u=o[s](u.value)).flag){case e.FilterNotMatched:case e.MustStop:s=o.length;break;case e.UnwrapSwitch:u=new n(null===(r=u.value._innerSequence.pop())||void 0===r?void 0:r.value)}return u},t.prototype._executeOperations=function(t,r){return this._computeValue.apply(this,function(t,r){for(var e=0,n=r.length,o=t.length;e<n;e++,o++)t[o]=r[e];return t}([t],h.apply(void 0,r)))},t}(),g=function(t){return new(y.bind.apply(y,function(t,r){for(var e=0,n=r.length,o=t.length;e<n;e++,o++)t[o]=r[e];return t}([void 0],t)))},d=(w=function(t,r){return(w=Object.setPrototypeOf||{__proto__:[]}instanceof Array&&function(t,r){t.__proto__=r}||function(t,r){for(var e in r)Object.prototype.hasOwnProperty.call(r,e)&&(t[e]=r[e])})(t,r)},function(t,r){if("function"!=typeof r&&null!==r)throw new TypeError("Class extends value "+String(r)+" is not a constructor or null");function e(){this.constructor=t}w(t,r),t.prototype=null===r?Object.create(r):(e.prototype=r.prototype,new e)}),S=function(t){function r(){for(var r=[],e=0;e<arguments.length;e++)r[e]=arguments[e];var n=t.apply(this,r)||this;return n._preProcessOperations=[],n._isComplete=!1,n}return d(r,t),r.prototype.close=function(){this._isComplete||(this._isComplete=!0,this._subscribers.forEach((function(t){t.complete&&t.complete()})))},r.prototype.compile=function(){for(var t=[],r=0;r<arguments.length;r++)t[r]=arguments[r];if(this._isComplete)return this;var e=this._buildNewSequence(this._innerSequence.filter((function(t){return!t.isOperationError()})).map((function(t){return t.value})),t).filter((function(t){return!t.isMustStop()})),n=e.findIndex((function(t){return t.isOperationError()}));return n>-1?(this._innerSequence=e.slice(0,n),this.next.apply(this,this._innerSequence.map((function(t){return t.value}))),this._innerSequence.push(e[n]),this._triggerExecution([e[n]],this._subscribers),this):this.next.apply(this,(this._innerSequence=e).map((function(t){return t.value})))},r.prototype.next=function(){for(var t=[],r=0;r<arguments.length;r++)t[r]=arguments[r];return this._isComplete||(this._innerSequence=this._buildNewSequence(t,this._preProcessOperations),this._triggerExecution(this._innerSequence,this._subscribers)),this},r.prototype._buildNewSequence=function(t,r){for(var o=[],i=0,u=t.length;i<u;i++)try{var s=this._executeOperations(t[i],r);if(s.isMustStop()){o.push(s);break}s.isFilterNotMatched()||o.push(s)}catch(r){this._error=r,o.push(new n(t[i],e.OperationError,r)),i=u}return o},r.prototype._triggerExecution=function(t,r){var e=this;r.forEach((function(r){return e.executeSubscriber(r,t)}))},r}(y),m=function(t,r){for(var e=0,n=r.length,o=t.length;e<n;e++,o++)t[o]=r[e];return t},O=function(){for(var t=[],r=0;r<arguments.length;r++)t[r]=arguments[r];return new(S.bind.apply(S,m([void 0],t)))},M=function(t,r){for(var e=0,n=r.length,o=t.length;e<n;e++,o++)t[o]=r[e];return t},x=function(){for(var t=[],r=0;r<arguments.length;r++)t[r]=arguments[r];return new(y.bind.apply(y,M([void 0],t)))},E=function(t,r){var e=new S;return t.addEventListener(r,(function(t){return e.next(t)})),e};function N(){for(var t,r=[],e=0;e<arguments.length;e++)r[e]=arguments[e];var n=new S;return(t=n._preProcessOperations).push.apply(t,r),n}var C=function(){var t=function(r,e){return(t=Object.setPrototypeOf||{__proto__:[]}instanceof Array&&function(t,r){t.__proto__=r}||function(t,r){for(var e in r)Object.prototype.hasOwnProperty.call(r,e)&&(t[e]=r[e])})(r,e)};return function(r,e){if("function"!=typeof e&&null!==e)throw new TypeError("Class extends value "+String(e)+" is not a constructor or null");function n(){this.constructor=r}t(r,e),r.prototype=null===e?Object.create(e):(n.prototype=e.prototype,new n)}}(),q=function(t){function r(r){for(var e=[],n=1;n<arguments.length;n++)e[n-1]=arguments[n];var o=t.call(this)||this;return o.sourceObs$=r,o.subscriptions=[],o.operators=[],o.operators=e,o._isComplete=r._isComplete,o.sourceObs$.subscribe({next:function(t){o._subscribers.forEach((function(r){if(r.next){var n=o._executeOperations(t,e);if(!n.isFilterNotMatched()&&!n.isMustStop())return r.next(n.value);n.isMustStop()&&o.close()}}))},error:function(t){o._error=t,o._subscribers.forEach((function(r){return r.error&&r.error(t)}))},complete:function(){o._isComplete=!0,o.unsubscribe(),o._subscribers.forEach((function(t){return t.complete&&t.complete()}))}}),o}return C(r,t),r.prototype.subscribe=function(t){var r=this;if(!l(t)&&!v(t))throw new Error("Please provide either a function or a Subscriber");var o=v(t)?t:_(t);this._subscribers.push(o);for(var i=[],u=this.sourceObs$._innerSequence,s=0,c=u.length;s<c;s++)try{i.push(this._executeOperations(u[s].value,this.operators))}catch(t){i.push(new n(u[s].value,e.OperationError,t)),s=c}return this.executeSubscriber(o,i),new b((function(){return r._subscribers=r._subscribers.filter((function(r){return r!==t}))}))},r.prototype.close=function(){this._subscribers.forEach((function(t){return t.complete&&t.complete()})),this.unsubscribe()},r.prototype.unsubscribe=function(){this.subscriptions.forEach((function(t){return t.unsubscribe()}))},r}(y),P=function(t,r){for(var e=0,n=r.length,o=t.length;e<n;e++,o++)t[o]=r[e];return t},F=function(t){for(var r=[],e=1;e<arguments.length;e++)r[e-1]=arguments[e];var n=new(q.bind.apply(q,P([void 0,t],r)));return n},j=function(t){var r=0;return function(o){return++r===t?new n(o):new n(o,r<t?e.FilterNotMatched:e.MustStop)}},k=function(t){var r=0;return function(o){return++r>t?new n(o,e.MustStop):new n(o)}},U=r.gC,I=r.hX,L=r.rM,V=r.Dp,A=r.KJ,$=r.UI,D=r.Nl,H=r.hL,J=r.of,K=r.Vl,T=r.IH,X=r.d_,z=r.wt,B=r.qn,G=r.bw,Q=r.C4;export{U as asLongAs,I as filter,L as fork,V as from,A as ifElse,$ as map,D as mutable,H as nth,J as of,K as onEvent,T as once,X as preProcess,z as switchMap,B as take,G as tap,Q as until}; | ||
var t={d:(r,e)=>{for(var n in e)t.o(e,n)&&!t.o(r,n)&&Object.defineProperty(r,n,{enumerable:!0,get:e[n]})},o:(t,r)=>Object.prototype.hasOwnProperty.call(t,r)},r={};t.d(r,{gC:()=>i,hX:()=>o,rM:()=>N,Dp:()=>O,iT:()=>T,dJ:()=>H,KJ:()=>u,UI:()=>a,Nl:()=>x,hL:()=>K,of:()=>E,IH:()=>c,d_:()=>P,wt:()=>p,qn:()=>V,bw:()=>f,C4:()=>s});var e,n=function(){function t(t,r,e){this._value=t,this._flag=r,this._error=e}return Object.defineProperty(t.prototype,"value",{get:function(){return this._value},enumerable:!1,configurable:!0}),Object.defineProperty(t.prototype,"flag",{get:function(){return this._flag},enumerable:!1,configurable:!0}),Object.defineProperty(t.prototype,"error",{get:function(){return this._error},enumerable:!1,configurable:!0}),t.prototype.isUnwrapSwitch=function(){return this._flag===e.UnwrapSwitch},t.prototype.isMustStop=function(){return this._flag===e.MustStop},t.prototype.isFilterNotMatched=function(){return this._flag===e.FilterNotMatched},t.prototype.isOperationError=function(){return this._flag===e.OperationError},t}();!function(t){t.UnwrapSwitch="UnwrapSwitch",t.MustStop="MustStop",t.FilterNotMatched="FilterNotMatched",t.OperationError="OperationError"}(e||(e={}));var o=function(t){return function(r){return new n(r,t(r)?void 0:e.FilterNotMatched)}},i=function(t){var r=!1;return function(i){return new n(i,(r=r||o(t)(i).isFilterNotMatched())?e.MustStop:void 0)}},u=function(t,r,e){return function(n){var i=o(t)(n),u=i.isFilterNotMatched()?null!=e?e:[]:r,c=u.shift();return c?u.reduce((function(t,r){return r(t.value)}),c(n)):i}},c=function(t){var r=!1;return function(o){return r?new n(o,e.MustStop):!t||t(o)?(r=!0,new n(o)):new n(o,e.FilterNotMatched)}},s=function(t){var r=!1;return function(i){return new n(i,(r=r||!o(t)(i).isFilterNotMatched())?e.MustStop:void 0)}},a=function(t){return function(r){return new n(t(r))}};function p(t){return function(r){return new n(t(r),e.UnwrapSwitch)}}var f=function(t){return function(r){return t(r),new n(r)}};function l(t){return"function"==typeof t}function h(){for(var t=[],r=0;r<arguments.length;r++)t[r]=arguments[r];return t.filter((function(t){return l(t)}))}var b=function(){function t(t){this._unsubscribeCallback=t}return t.prototype.unsubscribe=function(){this._unsubscribeCallback&&this._unsubscribeCallback()},t}();function y(t){return!l(t)&&function(t){for(var r=[],e=1;e<arguments.length;e++)r[e-1]=arguments[e];return r.some((function(r){return void 0!==t[r]&&null!==t[r]&&l(t[r])}))}(t,"next","error","complete")}function v(t,r,e){var n={next:t,error:r,complete:e};if(!y(n))throw new Error("Please provide functions for next, error and complete");return n}new b;var _,w=function(){function t(){for(var t=[],r=0;r<arguments.length;r++)t[r]=arguments[r];this._subscribers=[],this._isComplete=!0,this._innerSequence=t.map((function(t){return new n(t)}))}return t.prototype.pipe=function(){for(var r=[],o=0;o<arguments.length;o++)r[o]=arguments[o];for(var i=new t,u=[],c=this._innerSequence,s=0,a=c.length;s<a&&!c[s].isMustStop();s++)try{var p=this._executeOperations(c[s].value,r);p.isFilterNotMatched()||u.push(p)}catch(t){u.push(new n(c[s].value,e.OperationError,t)),s=a}return i._innerSequence=u,i},t.prototype.subscribe=function(t){var r=this;if(!l(t)&&!y(t))throw new Error("Please provide either a function or a Subscriber");var e=y(t)?t:v(t);return this._subscribers.push(e),this.executeSubscriber(e,this._innerSequence),new b((function(){return r._subscribers=r._subscribers.filter((function(r){return r!==t}))}))},t.prototype.executeSubscriber=function(t,r){for(var e=function(e,o){var i=r[e];return i.isOperationError()?(n._error=i.error,(t.error||function(){throw i.error})(i.error),"break"):i.isFilterNotMatched()?"continue":i.isMustStop()?"break":void(t.next&&t.next(i.value))},n=this,o=0,i=r.length;o<i&&"break"!==e(o);o++);this._isComplete&&t.complete&&t.complete()},t.prototype._computeValue=function(t){for(var r,o=[],i=1;i<arguments.length;i++)o[i-1]=arguments[i];for(var u=new n(t),c=0;c<o.length;c++)switch((u=o[c](u.value)).flag){case e.FilterNotMatched:case e.MustStop:c=o.length;break;case e.UnwrapSwitch:u=new n(null===(r=u.value._innerSequence.pop())||void 0===r?void 0:r.value)}return u},t.prototype._executeOperations=function(t,r){return this._computeValue.apply(this,function(t,r){for(var e=0,n=r.length,o=t.length;e<n;e++,o++)t[o]=r[e];return t}([t],h.apply(void 0,r)))},t}(),O=function(t){return new(w.bind.apply(w,function(t,r){for(var e=0,n=r.length,o=t.length;e<n;e++,o++)t[o]=r[e];return t}([void 0],t)))},g=(_=function(t,r){return(_=Object.setPrototypeOf||{__proto__:[]}instanceof Array&&function(t,r){t.__proto__=r}||function(t,r){for(var e in r)Object.prototype.hasOwnProperty.call(r,e)&&(t[e]=r[e])})(t,r)},function(t,r){if("function"!=typeof r&&null!==r)throw new TypeError("Class extends value "+String(r)+" is not a constructor or null");function e(){this.constructor=t}_(t,r),t.prototype=null===r?Object.create(r):(e.prototype=r.prototype,new e)}),d=function(t,r){for(var e=0,n=r.length,o=t.length;e<n;e++,o++)t[o]=r[e];return t},S=function(t){function r(){for(var r=[],e=0;e<arguments.length;e++)r[e]=arguments[e];var n=t.apply(this,r)||this;return n._preProcessOperations=[],n._isComplete=!1,n}return g(r,t),r.prototype.close=function(){return this._isComplete||(this._isComplete=!0,this._subscribers.filter((function(t){return t.complete})).forEach((function(t){t.complete()}))),this},r.prototype.compile=function(){for(var t=[],r=0;r<arguments.length;r++)t[r]=arguments[r];if(this._isComplete)return this;var e=this._buildNewSequence(this._innerSequence.filter((function(t){return!t.isOperationError()})).map((function(t){return t.value})),d(d([],t),this._preProcessOperations)).filter((function(t){return!t.isMustStop()})),n=e.findIndex((function(t){return t.isOperationError()}));return n>-1?(this._innerSequence=e.slice(0,n),this.next.apply(this,this._innerSequence.map((function(t){return t.value}))),this._innerSequence.push(e[n]),this._triggerExecution([e[n]],this._subscribers),this):(this._triggerExecution(this._innerSequence=e,this._subscribers),this)},r.prototype.next=function(){for(var t=[],r=0;r<arguments.length;r++)t[r]=arguments[r];return this._isComplete||(this._innerSequence=this._buildNewSequence(t,this._preProcessOperations),this._triggerExecution(this._innerSequence,this._subscribers)),this},r.prototype._buildNewSequence=function(t,r){for(var o=[],i=0,u=t.length;i<u;i++)try{var c=this._executeOperations(t[i],r);if(c.isMustStop()){o.push(c);break}c.isFilterNotMatched()||o.push(c)}catch(r){this._error=r,o.push(new n(t[i],e.OperationError,r)),i=u}return o},r.prototype._triggerExecution=function(t,r){var e=this;r.forEach((function(r){return e.executeSubscriber(r,t)}))},r}(w),m=function(t,r){for(var e=0,n=r.length,o=t.length;e<n;e++,o++)t[o]=r[e];return t},x=function(){for(var t=[],r=0;r<arguments.length;r++)t[r]=arguments[r];return new(S.bind.apply(S,m([void 0],t)))},j=function(t,r){for(var e=0,n=r.length,o=t.length;e<n;e++,o++)t[o]=r[e];return t},E=function(){for(var t=[],r=0;r<arguments.length;r++)t[r]=arguments[r];return new(w.bind.apply(w,j([void 0],t)))};function P(){for(var t,r=[],e=0;e<arguments.length;e++)r[e]=arguments[e];var n=new S;return(t=n._preProcessOperations).push.apply(t,r),n}var M=function(){var t=function(r,e){return(t=Object.setPrototypeOf||{__proto__:[]}instanceof Array&&function(t,r){t.__proto__=r}||function(t,r){for(var e in r)Object.prototype.hasOwnProperty.call(r,e)&&(t[e]=r[e])})(r,e)};return function(r,e){if("function"!=typeof e&&null!==e)throw new TypeError("Class extends value "+String(e)+" is not a constructor or null");function n(){this.constructor=r}t(r,e),r.prototype=null===e?Object.create(e):(n.prototype=e.prototype,new n)}}(),q=function(t){function r(r){for(var e=[],n=1;n<arguments.length;n++)e[n-1]=arguments[n];var o=t.call(this)||this;return o.sourceObs$=r,o.subscriptions=[],o.operators=[],o.operators=e,o._isComplete=r._isComplete,o.sourceObs$.subscribe({name:"subscriber fork constructor",next:function(t){o._subscribers.filter((function(t){return t.next})).forEach((function(r){var n=o._executeOperations(t,e);if(!n.isFilterNotMatched()&&!n.isMustStop())return r.next(n.value);n.isMustStop()&&o.close()}))},error:function(t){o._error=t,o._subscribers.filter((function(t){return t.error})).forEach((function(r){return r.error(t)}))},complete:function(){o._isComplete=!0,o.unsubscribe(),o._subscribers.filter((function(t){return t.complete})).forEach((function(t){return t.complete()}))}}),o}return M(r,t),r.prototype.subscribe=function(t){var r=this;if(!l(t)&&!y(t))throw new Error("Please provide either a function or a Subscriber");var o=y(t)?t:v(t);this._subscribers.push(o);for(var i=[],u=this.sourceObs$._innerSequence,c=0,s=u.length;c<s;c++)try{if(u[c].isOperationError())throw u[c].error;i.push(this._executeOperations(u[c].value,this.operators))}catch(t){i.push(new n(u[c].value,e.OperationError,t)),c=s}return this.executeSubscriber(o,i),new b((function(){return r._subscribers=r._subscribers.filter((function(r){return r!==t}))}))},r.prototype.close=function(){this._subscribers.forEach((function(t){return t.complete&&t.complete()})),this.unsubscribe()},r.prototype.unsubscribe=function(){this.subscriptions.forEach((function(t){return t.unsubscribe()}))},r}(w),C=function(t,r){for(var e=0,n=r.length,o=t.length;e<n;e++,o++)t[o]=r[e];return t},N=function(t){for(var r=[],e=1;e<arguments.length;e++)r[e-1]=arguments[e];var n=new(q.bind.apply(q,C([void 0,t],r)));return n},F=function(){var t=function(r,e){return(t=Object.setPrototypeOf||{__proto__:[]}instanceof Array&&function(t,r){t.__proto__=r}||function(t,r){for(var e in r)Object.prototype.hasOwnProperty.call(r,e)&&(t[e]=r[e])})(r,e)};return function(r,e){if("function"!=typeof e&&null!==e)throw new TypeError("Class extends value "+String(e)+" is not a constructor or null");function n(){this.constructor=r}t(r,e),r.prototype=null===e?Object.create(e):(n.prototype=e.prototype,new n)}}(),k=function(t){function r(r){var o=t.call(this)||this;return o.proxy=Object.assign(new Proxy(r,{apply:function(t,r,i){var u,c;try{c=new n(u=t.apply(r,i))}catch(t){c=new n(u,e.OperationError,t)}if(o._innerSequence=[c],o._subscribers.forEach((function(t){return o.executeSubscriber(t,o._innerSequence)})),c.isOperationError())throw c.error;return u}})),o}return F(r,t),r.create=function(t){var e=new r(t),n=function(){for(var t=[],r=0;r<arguments.length;r++)t[r]=arguments[r];return e.proxy.apply(e,t)};return e._innerSequence=[],n.subscribe=e.subscribe.bind(e),n.pipe=e.pipe.bind(e),n.asObservable=function(){return e},n},r}(w),T=function(t){return k.create(t)},I=function(){var t=function(r,e){return(t=Object.setPrototypeOf||{__proto__:[]}instanceof Array&&function(t,r){t.__proto__=r}||function(t,r){for(var e in r)Object.prototype.hasOwnProperty.call(r,e)&&(t[e]=r[e])})(r,e)};return function(r,e){if("function"!=typeof e&&null!==e)throw new TypeError("Class extends value "+String(e)+" is not a constructor or null");function n(){this.constructor=r}t(r,e),r.prototype=null===e?Object.create(e):(n.prototype=e.prototype,new n)}}(),U=function(t,r){for(var e=0,n=r.length,o=t.length;e<n;e++,o++)t[o]=r[e];return t},A=function(t){function r(r){var o=t.call(this)||this;return o.promise=r.then((function(t){o._innerSequence.push(new n(t))})).catch((function(t){o._innerSequence.push(new n(void 0,e.OperationError,t))})),o}return I(r,t),r.prototype.pipe=function(){for(var t=[],r=0;r<arguments.length;r++)t[r]=arguments[r];return N.apply(void 0,U([this],t))},r.prototype.subscribe=function(t){var r=this;if(!l(t)&&!y(t))throw new Error("Please provide either a function or a Subscriber");var e=y(t)?t:v(t);return this._subscribers.push(e),this.promise.then((function(){return r.executeSubscriber(e,r._innerSequence)})),new b((function(){return r._subscribers=r._subscribers.filter((function(r){return r!==t}))}))},r}(w),J=function(){return(J=Object.assign||function(t){for(var r,e=1,n=arguments.length;e<n;e++)for(var o in r=arguments[e])Object.prototype.hasOwnProperty.call(r,o)&&(t[o]=r[o]);return t}).apply(this,arguments)},L=function(){return(L=Object.assign||function(t){for(var r,e=1,n=arguments.length;e<n;e++)for(var o in r=arguments[e])Object.prototype.hasOwnProperty.call(r,o)&&(t[o]=r[o]);return t}).apply(this,arguments)},D=function(){return(D=Object.assign||function(t){for(var r,e=1,n=arguments.length;e<n;e++)for(var o in r=arguments[e])Object.prototype.hasOwnProperty.call(r,o)&&(t[o]=r[o]);return t}).apply(this,arguments)},$=function(){return($=Object.assign||function(t){for(var r,e=1,n=arguments.length;e<n;e++)for(var o in r=arguments[e])Object.prototype.hasOwnProperty.call(r,o)&&(t[o]=r[o]);return t}).apply(this,arguments)},H={get:function(t,r){void 0===r&&(r={type:"json"});var e=r.type,n=function(t,r){var e={};for(var n in t)Object.prototype.hasOwnProperty.call(t,n)&&r.indexOf(n)<0&&(e[n]=t[n]);if(null!=t&&"function"==typeof Object.getOwnPropertySymbols){var o=0;for(n=Object.getOwnPropertySymbols(t);o<n.length;o++)r.indexOf(n[o])<0&&Object.prototype.propertyIsEnumerable.call(t,n[o])&&(e[n[o]]=t[n[o]])}return e}(r,["type"]);return new A(new Promise((function(r,o){fetch(t,J(J({},n),{method:"GET"})).then((function(t){if(!t.ok)throw Error(t.statusText);return t})).then((function(t){return r("text"===e?t.text():"blob"===e?t.blob():t.json())})).catch((function(t){return o(t)}))})))},post:function(t,r){void 0===r&&(r={type:"json"});var e=r.type,n=function(t,r){var e={};for(var n in t)Object.prototype.hasOwnProperty.call(t,n)&&r.indexOf(n)<0&&(e[n]=t[n]);if(null!=t&&"function"==typeof Object.getOwnPropertySymbols){var o=0;for(n=Object.getOwnPropertySymbols(t);o<n.length;o++)r.indexOf(n[o])<0&&Object.prototype.propertyIsEnumerable.call(t,n[o])&&(e[n[o]]=t[n[o]])}return e}(r,["type"]);return new A(new Promise((function(r,o){fetch(t,L(L({},n),{method:"POST"})).then((function(t){if(!t.ok)throw Error(t.statusText);return t})).then((function(t){return r("text"===e?t.text():"blob"===e?t.blob():t.json())})).catch((function(t){return o(t)}))})))},put:function(t,r){void 0===r&&(r={type:"json"});var e=r.type,n=function(t,r){var e={};for(var n in t)Object.prototype.hasOwnProperty.call(t,n)&&r.indexOf(n)<0&&(e[n]=t[n]);if(null!=t&&"function"==typeof Object.getOwnPropertySymbols){var o=0;for(n=Object.getOwnPropertySymbols(t);o<n.length;o++)r.indexOf(n[o])<0&&Object.prototype.propertyIsEnumerable.call(t,n[o])&&(e[n[o]]=t[n[o]])}return e}(r,["type"]);return new A(new Promise((function(r,o){fetch(t,D(D({},n),{method:"PUT"})).then((function(t){if(!t.ok)throw Error(t.statusText);return t})).then((function(t){return r("text"===e?t.text():"blob"===e?t.blob():t.json())})).catch((function(t){return o(t)}))})))},delete:function(t,r){void 0===r&&(r={type:"json"});var e=r.type,n=function(t,r){var e={};for(var n in t)Object.prototype.hasOwnProperty.call(t,n)&&r.indexOf(n)<0&&(e[n]=t[n]);if(null!=t&&"function"==typeof Object.getOwnPropertySymbols){var o=0;for(n=Object.getOwnPropertySymbols(t);o<n.length;o++)r.indexOf(n[o])<0&&Object.prototype.propertyIsEnumerable.call(t,n[o])&&(e[n[o]]=t[n[o]])}return e}(r,["type"]);return new A(new Promise((function(r,o){fetch(t,$($({},n),{method:"DELETE"})).then((function(t){if(!t.ok)throw Error(t.statusText);return t})).then((function(t){return r("text"===e?t.text():"blob"===e?t.blob():t.json())})).catch((function(t){return o(t)}))})))}},K=function(t){var r=0;return function(o){return++r===t?new n(o):new n(o,r<t?e.FilterNotMatched:e.MustStop)}},V=function(t){var r=0;return function(o){return++r>t?new n(o,e.MustStop):new n(o)}},X=r.gC,G=r.hX,z=r.rM,B=r.Dp,Q=r.iT,R=r.dJ,W=r.KJ,Y=r.UI,Z=r.Nl,tt=r.hL,rt=r.of,et=r.IH,nt=r.d_,ot=r.wt,it=r.qn,ut=r.bw,ct=r.C4;export{X as asLongAs,G as filter,z as fork,B as from,Q as fromFunction,R as http,W as ifElse,Y as map,Z as mutable,tt as nth,rt as of,et as once,nt as preProcess,ot as switchMap,it as take,ut as tap,ct as until}; |
{ | ||
"name": "fleuvejs", | ||
"version": "1.2.3", | ||
"version": "1.2.4", | ||
"description": "A simple JavaScript Library for Observables", | ||
@@ -32,2 +32,3 @@ "main": "build/index.js", | ||
"jest": "^27.0.6", | ||
"jest-fetch-mock": "^3.0.3", | ||
"ts-jest": "^27.0.3", | ||
@@ -34,0 +35,0 @@ "ts-loader": "^9.2.3", |
102
README.md
@@ -26,3 +26,3 @@ # FleuveJS | ||
## Installation | ||
`npm i observablejs` | ||
`npm i fleuvejs` | ||
@@ -32,3 +32,3 @@ Or, if you'd prefer to work on a vanilla project: | ||
```js | ||
import { Observable } from 'https://unpkg.com/observablejs@latest/bundle/observable.bundle.js'; | ||
import {of, map, mutable} from 'https://unpkg.com/fleuvejs@latest/bundle/fleuve.bundle.js'; | ||
``` | ||
@@ -71,3 +71,3 @@ | ||
error: (err) => console.error(err), | ||
compleyte: () => console.log('observable complete') | ||
complete: () => console.log('observable complete') | ||
}); | ||
@@ -85,15 +85,3 @@ | ||
``` | ||
### Add an event listener | ||
*Do not use anymore: will be deprecated and maybe replaced by a better implementation.* | ||
You can bind users interactions to an Observable. | ||
```html | ||
<button id="clickMe">Click Me</button> | ||
``` | ||
```js | ||
const obs$ = onEvent(document.getElementById("clickMe"), "click"); | ||
obs$.subscribe((event) => console.log('event triggered', event)); | ||
``` | ||
## How To Use MutableObservables | ||
@@ -191,2 +179,19 @@ | ||
#### `fromFunction` - static | ||
*This operator is static: it means your cannot use it as a parameter for methods such as `pipe` or `compile`* | ||
This operator allows you to create a special kind of Observable: it wraps a given function. Each time the function is called, all the subscribers are notified. | ||
You can also fork this Observable by using the `fork` operator with the `asObservable` method. | ||
```ts | ||
function sum(...args: number[]) { | ||
return args.reduce((acc, curr) => acc + curr, 0); | ||
} | ||
const sum$ = fromFunction(sum); | ||
const fork$ = fork(sum$.asObservable(), map((x) => x * 2)); | ||
fork$.subscribe({next: (res) => console.log(res)}); // this should display "8" | ||
``` | ||
#### `map` | ||
@@ -302,56 +307,13 @@ | ||
- catchError: catch any error and treat it. Prevents onError to be called | ||
- debounce : debounces the processing of event values | ||
- throttle : throttles the processing of event values | ||
- reduce : reduces the Observable sequence to a unique value | ||
- min : find the min value (with or without predicate) | ||
- max : find the max value (with or without predicate) | ||
- slice : returns a section of the Observable sequence (just like the slice method of the Array prototype) | ||
- debounce: debounces the processing of event values | ||
- throttle: throttles the processing of event values | ||
- reduce: reduces the Observable sequence to a unique value | ||
- min: find the min value (with or without predicate) | ||
- max: find the max value (with or without predicate) | ||
- slice: returns a section of the Observable sequence (just like the slice method of the Array prototype) | ||
- some: returns true if at least one event matches a predicate | ||
### Static | ||
#### Functions | ||
- around | ||
- before | ||
- after | ||
- whenThrowing | ||
- onFunction | ||
- debounceFn | ||
- throttleFn | ||
- onceFn | ||
- timesFn | ||
- memoize (under consideration) | ||
Example: | ||
```js | ||
function divide(x, y) { | ||
if (y === 0) { | ||
throw new Error('Invalid Denominator'); | ||
} | ||
console.log(`x / y = ${x/y}`) | ||
return x / y; | ||
} | ||
const aroundDivide$ = around(divide); | ||
const beforeDivide$ = before(divide); | ||
const afterDivide$ = after(divide); | ||
const whenThrowing = whenThrowing(divide); | ||
aroundDivide$.subscribe((x) => console.log('Around division:', x)); | ||
beforeDivide$.subscribe(() => console.log('Before division')); | ||
afterDivide$.subscribe((x) => console.log('After division:', x)); | ||
whenThrowing$.subscribe((err) => console.log('Throwing division:', err)); | ||
// Should display 'Around division: undefined', 'x / y = 2', 'Around division: 2' | ||
aroundDivide$(10, 5); | ||
// Should display 'Before division', 'x / y = 2' | ||
beforeDivide$(10, 5); | ||
// Should display 'x / y = 2', 'After division: 2' | ||
afterDivide$(10, 5); | ||
// Should display 'Throwing: Error: Invalid Denominator' | ||
whenThrowing$(10, 0); | ||
``` | ||
#### Creation | ||
@@ -362,18 +324,8 @@ - compose: to compose finite and infinite Observable creators | ||
- websocket | ||
- promise | ||
#### Replacement | ||
- replace | ||
- replaceNth | ||
- replaceN | ||
- replaceAll | ||
#### Predicates | ||
- or | ||
- and | ||
- xor | ||
- not | ||
### Allow to work with IndexedDB | ||
### Allow to work with Promises | ||
### Allow to work with RxJs Observables |
@@ -17,2 +17,3 @@ import { isFunction } from "../helpers/function.helper"; | ||
export interface Subscriber<T = any> { | ||
name?: string; | ||
next?: OnNext<T>, error?: OnError, complete?: OnComplete | ||
@@ -48,2 +49,6 @@ } | ||
}; | ||
export interface SubscribeFunction<T> { | ||
(subscriber: OnNext<T> | Subscriber<T>): Subscription; | ||
} | ||
import { OperationResult, OperatorFunction } from "./operator"; | ||
import { OnNext, Subscriber, Subscription } from "./subscription"; | ||
import { SubscribeFunction } from "./subscription"; | ||
@@ -9,3 +9,3 @@ export namespace Types { | ||
): Observable<U>; | ||
subscribe(subscriber: OnNext<T> | Subscriber<T>): Subscription; | ||
subscribe: SubscribeFunction<T>; | ||
} | ||
@@ -22,2 +22,4 @@ | ||
} | ||
export interface PromiseObservable<T = never> extends Observable<T> {} | ||
} | ||
@@ -28,1 +30,2 @@ | ||
export type ObservableFork<T> = Types.ObservableFork<T>; | ||
export type PromiseObservable<T> = Types.PromiseObservable<T>; |
@@ -11,3 +11,2 @@ import { OperatorFunction, OperationResult } from "../models/operator"; | ||
describe("MutableObservable", () => { | ||
it("will succeed", () => expect(true).toBe(true)); | ||
describe("compile", () => { | ||
@@ -160,2 +159,12 @@ it("should execute each function", () => { | ||
}); | ||
it('should return without doing anything', () => { | ||
const obs$ = mutable<number>().close(); | ||
const spyBuildNewSequence = jest.spyOn((obs$ as any), '_buildNewSequence'); | ||
const spyTriggerExecution = jest.spyOn((obs$ as any), '_triggerExecution'); | ||
obs$.next(12); | ||
expect(spyBuildNewSequence).not.toHaveBeenCalled(); | ||
expect(spyTriggerExecution).not.toHaveBeenCalled(); | ||
}); | ||
}); | ||
@@ -174,3 +183,11 @@ | ||
}); | ||
it('should do nothing if the Observable is already closed', () => { | ||
const obs$ = mutable<number>().close(); | ||
const completeCb = jest.fn(); | ||
obs$.subscribe({complete: completeCb}); | ||
obs$.close(); | ||
expect(completeCb).toBeCalledTimes(1); | ||
}); | ||
}); | ||
}); |
@@ -6,7 +6,10 @@ import { | ||
} from "../models/operator"; | ||
import { Subscriber } from "../models/subscription"; | ||
import { OnComplete, Subscriber } from "../models/subscription"; | ||
import { Types } from "../models/types"; | ||
import { Observable } from "./observable"; | ||
export class MutableObservable<T = never> extends Observable<T> implements Types.MutableObservable<T> { | ||
export class MutableObservable<T = never> | ||
extends Observable<T> | ||
implements Types.MutableObservable<T> | ||
{ | ||
private _preProcessOperations: OperatorFunction<T, any>[] = []; | ||
@@ -19,13 +22,15 @@ | ||
close(): void { | ||
close(): this { | ||
if (this._isComplete) { | ||
return; | ||
return this; | ||
} | ||
this._isComplete = true; | ||
this._subscribers.forEach((s) => { | ||
if (s.complete) { | ||
s.complete(); | ||
} | ||
}); | ||
this._subscribers | ||
.filter((s) => s.complete) | ||
.forEach((s) => { | ||
(s.complete as OnComplete)(); | ||
}); | ||
return this; | ||
} | ||
@@ -43,6 +48,8 @@ | ||
const newSequence = this._buildNewSequence( | ||
this._innerSequence.filter((event) => !event.isOperationError()).map((event) => event.value), | ||
operations | ||
this._innerSequence | ||
.filter((event) => !event.isOperationError()) | ||
.map((event) => event.value), | ||
[...operations, ...this._preProcessOperations] | ||
).filter((event) => !event.isMustStop()); | ||
const idxError = newSequence.findIndex((opRes) => opRes.isOperationError()); | ||
@@ -58,3 +65,5 @@ if (idxError > -1) { | ||
return this.next(...(this._innerSequence = newSequence).map((event) => event.value)); | ||
this._triggerExecution(this._innerSequence = newSequence, this._subscribers); | ||
return this; | ||
} | ||
@@ -61,0 +70,0 @@ |
@@ -7,6 +7,25 @@ import { filter, fork, map, mutable, until } from "../operators"; | ||
import { Subscription } from "../models/subscription"; | ||
import { promisify } from "util"; | ||
import { PromiseObservable } from "./promise-observable"; | ||
import { doesNotMatch } from "assert"; | ||
describe("ObservableFork", () => { | ||
it("will succeed", () => expect(true).toBe(true)); | ||
describe('creation', () => { | ||
it('should trigger the error callback of subscribers', () => { | ||
const source$ = of(12).pipe(map(() => {throw new Error('error')})); | ||
const obs$ = fork(source$); | ||
const errorCb = jest.fn((err) => expect(err).toEqual(new Error('error'))); | ||
obs$.subscribe({error: errorCb}); | ||
expect(errorCb).toHaveBeenNthCalledWith(1, new Error('error')); | ||
}); | ||
it('should trigger the complete callback of subscribers', () => { | ||
const obs$ = fork(of(12)); | ||
const completeCb = jest.fn(); | ||
obs$.subscribe({complete: completeCb}); | ||
expect(completeCb).toHaveBeenNthCalledWith(1); | ||
}); | ||
}); | ||
describe("close", () => { | ||
@@ -131,3 +150,3 @@ it("should stop forked Observables", (done) => { | ||
const completeCb = jest.fn(); | ||
forked$.subscribe({ | ||
forked$.subscribe({ | ||
next: (x) => expect(x).toEqual(100), | ||
@@ -152,2 +171,16 @@ error: () => fail(`Should not trigger the onError callback`), | ||
}); | ||
it('should work', (done) => { | ||
const promiseObs$ = new PromiseObservable<number>(new Promise((res) => setTimeout(() => res(1), 2000))); | ||
const fork$ = promiseObs$.pipe(); | ||
const spy = jest.fn(); | ||
fork$.subscribe({ | ||
next: (res) => { | ||
spy(); | ||
expect(spy).toHaveBeenCalledTimes(1); | ||
done(); | ||
} | ||
}); | ||
}) | ||
}); | ||
@@ -154,0 +187,0 @@ |
import { Observable } from "./observable"; | ||
import { isFunction } from "../helpers/function.helper"; | ||
import { OperatorFunction, OperationResult, OperationResultFlag } from "../models/operator"; | ||
import { | ||
OperatorFunction, | ||
OperationResult, | ||
OperationResultFlag, | ||
} from "../models/operator"; | ||
import { | ||
isInstanceOfSubscriber, | ||
OnComplete, | ||
OnError, | ||
OnNext, | ||
@@ -13,3 +19,6 @@ Subscriber, | ||
export class ObservableFork<T> extends Observable<T> implements Types.ObservableFork<T> { | ||
export class ObservableFork<T> | ||
extends Observable<T> | ||
implements Types.ObservableFork<T> | ||
{ | ||
private subscriptions: Subscription[] = []; | ||
@@ -19,3 +28,3 @@ private operators: OperatorFunction<T, OperationResult<any>>[] = []; | ||
constructor( | ||
private sourceObs$: Observable<T>, | ||
private sourceObs$: Types.Observable<T>, | ||
...operators: OperatorFunction<T, OperationResult<any>>[] | ||
@@ -28,22 +37,22 @@ ) { | ||
this.sourceObs$.subscribe({ | ||
name: 'subscriber fork constructor', | ||
next: (value) => { | ||
this._subscribers.forEach( | ||
(s) => | ||
{ | ||
if (s.next) { | ||
const result = this._executeOperations<T, T>(value, operators); | ||
if (!result.isFilterNotMatched() && !result.isMustStop()) { | ||
return s.next(result.value) | ||
} | ||
this._subscribers | ||
.filter((s) => s.next) | ||
.forEach((s) => { | ||
const result = this._executeOperations<T, T>(value, operators); | ||
if (!result.isFilterNotMatched() && !result.isMustStop()) { | ||
return (s.next as OnNext<T>)(result.value); | ||
} | ||
if (result.isMustStop()) { | ||
this.close(); | ||
} | ||
} | ||
} | ||
); | ||
if (result.isMustStop()) { | ||
this.close(); | ||
} | ||
}); | ||
}, | ||
error: (err) => { | ||
this._error = err; | ||
this._subscribers.forEach((s) => s.error && s.error(err)); | ||
this._subscribers | ||
.filter((s) => s.error) | ||
.forEach((s) => (s.error as OnError)(err)); | ||
}, | ||
@@ -54,3 +63,5 @@ | ||
this.unsubscribe(); | ||
this._subscribers.forEach((s) => s.complete && s.complete()); | ||
this._subscribers | ||
.filter((s) => s.complete) | ||
.forEach((s) => (s.complete as OnComplete)()); | ||
}, | ||
@@ -72,8 +83,20 @@ }); | ||
const newSequence: OperationResult<T>[] = []; | ||
const sourceSequence = (this.sourceObs$ as any)._innerSequence; // FIXME ew | ||
for (let i = 0, l = sourceSequence.length; i < l; i++ ) { | ||
const sourceSequence = (this.sourceObs$ as any)._innerSequence as OperationResult<T>[]; // FIXME ew | ||
for (let i = 0, l = sourceSequence.length; i < l; i++) { | ||
try { | ||
newSequence.push(this._executeOperations(sourceSequence[i].value, this.operators)); | ||
if (sourceSequence[i].isOperationError()) { | ||
throw sourceSequence[i].error; | ||
} | ||
newSequence.push( | ||
this._executeOperations(sourceSequence[i].value, this.operators) | ||
); | ||
} catch (error) { | ||
newSequence.push(new OperationResult(sourceSequence[i].value, OperationResultFlag.OperationError, error as Error)); | ||
newSequence.push( | ||
new OperationResult( | ||
sourceSequence[i].value, | ||
OperationResultFlag.OperationError, | ||
error as Error | ||
) | ||
); | ||
i = l; | ||
@@ -83,10 +106,9 @@ } | ||
this.executeSubscriber( | ||
_subscriber, | ||
newSequence | ||
); | ||
this.executeSubscriber(_subscriber, newSequence); | ||
return new Subscription( | ||
() => | ||
(this._subscribers = this._subscribers.filter((s) => s !== subscriber)) | ||
(this._subscribers = this._subscribers.filter( | ||
(s) => s !== subscriber | ||
)) | ||
); | ||
@@ -93,0 +115,0 @@ } |
@@ -10,4 +10,2 @@ import { filterNonFunctions, isFunction } from "../helpers/function.helper"; | ||
subscriberOf, | ||
OnComplete, | ||
OnError, | ||
OnNext, | ||
@@ -98,2 +96,3 @@ Subscriber, | ||
_subscriber.next && _subscriber.next(operationResult.value); | ||
} | ||
@@ -100,0 +99,0 @@ |
@@ -19,2 +19,2 @@ import { | ||
); | ||
}; | ||
}; |
@@ -7,3 +7,3 @@ import { | ||
export const tap = function <T = any>( | ||
f: OperatorFunction<T, boolean> | ||
f: OperatorFunction<T, void> | ||
): OperatorFunction<T, OperationResult<T>> { | ||
@@ -10,0 +10,0 @@ return (source: T) => { |
export * from './from'; | ||
export * from './mutable'; | ||
export * from './of'; | ||
export * from './on-event'; | ||
export * from './pre-process'; | ||
export * from './fork'; |
@@ -1,21 +0,49 @@ | ||
import { Observable } from '../../../observable/observable'; | ||
import { until } from '../../predicates/until'; | ||
import { map } from '../../transform/map'; | ||
import {preProcess} from './pre-process'; | ||
import { Observable } from "../../../observable/observable"; | ||
import { until } from "../../predicates/until"; | ||
import { map } from "../../transform/map"; | ||
import { preProcess } from "./pre-process"; | ||
import { tap } from "../../side-effects/tap"; | ||
import { filter } from "../../predicates/filter"; | ||
describe('preProcess', () => { | ||
it('will succeed', () => expect(true).toBe(true)); | ||
describe("preProcess", () => { | ||
it("will succeed", () => expect(true).toBe(true)); | ||
it('should return a new Observable', () => { | ||
expect(preProcess()).toBeInstanceOf(Observable); | ||
}); | ||
it("should return a new Observable", () => { | ||
expect(preProcess()).toBeInstanceOf(Observable); | ||
}); | ||
it('should apply the pre-processing operations every time we next', () => { | ||
const obs$ = preProcess<number>(map(x => x * 2), until(x => x >= 100)); | ||
let i = 0; | ||
it("should apply the pre-processing operations every time we next", () => { | ||
const obs$ = preProcess<number>( | ||
map((x) => x * 2), | ||
until((x) => x >= 100) | ||
); | ||
let i = 0; | ||
obs$.subscribe(x => (i < 100 ? expect(x).toEqual(i * 2) : expect(x).toEqual(50))); | ||
obs$.subscribe((x) => | ||
i < 100 ? expect(x).toEqual(i * 2) : expect(x).toEqual(50) | ||
); | ||
for(; i < 200; i++) { obs$.next(i); } | ||
}); | ||
}) | ||
for (; i < 200; i++) { | ||
obs$.next(i); | ||
} | ||
}); | ||
it("should apply the pre-processing operations every time we compile", () => { | ||
const toCall = jest.fn(); | ||
const obs$ = preProcess<number>(filter((x) => { | ||
toCall(); | ||
return x !== 5; | ||
})); | ||
obs$.next( | ||
...(() => { | ||
const sequence = []; | ||
for (let i = 0; i < 10; i++) { | ||
sequence.push(i); | ||
} | ||
return sequence; | ||
})() | ||
); | ||
obs$.compile(map((x) => x * 2)); | ||
expect(toCall).toHaveBeenCalledTimes(19); | ||
}); | ||
}); |
@@ -1,1 +0,4 @@ | ||
export * from './creation'; | ||
export * from './creation'; | ||
export * from './functions'; | ||
/* istanbul ignore next */ | ||
export {http} from './http'; |
@@ -18,4 +18,5 @@ { | ||
"**/*.test.ts", | ||
"./build/**/*" | ||
"./build/**/*", | ||
"**/*mock*/**/*" | ||
] | ||
} |
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
Network access
Supply chain riskThis module accesses the network.
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
181932
171
3660
10
324
8