-
-
Notifications
You must be signed in to change notification settings - Fork 3
/
liveview.min.js
1 lines (1 loc) · 8.3 KB
/
liveview.min.js
1
!function(globalScope){function Process(){if(!(this instanceof Process))return new Process;this.title="titanium";this.version="";this.moduleLoadList=[];this.versions={};this.arch=Ti.Platform.architecture;this.platform=Ti.Platform.osname;this.hardware=(""+Ti.Platform.model).replace("google_")}Process.prototype.__proto__=Emitter.prototype;function Emitter(obj){if(obj){return mixin(obj)}}function mixin(obj){for(var key in Emitter.prototype){obj[key]=Emitter.prototype[key]}return obj}Emitter.prototype.on=function(event,fn){this._callbacks=this._callbacks||{};(this._callbacks[event]=this._callbacks[event]||[]).push(fn);return this};Emitter.prototype.once=function(event,fn){var self=this;this._callbacks=this._callbacks||{};function on(){self.off(event,on);fn.apply(this,arguments)}fn._off=on;this.on(event,on);return this};Emitter.prototype.off=function(event,fn){this._callbacks=this._callbacks||{};var callbacks=this._callbacks[event];if(!callbacks){return this}if(1==arguments.length){delete this._callbacks[event];return this}var i=callbacks.indexOf(fn._off||fn);if(~i){callbacks.splice(i,1)}return this};Emitter.prototype.emit=function(event){this._callbacks=this._callbacks||{};var args=[].slice.call(arguments,1);var callbacks=this._callbacks[event];if(callbacks){callbacks=callbacks.slice(0);for(var i=0,len=callbacks.length;i<len;++i){callbacks[i].apply(this,args)}}return this};Emitter.prototype.listeners=function(event){this._callbacks=this._callbacks||{};return this._callbacks[event]||[]};Emitter.prototype.hasListeners=function(event){return!!this.listeners(event).length};if(typeof module!=="undefined"){module.exports=Socket}function Socket(opts){if(!(this instanceof Socket)){return new Socket(opts)}opts=opts||{};this.timeout=5e3;this.host=opts.host;this.port=opts.port;this.retry=opts.retry;this.bytesRead=0;this.bytesWritten=0;this.ignore=[]}Socket.prototype.__proto__=Emitter.prototype;Socket.prototype.connect=function(opts,fn){var self=this;opts=opts||{};var reConnect=!!opts.reConnect;if("function"==typeof opts){fn=opts;opts={}}self.host=opts.host||self.host||"127.0.0.1";self.port=opts.port||self.port;self.retry=opts.retry||self.retry;this._proxy=Ti.Network.Socket.createTCP({host:self.host,port:self.port,connected:function(e){self.connected=true;self._connection=e.socket;fn&&fn(e);self.emit(reConnect?"reconnect":"connect",e);Ti.Stream.pump(e.socket,function(e){if(e.bytesProcessed<0){self.close(true);return}self.emit("data",""+e.buffer)},1024,true)},error:function(e){var err={code:e.errorCode,error:e.error};if(!~self.ignore.indexOf(err.code)){return self.emit("error",err)}self.emit("error ignored",err)}});this._proxy.connect()};Socket.prototype.close=function(serverEnded){var self=this;self.connected=false;self.closing=!serverEnded;if(self.closing){self.write(function(){self._proxy.close();self.emit("close")});return}var retry=~~self.retry;self.emit("end");if(!retry){return}setTimeout(function(){self.emit("reconnecting");self.connect({reConnect:true})},retry)};Socket.prototype.write=function(data,fn){if("function"==typeof data){fn=data;data=null}data=data?""+data:"";var msg=Ti.createBuffer({value:data});var callback=fn||function(){};Ti.Stream.write(this._connection,msg,function(){callback([].slice(arguments))})};Socket.prototype.setKeepAlive=function(enable,initialDelay){var self=this;if(!enable){self._keepAlive&&clearInterval(self._keepAlive);self._keepAlive=null;return}self._keepAlive=setInterval(function(){self.write("ping")},initialDelay||3e5)};function Module(id){this.filename=id+".js";this.id=id;this.exports={};this.loaded=false}var global=Module._global=Module.global={};var process=global.process=Process();global.ENV="liveview";global.logging=false;global.CATCH_ERRORS=true;Module._cache={};Module._requireNative=function(){throw new Error("Module.patch must be run first")};Module._includeNative=function(){throw new Error("Module.patch must be run first")};Module.patch=function(globalCtx,url,port){var defaultURL=process.platform==="android"&&process.hardware==="sdk"?"10.0.2.2":"FSERVER_HOST";Module._url=url||defaultURL;Module._port=port||8324;Module._requireNative=globalCtx.require;Module.evtServer&&Module.evtServer.close();Module.connectServer();Module.require("app")};Module.global.reload=function(){try{Module.evtServer.close();Module=null;console.log("[LiveView] Reloading App");Ti.App._restart()}catch(e){console.log("[LiveView] Reloading App via Legacy Method");Module.require("app")}};Module.connectServer=function(){var retryInterval=null;var client=Module.evtServer=new Socket({host:"TCP_HOST",port:8323},function(){console.log("[LiveView]","Connected to Event Server")});client.on("close",function(){console.log("[LiveView]","Closed Previous Event Server client")});client.on("connect",function(){if(retryInterval){clearInterval(retryInterval);console.log("[LiveView]","Reconnected to Event Server")}});client.on("data",function(data){if(!data){return}try{var evt=JSON.parse(""+data);if(evt.type==="event"&&evt.name==="reload"){Module._cache=[];Module.global.reload()}}catch(e){}});client.on("end",function(){console.error("[LiveView]","Disconnected from Event Server");retryInterval=setInterval(function(){console.log("[LiveView]","Attempting reconnect to Event Server");client.connect()},2e3)});client.on("error",function(e){var err=e.error;var code=~~e.code;if(code===61){err="Event Server unavailable. Connection Refused @ "+Module._url+":"+Module._port+"\n[LiveView] Please ensure your device and computer are on the same network and the port is not blocked."}console.error("[LiveView] "+err)});client.connect();Module.require("app")};Module.include=function(ctx,id){var file=id.replace(".js","");var src=Module.prototype._getRemoteSource(file,1e3);eval.call(ctx,src)};Module.require=function(id){var fullPath=id;var cached=Module.getCached(fullPath);if(!!cached){return cached.exports}var freshModule=new Module(fullPath);freshModule.cache();freshModule._compile();while(!freshModule.loaded){}return freshModule.exports};Module.getCached=function(id){return Module._cache[id]};Module.exists=function(id){var path=Ti.Filesystem.resourcesDirectory+id+".js";var file=Ti.Filesystem.getFile(path);return file.exists()};Module.prototype._getRemoteSource=function(file,timeout){var expireTime=(new Date).getTime()+timeout;var request=Ti.Network.createHTTPClient();var rsp=null;var file="http://"+Module._url+":"+Module._port+"/"+(file||this.id)+".js";request.cache=false;request.open("GET",file);request.setRequestHeader("x-platform",process.platform);request.send();while(!rsp){if(request.readyState===4){if(request.status!==200){rsp=true;return null}rsp=request.responseText}else if(expireTime-(new Date).getTime()<=0){rsp=true;throw new Error("[LiveView] File Server unavailable. Host Unreachable @ "+Module._url+":"+Module._port+"\n[LiveView] Please ensure your device and computer are on the same network and the port is not blocked.")}}return rsp};Module.prototype._getSource=function(){var id=this.id;var isRemote=/^(http|https)$/.test(id)||global.ENV==="liveview";if(isRemote){return this._getRemoteSource(null,1e3)}else{if(id==="app"){id="_app"}var file=Ti.Filesystem.getFile(Ti.Filesystem.resourcesDirectory,id+".js");return(file.read()||{}).text}};Module._wrap=function(source){source=source.replace(/T[i||itanium]+.include\([\'|\"]([^\"\'\r\n$]*)[\'|\"]\)/g,"Module.include(this,'$1')");return global.CATCH_ERRORS?Module._errWrapper[0]+source+Module._errWrapper[1]:source};Module._errWrapper=["try {",'} catch (err) { lvGlobal.process.emit("uncaughtException", {module: __filename, error: err})}'];Module.prototype._compile=function(){var src=this._getSource();if(!src){this.exports=Module._requireNative(this.id);this.loaded=true;return}var source=Module._wrap(src);try{var fn=Function("exports, require, module, __filename, __dirname, lvGlobal",source);fn(this.exports,Module.require,this,this.filename,this.__dirname,global)}catch(err){process.emit("uncaughtException",{module:this.filename,error:err})}this.loaded=true};Module.prototype.cache=function(){this.timestamp=(new Date).getTime();Module._cache[this.id]=this};process.on("uncaughtException",function(err){console.log("[LiveView] Error Evaluating",err.module,"@ Line:",err.error.line);console.error(""+err.error);console.error("File:",err.module);console.error("Line:",err.error.line);console.error("SourceId:",err.error.sourceId);console.error("Backtrace:\n",(""+err.error.backtrace).replace(/'\n'/g,"\n"))});Module.patch(globalScope)}(this);