remove global
This commit is contained in:
71
pronto.cm
71
pronto.cm
@@ -3,9 +3,6 @@
|
||||
// Based on Douglas Crockford's parseq, adapted for Cell.
|
||||
// Time is in seconds.
|
||||
|
||||
var os = use('os')
|
||||
var $_ = os.$_
|
||||
|
||||
function make_reason(factory, excuse, evidence) {
|
||||
def reason = new Error(`pronto.${factory}${excuse ? ': ' + excuse : ''}`)
|
||||
reason.evidence = evidence
|
||||
@@ -303,69 +300,6 @@ function sequence(requestor_array) {
|
||||
}
|
||||
}
|
||||
|
||||
// time_limit(requestor, seconds)
|
||||
// Wraps a requestor with a time limit.
|
||||
function time_limit(requestor, seconds) {
|
||||
def factory = 'time_limit'
|
||||
if (!is_requestor(requestor))
|
||||
throw make_reason(factory, 'Not a requestor.', requestor)
|
||||
if (typeof seconds != 'number' || seconds <= 0)
|
||||
throw make_reason(factory, 'Bad time limit.', seconds)
|
||||
|
||||
return function time_limit_requestor(callback, value) {
|
||||
check_callback(callback, factory)
|
||||
let finished = false
|
||||
let requestor_cancel = null
|
||||
let timer_cancel = null
|
||||
|
||||
function cancel(reason) {
|
||||
if (finished) return
|
||||
finished = true
|
||||
if (timer_cancel) {
|
||||
timer_cancel()
|
||||
timer_cancel = null
|
||||
}
|
||||
if (requestor_cancel) {
|
||||
try { requestor_cancel(reason) } catch (_) {}
|
||||
requestor_cancel = null
|
||||
}
|
||||
}
|
||||
|
||||
timer_cancel = $_.delay(function() {
|
||||
if (finished) return
|
||||
def reason = make_reason(factory, 'Timeout.', seconds)
|
||||
if (requestor_cancel) {
|
||||
try { requestor_cancel(reason) } catch (_) {}
|
||||
requestor_cancel = null
|
||||
}
|
||||
finished = true
|
||||
callback(null, reason)
|
||||
}, seconds)
|
||||
|
||||
try {
|
||||
requestor_cancel = requestor(function(val, reason) {
|
||||
if (finished) return
|
||||
finished = true
|
||||
if (timer_cancel) {
|
||||
timer_cancel()
|
||||
timer_cancel = null
|
||||
}
|
||||
callback(val, reason)
|
||||
}, value)
|
||||
} catch (ex) {
|
||||
cancel(ex)
|
||||
callback(null, ex)
|
||||
}
|
||||
|
||||
return function(reason) {
|
||||
if (requestor_cancel) {
|
||||
try { requestor_cancel(reason) } catch (_) {}
|
||||
requestor_cancel = null
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// requestorize(unary)
|
||||
// Converts a unary function into a requestor.
|
||||
function requestorize(unary) {
|
||||
@@ -421,7 +355,8 @@ return {
|
||||
parallel,
|
||||
race,
|
||||
sequence,
|
||||
time_limit,
|
||||
requestorize,
|
||||
objectify
|
||||
objectify,
|
||||
is_requestor,
|
||||
check_callback
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user