Skip to content

Instantly share code, notes, and snippets.

@bob-lee
Last active November 2, 2018 22:18
Show Gist options
  • Save bob-lee/6e12b4daad6b9b4eabf740d5d02df54a to your computer and use it in GitHub Desktop.
Save bob-lee/6e12b4daad6b9b4eabf740d5d02df54a to your computer and use it in GitHub Desktop.
export class LazyLoadDirective implements AfterViewInit, OnDestroy {
// ...
@Input() index = -1;
public ngAfterViewInit() {
// ...
if (this.index !== -1) { // if [index] given, listen to intersecting index
const sub = this._service.announcedIntersection.subscribe(params => {
const { index, state } = params;
if (!this.toLoad &&
this.index !== index &&
this._url && this._url !== NOT_GIVEN &&
(this.index - index) <= this._service.loadAheadCount) { // close to intersecting
this.toLoad = true;
const state = IntersectionState.NearIntersecting;
this.lazyLoad.emit(state);
}
});
this._subscription.add(sub);
}
}
private load(state: IntersectionState): void {
this.removeListeners();
if (this.toLoad) return;
this.toLoad = true;
this.lazyLoad.emit(state);
if (this.index !== -1) { // if [index] given, announce intersecting index
this._service.announceIntersection({ index: this.index, state });
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment