| Server IP : 68.183.124.220 / Your IP : 216.73.217.137 Web Server : Apache/2.4.18 (Ubuntu) System : Linux Sandbox-A 4.4.0-210-generic #242-Ubuntu SMP Fri Apr 16 09:57:56 UTC 2021 x86_64 User : gavin ( 1000) PHP Version : 7.0.33-0ubuntu0.16.04.16 Disable Function : pcntl_alarm,pcntl_fork,pcntl_waitpid,pcntl_wait,pcntl_wifexited,pcntl_wifstopped,pcntl_wifsignaled,pcntl_wifcontinued,pcntl_wexitstatus,pcntl_wtermsig,pcntl_wstopsig,pcntl_signal,pcntl_signal_dispatch,pcntl_get_last_error,pcntl_strerror,pcntl_sigprocmask,pcntl_sigwaitinfo,pcntl_sigtimedwait,pcntl_exec,pcntl_getpriority,pcntl_setpriority, MySQL : OFF | cURL : ON | WGET : ON | Perl : ON | Python : ON | Sudo : ON | Pkexec : ON Directory : /home/gavin/workspace/happymandarin/node_modules/when/ |
Upload File : |
/** @license MIT License (c) copyright 2012-2013 original author or authors */
/**
* poll.js
*
* Helper that polls until cancelled or for a condition to become true.
*
* @author Scott Andrews
*/
(function (define) { 'use strict';
define(function(require) {
var when = require('./when');
var attempt = when['try'];
var cancelable = require('./cancelable');
/**
* Periodically execute the task function on the msec delay. The result of
* the task may be verified by watching for a condition to become true. The
* returned deferred is cancellable if the polling needs to be cancelled
* externally before reaching a resolved state.
*
* The next vote is scheduled after the results of the current vote are
* verified and rejected.
*
* Polling may be terminated by the verifier returning a truthy value,
* invoking cancel() on the returned promise, or the task function returning
* a rejected promise.
*
* Usage:
*
* var count = 0;
* function doSomething() { return count++ }
*
* // poll until cancelled
* var p = poll(doSomething, 1000);
* ...
* p.cancel();
*
* // poll until condition is met
* poll(doSomething, 1000, function(result) { return result > 10 })
* .then(function(result) { assert result == 10 });
*
* // delay first vote
* poll(doSomething, 1000, anyFunc, true);
*
* @param task {Function} function that is executed after every timeout
* @param interval {number|Function} timeout in milliseconds
* @param [verifier] {Function} function to evaluate the result of the vote.
* May return a {Promise} or a {Boolean}. Rejecting the promise or a
* falsey value will schedule the next vote.
* @param [delayInitialTask] {boolean} if truthy, the first vote is scheduled
* instead of immediate
*
* @returns {Promise}
*/
return function poll(task, interval, verifier, delayInitialTask) {
var deferred, canceled, reject;
canceled = false;
deferred = cancelable(when.defer(), function () { canceled = true; });
reject = deferred.reject;
verifier = verifier || function () { return false; };
if (typeof interval !== 'function') {
interval = (function (interval) {
return function () { return when().delay(interval); };
})(interval);
}
function certify(result) {
deferred.resolve(result);
}
function schedule(result) {
attempt(interval).then(vote, reject);
if (result !== void 0) {
deferred.notify(result);
}
}
function vote() {
if (canceled) { return; }
when(task(),
function (result) {
when(verifier(result),
function (verification) {
return verification ? certify(result) : schedule(result);
},
function () { schedule(result); }
);
},
reject
);
}
if (delayInitialTask) {
schedule();
} else {
// if task() is blocking, vote will also block
vote();
}
// make the promise cancelable
deferred.promise = Object.create(deferred.promise);
deferred.promise.cancel = deferred.cancel;
return deferred.promise;
};
});
})(typeof define === 'function' && define.amd ? define : function (factory) { module.exports = factory(require); });