Last active
October 13, 2017 22:14
-
-
Save mkulke/a4f47c4fdd45494110004103924054a6 to your computer and use it in GitHub Desktop.
rx_omit_duplicates.ts
This file contains hidden or 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 Rx = require('rxjs/Rx'); | |
interface Obj { | |
id: number; | |
} | |
type Chunk = Obj[] | |
const chunks$: Rx.Observable<Chunk> = Rx.Observable.from([ | |
[ { id: 1 }, { id: 2} ], | |
[ { id: 3 }, { id: 2} ], | |
[ { id: 1 } ], | |
]); | |
interface Acc { | |
chunk?: Chunk; | |
ids: number[]; | |
} | |
const seed: Acc = { | |
ids: [], | |
} | |
function omitDuplicates(chunks$: Rx.Observable<Chunk>): Rx.Observable<Chunk> { | |
return chunks$ | |
.scan((acc, chunk) => { | |
const filteredChunk = chunk.filter(obj => { | |
return acc.ids.indexOf(obj.id) === -1; | |
}); | |
const ids = filteredChunk.map(obj => obj.id); | |
return { | |
chunk: filteredChunk, | |
ids: [...acc.ids, ...ids], | |
}; | |
}, seed) | |
.map(acc => acc.chunk); | |
} | |
chunks$ | |
.let(omitDuplicates) | |
.subscribe(console.log); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment