Sha256: 7df60374b0506e06b706a5a653407ea8ae4aaa8eea72f13de314c55fecb26e09

Contents?: true

Size: 1.64 KB

Versions: 85

Compression:

Stored size: 1.64 KB

Contents

function Uploader(url, cb) {
  this.ws = new WebSocket(url);
  if (cb) this.ws.onopen = cb;
  this.sendQueue = [];
  this.sending = null;
  this.sendCallback = null;
  this.ondone = null;
  var self = this;
  this.ws.onmessage = function(event) {
    var data = JSON.parse(event.data);
    if (data.event == 'complete') {
      if (data.path != self.sending.path) {
        self.sendQueue = [];
        self.sending = null;
        self.sendCallback = null;
        throw new Error('Got message for wrong file!');
      }
      self.sending = null;
      var callback = self.sendCallback;
      self.sendCallback = null;
      if (callback) callback();
      if (self.sendQueue.length === 0 && self.ondone) self.ondone(null);
      if (self.sendQueue.length > 0) {
        var args = self.sendQueue.pop();
        setTimeout(function() { self.sendFile.apply(self, args); }, 0);
      }
    }
    else if (data.event == 'error') {
      self.sendQueue = [];
      self.sending = null;
      var callback = self.sendCallback;
      self.sendCallback = null;
      var error = new Error('Server reported send error for file ' + data.path);
      if (callback) callback(error);
      if (self.ondone) self.ondone(error);
    }
  }
}

Uploader.prototype.sendFile = function(file, cb) {
  if (this.ws.readyState != WebSocket.OPEN) throw new Error('Not connected');
  if (this.sending) {
    this.sendQueue.push(arguments);
    return;
  }
  var fileData = { name: file.name, path: file.webkitRelativePath };
  this.sending = fileData;
  this.sendCallback = cb;
  this.ws.send(JSON.stringify(fileData));
  this.ws.send(file);
}

Uploader.prototype.close = function() {
  this.ws.close();
}

Version data entries

85 entries across 85 versions & 1 rubygems

Version Path
flok-0.0.105 spec/iface/driver/assets/node_modules/socket.io/node_modules/socket.io-client/node_modules/engine.io-client/node_modules/ws/examples/fileapi/public/uploader.js
flok-0.0.103 spec/iface/driver/assets/node_modules/socket.io/node_modules/socket.io-client/node_modules/engine.io-client/node_modules/ws/examples/fileapi/public/uploader.js
flok-0.0.102 spec/iface/driver/assets/node_modules/socket.io/node_modules/socket.io-client/node_modules/engine.io-client/node_modules/ws/examples/fileapi/public/uploader.js
flok-0.0.101 spec/iface/driver/assets/node_modules/socket.io/node_modules/socket.io-client/node_modules/engine.io-client/node_modules/ws/examples/fileapi/public/uploader.js
flok-0.0.100 spec/iface/driver/assets/node_modules/socket.io/node_modules/socket.io-client/node_modules/engine.io-client/node_modules/ws/examples/fileapi/public/uploader.js
flok-0.0.99 spec/iface/driver/assets/node_modules/socket.io/node_modules/socket.io-client/node_modules/engine.io-client/node_modules/ws/examples/fileapi/public/uploader.js
flok-0.0.98 spec/iface/driver/assets/node_modules/socket.io/node_modules/socket.io-client/node_modules/engine.io-client/node_modules/ws/examples/fileapi/public/uploader.js
flok-0.0.97 spec/iface/driver/assets/node_modules/socket.io/node_modules/socket.io-client/node_modules/engine.io-client/node_modules/ws/examples/fileapi/public/uploader.js
flok-0.0.96 spec/iface/driver/assets/node_modules/socket.io/node_modules/socket.io-client/node_modules/engine.io-client/node_modules/ws/examples/fileapi/public/uploader.js
flok-0.0.95 spec/iface/driver/assets/node_modules/socket.io/node_modules/socket.io-client/node_modules/engine.io-client/node_modules/ws/examples/fileapi/public/uploader.js
flok-0.0.94 spec/iface/driver/assets/node_modules/socket.io/node_modules/socket.io-client/node_modules/engine.io-client/node_modules/ws/examples/fileapi/public/uploader.js
flok-0.0.93 spec/iface/driver/assets/node_modules/socket.io/node_modules/socket.io-client/node_modules/engine.io-client/node_modules/ws/examples/fileapi/public/uploader.js
flok-0.0.92 spec/iface/driver/assets/node_modules/socket.io/node_modules/socket.io-client/node_modules/engine.io-client/node_modules/ws/examples/fileapi/public/uploader.js
flok-0.0.91 spec/iface/driver/assets/node_modules/socket.io/node_modules/socket.io-client/node_modules/engine.io-client/node_modules/ws/examples/fileapi/public/uploader.js
flok-0.0.90 spec/iface/driver/assets/node_modules/socket.io/node_modules/socket.io-client/node_modules/engine.io-client/node_modules/ws/examples/fileapi/public/uploader.js
flok-0.0.89 spec/iface/driver/assets/node_modules/socket.io/node_modules/socket.io-client/node_modules/engine.io-client/node_modules/ws/examples/fileapi/public/uploader.js
flok-0.0.88 spec/iface/driver/assets/node_modules/socket.io/node_modules/socket.io-client/node_modules/engine.io-client/node_modules/ws/examples/fileapi/public/uploader.js
flok-0.0.87 spec/iface/driver/assets/node_modules/socket.io/node_modules/socket.io-client/node_modules/engine.io-client/node_modules/ws/examples/fileapi/public/uploader.js
flok-0.0.86 spec/iface/driver/assets/node_modules/socket.io/node_modules/socket.io-client/node_modules/engine.io-client/node_modules/ws/examples/fileapi/public/uploader.js
flok-0.0.85 spec/iface/driver/assets/node_modules/socket.io/node_modules/socket.io-client/node_modules/engine.io-client/node_modules/ws/examples/fileapi/public/uploader.js