website: ping on interval

This commit is contained in:
Jack Pearkes 2015-04-27 17:59:39 -07:00
parent f63a0e8650
commit 607ecb22dc
4 changed files with 93 additions and 5 deletions

View File

@ -16,14 +16,17 @@ Demo.DemoController = Ember.ObjectController.extend({
var data = JSON.parse(message.data),
controller = this;
// ignore pongs
if (data.pong) {
return
}
// Add the item
if (data.stdout !== "") {
console.log("stdout:", data.stout);
controller.appendLog(data.stdout, false);
}
if (data.stderr !== "") {
console.log("stderr:", data.stderr);
controller.appendLog(data.stderr, false);
}

View File

@ -0,0 +1,63 @@
Ember.Clock = Ember.Object.extend({
second: null,
checks: 0,
isPolling: false,
pollImmediately: true,
pollInterval: null,
tickInterval: null,
defaultPollInterval: 10000,
defaultTickInterval: 1000,
init: function () {
this.scheduleTick();
this.pollImmediately && this.startPolling();
},
computedPollInterval: function() {
return this.pollInterval || this.defaultPollInterval; // Time between polls (in ms)
}.property().readOnly(),
computedTickInterval: function() {
return this.tickInterval || this.defaultTickInterval; // Time between ticks (in ms)
}.property().readOnly(),
// Schedules the function `f` to be executed every `interval` time.
schedulePoll: function(f) {
return Ember.run.later(this, function() {
f.apply(this);
this.incrementProperty('checks')
this.set('timer', this.schedulePoll(f));
}, this.get('computedPollInterval'));
},
scheduleTick: function () {
return Ember.run.later(this, function () {
this.tick();
this.scheduleTick();
}, this.get('computedTickInterval'));
},
// Stops the polling
stopPolling: function() {
this.set('isPolling', false);
Ember.run.cancel(this.get('timer'));
},
// Starts the polling, i.e. executes the `onPoll` function every interval.
startPolling: function() {
this.set('isPolling', true);
this.set('timer', this.schedulePoll(this.get('onPoll')));
},
// Moves the clock
tick: function () {
var now = new Date();
this.setProperties({
second: now.getSeconds()
});
},
// Override this while making a new Clock object.
onPoll: function(){
},
});

View File

@ -1,5 +1,29 @@
Demo.DemoStepRoute = Ember.Route.extend({
model: function(params) {
return this.store.find('step', params.id);
}
},
afterModel: function(model) {
var clock = Ember.Clock.create({
defaultPollInterval: 5000,
pollImmediately: false,
onPoll: function() {
var socket = this.controllerFor('demo').get('socket');
socket.send(JSON.stringify({type: "ping"}));
}.bind(this)
});
this.set('clock', clock);
},
activate: function() {
this.get('clock').startPolling();
},
deactivate: function() {
var clock = this.get('clock');
if(clock.get('isPolling')) {
clock.stopPolling();
}
},
});

View File

@ -55,8 +55,6 @@ Demo.DemoStepView = Ember.View.extend({
submitted: function() {
var element = this.$();
console.log("submitted");
// Focus the input
element.find('input.shell')[0].focus();