woodpecker/server/app/scripts/commit_updates.js

99 lines
2.1 KiB
JavaScript
Raw Normal View History

;// Live commit updates
if(typeof(Drone) === 'undefined') { Drone = {}; }
(function () {
Drone.CommitUpdates = function(socket) {
if(typeof(socket) === "string") {
var url = [(window.location.protocol == 'https:' ? 'wss' : 'ws'),
'://',
window.location.host,
socket].join('')
this.socket = new WebSocket(url);
} else {
this.socket = socket;
}
this.lineFormatter = new Drone.LineFormatter();
this.attach();
}
Drone.CommitUpdates.prototype = {
lineBuffer: "",
autoFollow: false,
startOutput: function(el) {
if(typeof(el) === 'string') {
this.el = document.getElementById(el);
} else {
this.el = el;
}
2014-03-02 07:08:30 +00:00
if(!this.reqId) {
this.updateScreen();
}
},
stopOutput: function() {
this.stoppingRefresh = true;
},
attach: function() {
2014-03-02 07:08:30 +00:00
this.socket.onopen = this.onOpen;
this.socket.onerror = this.onError;
this.socket.onmessage = this.onMessage.bind(this);
2014-03-02 07:08:30 +00:00
this.socket.onclose = this.onClose;
},
updateScreen: function() {
if(this.lineBuffer.length > 0) {
this.el.innerHTML += this.lineBuffer;
this.lineBuffer = '';
if (this.autoFollow) {
window.scrollTo(0, document.body.scrollHeight);
}
}
2014-03-02 07:08:30 +00:00
if(this.stoppingRefresh) {
this.stoppingRefresh = false;
} else {
window.requestAnimationFrame(this.updateScreen.bind(this));
}
},
onOpen: function() {
console.log('output websocket open');
},
onError: function(e) {
console.log('websocket error: ' + e);
},
onMessage: function(e) {
this.lineBuffer += this.lineFormatter.format(e.data);
},
onClose: function(e) {
console.log('output websocket closed: ' + JSON.stringify(e));
window.location.reload();
}
};
// Polyfill rAF for older browsers
window.requestAnimationFrame = window.requestAnimationFrame ||
window.webkitRequestAnimationFrame ||
function(callback, element) {
return window.setTimeout(function() {
callback(+new Date());
}, 1000 / 60);
};
2014-03-02 07:08:30 +00:00
window.cancelRequestAnimationFrame = window.cancelRequestAnimationFrame ||
window.cancelWebkitRequestAnimationFrame ||
function(fn) {
window.clearTimeout(fn);
};
})();