-
-
Save mihajm/9415da165160b94a5ffcffa5943d4947 to your computer and use it in GitHub Desktop.
A very simplified example
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@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