Contact
CoCalc Logo Icon
StoreFeaturesDocsShareSupport News AboutSign UpSign In
| Download
Views: 39536
1
###
2
Saving blobs to hub
3
4
CoCalc: Collaborative web-based SageMath, Jupyter, LaTeX and Terminals.
5
Copyright 2015, SageMath, Inc., GPL v3.
6
###
7
8
misc = require('smc-util/misc')
9
message = require('smc-util/message')
10
winston = require('winston')
11
12
{defaults, required} = misc
13
14
_save_blob_callbacks = {}
15
exports.receive_save_blob_message = (opts) -> # temporarily used by file_session_manager
16
opts = defaults opts,
17
sha1 : required
18
cb : required
19
timeout : 30 # maximum time in seconds to wait for response message
20
winston.debug("receive_save_blob_message: #{opts.sha1}")
21
sha1 = opts.sha1
22
id = misc.uuid()
23
if not _save_blob_callbacks[sha1]?
24
_save_blob_callbacks[sha1] = [[opts.cb, id]]
25
else
26
_save_blob_callbacks[sha1].push([opts.cb, id])
27
28
# Timeout functionality -- send a response after opts.timeout seconds,
29
# in case no hub responded.
30
if not opts.timeout
31
return
32
f = () ->
33
v = _save_blob_callbacks[sha1]
34
if v?
35
mesg = message.save_blob
36
sha1 : sha1
37
error : "timed out after local hub waited for #{opts.timeout} seconds"
38
39
w = []
40
for x in v # this is O(n) instead of O(1), but who cares since n is usually 1.
41
if x[1] == id
42
x[0](mesg)
43
else
44
w.push(x)
45
46
if w.length == 0
47
delete _save_blob_callbacks[sha1]
48
else
49
_save_blob_callbacks[sha1] = w
50
51
setTimeout(f, opts.timeout*1000)
52
53
exports.handle_save_blob_message = (mesg) ->
54
winston.debug("handle_save_blob_message: #{mesg.sha1}")
55
v = _save_blob_callbacks[mesg.sha1]
56
if v?
57
for x in v
58
x[0](mesg)
59
delete _save_blob_callbacks[mesg.sha1]
60