Skip to content

Instantly share code, notes, and snippets.

@mkulke
Last active October 13, 2017 22:14
Show Gist options
  • Save mkulke/a4f47c4fdd45494110004103924054a6 to your computer and use it in GitHub Desktop.
Save mkulke/a4f47c4fdd45494110004103924054a6 to your computer and use it in GitHub Desktop.
rx_omit_duplicates.ts
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