feat: allow frontend to specify its own eventsource lib

This commit is contained in:
thecodrr
2020-09-17 10:10:38 +05:00
parent 88fe786cfa
commit 24c28483c1
3 changed files with 13 additions and 9 deletions

View File

@@ -12,12 +12,21 @@ import Backup from "../database/backup";
import Conflicts from "./sync/conflicts";
import EventManager from "../utils/event-manager";
import Session from "./session";
import { EventSourcePolyfill } from "event-source-polyfill";
import { HOST } from "../utils/constants";
/**
* @type {EventSource}
*/
var NNEventSource;
class Database {
constructor(context) {
/**
*
* @param {any} context
* @param {EventSource} eventsource
*/
constructor(context, eventsource) {
this.context = new Storage(context);
NNEventSource = eventsource;
this._syncInterval = 0;
}
@@ -67,6 +76,7 @@ class Database {
}
async _onUserStateChanged(user) {
if (!NNEventSource) return;
if (this.evtSource) {
this.evtSource.close();
}
@@ -76,7 +86,7 @@ class Database {
user = await this.user.get();
}
this.evtSource = new EventSourcePolyfill(`${HOST}/events`, {
this.evtSource = new NNEventSource(`${HOST}/events`, {
headers: { Authorization: `Bearer ${user.accessToken}` },
});