Last active
March 25, 2020 13:17
-
-
Save subhog/ac14b26a3f11f026a17bf2fd8d7722a7 to your computer and use it in GitHub Desktop.
Showcasing DistinctElements and SwitchByMap RX operators
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { red, yellow } from "chalk"; | |
import { differenceWith, isEqual } from "lodash"; | |
import { Observable, of, Subject } from "rxjs"; | |
import { | |
concatMap, delay, groupBy, map, mergeMap, scan, switchMap, | |
} from "rxjs/operators"; | |
/* | |
Transform stream of arrays into stream of distinct elements | |
*/ | |
const distinctElements = () => | |
(source$: Observable<any[]>) => | |
source$.pipe( | |
scan<any[], { | |
previous: any[]; | |
difference: any[]; | |
}>( | |
(accumulator, current) => ({ | |
previous: current, | |
difference: differenceWith(current, accumulator.previous, isEqual), | |
}) | |
, { | |
previous: [], | |
difference: [], | |
}), | |
map(x => x.difference), | |
concatMap(x => x) | |
); | |
/* | |
Map to observable, complete previous inner observable within group | |
*/ | |
const switchByMap = (groupSelector, mapOperation) => source$ => source$.pipe( | |
groupBy(groupSelector), | |
mergeMap((group$: Observable<User>) => group$.pipe( | |
switchMap(mapOperation) | |
)) | |
); | |
/* | |
================================================================================ | |
*/ | |
type User = { | |
name: string; | |
status: string; | |
} | |
const users$ = new Subject<User[]>(); | |
const status$ = users$.pipe( | |
distinctElements() | |
); | |
const expensiveOperation = (x) => of(x).pipe(delay(500)); | |
users$.subscribe( | |
users => console.log(red("•USERS•"), users) | |
); | |
status$.subscribe( | |
r => console.log(yellow("•STATUS•"), r) | |
); | |
status$.pipe( | |
switchByMap( | |
user => user.name, | |
expensiveOperation | |
) | |
).subscribe( | |
r => console.log("•OPERATION•", r) | |
); | |
/* | |
================================================================================ | |
*/ | |
users$.next([{ | |
name: "Ana", | |
status: "Running", | |
}, { | |
name: "Tom", | |
status: "Swimming", | |
}, { | |
name: "John", | |
status: "Painting", | |
}]); | |
users$.next([{ | |
name: "Ana", | |
status: "Running", | |
}, { | |
name: "Tom", | |
status: "Swimming", | |
}, { | |
name: "John", | |
status: "Painting", | |
}]); | |
users$.next([{ | |
name: "Ana", | |
status: "Running", | |
}, { | |
name: "Tom", | |
status: "Swimming", | |
}, { | |
name: "John", | |
status: "Painting", | |
}, { | |
name: "Jack", | |
status: "Woke up", | |
}]); | |
users$.next([{ | |
name: "Ana", | |
status: "Gardening", | |
}, { | |
name: "Tom", | |
status: "Gardening", | |
}, { | |
name: "John", | |
status: "Painting", | |
}, { | |
name: "Jack", | |
status: "Gardening", | |
}]); | |
setTimeout(() => { | |
users$.next([{ | |
name: "Ana", | |
status: "Reading", | |
}, { | |
name: "Tom", | |
status: "Gardening", | |
}, { | |
name: "John", | |
status: "Painting", | |
}, { | |
name: "Jack", | |
status: "Gardening", | |
}]); | |
}, 1200); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Output: