frc-ui
Version:
React Web UI
178 lines (130 loc) • 4.38 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", {
value: true
});
exports["default"] = void 0;
function _defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } }
function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); Object.defineProperty(Constructor, "prototype", { writable: false }); return Constructor; }
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
var __socket = function instanceInit() {
var instance;
return function (newInstance) {
if (newInstance) instance = newInstance;
Object.seal(instance);
return instance;
};
}();
var Socket = /*#__PURE__*/_createClass(function Socket() {
var _this = this;
_classCallCheck(this, Socket);
this.worker = null;
this._callbacks = {};
this.leave = function () {
if (_this.worker) {
_this.worker.close();
}
};
this.start = function (opt) {
var userId = opt.userId,
token = opt.token,
host = opt.host,
path = opt.path;
if (!userId || !token || !Object.prototype.hasOwnProperty.call(window, 'SharedWork')) {
return;
}
if (_this.worker) {
return;
}
var option = {
token: JSON.stringify({
token: token
}),
query: function query(data) {
try {
return JSON.parse(data);
} catch (e) {
console.error(e);
return null;
}
}
}; // @ts-ignore
_this.worker = new window.SharedWork("".concat(host || '').concat(path || ''), option);
_this.worker.on('login', function (event) {
var code = event.code;
if (code === 0) {
console.log('Socket Login: ', event);
_this.emit('login', event);
} else {
console.log('Socket Login Fail: ', event);
_this.emit('login-fail', event);
}
});
_this.worker.on('connect', function (event) {
console.log('Socket Connect : ', event);
_this.emit('connect', event);
});
_this.worker.on('reconnect', function (event) {
console.log('Socket Reconnect : ', event);
_this.emit('reconnect', event);
});
_this.worker.on('close', function (event) {
console.log('Socket Close : ', event);
_this.emit('close', event);
});
_this.worker.on('error', function (event) {
console.log('Socket Error : ', event);
_this.emit('error', event);
});
_this.worker.on('message', function (event) {
console.log('Socket Message : ', event);
_this.emit('message', event);
});
};
this.on = function (event, fn) {
_this._callbacks = _this._callbacks || {};
(_this._callbacks['$' + event] = _this._callbacks['$' + event] || []).push(fn);
return _this;
};
this.off = function (event, fn) {
_this._callbacks = _this._callbacks || {};
if (!event && !fn) {
_this._callbacks = {};
return _this;
}
var callbacks = _this._callbacks['$' + event];
if (!callbacks) return _this;
if (!!event && !fn) {
delete _this._callbacks['$' + event];
return _this;
}
var cb;
for (var i = 0; i < callbacks.length; i++) {
cb = callbacks[i];
if (cb === fn || cb.fn === fn) {
callbacks.splice(i, 1);
break;
}
}
return _this;
};
this.emit = function (event, arg) {
_this._callbacks = _this._callbacks || {}; // @ts-ignore
var callbacks = _this._callbacks['$' + event];
if (callbacks) {
callbacks = callbacks.slice(0);
for (var i = 0, len = callbacks.length; i < len; ++i) {
if (typeof callbacks[i] === 'function') callbacks[i](arg);
}
}
return _this;
};
var instance = __socket();
if (instance) {
return instance;
}
this.off.bind(this);
window.addEventListener('beforeunload', this.leave);
__socket(this);
});
var _default = new Socket();
exports["default"] = _default;