Eduard Kuzmenko
4 years ago
20 changed files with 132 additions and 108 deletions
@ -1,28 +1,46 @@
@@ -1,28 +1,46 @@
|
||||
import Scrollable from "../components/scrollable"; |
||||
|
||||
export default class ScrollableLoader { |
||||
public loading = false; |
||||
private scrollable: Scrollable; |
||||
private getPromise: () => Promise<any>; |
||||
private promise: Promise<any>; |
||||
private loaded = false; |
||||
|
||||
constructor(options: { |
||||
scrollable: Scrollable, |
||||
getPromise: () => Promise<any> |
||||
scrollable: ScrollableLoader['scrollable'], |
||||
getPromise: ScrollableLoader['getPromise'] |
||||
}) { |
||||
let loading = false; |
||||
Object.assign(this, options); |
||||
|
||||
options.scrollable.onScrolledBottom = () => { |
||||
if(loading) { |
||||
return; |
||||
this.load(); |
||||
}; |
||||
} |
||||
|
||||
public load() { |
||||
if(this.loaded) { |
||||
return Promise.resolve(); |
||||
} |
||||
|
||||
if(this.loading) { |
||||
return this.promise; |
||||
} |
||||
|
||||
loading = true; |
||||
options.getPromise().then(done => { |
||||
loading = false; |
||||
this.loading = true; |
||||
this.promise = this.getPromise().then(done => { |
||||
this.loading = false; |
||||
this.promise = undefined; |
||||
|
||||
if(done) { |
||||
options.scrollable.onScrolledBottom = null; |
||||
this.loaded = true; |
||||
this.scrollable.onScrolledBottom = null; |
||||
} else { |
||||
options.scrollable.checkForTriggers(); |
||||
this.scrollable.checkForTriggers(); |
||||
} |
||||
}, () => { |
||||
loading = false; |
||||
this.promise = undefined; |
||||
this.loading = false; |
||||
}); |
||||
}; |
||||
} |
||||
} |
||||
|
Loading…
Reference in new issue