I have the following code, to get the last chast messages:
const pageSize = 10;
this.notifier = new Subject<any>();
let last: Observable<any>;
let infiniteList = Observable
// Use zip to combine the notifier's emissions with the last
// child value:
.zip(this.notifier, Observable.defer(() => last))
// Use concatMap to emit a page of children into the
// composed observable (note that first is used to complete
// the inner list):
.concatMap(([unused, last]) => this.af.database.list("chat_messages", {
query: {
// If there is a last value, start at that value but ask
// for one more:
limitToLast: last ? (pageSize + 1) : pageSize
}
})
.first()
)
// Use scan to accumulate the page into the infinite list:
.scan((acc, list) => {
// If this isn't the initial page, the page was started
// at the last value, so remove it from the beginning of
// the list:
if (acc.length > 0) {
list.shift();
}
return acc.concat(list);
}, [])
// Use share so that the last observable (see below) doesn't
// result in a second subscription:
.share();
// Each time a page is emitted, map to its last child value so
// that it can be fed back into the composed infinite list:
last = infiniteList
.map((list) => {
list.reverse();
if (list.length === 0) {
return null;
}
return list[list.length - 1].date_published;
})
.startWith(null);
infiniteList.subscribe((list) => {
this.chatMessages = list;
});
this.notifier.next();
Whenever the user scrolls to the bottom of the list I do this.notifier.next();
to read more from the chat history. 10 messages at a time.
The problem: the first 10 works great, but when I scroll to the bottom to read new data, I get the same information in the opposite order. For example, I had this:
1- Good
2- Fine, you?
10- Hi alL! How are you?
---scroll to the previous 10 message--
12-. Hi all! How are you?
13-. Fine, you?
14-. Good