2020-05-09 06:51:18 +03:00
|
|
|
export default class Subscription {
|
|
|
|
constructor(store, api, channel) {
|
|
|
|
this.store = store;
|
|
|
|
this.api = api;
|
|
|
|
this.channel = channel;
|
|
|
|
|
|
|
|
this.channel.setOnChannelError(this.onChannelError.bind(this));
|
|
|
|
this.firstRoundComplete = false;
|
|
|
|
}
|
|
|
|
|
2020-05-01 05:54:12 +03:00
|
|
|
start() {
|
2020-05-12 00:58:13 +03:00
|
|
|
if (this.api.ship) {
|
2020-05-09 06:51:18 +03:00
|
|
|
this.firstRound();
|
2020-05-01 05:54:12 +03:00
|
|
|
} else {
|
2020-05-09 06:51:18 +03:00
|
|
|
console.error('~~~ ERROR: Must set api.ship before operation ~~~');
|
2020-05-01 05:54:12 +03:00
|
|
|
}
|
2020-09-25 02:24:02 +03:00
|
|
|
this.setupSlog();
|
|
|
|
}
|
|
|
|
|
|
|
|
setupSlog() {
|
2020-10-06 19:28:50 +03:00
|
|
|
let available = false;
|
2020-10-06 19:31:42 +03:00
|
|
|
const slog = new EventSource('/~_~/slog', { withCredentials: true });
|
2020-09-25 02:24:02 +03:00
|
|
|
|
2021-05-06 01:22:08 +03:00
|
|
|
slog.onopen = (e) => {
|
2020-09-25 02:24:02 +03:00
|
|
|
console.log('slog: opened stream');
|
2020-10-06 19:28:50 +03:00
|
|
|
available = true;
|
2021-05-06 01:22:08 +03:00
|
|
|
};
|
2020-09-25 02:24:02 +03:00
|
|
|
|
2021-05-06 01:22:08 +03:00
|
|
|
slog.onmessage = (e) => {
|
2020-11-11 22:59:45 +03:00
|
|
|
this.handleEvent({ slog: e.data });
|
2021-05-06 01:22:08 +03:00
|
|
|
};
|
2020-09-25 02:24:02 +03:00
|
|
|
|
2021-05-06 01:22:08 +03:00
|
|
|
slog.onerror = (e) => {
|
2020-09-25 02:24:02 +03:00
|
|
|
console.error('slog: eventsource error:', e);
|
2020-10-06 19:28:50 +03:00
|
|
|
if (available) {
|
|
|
|
window.setTimeout(() => {
|
2021-05-06 01:22:08 +03:00
|
|
|
if (slog.readyState !== EventSource.CLOSED)
|
|
|
|
return;
|
2020-10-06 19:28:50 +03:00
|
|
|
console.log('slog: reconnecting...');
|
|
|
|
this.setupSlog();
|
|
|
|
}, 10000);
|
|
|
|
}
|
2021-05-06 01:22:08 +03:00
|
|
|
};
|
2020-05-01 05:54:12 +03:00
|
|
|
}
|
|
|
|
|
2020-05-09 06:51:18 +03:00
|
|
|
delete() {
|
|
|
|
this.channel.delete();
|
2020-05-01 05:54:12 +03:00
|
|
|
}
|
|
|
|
|
2020-05-09 06:51:18 +03:00
|
|
|
onChannelError(err) {
|
|
|
|
console.error('event source error: ', err);
|
|
|
|
console.log('initiating new channel');
|
|
|
|
this.firstRoundComplete = false;
|
|
|
|
setTimeout(2000, () => {
|
2020-05-12 00:58:13 +03:00
|
|
|
this.store.handleEvent({
|
2020-05-09 06:51:18 +03:00
|
|
|
data: { clear : true }
|
|
|
|
});
|
|
|
|
|
|
|
|
this.start();
|
|
|
|
});
|
2020-05-01 05:54:12 +03:00
|
|
|
}
|
|
|
|
|
2020-05-09 06:51:18 +03:00
|
|
|
subscribe(path, app) {
|
2020-05-12 00:58:13 +03:00
|
|
|
this.api.bind(path, 'PUT', this.api.ship, app,
|
2020-05-01 05:54:12 +03:00
|
|
|
this.handleEvent.bind(this),
|
2020-05-09 06:51:18 +03:00
|
|
|
(err) => {
|
|
|
|
console.log(err);
|
|
|
|
this.subscribe(path, app);
|
|
|
|
},
|
|
|
|
() => {
|
|
|
|
this.subscribe(path, app);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
firstRound() {
|
2020-11-10 21:32:26 +03:00
|
|
|
this.subscribe('/session/', 'herm');
|
2020-05-09 06:51:18 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
handleEvent(diff) {
|
2020-05-12 00:58:13 +03:00
|
|
|
this.store.handleEvent(diff);
|
2020-05-01 05:54:12 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|