blob: 881356cbbd3d99ce43b0c12e528038b9aa8dba2e [file] [log] [blame]
Adrià Vilanova Martínez0d92a0c2023-11-06 01:37:20 +01001import {waitFor} from 'poll-until-promise';
2
3import {shouldImplement} from '../../../../common/commonUtils.js';
4
5import BaseInfoHandler from './base.js';
6
7export default class ResponseEventBasedInfoHandler extends BaseInfoHandler {
8 constructor() {
9 super();
10
11 if (this.constructor == ResponseEventBasedInfoHandler) {
12 throw new Error('The base class cannot be instantiated.');
13 }
14
15 this.setUpDefaultInfoValue();
16 this.setUpEventHandler();
17 }
18
19 /**
20 * Should return the name of the XHR interceptor event for the API response
21 * which has the information being handled.
22 */
23 getEvent() {
24 shouldImplement('getEvent');
25 }
26
27 /**
28 * This function should return a promise which resolves to a boolean
29 * specifying whether this.info is the information related to the view that
30 * the user is currently on.
31 */
32 async isInfoCurrent(_injectionDetails) {
33 shouldImplement('isInfoCurrent');
34 }
35
36 /**
37 * Should return the options for the waitFor function which is called when
38 * checking whether the information is current or not.
39 */
40 getWaitForCurrentInfoOptions() {
41 shouldImplement('getWaitForCurrentInfoOptions');
42 }
43
44 setUpDefaultInfoValue() {
45 this.info = {
46 body: {},
47 id: -1,
48 timestamp: 0,
49 };
50 }
51
52 setUpEventHandler() {
53 window.addEventListener(this.getEvent(), e => {
54 if (e.detail.id < this.info.id) return;
55
56 this.info = {
57 body: e.detail.body,
58 id: e.detail.id,
59 timestamp: Date.now(),
60 };
61 });
62 }
63
64 async getCurrentInfo(injectionDetails) {
65 const options = this.getWaitForCurrentInfoOptions();
66 return waitFor(
67 () => this.attemptToGetCurrentInfo(injectionDetails), options);
68 }
69
70 async attemptToGetCurrentInfo(injectionDetails) {
71 const isInfoCurrent = await this.isInfoCurrent(injectionDetails);
72 if (!isInfoCurrent) throw new Error('Didn\'t receive current information');
73
74 return this.info;
75 }
76}