@state-sync/redux-path-reducer
Version:
state-sync client only json path reducer
131 lines (130 loc) • 5.17 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
var uuid = require("uuid");
var webstomp_client_1 = require("webstomp-client");
var StompConnection = /** @class */ (function () {
function StompConnection(config, statusListener, eventListener, onReady) {
this.config = config;
this.sessionToken = uuid.v4();
this.statusListener = statusListener;
this.eventListener = eventListener;
this.onReady = onReady;
this.statusListener.onDisconnect(0);
this.pending = [];
}
StompConnection.prototype.send = function (event) {
var msg = JSON.stringify(event);
try {
if (this.stompClient) {
this.stompClient.send('/app/request/' + this.sessionToken, msg);
}
else {
this.pending.push(event);
}
}
catch (err) {
if (err.name == 'InvalidStateError') {
this.pending.push(event);
}
else {
console.log('StompConnection.send failed, reconnect', msg);
this.disconnect();
}
}
};
StompConnection.prototype.connect = function () {
this.statusListener.onConnecting();
if (this.config.csrfUrl) {
var xhttp = new XMLHttpRequest();
// xhttp.onreadystatechange = (body) => this.wsConnect();
// xhttp.onerror = () => this.onStompDisconnected();
xhttp.open("GET", this.config.csrfUrl, false);
xhttp.send();
var csrfToken = xhttp.responseText;
this.wsConnect(csrfToken);
}
else {
this.wsConnect();
}
};
StompConnection.prototype.wsConnect = function (csrfToken) {
var _this = this;
var headers = {};
if (csrfToken) {
headers['X-CSRF-TOKEN'] = csrfToken;
}
this.stompClient = webstomp_client_1.client(this.config.url + '?access_token=' + this.config.accessToken, { debug: this.config.debug });
this.stompClient.connect(headers, function (frame) { return _this.onStompConnected(frame); }, function (msg) { return _this.onStompDisconnected(msg); });
};
StompConnection.prototype.onStompConnected = function (frame) {
if (this.config.debugConnectFrame)
console.info(frame);
this.statusListener.onConnected();
// this.sessionToken = frame.header;
this.stompClient.send('/app/init/' + this.sessionToken);
this.onSystemConnected();
};
StompConnection.prototype.onStompDisconnected = function (msg) {
this.fullyConnected = false;
this.sessionToken = '';
this.statusListener.onDisconnect(this.config.timeout);
this.config.authListener.onAuthRequired('');
// if (this.config.checkTokenUrl) {
// // check access token
// let xhttp = new XMLHttpRequest();
// xhttp.onload = (ev) => {
// if(xhttp.readyState === 4) {
// const json = JSON.parse(xhttp.responseText);
// if (json.error) {
// this.config.authListener.onAuthRequired('');
// } else {
// setTimeout(() => this.connect(), this.config.timeout);
// }
// }
// };
// xhttp.onerror = (ev) => {
// console.info(ev);
// setTimeout(() => this.connect(), this.config.timeout);
// };
// xhttp.open('GET', this.config.checkTokenUrl + '/' + this.config.accessToken, true);
// xhttp.send();
// } else {
// if (msg && msg.command === 'ERROR') {
// this.config.authListener.onAuthRequired('');
// } else {
// setTimeout(() => this.connect(), this.config.timeout);
// }
// }
};
StompConnection.prototype.onSystemConnected = function () {
var _this = this;
this.statusListener.onConfigured();
this.sessionSubscription = this.stompClient.subscribe('/out/' + this.sessionToken, function (message) {
var event = JSON.parse(message.body);
_this.eventListener.onEvent(event);
_this.onSessionChannelConnected();
});
this.fullyConnected = true;
this.statusListener.onReady();
this.onReady();
};
StompConnection.prototype.isFullyConnected = function () {
return this.fullyConnected;
};
StompConnection.prototype.onSessionChannelConnected = function () {
for (var _i = 0, _a = this.pending; _i < _a.length; _i++) {
var event_1 = _a[_i];
this.send(event_1);
}
this.pending = [];
};
StompConnection.prototype.disconnect = function () {
var _this = this;
this.stompClient.disconnect(function () {
console.info('stomp client disconnected');
_this.onStompDisconnected();
});
};
return StompConnection;
}());
exports.default = StompConnection;