Skip to content

Instantly share code, notes, and snippets.

@mihajm
Created March 22, 2025 14:05
Show Gist options
  • Save mihajm/9415da165160b94a5ffcffa5943d4947 to your computer and use it in GitHub Desktop.
Save mihajm/9415da165160b94a5ffcffa5943d4947 to your computer and use it in GitHub Desktop.
A very simplified example
@Injectable()
export class ExampleService {
  readonly totalCount = httpResource<number>('exampleCountUrl', {
    defaultValue: 0,
  });
  readonly limit = signal(50);
  readonly page = signal(0);
  private readonly offset = computed(() => this.page() * this.limit());
  readonly todosFetcherResource = httpResource<MediaTitle[]>(
    () => {
      if (this.totalCount.value() <= this.offset()) return; // disable the resource by returning undefined
      return {
        url: 'exampleMediaTitleListUrl',
        params: {
          limit: this.limit(),
          offset: this.offset(),
        },
      };
    },
    {
      defaultValue: [],
    }
  );
  readonly mergedArray = linkedSignal<MediaTitle[], MediaTitle[]>(
    {
      source: () => this.todosFetcherResource.value(),
      computation: (source, prev) =>
        prev ? [...prev.value, ...source] : source,
      equal: (a, b) => {
        // you can use a better comparison here
        // but length will probably be the only thing that changes
        return a.length === b.length;
      },
    },
  );
  onNearEndScroll() {
    this.page.update((v) => v + 1);
  }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment