GithubHelp home page GithubHelp logo

Negative index issue about ngx-ui-scroll HOT 4 CLOSED

dhilt avatar dhilt commented on August 14, 2024
Negative index issue

from ngx-ui-scroll.

Comments (4)

dabcat avatar dabcat commented on August 14, 2024 1

@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! 👍

from ngx-ui-scroll.

dhilt avatar dhilt commented on August 14, 2024 1

@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.

from ngx-ui-scroll.

dhilt avatar dhilt commented on August 14, 2024

@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!

from ngx-ui-scroll.

dabcat avatar dabcat commented on August 14, 2024

@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.

from ngx-ui-scroll.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.