forked from foobarfighter/foounit
/
polling_block.js
34 lines (30 loc) · 909 Bytes
/
polling_block.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
foounit.PollingBlock = function (func, timeout){
this._func = func;
this._tasks = [];
this._timeout = timeout;
this._exception = undefined;
this._pollInterval = 50;
};
foounit.mixin(foounit.PollingBlock.prototype, foounit.BlockQueue.prototype);
foounit.mixin(foounit.PollingBlock.prototype, {
getTimeout: function (){ return this._timeout; }
, run: function (){
var self = this
, start = foounit.getTime()
, interval;
interval = foounit.setInterval(function (){
try {
self._func.apply({}, []);
foounit.clearInterval(interval);
self.onComplete(self);
} catch (e){
if (foounit.getTime() - start >= self._timeout){
foounit.clearInterval(interval);
e.message = 'waitFor timeout: ' + e.message;
self._exception = e;
self.onFailure(self);
}
}
}, this._pollInterval);
}
});