Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Negative index issue #37

Closed
dabcat opened this issue May 3, 2018 · 4 comments
Closed

Negative index issue #37

dabcat opened this issue May 3, 2018 · 4 comments

Comments

@dabcat
Copy link

dabcat commented May 3, 2018

Thanks for module, it's been exactly what I was looking for. One issues that I'm having:

  • I would like to prevent triggering scroll event when the index goes to negative, basically I dont want to go into loops

I'm using v0.0.4-rc.1

@dhilt
Copy link
Owner

dhilt commented May 3, 2018

@dabcat Thanks for the issue! Preventing negative indexes is the Datasource responsibility. In terms of the uiScroll, you want the Datasource to be limited on backward direction. So you need to adjust your Datasource implementation to return empty array when the index becomes negative. But you need to be careful for not to cut edge positive items. Let me explain a bit.

When a bufferSize (default is 5 currently) of items is requested let us say starting from index -4, you indeed need to return an empty array (assuming that you want to index your dataset starting with index 1). For starting index -3 the return array should consist of single item # 1 in your dataset (items -3, -2, -1 and 0 are ignored). For -7 it should be array of 2 items # 1 and # 2 and so forth.

In the Different item heights demo we have an example of limited callback-based Datasource starting from index 1. If I remove forward direction limitation, I would get something like

datasource: IDatasource = {
  get: (index, count, success) => {
    const data = [];
    const start = Math.max(1, index); // so # 1 is the minimal possible index value
    const end = index + count - 1;
    if (start <= end) {
      for (let i = start; i <= end; i++) {
        data.push({ id: i, text: 'item #' + i, height: 20 + i });
      }
    }
    success(data);
  }
}

The uiScroll stops scrolling (by given direction) when the result of Datasource.get is an empty array or an array of length less than bufferSize!

@dabcat
Copy link
Author

dabcat commented May 3, 2018

@dhilt Thanks for prompt reply!

Seems like there is a bug when using Observables instead of promises, it wont trigger datasource again when reaching end of list:

public datasource: Datasource = {
    get: (index, count) => {
      const start = Math.max(1, index);
      const end = index + count - 1;
      return (start <= end) ? this.fetchItems(start, count) : [];
    },
    settings: {
      bufferSize: 100
    }
  };

fetchItems(buffer, count): Observable<any> {
    return this.http.post(this.url, {
      filters: [],
      index: buffer,
      quantity: count
    }).pipe(
      filter((res: any) => res.data.list.length > 0),
      map((res: any) => res.data.list)
    )
  }

So to clarify, scrolling down should load new results but scrolling up shouldn't once index is less or equal to zero.

@dabcat
Copy link
Author

dabcat commented May 3, 2018

@dhilt not a bug, my mistake... sorry!

should return empty stream instead of empty array:

return (start <= end) ? this.fetchItems(start, count) : Observable.of([]);

Thanks for the help, much appreciated! 👍

@dhilt
Copy link
Owner

dhilt commented May 3, 2018

@dabcat The returning array should be an observable too, so you can't just return [] from the Datasource.get, you may wrap it with Observable, something like

return new Observable(subscriber => subscriber.next([]));

Also, I think, a following workaround should work:

    get: (index, count, cb) => {
      return (Math.max(1, index) <= index + count - 1) ? this.fetchItems(start, count) : cb([]);
    }

The uiScroll checks Datasource.get each time it is called. So it could be Observable-based at one iteration and callback-based at another one. But I would recommend to use single signature, just for a consistency.

Also, you may move all negative logic from Client to Server. Server may return an empty array.

@dhilt dhilt closed this as completed May 3, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants