Comparing version 1.0.5 to 1.0.6
@@ -8,3 +8,3 @@ /* removed in prod */ import { RN } from '../mod'; | ||
initialValue: T, | ||
predicate: (e: T) => boolean, | ||
predicate: (srcValue: T, srcIndex: number, index: number) => boolean, | ||
): Operator<T, T> => | ||
@@ -15,3 +15,3 @@ (( src: RN<T> ) => new FilterRN<T>( src, initialValue, predicate )); | ||
class FilterRN<T> extends RN<T> { | ||
private predicate: (e: T) => boolean; | ||
private predicate: (srcValue: T, srcIndex: number, index: number) => boolean; | ||
@@ -21,3 +21,3 @@ constructor( | ||
initialValue: T, | ||
predicate: (e: T) => boolean, | ||
predicate: (srcValue: T, srcIndex: number, index: number) => boolean, | ||
) { | ||
@@ -29,7 +29,8 @@ super( initialValue, [src] ); | ||
protected fire() { | ||
const nextVal = this.parents[0].value; | ||
if ( this.predicate( nextVal ) ) { | ||
this.fireWith( nextVal ); | ||
const src = this.parents[0]; | ||
// note: 'this.index' is not updated yet (will be updated in this.fireWith()) | ||
if ( this.predicate( src.value, src.index, this.index + 1 ) ) { | ||
this.fireWith( src.value ); | ||
} | ||
} | ||
} |
@@ -7,3 +7,5 @@ /* removed in prod */ import { RN } from '../mod'; | ||
export const flatMap = <T, U>( fn: (e: T) => RN<U> ): Operator<T, U> => | ||
export const flatMap = <T, U>( | ||
fn: (srcValue: T, srcIndex: number, index: number) => RN<U> | ||
): Operator<T, U> => | ||
(( src: RN<T> ) => new FlatMapRN<T, U>( src, fn )); | ||
@@ -15,7 +17,10 @@ | ||
private subscriptions: Subscription[]; | ||
private fn: (e: T) => RN<U>; | ||
private fn: (srcValue: T, srcIndex: number, index: number) => RN<U>; | ||
constructor( src: RN<T>, fn: (e: T) => RN<U> ) { | ||
super( fn( src.value ).value, [src] ); | ||
this.latestRN = fn( src.value ); | ||
constructor( | ||
src: RN<T>, | ||
fn: (srcValue: T, srcIndex: number, index: number) => RN<U> | ||
) { | ||
super( fn( src.value, src.index, -1 ).value, [src] ); | ||
this.latestRN = fn( src.value, src.index, -1 ); | ||
this.fn = fn; | ||
@@ -27,3 +32,5 @@ this.subscriptions = []; | ||
protected fire() { // switch latestRN here | ||
this.latestRN = this.fn( this.parents[0].value ); | ||
const src = this.parents[0]; | ||
// note: 'this.index' is not updated yet (will be updated in this.fireWith()) | ||
this.latestRN = this.fn( src.value, src.index, this.index + 1 ); | ||
this.subscriptions.push( this.latestRN.subscribe( e => this.fireWith( e ) ) ); | ||
@@ -30,0 +37,0 @@ } |
@@ -6,19 +6,22 @@ /* removed in prod */ import { RN } from '../mod'; | ||
export const map = <T, U>( fn: (value: T, index: number) => U ): Operator<T, U> => | ||
(( src: RN<T> ) => new MapRN<T, U>( src, fn )); | ||
export const map = <T, U>( fn: (srcValue: T, srcIndex: number, index: number) => U ): Operator<T, U> => | ||
(( src: RN<T> ) => new MapRN<T, U>( src, fn )); | ||
export const mapTo = <T, U>( value: U ): Operator<T, U> => | ||
(( src: RN<T> ) => new MapRN<T, U>( src, () => value )); | ||
(( src: RN<T> ) => new MapRN<T, U>( src, () => value )); | ||
export const pluck = <T, K extends keyof T>( member: K ): Operator<T, T[K]> => | ||
(( src: RN<T> ) => new MapRN<T, T[K]>( src, value => value[ member ] )); | ||
(( src: RN<T> ) => new MapRN<T, T[K]>( src, value => value[ member ] )); | ||
export const withTimestamp = <T>(): Operator<T, [T, number]> => | ||
(( src: RN<T> ) => new MapRN<T, [T, number]>( src, value => [value, Date.now()] )); | ||
(( src: RN<T> ) => new MapRN<T, [T, number]>( src, value => [value, Date.now()] )); | ||
class MapRN<T, U> extends RN<U> { | ||
private fn: (value: T, index: number) => U; | ||
private fn: (srcValue: T, srcIndex: number, index: number) => U; | ||
constructor( src: RN<T>, fn: (value: T, index: number) => U ) { | ||
super( fn( src.value, src.index ), [src] ); | ||
constructor( | ||
src: RN<T>, | ||
fn: (srcValue: T, srcIndex: number, index: number) => U | ||
) { | ||
super( fn( src.value, src.index, -1 ), [src] ); | ||
this.fn = fn; | ||
@@ -28,5 +31,6 @@ } | ||
protected fire() { | ||
const next = this.parents[0]; | ||
this.fireWith( this.fn( next.value, next.index ) ); | ||
const src = this.parents[0]; | ||
// note: 'this.index' is not updated yet (will be updated in this.fireWith()) | ||
this.fireWith( this.fn( src.value, src.index, this.index + 1 ) ); | ||
} | ||
} |
@@ -8,3 +8,3 @@ /* removed in prod */ import { RN } from '../mod'; | ||
initialValue: U, | ||
fn: (prev: U, curr: T, index?: number) => U | ||
fn: (state: U, srcValue: T, srcIndex: number, index: number) => U | ||
): Operator<T, U> => | ||
@@ -16,3 +16,3 @@ (( src: RN<T> ) => new ScanRN<T, U>( initialValue, src, fn )); | ||
private scanState: U; | ||
private fn: (prev: U, curr: T, index?: number) => U; | ||
private fn: (state: U, srcValue: T, srcIndex: number, index: number) => U; | ||
@@ -22,3 +22,3 @@ constructor( | ||
src: RN<T>, | ||
fn: (prev: U, curr: T, index?: number) => U | ||
fn: (state: U, srcValue: T, srcIndex: number, index: number) => U | ||
) { | ||
@@ -31,4 +31,6 @@ super( initialValue, [src] ); | ||
protected fire() { | ||
this.fireWith( this.fn( this.scanState, this.parents[0].value, this.index ) ); | ||
const src = this.parents[0]; | ||
// note: 'this.index' is not updated yet (will be updated in this.fireWith()) | ||
this.fireWith( this.fn( this.scanState, src.value, src.index, this.index + 1 ) ); | ||
} | ||
} |
@@ -13,3 +13,3 @@ /* removed in prod */ import { RN } from '../mod'; | ||
initialValue, | ||
(_, index) => index < skipNum, | ||
(_srcValue, srcIndex, _index) => srcIndex < skipNum, | ||
)); | ||
@@ -19,3 +19,3 @@ | ||
initialValue: T, | ||
predicate: (value: T, index: number) => boolean | ||
predicate: (srcValue: T, srcIndex: number, index: number) => boolean | ||
): Operator<T, T> => (( src: RN<T> ) => | ||
@@ -26,3 +26,3 @@ new SkipWhileRN<T>( src, initialValue, predicate )); | ||
class SkipWhileRN<T> extends RN<T> { | ||
private predicate: (value: T, index: number) => boolean; | ||
private predicate: (srcValue: T, srcIndex: number, index: number) => boolean; | ||
@@ -32,3 +32,3 @@ constructor( | ||
initialValue: T, | ||
predicate: (value: T, index: number) => boolean | ||
predicate: (srcValue: T, srcIndex: number, index: number) => boolean | ||
) { | ||
@@ -40,8 +40,8 @@ super( initialValue, [src] ); | ||
protected fire() { | ||
const nextValue = this.parents[0].value; | ||
const nextIndex = this.parents[0].index; | ||
if ( !this.predicate( nextValue, nextIndex ) ) { | ||
this.fireWith( nextValue ); | ||
const src = this.parents[0]; | ||
// note: 'this.index' is not updated yet (will be updated in this.fireWith()) | ||
if ( !this.predicate( src.value, src.index, this.index + 1 ) ) { | ||
this.fireWith( src.value ); | ||
} | ||
} | ||
} |
@@ -17,2 +17,3 @@ /* removed in prod */ import { RN } from '../mod'; | ||
this.appeared = new Set<T|T[K]>(); | ||
this.appeared.add( src.value ); | ||
this.key = key; | ||
@@ -19,0 +20,0 @@ } |
@@ -20,9 +20,9 @@ /* removed in prod */ import { RN } from '../mod'; | ||
protected fire() { | ||
const nextVal = this.parents[0].value; | ||
const currVal = this.value; | ||
const currVal = this.parents[0].value; | ||
const prevVal = this.value; | ||
if ( !this.eq( nextVal, currVal ) ) { | ||
this.fireWith( nextVal ); | ||
if ( !this.eq( currVal, prevVal ) ) { | ||
this.fireWith( currVal ); | ||
} | ||
} | ||
} |
@@ -7,3 +7,5 @@ /* removed in prod */ import { RN } from '../mod'; | ||
export const switchMap = <T, U>( fn: (e: T) => RN<U> ): Operator<T, U> => | ||
export const switchMap = <T, U>( | ||
fn: (srcValue: T, srcIndex: number, index: number) => RN<U> | ||
): Operator<T, U> => | ||
(( src: RN<T> ) => new SwitchMapRN<T, U>( src, fn )); | ||
@@ -15,7 +17,10 @@ | ||
private subscription: Subscription; | ||
private fn: (e: T) => RN<U>; | ||
private fn: (srcValue: T, srcIndex: number, index: number) => RN<U>; | ||
constructor( src: RN<T>, fn: (e: T) => RN<U> ) { | ||
super( fn( src.value ).value, [src] ); | ||
this.latestRN = fn( src.value ); | ||
constructor( | ||
src: RN<T>, | ||
fn: (srcValue: T, srcIndex: number, index: number) => RN<U> | ||
) { | ||
super( fn( src.value, src.index, -1 ).value, [src] ); | ||
this.latestRN = fn( src.value, src.index, -1 ); | ||
this.fn = fn; | ||
@@ -27,3 +32,5 @@ this.subscription = this.latestRN.subscribe( e => this.fireWith( e ) ); | ||
this.subscription.unsubscribe(); | ||
this.latestRN = this.fn( this.parents[0].value ); | ||
const src = this.parents[0]; | ||
// note: 'this.index' is not updated yet (will be updated in this.fireWith()) | ||
this.latestRN = this.fn( src.value, src.index, this.index + 1 ); | ||
this.subscription = this.latestRN.subscribe( e => this.fireWith( e ) ); | ||
@@ -30,0 +37,0 @@ } |
@@ -7,6 +7,7 @@ /* removed in prod */ import { RN } from '../mod'; | ||
export const take = <T>( takeNum: number ): Operator<T, T> => | ||
(( src: RN<T> ) => new TakeWhileRN<T>( src, (_, index) => index < takeNum )); | ||
(( src: RN<T> ) => new TakeWhileRN<T>( | ||
src, (_srcValue, srcIndex, _index) => srcIndex < takeNum )); | ||
export const takeWhile = <T>( | ||
predicate: (value: T, index: number) => boolean | ||
predicate: (srcValue: T, srcIndex: number, index: number) => boolean | ||
): Operator<T, T> => (( src: RN<T> ) => new TakeWhileRN<T>( src, predicate )); | ||
@@ -16,7 +17,7 @@ | ||
class TakeWhileRN<T> extends RN<T> { | ||
private predicate: (value: T, index: number) => boolean; | ||
private predicate: (srcValue: T, srcIndex: number, index: number) => boolean; | ||
constructor( | ||
src: RN<T>, | ||
predicate: (value: T, index: number) => boolean | ||
predicate: (srcValue: T, srcIndex: number, index: number) => boolean | ||
) { | ||
@@ -28,6 +29,7 @@ super( src.value, [src] ); | ||
protected fire() { | ||
const next = this.parents[0]; | ||
const src = this.parents[0]; | ||
if ( this.predicate( next.value, next.index ) ) { | ||
this.fireWith( next.value ); | ||
// note: 'this.index' is not updated yet (will be updated in this.fireWith()) | ||
if ( this.predicate( src.value, src.index, this.index + 1 ) ) { | ||
this.fireWith( src.value ); | ||
} else { | ||
@@ -34,0 +36,0 @@ this.complete(); |
@@ -159,5 +159,4 @@ import { RNId } from './types/RNId'; | ||
/* @deprecated This is an internal implementation detail, do not use. */ | ||
get value() { return this.valueInternal; } | ||
get index() { return ( this.indexInternal < 0 ? 0 : this.indexInternal ); } | ||
get index() { return this.indexInternal; } | ||
@@ -408,11 +407,18 @@ | ||
filter( initialValue: T, predicate: (e: T) => boolean ): RN<T> { | ||
filter( | ||
initialValue: T, | ||
predicate: (e: T) => boolean | ||
): RN<T> { | ||
return filter<T>( initialValue, predicate )( this ); | ||
} | ||
flatMap<U>( fn: (e: T) => RN<U> ): RN<U> { | ||
flatMap<U>( | ||
fn: (srcValue: T, srcIndex: number, index: number) => RN<U> | ||
): RN<U> { | ||
return flatMap<T, U>( fn )( this ); | ||
} | ||
map<U>( fn: (value: T, index: number) => U ): RN<U> { | ||
map<U>( | ||
fn: (srcValue: T, srcIndex: number, index: number) => U | ||
): RN<U> { | ||
return map<T, U>( fn )( this ); | ||
@@ -435,3 +441,3 @@ } | ||
initialValue: U, | ||
fn: (prev: U, curr: T, index?: number) => U | ||
fn: (state: U, srcValue: T, srcIndex: number, index: number) => U | ||
): RN<U> { | ||
@@ -455,3 +461,3 @@ return scan<T, U>( initialValue, fn )( this ); | ||
initialValue: T, | ||
predicate: (value: T, index: number) => boolean, | ||
predicate: (srcValue: T, srcIndex: number, index: number) => boolean, | ||
): RN<T> { | ||
@@ -461,3 +467,5 @@ return skipWhile<T>( initialValue, predicate )( this ); | ||
switchMap<U>( fn: (e: T) => RN<U> ): RN<U> { | ||
switchMap<U>( | ||
fn: (srcValue: T, srcIndex: number, index: number) => RN<U> | ||
): RN<U> { | ||
return switchMap<T, U>( fn )( this ); | ||
@@ -470,3 +478,5 @@ } | ||
takeWhile( predicate: (value: T, index: number) => boolean ): RN<T> { | ||
takeWhile( | ||
predicate: (srcValue: T, srcIndex: number, index: number) => boolean | ||
): RN<T> { | ||
return takeWhile<T>( predicate )( this ); | ||
@@ -473,0 +483,0 @@ } |
@@ -17,284 +17,293 @@ import { RN, | ||
import { interval as rxjsInterval } from 'rxjs'; | ||
// fv1.addChild | ||
// fv1.curr | ||
// fv1.latestFire | ||
// fv1.priority | ||
// fv1.id | ||
const a = interval(100, true); | ||
// const array: number[] = []; | ||
// [ 2, 14, 15, 25, 21, 40, 31, 45, 26, 22, 70, 45, 85, 59, 47, 56, 75, 53, 70, 33 ] | ||
// .forEach( e => addAsHeap( array, e ) ); | ||
// delay | ||
setTimeout(() => { | ||
const b = a.map( (sv, si, i) => [sv, si, i] ) | ||
b.listen( true, console.log ); | ||
}, 550); | ||
// removeAsHeap( array ); | ||
// console.log( array ); | ||
// import { interval as rxjsInterval } from 'rxjs'; | ||
// // fv1.addChild | ||
// // fv1.curr | ||
// // fv1.latestFire | ||
// // fv1.priority | ||
// // fv1.id | ||
// // const array: number[] = []; | ||
// // [ 2, 14, 15, 25, 21, 40, 31, 45, 26, 22, 70, 45, 85, 59, 47, 56, 75, 53, 70, 33 ] | ||
// // .forEach( e => addAsHeap( array, e ) ); | ||
// // removeAsHeap( array ); | ||
// // console.log( array ); | ||
const testid: string = 'toObservable2'; | ||
console.log('testid = ', testid); | ||
switch ( testid ) { | ||
case 'toObservable2' : { | ||
const rn = interval( 1000, true ).take(5); | ||
const obs = toObservable( rn, false ); | ||
obs.subscribe( | ||
v => console.log('next', v), | ||
() => console.log('error'), | ||
() => console.log('complete') ); | ||
break; | ||
} | ||
case 'toObservable' : { | ||
const rn = interval( 1000, true ).take(5); | ||
const obs = toObservable( rn, true ); | ||
obs.subscribe( | ||
v => console.log('next', v), | ||
() => console.log('error'), | ||
() => console.log('complete') ); | ||
break; | ||
} | ||
// const testid: string = 'toObservable2'; | ||
// console.log('testid = ', testid); | ||
case 'fromObservable' : { | ||
const a = fromObservable( 0, rxjsInterval( 1000 ) ); | ||
a.listen( false, console.log ); | ||
break; | ||
} | ||
// switch ( testid ) { | ||
case 'complete-check4' : { | ||
const _ = interval(1000, true) | ||
const a = _.map( e => 100 * e ); | ||
const b = interval(500, true); | ||
const c = combine( a, b ); | ||
a.subscribe(console.log, () => {}, e => console.log('a complete', e)); | ||
b.subscribe(console.log, () => {}, e => console.log('b complete', e)); | ||
c.subscribe(console.log, () => {}, e => console.log('c complete', e)); | ||
setTimeout(() => { _.stop() }, 3000); | ||
setTimeout(() => { b.stop() }, 5000); | ||
break; | ||
} | ||
// case 'toObservable2' : { | ||
// const rn = interval( 1000, true ).take(5); | ||
// const obs = toObservable( rn, false ); | ||
// obs.subscribe( | ||
// v => console.log('next', v), | ||
// () => console.log('error'), | ||
// () => console.log('complete') ); | ||
// break; | ||
// } | ||
case 'complete-check3' : { | ||
const a = interval(1000, true).map( e => 100 * e ); | ||
const b = interval(500, true); | ||
const c = combine( a, b ); | ||
a.subscribe(console.log, () => {}, e => console.log('a complete', e)); | ||
b.subscribe(console.log, () => {}, e => console.log('b complete', e)); | ||
c.subscribe(console.log, () => {}, e => console.log('c complete', e)); | ||
setTimeout(() => { b.stop() }, 3000); | ||
break; | ||
} | ||
// case 'toObservable' : { | ||
// const rn = interval( 1000, true ).take(5); | ||
// const obs = toObservable( rn, true ); | ||
// obs.subscribe( | ||
// v => console.log('next', v), | ||
// () => console.log('error'), | ||
// () => console.log('complete') ); | ||
// break; | ||
// } | ||
case 'complete-check2' : { | ||
const a = interval(1000, true).map( e => 100 * e ); | ||
const b = interval(500, true); | ||
const c = combine( a, b ); | ||
const sbs1 = c.subscribe(console.log); | ||
const sbs2 = a.subscribe(console.log); | ||
setTimeout(() => { sbs1.unsubscribe() }, 5000); | ||
setTimeout(() => { sbs2.unsubscribe() }, 3000); | ||
break; | ||
} | ||
// case 'fromObservable' : { | ||
// const a = fromObservable( 0, rxjsInterval( 1000 ) ); | ||
// a.listen( false, console.log ); | ||
// break; | ||
// } | ||
case 'complete-check1' : { | ||
const a = interval(1000, true); | ||
const b = a.mapTo(1); | ||
const sbs = b.subscribe(console.log); | ||
setTimeout(() => { sbs.unsubscribe() }, 3000); | ||
break; | ||
} | ||
// case 'complete-check4' : { | ||
// const _ = interval(1000, true) | ||
// const a = _.map( e => 100 * e ); | ||
// const b = interval(500, true); | ||
// const c = combine( a, b ); | ||
// a.subscribe(console.log, () => {}, e => console.log('a complete', e)); | ||
// b.subscribe(console.log, () => {}, e => console.log('b complete', e)); | ||
// c.subscribe(console.log, () => {}, e => console.log('c complete', e)); | ||
// setTimeout(() => { _.stop() }, 3000); | ||
// setTimeout(() => { b.stop() }, 5000); | ||
// break; | ||
// } | ||
case 'flatMap' : { | ||
const a = interval(2000, true); | ||
const b = a.flatMap( e => interval(700, true).map( x => [e, e * (x + 1)] ).take(4) ); | ||
b.subscribe( console.log ); | ||
break; | ||
} | ||
// case 'complete-check3' : { | ||
// const a = interval(1000, true).map( e => 100 * e ); | ||
// const b = interval(500, true); | ||
// const c = combine( a, b ); | ||
// a.subscribe(console.log, () => {}, e => console.log('a complete', e)); | ||
// b.subscribe(console.log, () => {}, e => console.log('b complete', e)); | ||
// c.subscribe(console.log, () => {}, e => console.log('c complete', e)); | ||
// setTimeout(() => { b.stop() }, 3000); | ||
// break; | ||
// } | ||
case 'switchMap' : { | ||
const a = interval(1000, true); | ||
const b = a.switchMap( e => interval(300, true).map( x => e * x ) ); | ||
b.subscribe( console.log ); | ||
break; | ||
} | ||
// case 'complete-check2' : { | ||
// const a = interval(1000, true).map( e => 100 * e ); | ||
// const b = interval(500, true); | ||
// const c = combine( a, b ); | ||
// const sbs1 = c.subscribe(console.log); | ||
// const sbs2 = a.subscribe(console.log); | ||
// setTimeout(() => { sbs1.unsubscribe() }, 5000); | ||
// setTimeout(() => { sbs2.unsubscribe() }, 3000); | ||
// break; | ||
// } | ||
case 'withTimestamp' : { | ||
const values = [3, 1, 4, 1, 5, 9, 2, 6, 5, 3, 5, 8, 9]; | ||
const a = interval(1000, true); | ||
const b = interval(300, true); | ||
const c = a.map( (_, i) => values[i] ) | ||
.takeWhile( (_, i) => i < values.length ) | ||
.withTimestamp(); | ||
c.subscribe( console.log ); | ||
break; | ||
} | ||
// case 'complete-check1' : { | ||
// const a = interval(1000, true); | ||
// const b = a.mapTo(1); | ||
// const sbs = b.subscribe(console.log); | ||
// setTimeout(() => { sbs.unsubscribe() }, 3000); | ||
// break; | ||
// } | ||
case 'withLatest' : { | ||
const values = [3, 1, 4, 1, 5, 9, 2, 6, 5, 3, 5, 8, 9]; | ||
const a = interval(1000, true); | ||
const b = interval(300, true); | ||
const c = a.map( (_, i) => values[i] ) | ||
.takeWhile( (_, i) => i < values.length ) | ||
.withLatest( b ); | ||
c.subscribe( console.log ); | ||
break; | ||
} | ||
// case 'flatMap' : { | ||
// const a = interval(2000, true); | ||
// const b = a.flatMap( e => interval(700, true).map( x => [e, e * (x + 1)] ).take(4) ); | ||
// b.subscribe( console.log ); | ||
// break; | ||
// } | ||
case 'take' : { | ||
const values = [3, 1, 4, 1, 5, 9, 2, 6, 5, 3, 5, 8, 9]; | ||
const a = interval(1000, true); | ||
const b = a.map( (_, i) => values[i] ) | ||
.take(5); | ||
b.subscribe( console.log ); | ||
break; | ||
} | ||
// case 'switchMap' : { | ||
// const a = interval(1000, true); | ||
// const b = a.switchMap( e => interval(300, true).map( x => e * x ) ); | ||
// b.subscribe( console.log ); | ||
// break; | ||
// } | ||
case 'skipWhile' : { | ||
const values = [3, 1, 4, 1, 5, 9, 2, 6, 5, 3, 5, 8, 9]; | ||
const a = interval(1000, true); | ||
const b = a.map( (_, i) => values[i] ) | ||
.skipWhile( 0, (_, i) => i < 3 ) | ||
b.subscribe( console.log ); | ||
break; | ||
} | ||
// case 'withTimestamp' : { | ||
// const values = [3, 1, 4, 1, 5, 9, 2, 6, 5, 3, 5, 8, 9]; | ||
// const a = interval(1000, true); | ||
// const b = interval(300, true); | ||
// const c = a.map( (_, i) => values[i] ) | ||
// .takeWhile( (_, i) => i < values.length ) | ||
// .withTimestamp(); | ||
// c.subscribe( console.log ); | ||
// break; | ||
// } | ||
case 'skipUnchanged' : { | ||
const values = [7, 7, 7, 9, 3, 3, 2, 3, 8, 8, 4, 6, 2, 6, 4]; | ||
const a = interval(1000, true); | ||
const b = a.map( (_, i) => values[i] ) | ||
.takeWhile( (_, i) => i < values.length ) | ||
.skipUnchanged(); | ||
b.subscribe( console.log ); | ||
break; | ||
} | ||
// case 'withLatest' : { | ||
// const values = [3, 1, 4, 1, 5, 9, 2, 6, 5, 3, 5, 8, 9]; | ||
// const a = interval(1000, true); | ||
// const b = interval(300, true); | ||
// const c = a.map( (_, i) => values[i] ) | ||
// .takeWhile( (_, i) => i < values.length ) | ||
// .withLatest( b ); | ||
// c.subscribe( console.log ); | ||
// break; | ||
// } | ||
case 'skipAlreadyAppeared' : { | ||
const values = [3, 1, 4, 1, 5, 9, 2, 6, 5, 3, 5, 8, 9]; | ||
const a = interval(1000, true); | ||
const b = a.map( (_, i) => values[i] ) | ||
.takeWhile( (_, i) => i < values.length ) | ||
.skipAlreadyAppeared(); | ||
b.subscribe( console.log ); | ||
break; | ||
} | ||
// case 'take' : { | ||
// const values = [3, 1, 4, 1, 5, 9, 2, 6, 5, 3, 5, 8, 9]; | ||
// const a = interval(1000, true); | ||
// const b = a.map( (_, i) => values[i] ) | ||
// .take(5); | ||
// b.subscribe( console.log ); | ||
// break; | ||
// } | ||
case 'skip' : { | ||
const a = interval(1000, true); | ||
const b = a.skip( 999, 3 ); | ||
console.log(b.value); | ||
b.subscribe( console.log ); | ||
break; | ||
} | ||
// case 'skipWhile' : { | ||
// const values = [3, 1, 4, 1, 5, 9, 2, 6, 5, 3, 5, 8, 9]; | ||
// const a = interval(1000, true); | ||
// const b = a.map( (_, i) => values[i] ) | ||
// .skipWhile( 0, (_, i) => i < 3 ) | ||
// b.subscribe( console.log ); | ||
// break; | ||
// } | ||
case 'pluck' : { | ||
const a = interval(1000, true); | ||
const b = a.pairwise() | ||
.map( e => ({ prev: e[0], curr: e[1] }) ) | ||
.pluck('curr'); | ||
b.subscribe( console.log ); | ||
break; | ||
} | ||
// case 'skipUnchanged' : { | ||
// const values = [3, 3, 3, 1, 4, 1, 5, 9, 9, 9, 2, 6]; | ||
// const a = interval(1000, true); | ||
// const b = a.map( (_, i) => values[i] ) | ||
// .takeWhile( (_, i) => i < values.length ) | ||
// .skipUnchanged(); | ||
// b.listen( true, console.log ); | ||
// break; | ||
// } | ||
case 'pairwise' : { | ||
const a = interval(1000, true); | ||
const b = a.pairwise(); | ||
b.subscribe( console.log ); | ||
break; | ||
} | ||
// case 'skipAlreadyAppeared' : { | ||
// const values = [3, 1, 4, 1, 5, 9, 2, 6, 5, 3, 5, 8, 9]; | ||
// const a = interval(1000, true); | ||
// const b = a.map( (_, i) => values[i] ) | ||
// .takeWhile( (_, i) => i < values.length ) | ||
// .skipAlreadyAppeared(); | ||
// b.subscribe( console.log ); | ||
// break; | ||
// } | ||
case 'mapTo' : { | ||
const a = interval(1000, true); | ||
const b = a.mapTo(0); | ||
b.subscribe( console.log ); | ||
break; | ||
} | ||
// case 'skip' : { | ||
// const a = interval(1000, true); | ||
// const b = a.skip( 999, 3 ); | ||
// console.log(b.value); | ||
// b.subscribe( console.log ); | ||
// break; | ||
// } | ||
case 'merge': { | ||
const a = interval(1000, false); | ||
const b = interval(1000, false); | ||
const c = b.map( e => 100 * e ); | ||
const d = merge( a, c ); | ||
a.start(); | ||
setTimeout(() => { b.start(); }, 500); | ||
d.subscribe( e => console.log( e ) ); | ||
break; | ||
} | ||
// case 'pluck' : { | ||
// const a = interval(1000, true); | ||
// const b = a.pairwise() | ||
// .map( e => ({ prev: e[0], curr: e[1] }) ) | ||
// .pluck('curr'); | ||
// b.subscribe( console.log ); | ||
// break; | ||
// } | ||
case 'fib': { | ||
const seq = interval(1000, true); | ||
const fib = seq.scan<number>( 1, (prev, curr) => curr + prev ); | ||
fib.subscribe( e => console.log( e ) ); | ||
break; | ||
} | ||
// case 'pairwise' : { | ||
// const a = interval(1000, true); | ||
// const b = a.pairwise(); | ||
// b.subscribe( console.log ); | ||
// break; | ||
// } | ||
case 'scan': { | ||
const a = interval(1000, true); | ||
const b = a.pipe( scan( [] as number[], (prev, curr) => { | ||
prev.push( curr ); | ||
return prev; | ||
}) ); | ||
a.subscribe( e => console.log('a', e ) ); | ||
b.subscribe( e => console.log('b', e ) ); | ||
break; | ||
} | ||
// case 'mapTo' : { | ||
// const a = interval(1000, true); | ||
// const b = a.mapTo(0); | ||
// b.subscribe( console.log ); | ||
// break; | ||
// } | ||
case 'debounce3': { | ||
const a = interval(100, true); | ||
const b = a.pipe( filter( 0, e => e % 10 < 5 ) ); | ||
const c = b.pipe( debounce(300) ); | ||
b.subscribe( e => console.log('b', e ) ); | ||
c.subscribe( e => console.log('c', e ) ); | ||
break; | ||
} | ||
// case 'merge': { | ||
// const a = interval(1000, false); | ||
// const b = interval(1000, false); | ||
// const c = b.map( e => 100 * e ); | ||
// const d = merge( a, c ); | ||
// a.start(); | ||
// setTimeout(() => { b.start(); }, 500); | ||
// d.subscribe( e => console.log( e ) ); | ||
// break; | ||
// } | ||
case 'debounce2': { | ||
const a = manual(0); | ||
const b = a.pipe( debounce(275) ); | ||
setTimeout(() => { a.emit( 50); }, 50); | ||
setTimeout(() => { a.emit(750); }, 750); | ||
setTimeout(() => { a.emit(950); }, 950); | ||
a.subscribe( e => console.log('a', e ) ); | ||
b.subscribe( e => console.log('b', e ) ); | ||
break; | ||
} | ||
// case 'fib': { | ||
// const seq = interval(1000, true); | ||
// const fib = seq.scan<number>( 1, (prev, curr) => curr + prev ); | ||
// fib.subscribe( e => console.log( e ) ); | ||
// break; | ||
// } | ||
case 'debounce': { | ||
const a = manual(0); | ||
const b = a.pipe( debounce(1000) ); | ||
const c = a.debounce(1000); | ||
a.subscribe( e => console.log('a', e ) ); | ||
b.subscribe( e => console.log('b', e ) ); | ||
c.subscribe( e => console.log('c', e ) ); | ||
a.emit(1); | ||
break; | ||
} | ||
// case 'scan': { | ||
// const a = interval(1000, true); | ||
// const b = a.pipe( scan( [] as number[], (prev, curr) => { | ||
// prev.push( curr ); | ||
// return prev; | ||
// }) ); | ||
// a.subscribe( e => console.log('a', e ) ); | ||
// b.subscribe( e => console.log('b', e ) ); | ||
// break; | ||
// } | ||
case 'combine': { | ||
const a = interval(1000, true); | ||
const b = a.map( e => 100 * e ); | ||
const b2 = a.pipe( map( e => 100 * e ) ); | ||
const c1 = combine( a, b ).map( ([x, y]) => x + y ); | ||
const c2 = combine( a, b2 ).pipe( map( ([x, y]) => x + y ) ); | ||
c1.subscribe( e => console.log('c1', e ), undefined, e => console.log('end', e) ); | ||
c2.subscribe( e => console.log('c2', e ) ); | ||
setTimeout(() => { a.stop() }, 5000 ); | ||
break; | ||
} | ||
// case 'debounce3': { | ||
// const a = interval(100, true); | ||
// const b = a.pipe( filter( 0, e => e % 10 < 5 ) ); | ||
// const c = b.pipe( debounce(300) ); | ||
// b.subscribe( e => console.log('b', e ) ); | ||
// c.subscribe( e => console.log('c', e ) ); | ||
// break; | ||
// } | ||
case 'interval': { | ||
const a = interval(1000, true); | ||
a.subscribe( console.log, undefined, e => console.log('end', e) ); | ||
setTimeout(() => { a.stop() }, 5000 ); | ||
break; | ||
} | ||
// case 'debounce2': { | ||
// const a = manual(0); | ||
// const b = a.pipe( debounce(275) ); | ||
// setTimeout(() => { a.emit( 50); }, 50); | ||
// setTimeout(() => { a.emit(750); }, 750); | ||
// setTimeout(() => { a.emit(950); }, 950); | ||
// a.subscribe( e => console.log('a', e ) ); | ||
// b.subscribe( e => console.log('b', e ) ); | ||
// break; | ||
// } | ||
case 'manual': { | ||
const rn = manual(0); | ||
rn.subscribe( console.log ); | ||
rn.emit(1); | ||
break; | ||
} | ||
} | ||
// case 'debounce': { | ||
// const a = manual(0); | ||
// const b = a.pipe( debounce(1000) ); | ||
// const c = a.debounce(1000); | ||
// a.subscribe( e => console.log('a', e ) ); | ||
// b.subscribe( e => console.log('b', e ) ); | ||
// c.subscribe( e => console.log('c', e ) ); | ||
// a.emit(1); | ||
// break; | ||
// } | ||
// case 'combine': { | ||
// const a = interval(1000, true); | ||
// const b = a.map( e => 100 * e ); | ||
// const b2 = a.pipe( map( e => 100 * e ) ); | ||
// const c1 = combine( a, b ).map( ([x, y]) => x + y ); | ||
// const c2 = combine( a, b2 ).pipe( map( ([x, y]) => x + y ) ); | ||
// c1.subscribe( e => console.log('c1', e ), undefined, e => console.log('end', e) ); | ||
// c2.subscribe( e => console.log('c2', e ) ); | ||
// setTimeout(() => { a.stop() }, 5000 ); | ||
// break; | ||
// } | ||
// case 'interval': { | ||
// const a = interval(1000, true); | ||
// a.subscribe( console.log, undefined, e => console.log('end', e) ); | ||
// setTimeout(() => { a.stop() }, 5000 ); | ||
// break; | ||
// } | ||
// case 'manual': { | ||
// const rn = manual(0); | ||
// rn.subscribe( console.log ); | ||
// rn.emit(1); | ||
// break; | ||
// } | ||
// } |
import { RN } from '../mod'; | ||
export declare const filter: <T>(initialValue: T, predicate: (e: T) => boolean) => (src: RN<T>) => RN<T>; | ||
export declare const filter: <T>(initialValue: T, predicate: (srcValue: T, srcIndex: number, index: number) => boolean) => (src: RN<T>) => RN<T>; |
@@ -11,7 +11,8 @@ "use strict"; | ||
fire() { | ||
const nextVal = this.parents[0].value; | ||
if (this.predicate(nextVal)) { | ||
this.fireWith(nextVal); | ||
const src = this.parents[0]; | ||
// note: 'this.index' is not updated yet (will be updated in this.fireWith()) | ||
if (this.predicate(src.value, src.index, this.index + 1)) { | ||
this.fireWith(src.value); | ||
} | ||
} | ||
} |
import { RN } from '../mod'; | ||
export declare const flatMap: <T, U>(fn: (e: T) => RN<U>) => (src: RN<T>) => RN<U>; | ||
export declare const flatMap: <T, U>(fn: (srcValue: T, srcIndex: number, index: number) => RN<U>) => (src: RN<T>) => RN<U>; |
@@ -7,4 +7,4 @@ "use strict"; | ||
constructor(src, fn) { | ||
super(fn(src.value).value, [src]); | ||
this.latestRN = fn(src.value); | ||
super(fn(src.value, src.index, -1).value, [src]); | ||
this.latestRN = fn(src.value, src.index, -1); | ||
this.fn = fn; | ||
@@ -15,3 +15,5 @@ this.subscriptions = []; | ||
fire() { | ||
this.latestRN = this.fn(this.parents[0].value); | ||
const src = this.parents[0]; | ||
// note: 'this.index' is not updated yet (will be updated in this.fireWith()) | ||
this.latestRN = this.fn(src.value, src.index, this.index + 1); | ||
this.subscriptions.push(this.latestRN.subscribe(e => this.fireWith(e))); | ||
@@ -18,0 +20,0 @@ } |
import { RN } from '../mod'; | ||
export declare const map: <T, U>(fn: (value: T, index: number) => U) => (src: RN<T>) => RN<U>; | ||
export declare const map: <T, U>(fn: (srcValue: T, srcIndex: number, index: number) => U) => (src: RN<T>) => RN<U>; | ||
export declare const mapTo: <T, U>(value: U) => (src: RN<T>) => RN<U>; | ||
export declare const pluck: <T, K extends keyof T>(member: K) => (src: RN<T>) => RN<T[K]>; | ||
export declare const withTimestamp: <T>() => (src: RN<T>) => RN<[T, number]>; |
@@ -10,9 +10,10 @@ "use strict"; | ||
constructor(src, fn) { | ||
super(fn(src.value, src.index), [src]); | ||
super(fn(src.value, src.index, -1), [src]); | ||
this.fn = fn; | ||
} | ||
fire() { | ||
const next = this.parents[0]; | ||
this.fireWith(this.fn(next.value, next.index)); | ||
const src = this.parents[0]; | ||
// note: 'this.index' is not updated yet (will be updated in this.fireWith()) | ||
this.fireWith(this.fn(src.value, src.index, this.index + 1)); | ||
} | ||
} |
import { RN } from '../mod'; | ||
export declare const scan: <T, U>(initialValue: U, fn: (prev: U, curr: T, index?: number | undefined) => U) => (src: RN<T>) => RN<U>; | ||
export declare const scan: <T, U>(initialValue: U, fn: (state: U, srcValue: T, srcIndex: number, index: number) => U) => (src: RN<T>) => RN<U>; |
@@ -12,4 +12,6 @@ "use strict"; | ||
fire() { | ||
this.fireWith(this.fn(this.scanState, this.parents[0].value, this.index)); | ||
const src = this.parents[0]; | ||
// note: 'this.index' is not updated yet (will be updated in this.fireWith()) | ||
this.fireWith(this.fn(this.scanState, src.value, src.index, this.index + 1)); | ||
} | ||
} |
import { RN } from '../mod'; | ||
export declare const skip: <T>(initialValue: T, skipNum: number) => (src: RN<T>) => RN<T>; | ||
export declare const skipWhile: <T>(initialValue: T, predicate: (value: T, index: number) => boolean) => (src: RN<T>) => RN<T>; | ||
export declare const skipWhile: <T>(initialValue: T, predicate: (srcValue: T, srcIndex: number, index: number) => boolean) => (src: RN<T>) => RN<T>; |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
/* removed in prod */ const mod_1 = require("../mod"); | ||
exports.skip = (initialValue, skipNum) => ((src) => new SkipWhileRN(src, initialValue, (_, index) => index < skipNum)); | ||
exports.skip = (initialValue, skipNum) => ((src) => new SkipWhileRN(src, initialValue, (_srcValue, srcIndex, _index) => srcIndex < skipNum)); | ||
exports.skipWhile = (initialValue, predicate) => ((src) => new SkipWhileRN(src, initialValue, predicate)); | ||
@@ -12,8 +12,8 @@ class SkipWhileRN extends mod_1.RN { | ||
fire() { | ||
const nextValue = this.parents[0].value; | ||
const nextIndex = this.parents[0].index; | ||
if (!this.predicate(nextValue, nextIndex)) { | ||
this.fireWith(nextValue); | ||
const src = this.parents[0]; | ||
// note: 'this.index' is not updated yet (will be updated in this.fireWith()) | ||
if (!this.predicate(src.value, src.index, this.index + 1)) { | ||
this.fireWith(src.value); | ||
} | ||
} | ||
} |
@@ -9,2 +9,3 @@ "use strict"; | ||
this.appeared = new Set(); | ||
this.appeared.add(src.value); | ||
this.key = key; | ||
@@ -11,0 +12,0 @@ } |
@@ -12,8 +12,8 @@ "use strict"; | ||
fire() { | ||
const nextVal = this.parents[0].value; | ||
const currVal = this.value; | ||
if (!this.eq(nextVal, currVal)) { | ||
this.fireWith(nextVal); | ||
const currVal = this.parents[0].value; | ||
const prevVal = this.value; | ||
if (!this.eq(currVal, prevVal)) { | ||
this.fireWith(currVal); | ||
} | ||
} | ||
} |
import { RN } from '../mod'; | ||
export declare const switchMap: <T, U>(fn: (e: T) => RN<U>) => (src: RN<T>) => RN<U>; | ||
export declare const switchMap: <T, U>(fn: (srcValue: T, srcIndex: number, index: number) => RN<U>) => (src: RN<T>) => RN<U>; |
@@ -7,4 +7,4 @@ "use strict"; | ||
constructor(src, fn) { | ||
super(fn(src.value).value, [src]); | ||
this.latestRN = fn(src.value); | ||
super(fn(src.value, src.index, -1).value, [src]); | ||
this.latestRN = fn(src.value, src.index, -1); | ||
this.fn = fn; | ||
@@ -15,3 +15,5 @@ this.subscription = this.latestRN.subscribe(e => this.fireWith(e)); | ||
this.subscription.unsubscribe(); | ||
this.latestRN = this.fn(this.parents[0].value); | ||
const src = this.parents[0]; | ||
// note: 'this.index' is not updated yet (will be updated in this.fireWith()) | ||
this.latestRN = this.fn(src.value, src.index, this.index + 1); | ||
this.subscription = this.latestRN.subscribe(e => this.fireWith(e)); | ||
@@ -18,0 +20,0 @@ } |
import { RN } from '../mod'; | ||
export declare const take: <T>(takeNum: number) => (src: RN<T>) => RN<T>; | ||
export declare const takeWhile: <T>(predicate: (value: T, index: number) => boolean) => (src: RN<T>) => RN<T>; | ||
export declare const takeWhile: <T>(predicate: (srcValue: T, srcIndex: number, index: number) => boolean) => (src: RN<T>) => RN<T>; |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
/* removed in prod */ const mod_1 = require("../mod"); | ||
exports.take = (takeNum) => ((src) => new TakeWhileRN(src, (_, index) => index < takeNum)); | ||
exports.take = (takeNum) => ((src) => new TakeWhileRN(src, (_srcValue, srcIndex, _index) => srcIndex < takeNum)); | ||
exports.takeWhile = (predicate) => ((src) => new TakeWhileRN(src, predicate)); | ||
@@ -12,5 +12,6 @@ class TakeWhileRN extends mod_1.RN { | ||
fire() { | ||
const next = this.parents[0]; | ||
if (this.predicate(next.value, next.index)) { | ||
this.fireWith(next.value); | ||
const src = this.parents[0]; | ||
// note: 'this.index' is not updated yet (will be updated in this.fireWith()) | ||
if (this.predicate(src.value, src.index, this.index + 1)) { | ||
this.fireWith(src.value); | ||
} | ||
@@ -17,0 +18,0 @@ else { |
@@ -47,17 +47,17 @@ import { Subscriber } from './types/Subscriber'; | ||
filter(initialValue: T, predicate: (e: T) => boolean): RN<T>; | ||
flatMap<U>(fn: (e: T) => RN<U>): RN<U>; | ||
map<U>(fn: (value: T, index: number) => U): RN<U>; | ||
flatMap<U>(fn: (srcValue: T, srcIndex: number, index: number) => RN<U>): RN<U>; | ||
map<U>(fn: (srcValue: T, srcIndex: number, index: number) => U): RN<U>; | ||
mapTo<U>(value: U): RN<U>; | ||
pairwise(initialPrevValue?: T): RN<[T, T]>; | ||
pluck<K extends keyof T>(member: K): RN<T[K]>; | ||
scan<U>(initialValue: U, fn: (prev: U, curr: T, index?: number) => U): RN<U>; | ||
scan<U>(initialValue: U, fn: (state: U, srcValue: T, srcIndex: number, index: number) => U): RN<U>; | ||
skip(initialValue: T, skipNum: number): RN<T>; | ||
skipAlreadyAppeared<K extends keyof T>(key?: K): RN<T>; | ||
skipUnchanged(eq?: (a: T, b: T) => boolean): RN<T>; | ||
skipWhile(initialValue: T, predicate: (value: T, index: number) => boolean): RN<T>; | ||
switchMap<U>(fn: (e: T) => RN<U>): RN<U>; | ||
skipWhile(initialValue: T, predicate: (srcValue: T, srcIndex: number, index: number) => boolean): RN<T>; | ||
switchMap<U>(fn: (srcValue: T, srcIndex: number, index: number) => RN<U>): RN<U>; | ||
take(takeNum: number): RN<T>; | ||
takeWhile(predicate: (value: T, index: number) => boolean): RN<T>; | ||
takeWhile(predicate: (srcValue: T, srcIndex: number, index: number) => boolean): RN<T>; | ||
withLatest<U>(src: RN<U>): RN<[T, U]>; | ||
withTimestamp(): RN<[T, number]>; | ||
} |
@@ -94,5 +94,4 @@ "use strict"; | ||
// accessors | ||
/* @deprecated This is an internal implementation detail, do not use. */ | ||
get value() { return this.valueInternal; } | ||
get index() { return (this.indexInternal < 0 ? 0 : this.indexInternal); } | ||
get index() { return this.indexInternal; } | ||
get isCompleted() { | ||
@@ -99,0 +98,0 @@ return this.state === 'end-successfully' || this.state === 'end-with-error'; |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
const RN_1 = require("./RN"); | ||
const operators_1 = require("./operators"); | ||
const rxjs_1 = require("rxjs"); | ||
// fv1.addChild | ||
// fv1.curr | ||
// fv1.latestFire | ||
// fv1.priority | ||
// fv1.id | ||
// const array: number[] = []; | ||
// [ 2, 14, 15, 25, 21, 40, 31, 45, 26, 22, 70, 45, 85, 59, 47, 56, 75, 53, 70, 33 ] | ||
// .forEach( e => addAsHeap( array, e ) ); | ||
// removeAsHeap( array ); | ||
// console.log( array ); | ||
const testid = 'toObservable2'; | ||
console.log('testid = ', testid); | ||
switch (testid) { | ||
case 'toObservable2': { | ||
const rn = RN_1.interval(1000, true).take(5); | ||
const obs = RN_1.toObservable(rn, false); | ||
obs.subscribe(v => console.log('next', v), () => console.log('error'), () => console.log('complete')); | ||
break; | ||
} | ||
case 'toObservable': { | ||
const rn = RN_1.interval(1000, true).take(5); | ||
const obs = RN_1.toObservable(rn, true); | ||
obs.subscribe(v => console.log('next', v), () => console.log('error'), () => console.log('complete')); | ||
break; | ||
} | ||
case 'fromObservable': { | ||
const a = RN_1.fromObservable(0, rxjs_1.interval(1000)); | ||
a.listen(false, console.log); | ||
break; | ||
} | ||
case 'complete-check4': { | ||
const _ = RN_1.interval(1000, true); | ||
const a = _.map(e => 100 * e); | ||
const b = RN_1.interval(500, true); | ||
const c = RN_1.combine(a, b); | ||
a.subscribe(console.log, () => { }, e => console.log('a complete', e)); | ||
b.subscribe(console.log, () => { }, e => console.log('b complete', e)); | ||
c.subscribe(console.log, () => { }, e => console.log('c complete', e)); | ||
setTimeout(() => { _.stop(); }, 3000); | ||
setTimeout(() => { b.stop(); }, 5000); | ||
break; | ||
} | ||
case 'complete-check3': { | ||
const a = RN_1.interval(1000, true).map(e => 100 * e); | ||
const b = RN_1.interval(500, true); | ||
const c = RN_1.combine(a, b); | ||
a.subscribe(console.log, () => { }, e => console.log('a complete', e)); | ||
b.subscribe(console.log, () => { }, e => console.log('b complete', e)); | ||
c.subscribe(console.log, () => { }, e => console.log('c complete', e)); | ||
setTimeout(() => { b.stop(); }, 3000); | ||
break; | ||
} | ||
case 'complete-check2': { | ||
const a = RN_1.interval(1000, true).map(e => 100 * e); | ||
const b = RN_1.interval(500, true); | ||
const c = RN_1.combine(a, b); | ||
const sbs1 = c.subscribe(console.log); | ||
const sbs2 = a.subscribe(console.log); | ||
setTimeout(() => { sbs1.unsubscribe(); }, 5000); | ||
setTimeout(() => { sbs2.unsubscribe(); }, 3000); | ||
break; | ||
} | ||
case 'complete-check1': { | ||
const a = RN_1.interval(1000, true); | ||
const b = a.mapTo(1); | ||
const sbs = b.subscribe(console.log); | ||
setTimeout(() => { sbs.unsubscribe(); }, 3000); | ||
break; | ||
} | ||
case 'flatMap': { | ||
const a = RN_1.interval(2000, true); | ||
const b = a.flatMap(e => RN_1.interval(700, true).map(x => [e, e * (x + 1)]).take(4)); | ||
b.subscribe(console.log); | ||
break; | ||
} | ||
case 'switchMap': { | ||
const a = RN_1.interval(1000, true); | ||
const b = a.switchMap(e => RN_1.interval(300, true).map(x => e * x)); | ||
b.subscribe(console.log); | ||
break; | ||
} | ||
case 'withTimestamp': { | ||
const values = [3, 1, 4, 1, 5, 9, 2, 6, 5, 3, 5, 8, 9]; | ||
const a = RN_1.interval(1000, true); | ||
const b = RN_1.interval(300, true); | ||
const c = a.map((_, i) => values[i]) | ||
.takeWhile((_, i) => i < values.length) | ||
.withTimestamp(); | ||
c.subscribe(console.log); | ||
break; | ||
} | ||
case 'withLatest': { | ||
const values = [3, 1, 4, 1, 5, 9, 2, 6, 5, 3, 5, 8, 9]; | ||
const a = RN_1.interval(1000, true); | ||
const b = RN_1.interval(300, true); | ||
const c = a.map((_, i) => values[i]) | ||
.takeWhile((_, i) => i < values.length) | ||
.withLatest(b); | ||
c.subscribe(console.log); | ||
break; | ||
} | ||
case 'take': { | ||
const values = [3, 1, 4, 1, 5, 9, 2, 6, 5, 3, 5, 8, 9]; | ||
const a = RN_1.interval(1000, true); | ||
const b = a.map((_, i) => values[i]) | ||
.take(5); | ||
b.subscribe(console.log); | ||
break; | ||
} | ||
case 'skipWhile': { | ||
const values = [3, 1, 4, 1, 5, 9, 2, 6, 5, 3, 5, 8, 9]; | ||
const a = RN_1.interval(1000, true); | ||
const b = a.map((_, i) => values[i]) | ||
.skipWhile(0, (_, i) => i < 3); | ||
b.subscribe(console.log); | ||
break; | ||
} | ||
case 'skipUnchanged': { | ||
const values = [7, 7, 7, 9, 3, 3, 2, 3, 8, 8, 4, 6, 2, 6, 4]; | ||
const a = RN_1.interval(1000, true); | ||
const b = a.map((_, i) => values[i]) | ||
.takeWhile((_, i) => i < values.length) | ||
.skipUnchanged(); | ||
b.subscribe(console.log); | ||
break; | ||
} | ||
case 'skipAlreadyAppeared': { | ||
const values = [3, 1, 4, 1, 5, 9, 2, 6, 5, 3, 5, 8, 9]; | ||
const a = RN_1.interval(1000, true); | ||
const b = a.map((_, i) => values[i]) | ||
.takeWhile((_, i) => i < values.length) | ||
.skipAlreadyAppeared(); | ||
b.subscribe(console.log); | ||
break; | ||
} | ||
case 'skip': { | ||
const a = RN_1.interval(1000, true); | ||
const b = a.skip(999, 3); | ||
console.log(b.value); | ||
b.subscribe(console.log); | ||
break; | ||
} | ||
case 'pluck': { | ||
const a = RN_1.interval(1000, true); | ||
const b = a.pairwise() | ||
.map(e => ({ prev: e[0], curr: e[1] })) | ||
.pluck('curr'); | ||
b.subscribe(console.log); | ||
break; | ||
} | ||
case 'pairwise': { | ||
const a = RN_1.interval(1000, true); | ||
const b = a.pairwise(); | ||
b.subscribe(console.log); | ||
break; | ||
} | ||
case 'mapTo': { | ||
const a = RN_1.interval(1000, true); | ||
const b = a.mapTo(0); | ||
b.subscribe(console.log); | ||
break; | ||
} | ||
case 'merge': { | ||
const a = RN_1.interval(1000, false); | ||
const b = RN_1.interval(1000, false); | ||
const c = b.map(e => 100 * e); | ||
const d = RN_1.merge(a, c); | ||
a.start(); | ||
setTimeout(() => { b.start(); }, 500); | ||
d.subscribe(e => console.log(e)); | ||
break; | ||
} | ||
case 'fib': { | ||
const seq = RN_1.interval(1000, true); | ||
const fib = seq.scan(1, (prev, curr) => curr + prev); | ||
fib.subscribe(e => console.log(e)); | ||
break; | ||
} | ||
case 'scan': { | ||
const a = RN_1.interval(1000, true); | ||
const b = a.pipe(operators_1.scan([], (prev, curr) => { | ||
prev.push(curr); | ||
return prev; | ||
})); | ||
a.subscribe(e => console.log('a', e)); | ||
b.subscribe(e => console.log('b', e)); | ||
break; | ||
} | ||
case 'debounce3': { | ||
const a = RN_1.interval(100, true); | ||
const b = a.pipe(operators_1.filter(0, e => e % 10 < 5)); | ||
const c = b.pipe(operators_1.debounce(300)); | ||
b.subscribe(e => console.log('b', e)); | ||
c.subscribe(e => console.log('c', e)); | ||
break; | ||
} | ||
case 'debounce2': { | ||
const a = RN_1.manual(0); | ||
const b = a.pipe(operators_1.debounce(275)); | ||
setTimeout(() => { a.emit(50); }, 50); | ||
setTimeout(() => { a.emit(750); }, 750); | ||
setTimeout(() => { a.emit(950); }, 950); | ||
a.subscribe(e => console.log('a', e)); | ||
b.subscribe(e => console.log('b', e)); | ||
break; | ||
} | ||
case 'debounce': { | ||
const a = RN_1.manual(0); | ||
const b = a.pipe(operators_1.debounce(1000)); | ||
const c = a.debounce(1000); | ||
a.subscribe(e => console.log('a', e)); | ||
b.subscribe(e => console.log('b', e)); | ||
c.subscribe(e => console.log('c', e)); | ||
a.emit(1); | ||
break; | ||
} | ||
case 'combine': { | ||
const a = RN_1.interval(1000, true); | ||
const b = a.map(e => 100 * e); | ||
const b2 = a.pipe(operators_1.map(e => 100 * e)); | ||
const c1 = RN_1.combine(a, b).map(([x, y]) => x + y); | ||
const c2 = RN_1.combine(a, b2).pipe(operators_1.map(([x, y]) => x + y)); | ||
c1.subscribe(e => console.log('c1', e), undefined, e => console.log('end', e)); | ||
c2.subscribe(e => console.log('c2', e)); | ||
setTimeout(() => { a.stop(); }, 5000); | ||
break; | ||
} | ||
case 'interval': { | ||
const a = RN_1.interval(1000, true); | ||
a.subscribe(console.log, undefined, e => console.log('end', e)); | ||
setTimeout(() => { a.stop(); }, 5000); | ||
break; | ||
} | ||
case 'manual': { | ||
const rn = RN_1.manual(0); | ||
rn.subscribe(console.log); | ||
rn.emit(1); | ||
break; | ||
} | ||
} | ||
const a = RN_1.interval(100, true); | ||
// delay | ||
setTimeout(() => { | ||
const b = a.map((sv, si, i) => [sv, si, i]); | ||
b.listen(true, console.log); | ||
}, 550); | ||
// import { interval as rxjsInterval } from 'rxjs'; | ||
// // fv1.addChild | ||
// // fv1.curr | ||
// // fv1.latestFire | ||
// // fv1.priority | ||
// // fv1.id | ||
// // const array: number[] = []; | ||
// // [ 2, 14, 15, 25, 21, 40, 31, 45, 26, 22, 70, 45, 85, 59, 47, 56, 75, 53, 70, 33 ] | ||
// // .forEach( e => addAsHeap( array, e ) ); | ||
// // removeAsHeap( array ); | ||
// // console.log( array ); | ||
// const testid: string = 'toObservable2'; | ||
// console.log('testid = ', testid); | ||
// switch ( testid ) { | ||
// case 'toObservable2' : { | ||
// const rn = interval( 1000, true ).take(5); | ||
// const obs = toObservable( rn, false ); | ||
// obs.subscribe( | ||
// v => console.log('next', v), | ||
// () => console.log('error'), | ||
// () => console.log('complete') ); | ||
// break; | ||
// } | ||
// case 'toObservable' : { | ||
// const rn = interval( 1000, true ).take(5); | ||
// const obs = toObservable( rn, true ); | ||
// obs.subscribe( | ||
// v => console.log('next', v), | ||
// () => console.log('error'), | ||
// () => console.log('complete') ); | ||
// break; | ||
// } | ||
// case 'fromObservable' : { | ||
// const a = fromObservable( 0, rxjsInterval( 1000 ) ); | ||
// a.listen( false, console.log ); | ||
// break; | ||
// } | ||
// case 'complete-check4' : { | ||
// const _ = interval(1000, true) | ||
// const a = _.map( e => 100 * e ); | ||
// const b = interval(500, true); | ||
// const c = combine( a, b ); | ||
// a.subscribe(console.log, () => {}, e => console.log('a complete', e)); | ||
// b.subscribe(console.log, () => {}, e => console.log('b complete', e)); | ||
// c.subscribe(console.log, () => {}, e => console.log('c complete', e)); | ||
// setTimeout(() => { _.stop() }, 3000); | ||
// setTimeout(() => { b.stop() }, 5000); | ||
// break; | ||
// } | ||
// case 'complete-check3' : { | ||
// const a = interval(1000, true).map( e => 100 * e ); | ||
// const b = interval(500, true); | ||
// const c = combine( a, b ); | ||
// a.subscribe(console.log, () => {}, e => console.log('a complete', e)); | ||
// b.subscribe(console.log, () => {}, e => console.log('b complete', e)); | ||
// c.subscribe(console.log, () => {}, e => console.log('c complete', e)); | ||
// setTimeout(() => { b.stop() }, 3000); | ||
// break; | ||
// } | ||
// case 'complete-check2' : { | ||
// const a = interval(1000, true).map( e => 100 * e ); | ||
// const b = interval(500, true); | ||
// const c = combine( a, b ); | ||
// const sbs1 = c.subscribe(console.log); | ||
// const sbs2 = a.subscribe(console.log); | ||
// setTimeout(() => { sbs1.unsubscribe() }, 5000); | ||
// setTimeout(() => { sbs2.unsubscribe() }, 3000); | ||
// break; | ||
// } | ||
// case 'complete-check1' : { | ||
// const a = interval(1000, true); | ||
// const b = a.mapTo(1); | ||
// const sbs = b.subscribe(console.log); | ||
// setTimeout(() => { sbs.unsubscribe() }, 3000); | ||
// break; | ||
// } | ||
// case 'flatMap' : { | ||
// const a = interval(2000, true); | ||
// const b = a.flatMap( e => interval(700, true).map( x => [e, e * (x + 1)] ).take(4) ); | ||
// b.subscribe( console.log ); | ||
// break; | ||
// } | ||
// case 'switchMap' : { | ||
// const a = interval(1000, true); | ||
// const b = a.switchMap( e => interval(300, true).map( x => e * x ) ); | ||
// b.subscribe( console.log ); | ||
// break; | ||
// } | ||
// case 'withTimestamp' : { | ||
// const values = [3, 1, 4, 1, 5, 9, 2, 6, 5, 3, 5, 8, 9]; | ||
// const a = interval(1000, true); | ||
// const b = interval(300, true); | ||
// const c = a.map( (_, i) => values[i] ) | ||
// .takeWhile( (_, i) => i < values.length ) | ||
// .withTimestamp(); | ||
// c.subscribe( console.log ); | ||
// break; | ||
// } | ||
// case 'withLatest' : { | ||
// const values = [3, 1, 4, 1, 5, 9, 2, 6, 5, 3, 5, 8, 9]; | ||
// const a = interval(1000, true); | ||
// const b = interval(300, true); | ||
// const c = a.map( (_, i) => values[i] ) | ||
// .takeWhile( (_, i) => i < values.length ) | ||
// .withLatest( b ); | ||
// c.subscribe( console.log ); | ||
// break; | ||
// } | ||
// case 'take' : { | ||
// const values = [3, 1, 4, 1, 5, 9, 2, 6, 5, 3, 5, 8, 9]; | ||
// const a = interval(1000, true); | ||
// const b = a.map( (_, i) => values[i] ) | ||
// .take(5); | ||
// b.subscribe( console.log ); | ||
// break; | ||
// } | ||
// case 'skipWhile' : { | ||
// const values = [3, 1, 4, 1, 5, 9, 2, 6, 5, 3, 5, 8, 9]; | ||
// const a = interval(1000, true); | ||
// const b = a.map( (_, i) => values[i] ) | ||
// .skipWhile( 0, (_, i) => i < 3 ) | ||
// b.subscribe( console.log ); | ||
// break; | ||
// } | ||
// case 'skipUnchanged' : { | ||
// const values = [3, 3, 3, 1, 4, 1, 5, 9, 9, 9, 2, 6]; | ||
// const a = interval(1000, true); | ||
// const b = a.map( (_, i) => values[i] ) | ||
// .takeWhile( (_, i) => i < values.length ) | ||
// .skipUnchanged(); | ||
// b.listen( true, console.log ); | ||
// break; | ||
// } | ||
// case 'skipAlreadyAppeared' : { | ||
// const values = [3, 1, 4, 1, 5, 9, 2, 6, 5, 3, 5, 8, 9]; | ||
// const a = interval(1000, true); | ||
// const b = a.map( (_, i) => values[i] ) | ||
// .takeWhile( (_, i) => i < values.length ) | ||
// .skipAlreadyAppeared(); | ||
// b.subscribe( console.log ); | ||
// break; | ||
// } | ||
// case 'skip' : { | ||
// const a = interval(1000, true); | ||
// const b = a.skip( 999, 3 ); | ||
// console.log(b.value); | ||
// b.subscribe( console.log ); | ||
// break; | ||
// } | ||
// case 'pluck' : { | ||
// const a = interval(1000, true); | ||
// const b = a.pairwise() | ||
// .map( e => ({ prev: e[0], curr: e[1] }) ) | ||
// .pluck('curr'); | ||
// b.subscribe( console.log ); | ||
// break; | ||
// } | ||
// case 'pairwise' : { | ||
// const a = interval(1000, true); | ||
// const b = a.pairwise(); | ||
// b.subscribe( console.log ); | ||
// break; | ||
// } | ||
// case 'mapTo' : { | ||
// const a = interval(1000, true); | ||
// const b = a.mapTo(0); | ||
// b.subscribe( console.log ); | ||
// break; | ||
// } | ||
// case 'merge': { | ||
// const a = interval(1000, false); | ||
// const b = interval(1000, false); | ||
// const c = b.map( e => 100 * e ); | ||
// const d = merge( a, c ); | ||
// a.start(); | ||
// setTimeout(() => { b.start(); }, 500); | ||
// d.subscribe( e => console.log( e ) ); | ||
// break; | ||
// } | ||
// case 'fib': { | ||
// const seq = interval(1000, true); | ||
// const fib = seq.scan<number>( 1, (prev, curr) => curr + prev ); | ||
// fib.subscribe( e => console.log( e ) ); | ||
// break; | ||
// } | ||
// case 'scan': { | ||
// const a = interval(1000, true); | ||
// const b = a.pipe( scan( [] as number[], (prev, curr) => { | ||
// prev.push( curr ); | ||
// return prev; | ||
// }) ); | ||
// a.subscribe( e => console.log('a', e ) ); | ||
// b.subscribe( e => console.log('b', e ) ); | ||
// break; | ||
// } | ||
// case 'debounce3': { | ||
// const a = interval(100, true); | ||
// const b = a.pipe( filter( 0, e => e % 10 < 5 ) ); | ||
// const c = b.pipe( debounce(300) ); | ||
// b.subscribe( e => console.log('b', e ) ); | ||
// c.subscribe( e => console.log('c', e ) ); | ||
// break; | ||
// } | ||
// case 'debounce2': { | ||
// const a = manual(0); | ||
// const b = a.pipe( debounce(275) ); | ||
// setTimeout(() => { a.emit( 50); }, 50); | ||
// setTimeout(() => { a.emit(750); }, 750); | ||
// setTimeout(() => { a.emit(950); }, 950); | ||
// a.subscribe( e => console.log('a', e ) ); | ||
// b.subscribe( e => console.log('b', e ) ); | ||
// break; | ||
// } | ||
// case 'debounce': { | ||
// const a = manual(0); | ||
// const b = a.pipe( debounce(1000) ); | ||
// const c = a.debounce(1000); | ||
// a.subscribe( e => console.log('a', e ) ); | ||
// b.subscribe( e => console.log('b', e ) ); | ||
// c.subscribe( e => console.log('c', e ) ); | ||
// a.emit(1); | ||
// break; | ||
// } | ||
// case 'combine': { | ||
// const a = interval(1000, true); | ||
// const b = a.map( e => 100 * e ); | ||
// const b2 = a.pipe( map( e => 100 * e ) ); | ||
// const c1 = combine( a, b ).map( ([x, y]) => x + y ); | ||
// const c2 = combine( a, b2 ).pipe( map( ([x, y]) => x + y ) ); | ||
// c1.subscribe( e => console.log('c1', e ), undefined, e => console.log('end', e) ); | ||
// c2.subscribe( e => console.log('c2', e ) ); | ||
// setTimeout(() => { a.stop() }, 5000 ); | ||
// break; | ||
// } | ||
// case 'interval': { | ||
// const a = interval(1000, true); | ||
// a.subscribe( console.log, undefined, e => console.log('end', e) ); | ||
// setTimeout(() => { a.stop() }, 5000 ); | ||
// break; | ||
// } | ||
// case 'manual': { | ||
// const rn = manual(0); | ||
// rn.subscribe( console.log ); | ||
// rn.emit(1); | ||
// break; | ||
// } | ||
// } |
@@ -47,15 +47,15 @@ import { Subscriber } from './types/Subscriber'; | ||
filter(initialValue: T, predicate: (e: T) => boolean): RN<T>; | ||
flatMap<U>(fn: (e: T) => RN<U>): RN<U>; | ||
map<U>(fn: (value: T, index: number) => U): RN<U>; | ||
flatMap<U>(fn: (srcValue: T, srcIndex: number, index: number) => RN<U>): RN<U>; | ||
map<U>(fn: (srcValue: T, srcIndex: number, index: number) => U): RN<U>; | ||
mapTo<U>(value: U): RN<U>; | ||
pairwise(initialPrevValue?: T): RN<[T, T]>; | ||
pluck<K extends keyof T>(member: K): RN<T[K]>; | ||
scan<U>(initialValue: U, fn: (prev: U, curr: T, index?: number) => U): RN<U>; | ||
scan<U>(initialValue: U, fn: (state: U, srcValue: T, srcIndex: number, index: number) => U): RN<U>; | ||
skip(initialValue: T, skipNum: number): RN<T>; | ||
skipAlreadyAppeared<K extends keyof T>(key?: K): RN<T>; | ||
skipUnchanged(eq?: (a: T, b: T) => boolean): RN<T>; | ||
skipWhile(initialValue: T, predicate: (value: T, index: number) => boolean): RN<T>; | ||
switchMap<U>(fn: (e: T) => RN<U>): RN<U>; | ||
skipWhile(initialValue: T, predicate: (srcValue: T, srcIndex: number, index: number) => boolean): RN<T>; | ||
switchMap<U>(fn: (srcValue: T, srcIndex: number, index: number) => RN<U>): RN<U>; | ||
take(takeNum: number): RN<T>; | ||
takeWhile(predicate: (value: T, index: number) => boolean): RN<T>; | ||
takeWhile(predicate: (srcValue: T, srcIndex: number, index: number) => boolean): RN<T>; | ||
withLatest<U>(src: RN<U>): RN<[T, U]>; | ||
@@ -65,5 +65,5 @@ withTimestamp(): RN<[T, number]>; | ||
export declare const debounce: <T>(time: number) => (src: RN<T>) => RN<T>; | ||
export declare const filter: <T>(initialValue: T, predicate: (e: T) => boolean) => (src: RN<T>) => RN<T>; | ||
export declare const flatMap: <T, U>(fn: (e: T) => RN<U>) => (src: RN<T>) => RN<U>; | ||
export declare const map: <T, U>(fn: (value: T, index: number) => U) => (src: RN<T>) => RN<U>; | ||
export declare const filter: <T>(initialValue: T, predicate: (srcValue: T, srcIndex: number, index: number) => boolean) => (src: RN<T>) => RN<T>; | ||
export declare const flatMap: <T, U>(fn: (srcValue: T, srcIndex: number, index: number) => RN<U>) => (src: RN<T>) => RN<U>; | ||
export declare const map: <T, U>(fn: (srcValue: T, srcIndex: number, index: number) => U) => (src: RN<T>) => RN<U>; | ||
export declare const mapTo: <T, U>(value: U) => (src: RN<T>) => RN<U>; | ||
@@ -73,10 +73,10 @@ export declare const pluck: <T, K extends keyof T>(member: K) => (src: RN<T>) => RN<T[K]>; | ||
export declare const pairwise: <T>(initialPrevValue?: T | undefined) => (src: RN<T>) => RN<[T, T]>; | ||
export declare const scan: <T, U>(initialValue: U, fn: (prev: U, curr: T, index?: number | undefined) => U) => (src: RN<T>) => RN<U>; | ||
export declare const scan: <T, U>(initialValue: U, fn: (state: U, srcValue: T, srcIndex: number, index: number) => U) => (src: RN<T>) => RN<U>; | ||
export declare const skip: <T>(initialValue: T, skipNum: number) => (src: RN<T>) => RN<T>; | ||
export declare const skipWhile: <T>(initialValue: T, predicate: (value: T, index: number) => boolean) => (src: RN<T>) => RN<T>; | ||
export declare const skipWhile: <T>(initialValue: T, predicate: (srcValue: T, srcIndex: number, index: number) => boolean) => (src: RN<T>) => RN<T>; | ||
export declare const skipAlreadyAppeared: <T, K extends keyof T>(key?: K | undefined) => (src: RN<T>) => RN<T>; | ||
export declare const skipUnchanged: <T>(eq?: (a: T, b: T) => boolean) => (src: RN<T>) => RN<T>; | ||
export declare const switchMap: <T, U>(fn: (e: T) => RN<U>) => (src: RN<T>) => RN<U>; | ||
export declare const switchMap: <T, U>(fn: (srcValue: T, srcIndex: number, index: number) => RN<U>) => (src: RN<T>) => RN<U>; | ||
export declare const take: <T>(takeNum: number) => (src: RN<T>) => RN<T>; | ||
export declare const takeWhile: <T>(predicate: (value: T, index: number) => boolean) => (src: RN<T>) => RN<T>; | ||
export declare const takeWhile: <T>(predicate: (srcValue: T, srcIndex: number, index: number) => boolean) => (src: RN<T>) => RN<T>; | ||
export declare const withLatest: <T, U>(src2: RN<U>) => (src: RN<T>) => RN<[T, U]>; |
@@ -93,5 +93,4 @@ "use strict"; | ||
// accessors | ||
/* @deprecated This is an internal implementation detail, do not use. */ | ||
get value() { return this.valueInternal; } | ||
get index() { return (this.indexInternal < 0 ? 0 : this.indexInternal); } | ||
get index() { return this.indexInternal; } | ||
get isCompleted() { | ||
@@ -296,5 +295,6 @@ return this.state === 'end-successfully' || this.state === 'end-with-error'; | ||
fire() { | ||
const nextVal = this.parents[0].value; | ||
if (this.predicate(nextVal)) { | ||
this.fireWith(nextVal); | ||
const src = this.parents[0]; | ||
// note: 'this.index' is not updated yet (will be updated in this.fireWith()) | ||
if (this.predicate(src.value, src.index, this.index + 1)) { | ||
this.fireWith(src.value); | ||
} | ||
@@ -306,4 +306,4 @@ } | ||
constructor(src, fn) { | ||
super(fn(src.value).value, [src]); | ||
this.latestRN = fn(src.value); | ||
super(fn(src.value, src.index, -1).value, [src]); | ||
this.latestRN = fn(src.value, src.index, -1); | ||
this.fn = fn; | ||
@@ -314,3 +314,5 @@ this.subscriptions = []; | ||
fire() { | ||
this.latestRN = this.fn(this.parents[0].value); | ||
const src = this.parents[0]; | ||
// note: 'this.index' is not updated yet (will be updated in this.fireWith()) | ||
this.latestRN = this.fn(src.value, src.index, this.index + 1); | ||
this.subscriptions.push(this.latestRN.subscribe(e => this.fireWith(e))); | ||
@@ -329,8 +331,9 @@ } | ||
constructor(src, fn) { | ||
super(fn(src.value, src.index), [src]); | ||
super(fn(src.value, src.index, -1), [src]); | ||
this.fn = fn; | ||
} | ||
fire() { | ||
const next = this.parents[0]; | ||
this.fireWith(this.fn(next.value, next.index)); | ||
const src = this.parents[0]; | ||
// note: 'this.index' is not updated yet (will be updated in this.fireWith()) | ||
this.fireWith(this.fn(src.value, src.index, this.index + 1)); | ||
} | ||
@@ -358,6 +361,8 @@ } | ||
fire() { | ||
this.fireWith(this.fn(this.scanState, this.parents[0].value, this.index)); | ||
const src = this.parents[0]; | ||
// note: 'this.index' is not updated yet (will be updated in this.fireWith()) | ||
this.fireWith(this.fn(this.scanState, src.value, src.index, this.index + 1)); | ||
} | ||
} | ||
exports.skip = (initialValue, skipNum) => ((src) => new SkipWhileRN(src, initialValue, (_, index) => index < skipNum)); | ||
exports.skip = (initialValue, skipNum) => ((src) => new SkipWhileRN(src, initialValue, (_srcValue, srcIndex, _index) => srcIndex < skipNum)); | ||
exports.skipWhile = (initialValue, predicate) => ((src) => new SkipWhileRN(src, initialValue, predicate)); | ||
@@ -370,6 +375,6 @@ class SkipWhileRN extends RN { | ||
fire() { | ||
const nextValue = this.parents[0].value; | ||
const nextIndex = this.parents[0].index; | ||
if (!this.predicate(nextValue, nextIndex)) { | ||
this.fireWith(nextValue); | ||
const src = this.parents[0]; | ||
// note: 'this.index' is not updated yet (will be updated in this.fireWith()) | ||
if (!this.predicate(src.value, src.index, this.index + 1)) { | ||
this.fireWith(src.value); | ||
} | ||
@@ -383,2 +388,3 @@ } | ||
this.appeared = new Set(); | ||
this.appeared.add(src.value); | ||
this.key = key; | ||
@@ -407,6 +413,6 @@ } | ||
fire() { | ||
const nextVal = this.parents[0].value; | ||
const currVal = this.value; | ||
if (!this.eq(nextVal, currVal)) { | ||
this.fireWith(nextVal); | ||
const currVal = this.parents[0].value; | ||
const prevVal = this.value; | ||
if (!this.eq(currVal, prevVal)) { | ||
this.fireWith(currVal); | ||
} | ||
@@ -418,4 +424,4 @@ } | ||
constructor(src, fn) { | ||
super(fn(src.value).value, [src]); | ||
this.latestRN = fn(src.value); | ||
super(fn(src.value, src.index, -1).value, [src]); | ||
this.latestRN = fn(src.value, src.index, -1); | ||
this.fn = fn; | ||
@@ -426,3 +432,5 @@ this.subscription = this.latestRN.subscribe(e => this.fireWith(e)); | ||
this.subscription.unsubscribe(); | ||
this.latestRN = this.fn(this.parents[0].value); | ||
const src = this.parents[0]; | ||
// note: 'this.index' is not updated yet (will be updated in this.fireWith()) | ||
this.latestRN = this.fn(src.value, src.index, this.index + 1); | ||
this.subscription = this.latestRN.subscribe(e => this.fireWith(e)); | ||
@@ -435,3 +443,3 @@ } | ||
} | ||
exports.take = (takeNum) => ((src) => new TakeWhileRN(src, (_, index) => index < takeNum)); | ||
exports.take = (takeNum) => ((src) => new TakeWhileRN(src, (_srcValue, srcIndex, _index) => srcIndex < takeNum)); | ||
exports.takeWhile = (predicate) => ((src) => new TakeWhileRN(src, predicate)); | ||
@@ -444,5 +452,6 @@ class TakeWhileRN extends RN { | ||
fire() { | ||
const next = this.parents[0]; | ||
if (this.predicate(next.value, next.index)) { | ||
this.fireWith(next.value); | ||
const src = this.parents[0]; | ||
// note: 'this.index' is not updated yet (will be updated in this.fireWith()) | ||
if (this.predicate(src.value, src.index, this.index + 1)) { | ||
this.fireWith(src.value); | ||
} | ||
@@ -449,0 +458,0 @@ else { |
{ | ||
"name": "rnjs", | ||
"version": "1.0.5", | ||
"version": "1.0.6", | ||
"description": "Reactive Programming Library for TypeScript/JavaScript", | ||
@@ -5,0 +5,0 @@ "main": "dist/index.js", |
532
README.md
@@ -37,2 +37,42 @@ # RN | ||
* glitch-less | ||
* in RxJS | ||
```ts | ||
const a = interval(1000); | ||
const b = a.pipe( map( x => 100 * x ) ); | ||
const c = combineLatest( a, b ).pipe( map( ([x, y]) => x + y ) ); | ||
c.subscribe( console.log ); | ||
// 0 | ||
// 1 <-- glitch | ||
// 101 | ||
// 102 <-- glitch | ||
// 202 | ||
// 203 <-- glitch | ||
// 303 | ||
// 304 <-- glitch | ||
// 404 | ||
// 405 <-- glitch | ||
// 505 | ||
// ... | ||
``` | ||
* in RN | ||
```ts | ||
// RN | ||
const a = interval(1000, true); | ||
const b = a.map( x => 100 * x ); | ||
const c = combine( a, b ).map( ([x, y]) => x + y ); | ||
c.listen( true, console.log ); | ||
// 0 | ||
// 101 | ||
// 202 | ||
// 303 | ||
// 404 | ||
// 505 | ||
// ... | ||
``` | ||
* about "glitch" | ||
@@ -43,4 +83,6 @@ * [RX GLITCHES AREN'T ACTUALLY A PROBLEM](https://staltz.com/rx-glitches-arent-actually-a-problem.html) | ||
* initial value | ||
* every RN has initial value | ||
* every RN has an initial value | ||
* you can get the current value by `value` property | ||
@@ -70,6 +112,6 @@ (like `BehaviorSubject` in RxJS) | ||
* compatiblity with RxJS | ||
* RN has interconversion methods (toObservable, fromObservable) | ||
* RN has the same API of subscribe as RxJS | ||
-> RN can be passed to Angular async pipe | ||
without conversion to RxJS Observable | ||
* interconversion methods (toObservable, fromObservable) | ||
* same API of subscribe as RxJS | ||
* RN can be passed to Angular async pipe | ||
without conversion to RxJS Observable | ||
@@ -123,4 +165,484 @@ ## Coresspondence tables to RxJS | ||
## examples | ||
* interval, combine, map | ||
```ts | ||
import { interval, combine, map } from 'rnjs'; | ||
const a = interval(100, true); | ||
// ^ | ||
// +--- true --> start immediately | ||
const b = a.pipe( map( x => 100 * x ) ); | ||
const c = combine( a, b ).map( ([x, y]) => x + y ); | ||
c.subscribe( console.log ); | ||
// c.listen( true, console.log ); | ||
/* output | ||
0 | ||
101 | ||
202 | ||
303 | ||
404 | ||
505 | ||
... | ||
*/ | ||
``` | ||
```ts | ||
import { interval, combine } from 'rnjs'; | ||
const a = interval(100, false); | ||
// ^ | ||
// +--- false --> start manually | ||
const b = a.map( x => 100 * x ); | ||
// ^ | ||
// +--- all operators can be used without pipe method | ||
const c = combine( a, b ).map( ([x, y]) => x + y ); | ||
c.listen( true, console.log ); | ||
// ^ | ||
// +--- true --> get the first value | ||
a.start(); // start manually | ||
/* output | ||
0 <-- first value | ||
0 | ||
101 | ||
202 | ||
303 | ||
404 | ||
505 | ||
... | ||
*/ | ||
``` | ||
* map (with index) | ||
* index is initialized by | ||
* map function receives | ||
* source RN value | ||
* source RN index (optional) | ||
* this.index (optional) | ||
* in the next example, `a` starts before `b` is generated | ||
--> a.index is not equal to b.index. | ||
```ts | ||
const a = interval(100, true); | ||
const b = a.map( (sv, si, i) => [100 * sv, si, i] ); | ||
b.listen( true, console.log ); | ||
/* | ||
[ 0, 0, -1 ] | ||
[ 100, 1, 0 ] | ||
[ 200, 2, 1 ] | ||
[ 300, 3, 2 ] | ||
[ 400, 4, 3 ] | ||
[ 500, 5, 4 ] | ||
[ 600, 6, 5 ] | ||
[ 700, 7, 6 ] | ||
[ 800, 8, 7 ] | ||
[ 900, 9, 8 ] | ||
*/ | ||
``` | ||
* debounce, filter | ||
```ts | ||
import { interval } from 'rnjs'; | ||
const a = interval(100, true); | ||
const b = a.filter( 0, e => e % 10 < 5 ); | ||
// ^ | ||
// +--- filter requires initial value | ||
const c = b.debounce(300); | ||
b.listen( false, console.log ); | ||
c.listen( false, e => console.log( e, 'debounce') ); | ||
/* output | ||
1 | ||
2 | ||
3 | ||
4 | ||
4 'debounce' | ||
10 | ||
11 | ||
12 | ||
13 | ||
14 | ||
14 'debounce' | ||
... | ||
*/ | ||
/* | ||
interval : 0 |0--1--2--3--4--5--6--7--8--9--10-11-12-13-14-15-16-17-18-19- | ||
filter : 0 |0--1--2--3--4-----------------10-11-12-13-14---------------- | ||
debounce : 0 |---------------------4--------|--------------------14------- | ||
*/ | ||
``` | ||
* scan | ||
```ts | ||
import { interval } from 'rnjs'; | ||
const a = interval(100, true); | ||
const b = a.scan( [] as number[], (prev, curr) => { | ||
prev.push( curr ); | ||
return prev; | ||
}); | ||
a.listen( true, e => console.log('a', e ) ); | ||
b.listen( true, e => console.log('b', e ) ); | ||
/* output | ||
a 0 | ||
b [] | ||
a 1 | ||
b [ 1 ] | ||
a 2 | ||
b [ 1, 2 ] | ||
a 3 | ||
b [ 1, 2, 3 ] | ||
*/ | ||
``` | ||
* merge | ||
```ts | ||
import { interval, merge } from 'rnjs'; | ||
const a = interval(1000, false); | ||
const b = interval(1000, false); | ||
const c = b.map( e => 100 * e ); | ||
const d = merge( a, c ); | ||
a.start(); | ||
setTimeout(() => { b.start(); }, 500); | ||
d.listen( true, console.log ); | ||
/* output | ||
0 | ||
0 | ||
1 | ||
100 | ||
2 | ||
200 | ||
3 | ||
300 | ||
4 | ||
400 | ||
5 | ||
500 | ||
*/ | ||
/* | ||
a : 0 |0-------1-------2-------3-------4-------5-------6------- | ||
b : 0 |0------100-----200-----300-----400-----500-----600----- | ||
c : 0 |0---0---1--100--2--200--3--300--4--400--5--500--6--600-- | ||
*/ | ||
``` | ||
* pairwise | ||
```ts | ||
import { interval } from 'rnjs'; | ||
interval(100, true).pairwise() | ||
.listen( true, console.log ); | ||
/* output | ||
[ 0, 0 ] | ||
[ 0, 1 ] | ||
[ 1, 2 ] | ||
[ 2, 3 ] | ||
[ 3, 4 ] | ||
[ 4, 5 ] | ||
[ 5, 6 ] | ||
[ 6, 7 ] | ||
[ 7, 8 ] | ||
... | ||
*/ | ||
/* | ||
interval : 0 |0------1------2------3------4------5------6------ | ||
pairwise : [0,0] |[0,0]--[0,1]--[1,2]--[2,3]--[3,4]--[4,5]--[5,6]-- | ||
*/ | ||
``` | ||
* pluck | ||
```ts | ||
import { interval } from 'rnjs'; | ||
const a = interval(100, true); | ||
const b = a.pairwise() | ||
.map( e => ({ prev: e[0], curr: e[1] }) ) | ||
.pluck('curr'); | ||
b.listen( true, console.log ); | ||
/* output | ||
0 | ||
1 | ||
2 | ||
3 | ||
... | ||
*/ | ||
``` | ||
* take | ||
```ts | ||
const a = interval(100, true); | ||
const b = a.take(5); | ||
b.listen( true, console.log ); | ||
/* | ||
0 | ||
1 | ||
2 | ||
3 | ||
4 | ||
*/ | ||
``` | ||
* skip | ||
```ts | ||
const a = interval(100, true); | ||
const b = a.skip( 999, 3 ); | ||
// ^ | ||
// +--- skip requires initial value | ||
b.listen( true, console.log ); | ||
/* | ||
999 | ||
3 | ||
4 | ||
5 | ||
6 | ||
7 | ||
8 | ||
9 | ||
*/ | ||
/* | ||
interval : 0 |0---1---2---3---4---5---6---7---8---9---10--11--12--... | ||
skip : 999 |----------3---1---4---1---5---9---2---6---u---u---... | ||
*/ | ||
``` | ||
* takeWhile, skipAlreadyAppeared | ||
```ts | ||
import { interval } from 'rnjs'; | ||
const values = [3, 1, 4, 1, 5, 9, 2, 6, 5, 3, 5, 8, 9]; | ||
const a = interval(100, false); | ||
const b = a.map( (_sv, _si, i) => values[i] ) | ||
.takeWhile( (_sv, _si, i) => 0 <= i && i < values.length ) | ||
.skipAlreadyAppeared(); | ||
b.listen( false, console.log ); | ||
a.start(); | ||
/* output | ||
3 | ||
1 | ||
4 | ||
5 | ||
9 | ||
2 | ||
6 | ||
8 | ||
*/ | ||
/* | ||
interval : 0 |0---1---2---3---4---5---6---7---8---9---10--11--12--13--14--... | ||
map : 3 |3---1---4---1---5---9---2---6---5---3---5---8---9---u---u--- | ||
takeWhile : 3 |3---1---4---1---5---9---2---6---5---3---5---8---9--- | ||
skipAlreadyAppeared : 3 |3---1---4-------5---9---2---6---------------8------- | ||
*/ | ||
``` | ||
```ts | ||
const values = [3, 1, 4, 1, 5, 9, 2, 6, 5, 3, 5, 8, 9]; | ||
const a = interval(100, true); | ||
const b = a.map( (_sv, _si, i) => values[i] ) | ||
.takeWhile( (_sv, _si, i) => 0 <= i && i < values.length ) | ||
.skipAlreadyAppeared(); | ||
b.listen( false, console.log ); | ||
/* output | ||
3 | ||
1 | ||
4 | ||
5 | ||
9 | ||
2 | ||
6 | ||
8 | ||
*/ | ||
/* | ||
interval : 0 |0---1---2---3---4---5---6---7---8---9---10--11--12--13--14--... | ||
map : ud |3---1---4---1---5---9---2---6---5---3---5---8---9---u---u--- | ||
takeWhile : ud |3---1---4---1---5---9---2---6---5---3---5---8---9--- | ||
skipAlreadyAppeared : ud |3---1---4-------5---9---2---6---------------8------- | ||
*/ | ||
``` | ||
* skipUnchanged | ||
```ts | ||
const values = [3, 3, 3, 1, 4, 1, 5, 9, 9, 9, 2, 6]; | ||
const a = interval(100, true); | ||
const b = a.map( (_sv, _si, i) => values[i] ) | ||
.takeWhile( (_sv, _si, i) => 0 <= i && i < values.length ) | ||
.skipUnchanged(); | ||
b.listen( false, console.log ); | ||
/* | ||
3 | ||
1 | ||
4 | ||
1 | ||
5 | ||
9 | ||
2 | ||
6 | ||
*/ | ||
/* | ||
interval : 0 |0---1---2---3---4---5---6---7---8---9---10--11--12--... | ||
map : ud |--3---3---3---1---4---1---5---9---2---6---u---u---... | ||
takeWhile : ud |--3---3---3---1---4---1---5---9---2---6--- | ||
skipUnchanged : ud |--3-----------1---4---1---5---9---2---6--- | ||
*/ | ||
``` | ||
* withLatest | ||
```ts | ||
const a = interval(300, true); | ||
const b = interval(100, true); | ||
const c = a.withLatest( b ); | ||
c.listen( true, console.log ); | ||
/* | ||
[ 0, 0 ] | ||
[ 1, 2 ] | ||
[ 2, 5 ] | ||
[ 3, 8 ] | ||
[ 4, 11 ] | ||
[ 5, 14 ] | ||
[ 6, 17 ] | ||
[ 7, 20 ] | ||
[ 8, 23 ] | ||
[ 9, 26 ] | ||
[ 10, 29 ] | ||
*/ | ||
/* | ||
a : 0 |0-----------1-----------2-----------3-----------4----... | ||
b : 0 |0---1---2---3---4---5---6---7---8---9---10--11--12--... | ||
c : [0,0] |----------[1,2]-------[2,5]-------[3,8]-------[4,11]--... | ||
*/ | ||
``` | ||
* withTimestamp | ||
```ts | ||
const a = interval(300, true); | ||
const b = a.withTimestamp(); | ||
b.listen( true, console.log ); | ||
/* | ||
[ 0, 1544903873595 ] | ||
[ 1, 1544903873896 ] | ||
[ 2, 1544903874195 ] | ||
[ 3, 1544903874497 ] | ||
[ 4, 1544903874797 ] | ||
*/ | ||
``` | ||
* switchMap | ||
```ts | ||
const a = interval(1000, true); | ||
const b = a.switchMap( x => interval(300, true).mapTo(x) ); | ||
b.listen( true, console.log ); | ||
/* | ||
0 | ||
0 | ||
0 | ||
0 | ||
1 | ||
1 | ||
1 | ||
1 | ||
2 | ||
2 | ||
*/ | ||
/* | ||
interval : 0 |0---------1---------2---------... | ||
(mapped-1) : 0 |0--0--0--0--0--0--0--0--0--0--... | ||
(mapped-2) : 0 |1--1--1--1--1--1--1-... | ||
(mapped-3) : 0 |2--2--2--2... | ||
... | ||
--- switchMap & mapTo --- | ||
[0,0] |0--0--0--01--1--1--12--2--2--2... | ||
*/ | ||
``` | ||
* flatMap | ||
```ts | ||
const a = interval(1000, true); | ||
const b = a.flatMap( x => interval(300, true).mapTo(x) ); | ||
b.listen( true, console.log ); | ||
/* | ||
0 | ||
0 | ||
0 | ||
0 | ||
1 | ||
0 | ||
1 | ||
0 | ||
1 | ||
0 | ||
1 | ||
2 | ||
0 | ||
1 | ||
2 | ||
0 | ||
1 | ||
2 | ||
0 | ||
1 | ||
*/ | ||
/* | ||
interval : 0 |0---------1---------2---------... | ||
(mapped-1) : 0 |0--0--0--0--0--0--0--0--0--0--... | ||
(mapped-2) : 0 |1--1--1--1--1--1--1-... | ||
(mapped-3) : 0 |2--2--2--2... | ||
... | ||
--- flatMap & mapTo --- | ||
[0,0] |0--0--0--01-01-01-012012012012012012012012012 | ||
*/ | ||
``` |
@@ -140,5 +140,4 @@ import { RNId } from './types/RNId'; | ||
/* @deprecated This is an internal implementation detail, do not use. */ | ||
get value() { return this.valueInternal; } | ||
get index() { return ( this.indexInternal < 0 ? 0 : this.indexInternal ); } | ||
get index() { return this.indexInternal; } | ||
@@ -389,11 +388,18 @@ | ||
filter( initialValue: T, predicate: (e: T) => boolean ): RN<T> { | ||
filter( | ||
initialValue: T, | ||
predicate: (e: T) => boolean | ||
): RN<T> { | ||
return filter<T>( initialValue, predicate )( this ); | ||
} | ||
flatMap<U>( fn: (e: T) => RN<U> ): RN<U> { | ||
flatMap<U>( | ||
fn: (srcValue: T, srcIndex: number, index: number) => RN<U> | ||
): RN<U> { | ||
return flatMap<T, U>( fn )( this ); | ||
} | ||
map<U>( fn: (value: T, index: number) => U ): RN<U> { | ||
map<U>( | ||
fn: (srcValue: T, srcIndex: number, index: number) => U | ||
): RN<U> { | ||
return map<T, U>( fn )( this ); | ||
@@ -416,3 +422,3 @@ } | ||
initialValue: U, | ||
fn: (prev: U, curr: T, index?: number) => U | ||
fn: (state: U, srcValue: T, srcIndex: number, index: number) => U | ||
): RN<U> { | ||
@@ -436,3 +442,3 @@ return scan<T, U>( initialValue, fn )( this ); | ||
initialValue: T, | ||
predicate: (value: T, index: number) => boolean, | ||
predicate: (srcValue: T, srcIndex: number, index: number) => boolean, | ||
): RN<T> { | ||
@@ -442,3 +448,5 @@ return skipWhile<T>( initialValue, predicate )( this ); | ||
switchMap<U>( fn: (e: T) => RN<U> ): RN<U> { | ||
switchMap<U>( | ||
fn: (srcValue: T, srcIndex: number, index: number) => RN<U> | ||
): RN<U> { | ||
return switchMap<T, U>( fn )( this ); | ||
@@ -451,3 +459,5 @@ } | ||
takeWhile( predicate: (value: T, index: number) => boolean ): RN<T> { | ||
takeWhile( | ||
predicate: (srcValue: T, srcIndex: number, index: number) => boolean | ||
): RN<T> { | ||
return takeWhile<T>( predicate )( this ); | ||
@@ -502,3 +512,3 @@ } | ||
initialValue: T, | ||
predicate: (e: T) => boolean, | ||
predicate: (srcValue: T, srcIndex: number, index: number) => boolean, | ||
): Operator<T, T> => | ||
@@ -509,3 +519,3 @@ (( src: RN<T> ) => new FilterRN<T>( src, initialValue, predicate )); | ||
class FilterRN<T> extends RN<T> { | ||
private predicate: (e: T) => boolean; | ||
private predicate: (srcValue: T, srcIndex: number, index: number) => boolean; | ||
@@ -515,3 +525,3 @@ constructor( | ||
initialValue: T, | ||
predicate: (e: T) => boolean, | ||
predicate: (srcValue: T, srcIndex: number, index: number) => boolean, | ||
) { | ||
@@ -523,5 +533,6 @@ super( initialValue, [src] ); | ||
protected fire() { | ||
const nextVal = this.parents[0].value; | ||
if ( this.predicate( nextVal ) ) { | ||
this.fireWith( nextVal ); | ||
const src = this.parents[0]; | ||
// note: 'this.index' is not updated yet (will be updated in this.fireWith()) | ||
if ( this.predicate( src.value, src.index, this.index + 1 ) ) { | ||
this.fireWith( src.value ); | ||
} | ||
@@ -533,3 +544,5 @@ } | ||
export const flatMap = <T, U>( fn: (e: T) => RN<U> ): Operator<T, U> => | ||
export const flatMap = <T, U>( | ||
fn: (srcValue: T, srcIndex: number, index: number) => RN<U> | ||
): Operator<T, U> => | ||
(( src: RN<T> ) => new FlatMapRN<T, U>( src, fn )); | ||
@@ -541,7 +554,10 @@ | ||
private subscriptions: Subscription[]; | ||
private fn: (e: T) => RN<U>; | ||
private fn: (srcValue: T, srcIndex: number, index: number) => RN<U>; | ||
constructor( src: RN<T>, fn: (e: T) => RN<U> ) { | ||
super( fn( src.value ).value, [src] ); | ||
this.latestRN = fn( src.value ); | ||
constructor( | ||
src: RN<T>, | ||
fn: (srcValue: T, srcIndex: number, index: number) => RN<U> | ||
) { | ||
super( fn( src.value, src.index, -1 ).value, [src] ); | ||
this.latestRN = fn( src.value, src.index, -1 ); | ||
this.fn = fn; | ||
@@ -553,3 +569,5 @@ this.subscriptions = []; | ||
protected fire() { // switch latestRN here | ||
this.latestRN = this.fn( this.parents[0].value ); | ||
const src = this.parents[0]; | ||
// note: 'this.index' is not updated yet (will be updated in this.fireWith()) | ||
this.latestRN = this.fn( src.value, src.index, this.index + 1 ); | ||
this.subscriptions.push( this.latestRN.subscribe( e => this.fireWith( e ) ) ); | ||
@@ -566,19 +584,22 @@ } | ||
export const map = <T, U>( fn: (value: T, index: number) => U ): Operator<T, U> => | ||
(( src: RN<T> ) => new MapRN<T, U>( src, fn )); | ||
export const map = <T, U>( fn: (srcValue: T, srcIndex: number, index: number) => U ): Operator<T, U> => | ||
(( src: RN<T> ) => new MapRN<T, U>( src, fn )); | ||
export const mapTo = <T, U>( value: U ): Operator<T, U> => | ||
(( src: RN<T> ) => new MapRN<T, U>( src, () => value )); | ||
(( src: RN<T> ) => new MapRN<T, U>( src, () => value )); | ||
export const pluck = <T, K extends keyof T>( member: K ): Operator<T, T[K]> => | ||
(( src: RN<T> ) => new MapRN<T, T[K]>( src, value => value[ member ] )); | ||
(( src: RN<T> ) => new MapRN<T, T[K]>( src, value => value[ member ] )); | ||
export const withTimestamp = <T>(): Operator<T, [T, number]> => | ||
(( src: RN<T> ) => new MapRN<T, [T, number]>( src, value => [value, Date.now()] )); | ||
(( src: RN<T> ) => new MapRN<T, [T, number]>( src, value => [value, Date.now()] )); | ||
class MapRN<T, U> extends RN<U> { | ||
private fn: (value: T, index: number) => U; | ||
private fn: (srcValue: T, srcIndex: number, index: number) => U; | ||
constructor( src: RN<T>, fn: (value: T, index: number) => U ) { | ||
super( fn( src.value, src.index ), [src] ); | ||
constructor( | ||
src: RN<T>, | ||
fn: (srcValue: T, srcIndex: number, index: number) => U | ||
) { | ||
super( fn( src.value, src.index, -1 ), [src] ); | ||
this.fn = fn; | ||
@@ -588,4 +609,5 @@ } | ||
protected fire() { | ||
const next = this.parents[0]; | ||
this.fireWith( this.fn( next.value, next.index ) ); | ||
const src = this.parents[0]; | ||
// note: 'this.index' is not updated yet (will be updated in this.fireWith()) | ||
this.fireWith( this.fn( src.value, src.index, this.index + 1 ) ); | ||
} | ||
@@ -619,3 +641,3 @@ } | ||
initialValue: U, | ||
fn: (prev: U, curr: T, index?: number) => U | ||
fn: (state: U, srcValue: T, srcIndex: number, index: number) => U | ||
): Operator<T, U> => | ||
@@ -627,3 +649,3 @@ (( src: RN<T> ) => new ScanRN<T, U>( initialValue, src, fn )); | ||
private scanState: U; | ||
private fn: (prev: U, curr: T, index?: number) => U; | ||
private fn: (state: U, srcValue: T, srcIndex: number, index: number) => U; | ||
@@ -633,3 +655,3 @@ constructor( | ||
src: RN<T>, | ||
fn: (prev: U, curr: T, index?: number) => U | ||
fn: (state: U, srcValue: T, srcIndex: number, index: number) => U | ||
) { | ||
@@ -642,3 +664,5 @@ super( initialValue, [src] ); | ||
protected fire() { | ||
this.fireWith( this.fn( this.scanState, this.parents[0].value, this.index ) ); | ||
const src = this.parents[0]; | ||
// note: 'this.index' is not updated yet (will be updated in this.fireWith()) | ||
this.fireWith( this.fn( this.scanState, src.value, src.index, this.index + 1 ) ); | ||
} | ||
@@ -656,3 +680,3 @@ } | ||
initialValue, | ||
(_, index) => index < skipNum, | ||
(_srcValue, srcIndex, _index) => srcIndex < skipNum, | ||
)); | ||
@@ -662,3 +686,3 @@ | ||
initialValue: T, | ||
predicate: (value: T, index: number) => boolean | ||
predicate: (srcValue: T, srcIndex: number, index: number) => boolean | ||
): Operator<T, T> => (( src: RN<T> ) => | ||
@@ -669,3 +693,3 @@ new SkipWhileRN<T>( src, initialValue, predicate )); | ||
class SkipWhileRN<T> extends RN<T> { | ||
private predicate: (value: T, index: number) => boolean; | ||
private predicate: (srcValue: T, srcIndex: number, index: number) => boolean; | ||
@@ -675,3 +699,3 @@ constructor( | ||
initialValue: T, | ||
predicate: (value: T, index: number) => boolean | ||
predicate: (srcValue: T, srcIndex: number, index: number) => boolean | ||
) { | ||
@@ -683,6 +707,6 @@ super( initialValue, [src] ); | ||
protected fire() { | ||
const nextValue = this.parents[0].value; | ||
const nextIndex = this.parents[0].index; | ||
if ( !this.predicate( nextValue, nextIndex ) ) { | ||
this.fireWith( nextValue ); | ||
const src = this.parents[0]; | ||
// note: 'this.index' is not updated yet (will be updated in this.fireWith()) | ||
if ( !this.predicate( src.value, src.index, this.index + 1 ) ) { | ||
this.fireWith( src.value ); | ||
} | ||
@@ -705,2 +729,3 @@ } | ||
this.appeared = new Set<T|T[K]>(); | ||
this.appeared.add( src.value ); | ||
this.key = key; | ||
@@ -740,7 +765,7 @@ } | ||
protected fire() { | ||
const nextVal = this.parents[0].value; | ||
const currVal = this.value; | ||
const currVal = this.parents[0].value; | ||
const prevVal = this.value; | ||
if ( !this.eq( nextVal, currVal ) ) { | ||
this.fireWith( nextVal ); | ||
if ( !this.eq( currVal, prevVal ) ) { | ||
this.fireWith( currVal ); | ||
} | ||
@@ -752,3 +777,5 @@ } | ||
export const switchMap = <T, U>( fn: (e: T) => RN<U> ): Operator<T, U> => | ||
export const switchMap = <T, U>( | ||
fn: (srcValue: T, srcIndex: number, index: number) => RN<U> | ||
): Operator<T, U> => | ||
(( src: RN<T> ) => new SwitchMapRN<T, U>( src, fn )); | ||
@@ -760,7 +787,10 @@ | ||
private subscription: Subscription; | ||
private fn: (e: T) => RN<U>; | ||
private fn: (srcValue: T, srcIndex: number, index: number) => RN<U>; | ||
constructor( src: RN<T>, fn: (e: T) => RN<U> ) { | ||
super( fn( src.value ).value, [src] ); | ||
this.latestRN = fn( src.value ); | ||
constructor( | ||
src: RN<T>, | ||
fn: (srcValue: T, srcIndex: number, index: number) => RN<U> | ||
) { | ||
super( fn( src.value, src.index, -1 ).value, [src] ); | ||
this.latestRN = fn( src.value, src.index, -1 ); | ||
this.fn = fn; | ||
@@ -772,3 +802,5 @@ this.subscription = this.latestRN.subscribe( e => this.fireWith( e ) ); | ||
this.subscription.unsubscribe(); | ||
this.latestRN = this.fn( this.parents[0].value ); | ||
const src = this.parents[0]; | ||
// note: 'this.index' is not updated yet (will be updated in this.fireWith()) | ||
this.latestRN = this.fn( src.value, src.index, this.index + 1 ); | ||
this.subscription = this.latestRN.subscribe( e => this.fireWith( e ) ); | ||
@@ -786,6 +818,7 @@ } | ||
export const take = <T>( takeNum: number ): Operator<T, T> => | ||
(( src: RN<T> ) => new TakeWhileRN<T>( src, (_, index) => index < takeNum )); | ||
(( src: RN<T> ) => new TakeWhileRN<T>( | ||
src, (_srcValue, srcIndex, _index) => srcIndex < takeNum )); | ||
export const takeWhile = <T>( | ||
predicate: (value: T, index: number) => boolean | ||
predicate: (srcValue: T, srcIndex: number, index: number) => boolean | ||
): Operator<T, T> => (( src: RN<T> ) => new TakeWhileRN<T>( src, predicate )); | ||
@@ -795,7 +828,7 @@ | ||
class TakeWhileRN<T> extends RN<T> { | ||
private predicate: (value: T, index: number) => boolean; | ||
private predicate: (srcValue: T, srcIndex: number, index: number) => boolean; | ||
constructor( | ||
src: RN<T>, | ||
predicate: (value: T, index: number) => boolean | ||
predicate: (srcValue: T, srcIndex: number, index: number) => boolean | ||
) { | ||
@@ -807,6 +840,7 @@ super( src.value, [src] ); | ||
protected fire() { | ||
const next = this.parents[0]; | ||
const src = this.parents[0]; | ||
if ( this.predicate( next.value, next.index ) ) { | ||
this.fireWith( next.value ); | ||
// note: 'this.index' is not updated yet (will be updated in this.fireWith()) | ||
if ( this.predicate( src.value, src.index, this.index + 1 ) ) { | ||
this.fireWith( src.value ); | ||
} else { | ||
@@ -813,0 +847,0 @@ this.complete(); |
172618
155
4486
645