Last active
November 2, 2018 22:18
-
-
Save bob-lee/6e12b4daad6b9b4eabf740d5d02df54a to your computer and use it in GitHub Desktop.
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
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