uppy
Version:
Extensible JavaScript file upload widget with support for drag&drop, resumable uploads, previews, restrictions, file processing/encoding, remote providers like Instagram, Dropbox, Google Drive, S3 and more :dog:
87 lines (67 loc) • 2.17 kB
JavaScript
'use strict';
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
var ee = require('namespace-emitter');
module.exports = function () {
function UppySocket(opts) {
var _this = this;
_classCallCheck(this, UppySocket);
this.queued = [];
this.isOpen = false;
this.socket = new WebSocket(opts.target);
this.emitter = ee();
this.socket.onopen = function (e) {
_this.isOpen = true;
while (_this.queued.length > 0 && _this.isOpen) {
var first = _this.queued[0];
_this.send(first.action, first.payload);
_this.queued = _this.queued.slice(1);
}
};
this.socket.onclose = function (e) {
_this.isOpen = false;
};
this._handleMessage = this._handleMessage.bind(this);
this.socket.onmessage = this._handleMessage;
this.close = this.close.bind(this);
this.emit = this.emit.bind(this);
this.on = this.on.bind(this);
this.once = this.once.bind(this);
this.send = this.send.bind(this);
}
UppySocket.prototype.close = function close() {
return this.socket.close();
};
UppySocket.prototype.send = function send(action, payload) {
// attach uuid
if (!this.isOpen) {
this.queued.push({ action: action, payload: payload });
return;
}
this.socket.send(JSON.stringify({
action: action,
payload: payload
}));
};
UppySocket.prototype.on = function on(action, handler) {
console.log(action);
this.emitter.on(action, handler);
};
UppySocket.prototype.emit = function emit(action, payload) {
console.log(action);
this.emitter.emit(action, payload);
};
UppySocket.prototype.once = function once(action, handler) {
this.emitter.once(action, handler);
};
UppySocket.prototype._handleMessage = function _handleMessage(e) {
try {
var message = JSON.parse(e.data);
console.log(message);
this.emit(message.action, message.payload);
} catch (err) {
console.log(err);
}
};
return UppySocket;
}();
//# sourceMappingURL=UppySocket.js.map