website: ping on interval
This commit is contained in:
parent
f63a0e8650
commit
607ecb22dc
|
@ -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);
|
||||
}
|
||||
|
||||
|
|
|
@ -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(){
|
||||
},
|
||||
});
|
|
@ -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();
|
||||
}
|
||||
},
|
||||
});
|
||||
|
|
|
@ -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();
|
||||
|
||||
|
|
Loading…
Reference in New Issue