Skip to content

Instantly share code, notes, and snippets.

@LironHazan
Last active December 3, 2017 18:23
Show Gist options
  • Save LironHazan/82d983d0151a0dce47b4280598f84e62 to your computer and use it in GitHub Desktop.
Save LironHazan/82d983d0151a0dce47b4280598f84e62 to your computer and use it in GitHub Desktop.
const Rx = require('rxjs');
class AnimalsService {
constructor(data){
this._source = data;
}
// lazy loading, client will ask for x items (limit) it will be the length of the returned array,
simplePager(pageIndex=0, limit=10, data) {
const deleteCount = pageIndex*limit;
const source$ = Rx.Observable.from(this._source || data)
.reduce((acc, value, index) => {
if(deleteCount <= index) {
acc.push(value);
}
return acc;
}, []);
const observer = {
next: i => {console.log(JSON.stringify(i))},
error: err => {console.log(err)},
complete: () => {}
};
source$.subscribe(observer);
return source$.toPromise();
}
}
module.exports = AnimalsService;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment