File tree Expand file tree Collapse file tree 4 files changed +49
-1
lines changed Expand file tree Collapse file tree 4 files changed +49
-1
lines changed Original file line number Diff line number Diff line change @@ -35,4 +35,3 @@ inputObservable$
35
35
inputObservable$
36
36
. do ( ( text ) => div . textContent = text )
37
37
. subscribe ( ) ;
38
-
Original file line number Diff line number Diff line change @@ -15,6 +15,8 @@ export * from './operators/first';
15
15
export * from './operators/toPromise' ;
16
16
export * from './operators/flatMap' ;
17
17
export * from './operators/switchMap' ;
18
+ export * from './operators/scan' ;
19
+ export * from './operators/reduce' ;
18
20
19
21
// add on observable types
20
22
export * from './observables/fromEvent' ;
Original file line number Diff line number Diff line change
1
+ import { Observable } from '../Observable' ;
2
+ import { scan } from './scan' ;
3
+
4
+ export const reduce = function ( source$ , scanCallback , startValue = 0 ) {
5
+ return new Observable ( function ( { next, error, complete } ) {
6
+ let lastValue = 0 ;
7
+ const subscription = scan ( source$ , scanCallback , startValue ) . subscribe ( {
8
+ next ( value ) {
9
+ lastValue = value ;
10
+ } ,
11
+ error,
12
+ complete ( ) {
13
+ next ( lastValue ) ;
14
+ complete ( ) ;
15
+ }
16
+ } ) ;
17
+
18
+ return ( ) => subscription . unsubscribe ( ) ;
19
+ } ) ;
20
+ } ;
21
+
22
+ Observable . prototype . reduce = function ( scanCallback , startValue ) {
23
+ return reduce ( this , scanCallback , startValue ) ;
24
+ } ;
Original file line number Diff line number Diff line change
1
+ import { Observable } from '../Observable' ;
2
+
3
+ export const scan = function ( source$ , scanCallback , startValue = 0 ) {
4
+ return new Observable ( function ( { next, error, complete } ) {
5
+ let previousValue = startValue ;
6
+
7
+ const subscription = source$ . subscribe ( {
8
+ next ( value ) {
9
+ previousValue = scanCallback ( previousValue , value ) ;
10
+
11
+ next ( previousValue ) ;
12
+ } ,
13
+ error,
14
+ complete
15
+ } ) ;
16
+
17
+ return ( ) => subscription . unsubscribe ( ) ;
18
+ } ) ;
19
+ } ;
20
+
21
+ Observable . prototype . scan = function ( scanCallback , startValue ) {
22
+ return scan ( this , scanCallback , startValue ) ;
23
+ } ;
You can’t perform that action at this time.
0 commit comments