overleaf/services/web/public/js/ace/test/asyncjs/utils.js

66 lines
1.5 KiB
JavaScript
Raw Normal View History

2014-02-12 05:23:40 -05:00
/*!
* async.js
* Copyright(c) 2010 Fabian Jakobs <fabian.jakobs@web.de>
* MIT Licensed
*/
define(function(require, exports, module) {
var async = require("asyncjs/async")
async.plugin({
delay: function(delay) {
return this.each(function(item, next) {
setTimeout(function() {
next();
}, delay)
})
},
timeout: function(timeout) {
timeout = timeout || 0
var source = this.source
this.next = function(callback) {
var called
var id = setTimeout(function() {
called = true
callback("Source did not respond after " + timeout + "ms!")
}, timeout)
source.next(function(err, value) {
if (called)
return
called = true
clearTimeout(id)
callback(err, value)
})
}
return new this.constructor(this)
},
get: function(key) {
return this.map(function(value, next) {
next(null, value[key])
})
},
inspect: function() {
return this.each(function(item, next) {
console.log(JSON.stringify(item))
next()
})
},
print: function() {
return this.each(function(item, next) {
console.log(item)
next()
})
}
})
})