-
Notifications
You must be signed in to change notification settings - Fork 112
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
migrate session_contexts.coffee to plain js (#1740)
* migrate session_contexts.coffee to plain js * add a newline
- Loading branch information
Showing
4 changed files
with
114 additions
and
84 deletions.
There are no files selected for viewing
3 changes: 0 additions & 3 deletions
3
apps/dashboard/app/assets/javascripts/batch_connect/session_contexts.coffee
This file was deleted.
Oops, something went wrong.
80 changes: 0 additions & 80 deletions
80
apps/dashboard/app/assets/javascripts/batch_connect/sessions.coffee
This file was deleted.
Oops, something went wrong.
111 changes: 111 additions & 0 deletions
111
apps/dashboard/app/javascript/packs/batch_connect_sessions.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,111 @@ | ||
'use strict'; | ||
|
||
const pollers = []; | ||
|
||
class Timer { | ||
constructor(callback, delay){ | ||
this.delay = delay; | ||
this.remaining = delay; | ||
this.active = true; | ||
this.callback = callback; | ||
|
||
this.resume(); | ||
} | ||
|
||
resume() { | ||
if(!this.active) { return; } | ||
|
||
this.start = new Date(); | ||
this.clearTO(); | ||
this.timerId = setTimeout(this.callback, this.remaining); | ||
} | ||
|
||
restart() { | ||
if(!this.active) { return; } | ||
|
||
this.remaining = this.delay; | ||
resume(); | ||
} | ||
|
||
pause() { | ||
if(!this.active) { return; } | ||
|
||
this.clearTO(); | ||
this.remaining -= new Date() - this.start; | ||
} | ||
|
||
stop(){ | ||
if(!this.active) { return; } | ||
this.clearTO(); | ||
this.active = false; | ||
} | ||
|
||
clearTO(){ | ||
if(this.timerId !== undefined) { | ||
clearTimeout(this.timerId); | ||
} | ||
} | ||
|
||
} | ||
|
||
class Poller { | ||
constructor(url, delay) { | ||
this.url = url; | ||
this.delay = delay; | ||
this.poll(); | ||
} | ||
|
||
poll(){ | ||
this.timer = new Timer(this.request.bind(this), this.delay); | ||
} | ||
|
||
pause() { | ||
this.timer.pause(); | ||
} | ||
|
||
resume() { | ||
this.timer.resume(); | ||
} | ||
|
||
request(){ | ||
const that = this; | ||
$.getScript(this.url) | ||
.done((_script, _textStatus, _jqxhr) => { | ||
return; | ||
}).fail((_jqxhr, textStatus, errorThrown) => { | ||
if(textStatus) { console.log(`Failed to get session data. Server returned '${textStatus}'`); } | ||
if(errorThrown) { | ||
console.log("Failed to get session data because of error."); | ||
console.log(errorThrown); | ||
} | ||
return; | ||
}).always(() => { | ||
that.poll(); | ||
return; | ||
}); | ||
} | ||
} | ||
|
||
function makePollers(){ | ||
const obj = $('#batch_connect_sessions'); | ||
if(!obj) return; | ||
|
||
const url = obj.data('url'); | ||
const delay = obj.data('delay'); | ||
if(url && delay) { pollers.push(new Poller(url, delay)); } | ||
|
||
$(document) | ||
.on('show.bs.modal', () => { | ||
pollers.forEach((poller) => { | ||
poller.pause(); | ||
}); | ||
}).on('hidden.bs.modal', () => { | ||
pollers.forEach((poller) => { | ||
poller.resume(); | ||
}); | ||
}); | ||
} | ||
|
||
jQuery(function() { | ||
makePollers(); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters