1 | import Events from "./events";
|
2 |
|
3 | export default class Prefill {
|
4 | constructor(ias, options) {
|
5 | this.ias = ias;
|
6 | this.enabled = options;
|
7 | }
|
8 |
|
9 | prefill() {
|
10 | if (!this.enabled) {
|
11 | return;
|
12 | }
|
13 |
|
14 | this.ias.emitter.emit(Events.PREFILL);
|
15 |
|
16 | return Promise.all([this._prefillNext(), this._prefillPrev()]).then(() => {
|
17 | this.ias.emitter.emit(Events.PREFILLED);
|
18 |
|
19 |
|
20 | this.ias.measure();
|
21 | });
|
22 | }
|
23 |
|
24 | _prefillNext() {
|
25 | let distance = this.ias.distance();
|
26 |
|
27 | if (distance > 0) {
|
28 | return;
|
29 | }
|
30 |
|
31 | return this.ias.next()
|
32 | .then((hasNextUrl) => {
|
33 | if (!hasNextUrl) {
|
34 | return;
|
35 | }
|
36 |
|
37 | let distance = this.ias.distance();
|
38 |
|
39 | if (distance < 0) {
|
40 | return this._prefillNext();
|
41 | }
|
42 | })
|
43 | ;
|
44 | }
|
45 |
|
46 | _prefillPrev() {
|
47 | if (!this.ias.options.prev) {
|
48 | return;
|
49 | }
|
50 |
|
51 | return this.ias.prev();
|
52 | }
|
53 | }
|