derive-state
Advanced tools
Comparing version 0.1.0-alpha.1 to 0.1.0-alpha.2
@@ -187,3 +187,9 @@ 'use strict'; | ||
var distinctUntilChanged = function distinctUntilChanged() { | ||
var distinctUntilChanged = function distinctUntilChanged(eqFn) { | ||
if (eqFn === void 0) { | ||
eqFn = function eqFn(a, b) { | ||
return a === b; | ||
}; | ||
} | ||
return function (source) { | ||
@@ -193,3 +199,3 @@ return new DerivedState(function (next) { | ||
return source.subscribe(function (value) { | ||
if (value !== lastValue) { | ||
if (lastValue === EMPTY$1 || !eqFn(value, lastValue)) { | ||
next(value); | ||
@@ -196,0 +202,0 @@ } |
@@ -1,2 +0,2 @@ | ||
"use strict";function t(t,e){t.prototype=Object.create(e.prototype),t.prototype.constructor=t,t.__proto__=e}Object.defineProperty(exports,"__esModule",{value:!0});var e=function(){function t(){this.observers=new Set,this.closed=!1}var e=t.prototype;return e.addObserver=function(t,e){var r=this;if(this.closed)throw new Error("StatelessObservable was closed, can't subscribe");var i={next:t,disposed:e||n};return this.observers.add(i),function(){r.observers.delete(i)}},e.emit=function(t){if(this.closed)throw new Error("Observable was closed, can't emit new value");this.observers.forEach((function(e){return e.next(t)}))},e.dispose=function(){this.observers.forEach((function(t){return t.disposed()})),this.observers.clear(),this.closed=!0},t}(),n=function(){},r=function(){function t(t){var r=this;this.observerList=new e,this.state=s,this.teardown=t((function(t){r.observerList.closed||(r.state=t),r.observerList.emit(t)}))||n}var r,i=t.prototype;return i.subscribe=function(t,e){var n=this.observerList.addObserver(t,e);return this.state!==s&&t(this.state),n},i.hasValue=function(){return this.state!==s},i.getValue=function(){if(this.state===s)throw new Error("Can't retreive the value of the ObservableState, as it's empty");return this.state},i.dispose=function(){this.teardown(),this.state=s,this.observerList.dispose()},(r=[{key:"value",get:function(){var t=this;return new Promise((function(e){if(t.hasValue())return e(t.getValue());var n=t.subscribe((function(t){n(),e(t)}))}))}}])&&function(t,e){for(var n=0;n<e.length;n++){var r=e[n];r.enumerable=r.enumerable||!1,r.configurable=!0,"value"in r&&(r.writable=!0),Object.defineProperty(t,r.key,r)}}(t.prototype,r),t}(),i=function(e){function n(t){var n,r,i=arguments;return(n=e.call(this,(function(e){r=e,i.length>=1&&e(t)}))||this).next=r,n}return t(n,e),n.prototype.setValue=function(t){this.next(t)},n}(r),s=Symbol("empty"),o=Symbol("empty"),u=function(){return function(t){return new r((function(e){var n={skip:!0},r=t.subscribe((function(t){return n.skip?void 0:e(t)}));return n.skip=!1,r}))}},c=function(){function t(t){var r=this;this.observerList=new e,this.teardown=t((function(t){return r.observerList.emit(t)}))||n}var r=t.prototype;return r.subscribe=function(t,e){return this.observerList.addObserver(t,e)},r.dispose=function(){this.teardown(),this.observerList.dispose()},t}(),f=function(e){function n(){var t,n;return(t=e.call(this,(function(t){n=t}))||this).next=n,t}return t(n,e),n.prototype.emit=function(t){this.next(t)},n}(c);exports.DerivedState=r,exports.DerivedStateless=c,exports.State=i,exports.Stateless=f,exports.asStateless=function(t){return new c((function(e){return u()(t).subscribe(e)}))},exports.combine=function(t){return new r((function(e){var n=!1,r=Array.isArray(t)?[]:{},i=Object.entries(t),s=i.map((function(t){var s=t[0];return t[1].subscribe((function(t){r[s]=t,(n||Object.keys(r).length===i.length)&&(n=!0,e(r))}))}));return function(){return s.forEach((function(t){return t()}))}}))},exports.distinctUntilChanged=function(){return function(t){return new r((function(e){var n=o;return t.subscribe((function(t){t!==n&&e(t),n=t}))}))}},exports.filter=function(t){return function(e){return new r((function(n){return e.subscribe((function(e){return t(e)?n(e):void 0}))}))}},exports.map=function(t){return function(e){return new r((function(n){return e.subscribe((function(e){return n(t(e))}))}))}},exports.merge=function(t){return new r((function(e){var n=t.map((function(t){return t.subscribe(e)}));return function(){return n.forEach((function(t){return t()}))}}))},exports.pipe=function(t){for(var e=t,n=arguments.length,r=new Array(n>1?n-1:0),i=1;i<n;i++)r[i-1]=arguments[i];return r.forEach((function(t){e=t(e)})),e},exports.skipSynchronous=u,exports.switchMap=function(t){return function(e){return new r((function(n){var r=function(){},i=e.subscribe((function(e){r(),r=t(e).subscribe((function(t){return n(t)}))}));return function(){r(),i()}}))}},exports.take=function(t){return function(e){return new r((function(n){var r=0,i=e.subscribe((function(e){r<t&&n(e),++r>=t&&i&&i()}));return r>=t&&i(),i}))}},exports.withDefault=function(t){return function(e){return new r((function(n){return n(t),e.subscribe(n)}))}}; | ||
"use strict";function t(t,e){t.prototype=Object.create(e.prototype),t.prototype.constructor=t,t.__proto__=e}Object.defineProperty(exports,"__esModule",{value:!0});var e=function(){function t(){this.observers=new Set,this.closed=!1}var e=t.prototype;return e.addObserver=function(t,e){var r=this;if(this.closed)throw new Error("StatelessObservable was closed, can't subscribe");var i={next:t,disposed:e||n};return this.observers.add(i),function(){r.observers.delete(i)}},e.emit=function(t){if(this.closed)throw new Error("Observable was closed, can't emit new value");this.observers.forEach((function(e){return e.next(t)}))},e.dispose=function(){this.observers.forEach((function(t){return t.disposed()})),this.observers.clear(),this.closed=!0},t}(),n=function(){},r=function(){function t(t){var r=this;this.observerList=new e,this.state=o,this.teardown=t((function(t){r.observerList.closed||(r.state=t),r.observerList.emit(t)}))||n}var r,i=t.prototype;return i.subscribe=function(t,e){var n=this.observerList.addObserver(t,e);return this.state!==o&&t(this.state),n},i.hasValue=function(){return this.state!==o},i.getValue=function(){if(this.state===o)throw new Error("Can't retreive the value of the ObservableState, as it's empty");return this.state},i.dispose=function(){this.teardown(),this.state=o,this.observerList.dispose()},(r=[{key:"value",get:function(){var t=this;return new Promise((function(e){if(t.hasValue())return e(t.getValue());var n=t.subscribe((function(t){n(),e(t)}))}))}}])&&function(t,e){for(var n=0;n<e.length;n++){var r=e[n];r.enumerable=r.enumerable||!1,r.configurable=!0,"value"in r&&(r.writable=!0),Object.defineProperty(t,r.key,r)}}(t.prototype,r),t}(),i=function(e){function n(t){var n,r,i=arguments;return(n=e.call(this,(function(e){r=e,i.length>=1&&e(t)}))||this).next=r,n}return t(n,e),n.prototype.setValue=function(t){this.next(t)},n}(r),o=Symbol("empty"),s=Symbol("empty"),u=function(){return function(t){return new r((function(e){var n={skip:!0},r=t.subscribe((function(t){return n.skip?void 0:e(t)}));return n.skip=!1,r}))}},c=function(){function t(t){var r=this;this.observerList=new e,this.teardown=t((function(t){return r.observerList.emit(t)}))||n}var r=t.prototype;return r.subscribe=function(t,e){return this.observerList.addObserver(t,e)},r.dispose=function(){this.teardown(),this.observerList.dispose()},t}(),f=function(e){function n(){var t,n;return(t=e.call(this,(function(t){n=t}))||this).next=n,t}return t(n,e),n.prototype.emit=function(t){this.next(t)},n}(c);exports.DerivedState=r,exports.DerivedStateless=c,exports.State=i,exports.Stateless=f,exports.asStateless=function(t){return new c((function(e){return u()(t).subscribe(e)}))},exports.combine=function(t){return new r((function(e){var n=!1,r=Array.isArray(t)?[]:{},i=Object.entries(t),o=i.map((function(t){var o=t[0];return t[1].subscribe((function(t){r[o]=t,(n||Object.keys(r).length===i.length)&&(n=!0,e(r))}))}));return function(){return o.forEach((function(t){return t()}))}}))},exports.distinctUntilChanged=function(t){return void 0===t&&(t=function(t,e){return t===e}),function(e){return new r((function(n){var r=s;return e.subscribe((function(e){r!==s&&t(e,r)||n(e),r=e}))}))}},exports.filter=function(t){return function(e){return new r((function(n){return e.subscribe((function(e){return t(e)?n(e):void 0}))}))}},exports.map=function(t){return function(e){return new r((function(n){return e.subscribe((function(e){return n(t(e))}))}))}},exports.merge=function(t){return new r((function(e){var n=t.map((function(t){return t.subscribe(e)}));return function(){return n.forEach((function(t){return t()}))}}))},exports.pipe=function(t){for(var e=t,n=arguments.length,r=new Array(n>1?n-1:0),i=1;i<n;i++)r[i-1]=arguments[i];return r.forEach((function(t){e=t(e)})),e},exports.skipSynchronous=u,exports.switchMap=function(t){return function(e){return new r((function(n){var r=function(){},i=e.subscribe((function(e){r(),r=t(e).subscribe((function(t){return n(t)}))}));return function(){r(),i()}}))}},exports.take=function(t){return function(e){return new r((function(n){var r=0,i=e.subscribe((function(e){r<t&&n(e),++r>=t&&i&&i()}));return r>=t&&i(),i}))}},exports.withDefault=function(t){return function(e){return new r((function(n){return n(t),e.subscribe(n)}))}}; | ||
//# sourceMappingURL=derive-state.cjs.production.min.js.map |
@@ -183,3 +183,9 @@ function _defineProperties(target, props) { | ||
var distinctUntilChanged = function distinctUntilChanged() { | ||
var distinctUntilChanged = function distinctUntilChanged(eqFn) { | ||
if (eqFn === void 0) { | ||
eqFn = function eqFn(a, b) { | ||
return a === b; | ||
}; | ||
} | ||
return function (source) { | ||
@@ -189,3 +195,3 @@ return new DerivedState(function (next) { | ||
return source.subscribe(function (value) { | ||
if (value !== lastValue) { | ||
if (lastValue === EMPTY$1 || !eqFn(value, lastValue)) { | ||
next(value); | ||
@@ -192,0 +198,0 @@ } |
import { DerivedState } from '../state'; | ||
import { Observable } from '../interface'; | ||
export declare const distinctUntilChanged: <T>() => (source: Observable<T>) => DerivedState<T>; | ||
export declare const distinctUntilChanged: <T>(eqFn?: (a: T, b: T) => boolean) => (source: Observable<T>) => DerivedState<T>; |
@@ -7,3 +7,3 @@ import { ObservableState } from './interface'; | ||
constructor(derive: (next: (value: T) => void) => void | (() => void)); | ||
subscribe(callback: (value: T) => void, disposed?: () => void): () => boolean; | ||
subscribe(callback: (value: T) => void, disposed?: () => void): () => void; | ||
hasValue(): boolean; | ||
@@ -10,0 +10,0 @@ getValue(): T; |
@@ -6,3 +6,3 @@ import { Observable, ObservableState } from './interface'; | ||
constructor(derive: (next: (value: T) => void) => void | (() => void)); | ||
subscribe(callback: (value: T) => void, disposed?: () => void): () => boolean; | ||
subscribe(callback: (value: T) => void, disposed?: () => void): () => void; | ||
dispose(): void; | ||
@@ -9,0 +9,0 @@ } |
{ | ||
"version": "0.1.0-alpha.1", | ||
"version": "0.1.0-alpha.2", | ||
"license": "MIT", | ||
@@ -60,2 +60,2 @@ "main": "dist/index.js", | ||
} | ||
} | ||
} |
@@ -96,4 +96,9 @@ # DeriveState | ||
Creation operators | ||
- `combine(obj)`: subscribes to every observable in `obj`, and emits the value of all of them in the same structure as `obj` (works with arrays too) | ||
- `merge(array)`: subscribes to all the observables in array and emits every value from them. | ||
Pipeable operators | ||
- `map(fn)`: maps the values from the source stream by using the map function. | ||
@@ -100,0 +105,0 @@ - `filter(fn)`: filters changes based on the filter function. |
import { DerivedState } from '../state'; | ||
import { Observable } from '../interface'; | ||
export const distinctUntilChanged = <T>() => (source: Observable<T>) => | ||
new DerivedState<T>(next => { | ||
export const distinctUntilChanged = <T>( | ||
eqFn: (a: T, b: T) => boolean = (a, b) => a === b | ||
) => (source: Observable<T>) => | ||
new DerivedState<T>((next) => { | ||
let lastValue: T | typeof EMPTY = EMPTY; | ||
return source.subscribe(value => { | ||
if (value !== lastValue) { | ||
return source.subscribe((value) => { | ||
if (lastValue === EMPTY || !eqFn(value, lastValue)) { | ||
next(value); | ||
@@ -10,0 +12,0 @@ } |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
89410
1063
109