X-Git-Url: http://www.aleph1.co.uk/gitweb/?p=yaffs-website;a=blobdiff_plain;f=vendor%2Fjcalderonzumba%2Fgastonjs%2Fsrc%2FClient%2Fpoltergeist.js;fp=vendor%2Fjcalderonzumba%2Fgastonjs%2Fsrc%2FClient%2Fpoltergeist.js;h=4e86d089fe5f51ccfe3879ec5930cb08277d426d;hp=0000000000000000000000000000000000000000;hb=a2bd1bf0c2c1f1a17d188f4dc0726a45494cefae;hpb=57c063afa3f66b07c4bbddc2d6129a96d90f0aad diff --git a/vendor/jcalderonzumba/gastonjs/src/Client/poltergeist.js b/vendor/jcalderonzumba/gastonjs/src/Client/poltergeist.js new file mode 100644 index 000000000..4e86d089f --- /dev/null +++ b/vendor/jcalderonzumba/gastonjs/src/Client/poltergeist.js @@ -0,0 +1,78 @@ +Poltergeist = (function () { + + /** + * The MAIN class of the project + * @param port + * @param width + * @param height + * @param jsErrors + * @constructor + */ + function Poltergeist(port, width, height, jsErrors) { + var self; + this.browser = new Poltergeist.Browser(this, width, height, jsErrors); + + this.commandServer = new Poltergeist.Server(this, port); + this.commandServer.start(); + + self = this; + + phantom.onError = function (message, stack) { + return self.onError(message, stack); + }; + + this.running = false; + } + + /** + * Tries to execute a command send by a client and returns the command response + * or error if something happened + * @param command + * @param serverResponse + * @return {boolean} + */ + Poltergeist.prototype.serverRunCommand = function (command, serverResponse) { + var error; + this.running = true; + try { + return this.browser.serverRunCommand(command, serverResponse); + } catch (_error) { + error = _error; + if (error instanceof Poltergeist.Error) { + return this.serverSendError(error, serverResponse); + } + return this.serverSendError(new Poltergeist.BrowserError(error.toString(), error.stack), serverResponse); + } + }; + + /** + * Sends error back to the client + * @param error + * @param serverResponse + * @return {boolean} + */ + Poltergeist.prototype.serverSendError = function (error, serverResponse) { + var errorObject; + errorObject = { + error: { + name: error.name || 'Generic', + args: error.args && error.args() || [error.toString()] + } + }; + return this.commandServer.sendError(serverResponse, 500, errorObject); + }; + + /** + * Send the response back to the client + * @param response Data to send to the client + * @param serverResponse Phantomjs response object associated to the client request + * @return {boolean} + */ + Poltergeist.prototype.serverSendResponse = function (response, serverResponse) { + return this.commandServer.send(serverResponse, {response: response}); + }; + + return Poltergeist; +})(); + +window.Poltergeist = Poltergeist;