Skip to content

Commit

Permalink
feat(ping): removed timer
Browse files Browse the repository at this point in the history
  • Loading branch information
JaimeBeneytez committed Dec 15, 2015
1 parent bd45f02 commit a2179c7
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 18 deletions.
1 change: 0 additions & 1 deletion src/lib/ngAutobahn/session/session.js
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,6 @@

function _setSession(session) {
_session = session;
window.ngAutobahn.currentSession = session;
}

function _subscribeBroker(broker) {
Expand Down
40 changes: 23 additions & 17 deletions src/lib/ngAutobahn/utils/ping/ping.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,42 +42,48 @@
'$timeout',
'$interval',
function NgAutobahnPingFactory($timeout, $interval) {

return NgAutobahnPing;

function NgAutobahnPing(pingFn, errorFn) {
var self = this,
_timeout,
var _isPingPromiseResolved = true,
_interval;

self.start = start;
self.stop = stop;
this.start = start;
this.stop = stop;

function start() {
stop();
sendPingMessage();
_interval = $interval(sendPingMessage, config.delay, 0, false);
_interval = $interval(_intervalHandler, config.maxResponseDelay, 0, false);
}

function _intervalHandler() {
if (_isPingPromiseResolved) {
_invokePingFn();
} else {
_clearInterval();
_invokeErrorFn();
}
}

function stop() {
clearTimeout();
clearInterval();
_clearInterval();
_isPingPromiseResolved = true;
}

function sendPingMessage() {
_timeout = $timeout(_maxResponseDelayReachedHandler, config.maxResponseDelay);
pingFn().then(clearTimeout);
function _invokePingFn() {
_isPingPromiseResolved = false;
pingFn().then(_pingFunctionResolvedHandler);
}

function _maxResponseDelayReachedHandler() {
stop();
function _invokeErrorFn() {
errorFn();
}

function clearTimeout() {
$timeout.cancel(_timeout);
function _pingFunctionResolvedHandler() {
_isPingPromiseResolved = true;
}

function clearInterval() {
function _clearInterval() {
$interval.cancel(_interval);
}
}
Expand Down

0 comments on commit a2179c7

Please sign in to comment.