react-native-webview-comlink
Version:
Add JavaScript interface for react-native-webview, based on Comlink
1 lines • 5.88 kB
JavaScript
!function(){"use strict";"function"!=typeof Object.assign&&Object.defineProperty(Object,"assign",{value:function(e,t){if(null==e)throw new TypeError("Cannot convert undefined or null to object");for(var n=Object(e),i=1;i<arguments.length;i++){var r=arguments[i];if(null!=r)for(var o in r)Object.prototype.hasOwnProperty.call(r,o)&&(n[o]=r[o])}return n},writable:!0,configurable:!0}),String.prototype.startsWith||Object.defineProperty(String.prototype,"startsWith",{value:function(e,t){var n=t>0?0|t:0;return this.substring(n,n+e.length)===e}});var e=0;function t(){return e++}var n=function(){function e(e){this.element=e}return e.prototype.deref=function(){return this.element},e}(),i=function(){function e(){this.keys=[],this.values=[],this.slots=[]}var t=e.prototype;return t.set=function(e,t){var n=this.findIndex(e);if(-1===n){if(this.slots.length>0){var i=this.slots.pop();return this.keys[i]=e,void(this.values[i]=t)}this.keys.push(e),this.values.push(t)}else this.values[n]=t},t.get=function(e){var t=this.findIndex(e);if(-1!==t)return this.values[t]},t.delete=function(e){var t=this.findIndex(e);-1!==t&&(this.slots.push(t),this.keys.splice(t,1),this.values.splice(t,1))},t.findIndex=function(e){return this.keys.indexOf(e)},e}(),r=function(e,t){var n={};for(var i in e)Object.prototype.hasOwnProperty.call(e,i)&&t.indexOf(i)<0&&(n[i]=e[i]);if(null!=e&&"function"==typeof Object.getOwnPropertySymbols){var r=0;for(i=Object.getOwnPropertySymbols(e);r<i.length;r++)t.indexOf(i[r])<0&&Object.prototype.propertyIsEnumerable.call(e,i[r])&&(n[i[r]]=e[i[r]])}return n};function o(e,n){if("function"==typeof e){var i=n.tryGetLocalFuntionID(e);i||(i=t());var o=t();return n.registerLocalFunction(i,o,e),{type:"P",id:i,vid:o}}if(e instanceof Error){var s=e.message;e.name,e.stack;var u=r(e,["message","name","stack"]);return{type:"R",val:Object.assign({message:s},u)}}return{type:"R",val:e}}function s(e,t){switch(e.type){case"R":return e.val;case"P":var n=e.id,i=e.vid,o=t.tryGetRemoteFunction(n);return o?o.addRef(i):(o=u(n,i,t),t.registerRemoteFunction(n,o)),o;case"T":var s=e.val,a=s.message,c=r(s,["message"]);return Object.assign(new Error(a),c);default:return}}function u(e,n,i){var r=1,u=n,a=function(){r<=0||(r=0,i.unregisterRemoteFunction(e),i.notifyRelease({type:"RLS",id:e,vid:u}))};return Object.assign((function(){for(var n=arguments.length,r=new Array(n),u=0;u<n;u++)r[u]=arguments[u];return i.requestResponse({type:"REQ",id:e,rid:t(),args:r.map((function(e){return o(e,i)}))}).then((function(e){if(e){var t=s(e,i);if(t instanceof Error)throw t;return t}}))}),{addRef:function(e){return r<=0?0:(u=e,r+=1)},release:function(){return r<=0?0:1===r?(a(),r):r-=1},cleanup:a})}var a=function(){function e(e,t,n){this.endpoint=t,this.logger=n,this.localFunctionByID={},this.localFunctionIDByFunction="undefined"!=typeof Map?new Map:new i,this.resolveByRequestID={},this.remoteFunctionRefByID={},this.tag="RNWC|"+e+"|","undefined"!=typeof FinalizationRegistry&&(this.remoteFunctionRegistry=new FinalizationRegistry((function(e){e()})))}var t=e.prototype;return t.sendMessage=function(e){this.endpoint.postMessage(this.tag+JSON.stringify(e))},t.notifyRelease=function(e){this.sendMessage(e)},t.requestResponse=function(e){var t=this;return new Promise((function(n){t.resolveByRequestID[e.rid]=n,t.sendMessage(e)}))},t.registerLocalFunction=function(e,t,n){this.localFunctionByID.hasOwnProperty(e)?this.localFunctionByID[e].vid=t:(this.localFunctionByID[e]={localFunction:n,vid:t},this.localFunctionIDByFunction.set(n,e))},t.tryGetLocalFuntionID=function(e){return this.localFunctionIDByFunction.get(e)},t.registerRemoteFunction=function(e,t){this.remoteFunctionRefByID[e]="undefined"!=typeof WeakRef?new WeakRef(t):new n(t),this.remoteFunctionRegistry&&this.remoteFunctionRegistry.register(t,t.cleanup,t)},t.unregisterRemoteFunction=function(e){var t=this.tryGetRemoteFunction(e);t&&(delete this.remoteFunctionRefByID[e],this.remoteFunctionRegistry&&this.remoteFunctionRegistry.unregister(t))},t.tryGetRemoteFunction=function(e){var t;return null===(t=this.remoteFunctionRefByID[e])||void 0===t?void 0:t.deref()},t.canHandleMessage=function(e){return null==e?void 0:e.startsWith(this.tag)},t.handleMessage=function(e){var t,n=this,i=JSON.parse(e.substr(this.tag.length));switch(i.type){case"REQ":var r=i.id,u=i.args,a=i.rid;Promise.resolve().then((function(){var e,t=null===(e=n.localFunctionByID[r])||void 0===e?void 0:e.localFunction;if(!t)throw new Error("failed to invoke function, "+r+" is missing");return t.apply(null,u.map((function(e){return s(e,n)})))})).then((function(e){n.sendMessage({type:"RSP",ret:o(e,n),rid:a})})).catch((function(e){var t=o(e,n);t.type="T",n.sendMessage({type:"RSP",ret:t,rid:a})}));break;case"RSP":var c=i.rid,l=i.ret,f=this.resolveByRequestID[c];if(!f)return;delete this.resolveByRequestID[c],f(l);break;case"RLS":var d=i.id,h=i.vid;h===(null===(t=this.localFunctionByID[d])||void 0===t?void 0:t.vid)?(this.logger("release local function: "+d),this.localFunctionIDByFunction.delete(this.localFunctionByID[d].localFunction),delete this.localFunctionByID[d]):this.logger("not release local function: "+d+", "+h)}},e}();window.$EXPOSED_NAME||(window.$EXPOSED_NAME=function(e,t,n){var i=function(e){if(!e)return function(){};var t="function"==typeof e?e:function(e){return console.log(e)};return function(e){t("[WebViewComlink] "+e)}}($LOG_ENABLED);i(e+" injected");var r=new a(e,window.ReactNativeWebView,i),o=!1,s=function(){if(!o){o=!0;var e=function(e){var t=e.data;r.canHandleMessage(t)&&(i("received message from native "+t),r.handleMessage(t))};"android"===n?document.addEventListener("message",e):"ios"===n&&window.addEventListener("message",e)}};return Object.keys(t).reduce((function(e,n){var i=u(t[n],0,r);return e[n]=function(){return s(),i.apply(void 0,arguments)},e}),{})}("$EXPOSED_NAME",$EXPOSED_TARGET,"$PLATFORM_OS"))}();