forked from privacybydesign/yivi-frontend-packages
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstatus-listener.js
180 lines (148 loc) · 6.01 KB
/
status-listener.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
const ProtocolVersion = require('./protocol-version');
// Never use window.EventSource, because it doesn't support requests with additional HTTP headers.
// eslint-disable-next-line no-shadow
const EventSource = require('eventsource');
if (typeof fetch === 'undefined') require('isomorphic-fetch');
module.exports = class StatusListener {
constructor(mappings, options) {
this._isRunning = false;
this._isPolling = false;
this._options = options;
this._mappings = mappings;
this._listeningMethod = this._options.serverSentEvents ? 'sse' : 'polling';
this._sseUrl = this._options.serverSentEvents ? this._getFetchUrl(this._options.serverSentEvents.endpoint) : '';
this._pollingUrl = this._options.polling ? this._getFetchUrl(this._options.polling.endpoint) : '';
this._fetchParams = this._getFetchParams();
}
observe(stateChangeCallback, errorCallback) {
this._stateChangeCallback = stateChangeCallback;
this._errorCallback = errorCallback;
this._isRunning = true;
switch (this._listeningMethod) {
case 'sse':
return this._startSSE();
default:
return this._startPolling();
}
}
close() {
if (!this._isRunning) return false;
if (this._source) {
// If ready state is CLOSED (2), the close call will do nothing. Therefore we skip debug logging then.
if (this._options.debugging && this._source.readyState < 2) console.log('🌎 Closed EventSource');
this._source.close();
}
this._isRunning = false;
return true;
}
_getFetchUrl(endpoint) {
return ProtocolVersion.below(
this._mappings.frontendRequest.maxProtocolVersion,
ProtocolVersion.get('chained-sessions'),
)
? this._options.legacyUrl(this._mappings, endpoint)
: this._options.url(this._mappings, endpoint);
}
_getFetchParams() {
if (
ProtocolVersion.below(this._mappings.frontendRequest.maxProtocolVersion, ProtocolVersion.get('chained-sessions'))
)
return {};
return { headers: { Authorization: this._mappings.frontendRequest.authorization } };
}
_startSSE() {
if (this._options.debugging) console.log(`🌎 Using EventSource for server events on ${this._sseUrl}`);
this._source = new EventSource(this._sseUrl, this._fetchParams);
const timeout = this._options.serverSentEvents.timeout;
const canceller = setTimeout(() => {
if (this._options.debugging)
console.error(`🌎 EventSource could not connect to ${this._sseUrl} within ${timeout}ms`);
// Fall back to polling instead
setTimeout(() => this._source.close(), 0); // Never block on this
this._startPolling();
}, timeout);
this._source.addEventListener('open', () => clearTimeout(canceller));
this._source.addEventListener('message', (evnt) => {
clearTimeout(canceller);
let state = JSON.parse(evnt.data);
// Do additional parsing in case we received the legacy status response.
if (typeof state === 'string') {
state = { status: state };
}
if (this._options.debugging) console.log(`🌎 Server event: Remote state changed to '${state.status}'`);
this._stateChangeCallback(state);
});
this._source.addEventListener('error', (error) => {
clearTimeout(canceller);
this._source.close();
if (this._options.debugging) console.error('🌎 EventSource threw an error: ', error);
// Fall back to polling instead
setTimeout(() => this._source.close(), 0); // Never block on this
this._startPolling();
});
}
_startPolling() {
this._listeningMethod = 'polling'; // In case polling is activated as fallback
if (!this._options.polling || this._isPolling) return;
if (this._options.debugging) console.log(`🌎 Using polling for server events on ${this._pollingUrl}`);
this._currentStatus = this._options.polling.startState;
this._isPolling = true;
this._polling()
.then(() => {
if (this._options.debugging) console.log(`🌎 Stopped polling on ${this._pollingUrl}`);
})
.catch((error) => {
if (this._options.debugging) console.error(`🌎 Error thrown while polling of ${this._pollingUrl}: `, error);
this._errorCallback(error);
});
}
_pollOnce() {
return (
// eslint-disable-next-line compat/compat
fetch(this._pollingUrl, { ...this._fetchParams, cache: 'no-store' })
.then((r) => {
if (r.status !== 200)
throw new Error(
`Error in fetch: endpoint returned status other than 200 OK. Status: ${r.status} ${r.statusText}`,
);
return r;
})
.then((r) => r.json())
// Do additional parsing in case we received the legacy status response.
.then((state) => (typeof state === 'string' ? { status: state } : state))
);
}
_polling() {
return new Promise((resolve, reject) => {
if (!this._isRunning) {
this._isPolling = false;
resolve();
return;
}
// On Firefox for Android pending fetch request are actively aborted when navigating.
// So in case of an error, we do a second attempt to assure the error is permanent.
this._pollOnce()
.catch(() => {
if (this._options.debugging) console.log('Polling attempt failed; doing a second attempt to confirm error');
return this._pollOnce();
})
.then((state) => {
// Re-check running because variable might have been changed during fetch.
if (!this._isRunning) {
this._isPolling = false;
resolve();
return;
}
if (state.status !== this._currentStatus) {
if (this._options.debugging) console.log(`🌎 Server event: Remote state changed to '${state.status}'`);
this._currentStatus = state.status;
this._stateChangeCallback(state);
}
setTimeout(() => {
this._polling().then(resolve).catch(reject);
}, this._options.polling.interval);
})
.catch(reject);
});
}
};