@cocalc/project
Version:
CoCalc: project daemon
89 lines (78 loc) • 2.65 kB
JavaScript
// Generated by CoffeeScript 2.5.1
(function() {
//########################################################################
// This file is part of CoCalc: Copyright © 2020 Sagemath, Inc.
// License: AGPLv3 s.t. "Commons Clause" – see LICENSE.md for details
//########################################################################
/*
Saving blobs to hub
CoCalc: Collaborative web-based SageMath, Jupyter, LaTeX and Terminals.
Copyright 2015, SageMath, Inc., GPL v3.
*/
var _save_blob_callbacks, defaults, message, misc, required, winston;
misc = require('@cocalc/util/misc');
message = require('@cocalc/util/message');
winston = require('./logger').getLogger('blobs');
({defaults, required} = misc);
_save_blob_callbacks = {};
exports.receive_save_blob_message = function(opts) { // temporarily used by file_session_manager
var f, id, sha1;
opts = defaults(opts, {
sha1: required,
cb: required,
timeout: 30 // maximum time in seconds to wait for response message
});
winston.debug(`receive_save_blob_message: ${opts.sha1}`);
sha1 = opts.sha1;
id = misc.uuid();
if (_save_blob_callbacks[sha1] == null) {
_save_blob_callbacks[sha1] = [[opts.cb, id]];
} else {
_save_blob_callbacks[sha1].push([opts.cb, id]);
}
// Timeout functionality -- send a response after opts.timeout seconds,
// in case no hub responded.
if (!opts.timeout) {
return;
}
f = function() {
var i, len, mesg, v, w, x;
v = _save_blob_callbacks[sha1];
if (v != null) {
mesg = message.save_blob({
sha1: sha1,
error: `timed out after local hub waited for ${opts.timeout} seconds`
});
w = [];
// this is O(n) instead of O(1), but who cares since n is usually 1.
for (i = 0, len = v.length; i < len; i++) {
x = v[i];
if (x[1] === id) {
x[0](mesg);
} else {
w.push(x);
}
}
if (w.length === 0) {
return delete _save_blob_callbacks[sha1];
} else {
return _save_blob_callbacks[sha1] = w;
}
}
};
return setTimeout(f, opts.timeout * 1000);
};
exports.handle_save_blob_message = function(mesg) {
var i, len, v, x;
winston.debug(`handle_save_blob_message: ${mesg.sha1}`);
v = _save_blob_callbacks[mesg.sha1];
if (v != null) {
for (i = 0, len = v.length; i < len; i++) {
x = v[i];
x[0](mesg);
}
return delete _save_blob_callbacks[mesg.sha1];
}
};
}).call(this);
//# sourceMappingURL=blobs.js.map