@devcycle/nodejs-server-sdk
Version:
The DevCycle NodeJS Server SDK used for feature management.
49 lines • 1.82 kB
JavaScript
"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.SSEConnection = void 0;
const eventsource_1 = __importDefault(require("eventsource"));
class SSEConnection {
constructor(url, logger, callbacks) {
this.url = url;
this.logger = logger;
this.callbacks = callbacks;
this.openConnection();
}
openConnection() {
if (typeof eventsource_1.default === 'undefined') {
this.logger.warn('SSEConnection not opened. EventSource is not available.');
return;
}
this.connection = new eventsource_1.default(this.url, { withCredentials: true });
this.connection.onmessage = (event) => {
this.callbacks.onMessage(event.data);
};
this.connection.onerror = (err) => {
this.logger.warn(`SSEConnection warning. Connection failed. Error status: ${err.status}, message: ${err.message}`);
this.callbacks.onConnectionError();
};
this.connection.onopen = () => {
this.logger.debug('SSEConnection opened');
this.callbacks.onOpen();
};
}
isConnected() {
var _a, _b;
return ((_a = this.connection) === null || _a === void 0 ? void 0 : _a.readyState) === ((_b = this.connection) === null || _b === void 0 ? void 0 : _b.OPEN);
}
reopen() {
if (!this.isConnected()) {
this.close();
this.openConnection();
}
}
close() {
var _a;
(_a = this.connection) === null || _a === void 0 ? void 0 : _a.close();
}
}
exports.SSEConnection = SSEConnection;
//# sourceMappingURL=SSEConnection.js.map