miniapp-windmill-renderer
Version:
Windmill renderer-side proxy.
4 lines • 15.7 kB
JavaScript
/**
* windmill.renderer.min.js v0.5.1.
*/
console.log("Start windmill renderer.min (0.5.1) framework. Build at 2019-02-21 10:54"),function(e,t){"object"==typeof exports&&"undefined"!=typeof module?module.exports=t():"function"==typeof define&&define.amd?define(t):e["windmill-renderer"]=t()}(this,function(){"use strict";"function"!=typeof Object.assign&&Object.defineProperty(Object,"assign",{writable:!0,enumerable:!1,configurable:!0,value:function(e,t){var n=arguments;if(null==e)throw new TypeError("Cannot convert undefined or null to object");for(var i=Object(e),o=1;o<arguments.length;o++){var r=n[o];if(null!=r)for(var a in r)Object.prototype.hasOwnProperty.call(r,a)&&(i[a]=r[a])}return i}});var t,r=(t=1e5,function(){var e=String(t);return t+=1,e});function a(){return"undefined"!=typeof global?global:"undefined"!=typeof self?self:new Function("return this")()}function u(t,n,e){void 0===e&&(e=!0);var i=a();try{Object.defineProperty(i,t,{value:n,configurable:!0,enumerable:!0,writable:!e})}catch(e){i[t]=n}}function s(e){return Object.prototype.toString.call(e).slice(8,-1)}function p(e){return"Object"===s(e)}function c(){for(var e=[],t=arguments.length;t--;)e[t]=arguments[t]}var n=function(){},g={},i=["debug","info","log","warn","error"];function o(e){if("object"==typeof process&&process&&process.env,!0===g.debugMode||"true"===g.debugMode)return!0;var t=g.logLevel||"error";return i.indexOf(e)>=i.indexOf(t)}function f(e){o("debug")&&n("debug",e)}function h(e){o("warn")&&n("warn",e)}function l(e){o("error")&&n("error",e)}function d(e){l(e)}n=function(e,t){var n,i="["+((n=e).charAt(0).toUpperCase()+n.slice(1))+"][wml-r] "+t;return g.isWeex&&-1<["Android","iOS"].indexOf(g.platform)?console.log(i):console.error(i)};var m={network:["request","uploadFile","downloadFile"],miniApp:["setAppShareInfo","hideLauncherLoading","setWebViewTop","setWebViewBackgroundImage","getConfig","reLaunch"],mtop:["request"],storage:["length","setItem","getItem","removeItem","clearStorage","getStorageInfo",{name:"getItemSync",sync:!0},{name:"setItemSync",sync:!0},{name:"removeItemSync",sync:!0},{name:"clearStorageSync",sync:!0},{name:"getStorageInfoSync",sync:!0}],memoryStorage:["setItem","getItem"],tabBar:["show","hide","setTabBarItem","setTabBarStyle","setTabBarBadge","removeTabBarBadge","showTabBarRedDot","hideTabBarRedDot"],navigator:["push","pop","openMessagePage","reloadPage","getBackStack","popToHome","navigateTo","redirectTo","switchTab","navigateBackMiniProgram","navigateToMiniProgram"],user:["login","logout","info"],modal:["alert","confirm","toast","hideToast","showLoading","hideLoading","prompt"],navigatorBar:["show","hide","setTitle","setRightItem","setStyle","setDrawer","setActionSheet","hasIndexBadge","scaleIndexBadge","resetIndexBadge","getHeight","openDrawer","isDrawerOpened","closeDrawer","setLeftItem","showMenu","hideMenu","getStatusBarHeight","setNavigationBar","hideNavigationBarLoading","showNavigationBarLoading"],clipboard:["writeText","readText"],picker:["pick","pickDate","pickTime","chooseCity"],webSocket:["webSocket","send","close","onOpen","onMessage","onClose","onError"],calendar:["addEvent","checkEvent","removeEvent"],connection:["getType","getDownlinkMax","onChange"],screen:["setAlwaysOn","setBrightness","getScreenBrightness"],broadcast:["createChannel","postMessage","onMessage","close"],share:["doShare"],cookie:["read","write","getAllObjects"],audio:["load","play","pause","stop","setVolume","canPlayType"],wopc:["getSessionKey","setSessionKey","authorize","getSetting","openSetting"],WopcMtopPlugin:["request"],device:["onShake","vibrate","vibrateShort","onUserCaptureScreen","offUserCaptureScreen","scan"],keyboard:["hideKeyboard"],contact:["choosePhoneContact"],location:["getLocation"],alipay:["tradePay"],userTrack:["commit","commitut","commitEvent","customAdvance","pageAppear","pageDisappear","skipPage","updatePageUtparam","updateNextPageUtparam"],"nuvajs-exec":["exec"],aluWVJSBridge:["userIsLogin","refreshAlipayCookie"],TBDeviceInfo:["getUtdid"],actionSheet:["showActionSheet"],image:["chooseImage","previewImage","compressImage","getImageInfo","saveImage"],media:["startVoice","stopVoice"],audioRecord:["startRecord","stopRecord","cancelRecord"],audioPlayer:["playVoice","pauseVoice","stopVoice"],file:["saveFile","getFileInfo","getFileList","removeFile"],video:["chooseVideo","getVideoInfo","saveVideoToPhotosAlbum"],phone:["makePhoneCall"],compass:["startCompass","stopCompass","onCompassChange"],tbsecurity:["secureToken"],sendMtop:["request"],ZCache:["prefetch","fetch"],WVZCache:["prefetch","fetch"],sku:["show","hide"],chat:["open"],ucc:["uccBind","uccTrustLogin","uccUnbind"],address:["choose"],private:["addProperties"],taopai:["openTaopaiShoot","openTaopaiEdit"],poi:["openPOI"]};var v="__windmill_post_message__",y="__windmill_on_message__",e=function(){};e.prototype.$on=function(e,t){return void 0===t&&(t=c),f('$on "'+e+'"'),!e&&d("invalid event name "+e+" for windmill.$on."),t===c&&d("bind event "+e+" with no callback through windmill.$on."),this.bridge.onReceiveMessage({data:{type:e},origin:this.label,type:"Event"},t)},e.prototype.$once=function(e,t){return void 0===t&&(t=c),f('$once "'+e+'"'),!e&&d("invalid event name "+e+" for windmill.$once."),t===c&&d("bind event "+e+" with no callback through windmill.$once."),this.bridge.onReceiveMessage({data:{type:e,once:!0},origin:this.label,type:"Event"},t)},e.prototype.$off=function(e,t){return f('$off "'+e+'"'),!e&&d("invalid event name "+e+" for windmill.$off."),this.bridge.unbindEventListener("Event."+e,t)},e.prototype.$cycle=function(e,t){return void 0===t&&(t=c),f('$cycle "'+e+'"'),e&&/^app:|^page(:|@)/.test(e)||d("invalid lifecycle name "+e+" for windmill.$cycle."),t===c&&d("hook lifecycle "+e+" with no callback function through windmill.$cycle."),"AppWorker"!==this.label&&/^app:/.test(e)&&d("Shouldn't bind lifecycle hook for app in a page client."),this.bridge.onReceiveMessage({type:"Lifecycle",origin:this.label,data:{type:e}},t)},e.prototype.$decycle=function(e,t){return f('$decycle "'+e+'"'),e&&/^app:|^page(:|@)/.test(e)||d("invalid lifecycle name "+e+" for windmill.$decycle."),this.bridge.unbindEventListener("Lifecycle."+e,t)},e.prototype.$emit=function(e,t,n){return void 0===t&&(t={}),f('$emit("'+e+'")'),!e&&d("invalid event name "+e+" for windmill.$emit."),n&&n!==this.label?this.bridge.postMessage({data:{type:e,data:t},origin:this.label,target:n,type:"Event"}):this.bridge.emitEvent(""+e,t)},e.prototype.$callAsync=function(n,i,o,r){var a=this;return void 0===i&&(i={}),void 0===o&&(o={}),void 0===r&&(r={}),f('$callAsync "'+n+'"'),n||d("invalid method name "+n+" for windmill.$callAsync."),new Promise(function(e,t){a.bridge.dispatchMessage({type:"ModuleCall",origin:r.origin||a.label,target:r.target||"AppWorker",data:{method:n,params:i,context:o}},e,t,!1)})},e.prototype.$call=function(e,t,n,i,o,r){return void 0===t&&(t={}),void 0===n&&(n=c),void 0===i&&(i=c),void 0===o&&(o={}),void 0===r&&(r={}),f('$call "'+e+'", params: '+JSON.stringify(t)),e||d("invalid method name "+e+" for windmill.$call."),this.bridge.dispatchMessage({type:"ModuleCall",origin:r.origin||this.label,target:r.target||"AppWorker",data:{method:e,params:t,context:o}},n,i,!0)},e.prototype.$registerModules=function(e){!function(t,o){if(void 0===o&&(o=!1),p(t)){var e=function(n){var e=t[n];m[n]?o||h('The "'+n+'" module has already been registered.It would be overridden.'):m[n]=[];var i=m[n];Array.isArray(e)?e.forEach(function(e){if(p(e))i.push(e);else if("string"==typeof e){var t=e;-1===i.indexOf(t)?i.push(t):o||h('The "'+t+'" of the "'+n+'" module has already been registered.')}}):l('Invalid parameter type! The method list of the "'+n+'" module should be an Array.')};for(var n in t)e(n)}else l("Invalid parameter type! The module definition is not a plain object.")}(e,!0)},e.prototype.$getAvailableModules=function(){return function(e){var t={};try{t=JSON.parse(JSON.stringify(e))}catch(e){}return t}(m)};var b="[[EVENT_MAP]]";function w(e,t){var n=e[b];return Array.isArray(n[t])||(n[t]=[]),n[t]}var M=function(){f("Create new EventEmitter."),Object.defineProperty(this,b,{configurable:!1,enumerable:!1,writable:!1,value:{}})};M.prototype.on=function(e,t){return f('EventEmitter: on("'+e+'")'),w(this,e).push({listener:t,once:!1,callCount:0}),this},M.prototype.once=function(e,t){return f('EventEmitter: once("'+e+'")'),w(this,e).push({listener:t,once:!0,callCount:0}),this},M.prototype.off=function(e,t){f('EventEmitter: off("'+e+'"'+(t?", listener":"")+")");var n=this[b];if(t){var i=n[e];if(Array.isArray(i)){var o=i.findIndex(function(e){return e.listener===t});-1!==o&&i.splice(o,1)}}else delete n[e];return this};var S,k=3;function _(){return(65536*(1+Math.random())|0).toString(16).substring(1)}function I(e,t){windmill.runMessage({cmd:"postMessage",identify:t,data:{message:JSON.stringify(e),pageid:"[Page ID : "+(_()+_())+"-"+_()+"-"+_()+"-"+_()+"-"+_()+_()+_()+"]"}})}var A=!(M.prototype.emit=function(i){for(var e=[],t=arguments.length-1;0<t--;)e[t]=arguments[t+1];f('EventEmitter: emit("'+i+'", '+JSON.stringify(e)+")");var n=this[b],o=n[i];return Array.isArray(o)&&(n[i]=o.filter(function(t){try{t.listener.apply(null,e),t.callCount+=1}catch(e){throw l('Failed to emit the "'+i+'" event: '+e.toString()+"\nSource code: "+("function"==typeof(n=t.listener)?Function.prototype.toString.call(n):"")),e}var n;return!t.once})),this});function $(t){return S||(S={postMessage:function(e){return I(e,t)},onmessage:function(e){var t;if(!A)return A=!0,t=e,void(onmessage=function(e){t(e)})}}),S}var E=function(){this._messages=[]};E.prototype.postMessage=function(e){f("cache message in lazyMsgModule. "+JSON.stringify(e)),this._messages.push(e)},E.prototype.replaceWith=function(e){O=!1,this._messageModule=e,this._flush()},E.prototype._flush=function(){var t=this;this._messages.forEach(function(e){return t._messageModule.postMessage(e)})};var O,T,C=new E;function x(e,t,n){var i,o=e?(i=e).requireModule("@weex/windmill")||i.requireModule("windmill"):function(){var e=a();if("function"!=typeof e[v])return 0<k&&(k--,l("Can't find available \""+v+'" API in the context.')),!1;var t={};return t.postMessage=e[v],t.onmessage=function(e){f('Register the "'+y+'" API.'),u(y,e)},t}()||$(n);if(!o)return d("can't find message module ("+(e?"windmill module":"message module in webview")+")."),O=!0,C;if("function"==typeof t){f('Set the "onmessage" method to the bridge through callback.');try{o.onmessage(t)}catch(e){l('Failed to set the "onmessage" method to the bridge through callback.'),l(e.toString())}}return O&&C.replaceWith(o),o}function P(e){var t=e.match(/([^@]+)(@\S+)/);return t&&t[1]||e}var B=function(e,t){var a=this;void 0===t&&(t={}),f("Create a new RendererBridge."),this.identify=t.identify,f("[multi-app] The current worker identify is "+this.identify),T=t.isWeex,this._emitter=new M,f("Register onmessage api for "+(T?"Weex windmill module":"webview")+"."),T&&(this._weex=e),this._onMessage=function(e){if(f("Receive message from the worker. (message: "+JSON.stringify(e)+")"),!p(e)&&(l("The parameter of onmessage should be a plain object! Not "+s(e)),"string"==typeof e))try{e=JSON.parse(e)}catch(e){return void l("Failed to parse the message string. "+e.toString())}var t=e.type,n=e.data;void 0===n&&(n={});var i=e.origin,o=e.callbackId;switch(t){case"Callback":a._emitter.emit("callback@AppWorker:"+o,n);break;case"Event":var r=P(n.type);a._emitter.emit(t+"."+r,Object.assign({origin:i},n,{type:r}));break;case"Lifecycle":a._emitter.emit(t+"."+n.lifecycle,n.options);break;case"SetData":a._emitter.emit("Event.setdata",n);break;default:d('Unknown message type received from worker: "'+t+'"')}},x(e,this._onMessage,this.identify)};B.prototype.dispatchMessage=function(t,n,i,e){f("Dispatch message to worker. (message: "+JSON.stringify(t)+")"),t.callbackId=this._genCallbackId(function(e){return e.status&&"SUCCESS"===e.status?n(e):(e.status,l("call API "+t.data.method+" from renderer failed: "+e.status+", "+e.error),i(e))},{keepAlive:e});try{t.keepAlive=!0,this.postMessage(t)}catch(e){i(e)}},B.prototype.postMessage=function(e){try{f("Call postMessage to worker. (message: "+JSON.stringify(e)+")"),x(this._weex,this._onMessage,this.identify).postMessage(e)}catch(e){d(e.message||e.toString()||"postMessage failed.")}},B.prototype._genCallbackId=function(e,t){var n=t.keepAlive,i=t.callbackId,o=i||r();return this._emitter[n?"on":"once"]("callback@AppWorker:"+o,e),o},B.prototype.onReceiveMessage=function(e,t){var n=e.type,i=e.data;void 0===i&&(i={});var o=i.type;"Event"===n&&(o=P(o),i.type=o);var r=i.once?"once":"on";this._emitter[r](n+"."+o,t)},B.prototype.unbindEventListener=function(e,t){this._emitter.off(P(e),t)},B.prototype.emitEvent=function(e,t){void 0===t&&(t={});var n=P(e);this._emitter.emit("Event."+n,{type:n,data:t})};var R=function(n){function e(e,t){void 0===t&&(t={}),n.call(this),this._name=t.pageName||"",this.label=t.clientId||"",e&&p(e.config)&&(this._name=e.config.pageName||"",this.label=e.config.clientId||"",e.config.availableModules?(this.$registerModules(e.config.availableModules),this._gotAvailableModulesFromWeex=!0):h("No available modules in weex options.")),f("Create a new RendererRuntime. ("+this.label+", "+this._name+")"),this.bridge=new B(e,t),this._init()}return n&&(e.__proto__=n),((e.prototype=Object.create(n&&n.prototype)).constructor=e).prototype._init=function(){var t=this;this.bridge.onReceiveMessage({origin:"AppWorker",target:this.label,type:"Event",data:{type:"registerModules"}},function(e){e&&"registerModules"===e.type&&(f("Register available modules in renderer. ("+Object.keys(e.data)+")"),t.$registerModules(e.data))})},e.prototype.ready=function(){this.bridge.dispatchMessage({origin:this.label,target:"AppWorker",type:"Event",data:{type:"[[RendererRuntimeReady]]",data:{label:this.label,handshake:!this._gotAvailableModulesFromWeex}}},c,c)},e.prototype.$getCurrentActivePageId=function(){return this.label},e}(e),W=["$on","$once","$off","$emit","$cycle","$decycle","$call","$callAsync","$navTo","$getAvailableModules","$getCurrentActivePageId"];return function(e,t){var n=!(!e||!e.requireModule);!function(e){if(!e||!e.platform)return console.error("set logger environment failed.");Object.assign(g,e)}({platform:n&&e.config.env.platform,debugMode:n&&e.config.env.debugMode,isWeex:n}),f("Setup Windmill renderer framework."),e&&!e.requireModule&&(t=e,e=void 0),t||(t={}),f("Instantiate renderer in "+(((t=Object.assign({},t)).isWeex=n)?"Weex container":"WindVane container")+".");var i,o,r,a,s,c,l=new R(e,t),d=(i=l,o={},W.forEach(function(n){"function"==typeof i[n]&&(o[n]=function(){for(var e=[],t=arguments.length;t--;)e[t]=arguments[t];return i[n].apply(i,e)})}),o);return f("Prepare runtime APIs: "+JSON.stringify(W)),t.exposeModuleGetter&&u("__WINDMILL_MODULE_GETTER__",(r=function(){return d},s=a||function(){for(var e,t=[],n=arguments.length;n--;)t[n]=arguments[n];return(e=r()).$call.apply(e,t)},c=a||function(){for(var e,t=[],n=arguments.length;n--;)t[n]=arguments[n];return(e=r()).$callSync.apply(e,t)},function(o){if(!o)return f("Require a universal module proxy."),{call:s};var e=m[o];if(Array.isArray(e)){f('Require the "'+o+'" module.');var t={};return e.forEach(function(n){if("string"==typeof n)t[n]=function(){for(var e=[],t=arguments.length;t--;)e[t]=arguments[t];return s.apply(void 0,[o+"."+n].concat(e))};else if(p(n)&&n.name){var i=n.name;n.sync?t[i]=function(){for(var e=[],t=arguments.length;t--;)e[t]=arguments[t];return c.apply(void 0,[o+"."+i].concat(e))}:t[i]=function(){for(var e=[],t=arguments.length;t--;)e[t]=arguments[t];return s.apply(void 0,[o+"."+i].concat(e))}}}),t}h('Require unknown module "'+o+"\".It may not exist or haven't been registered.")})),l.ready(),d}});