UNPKG

js-bridge-adapter

Version:

web 端 bridge 适配器,适用于对接任意 native 端 bridge 方案

2 lines (1 loc) 4.66 kB
!function(n,e){"object"==typeof exports&&"undefined"!=typeof module?e(exports,require("js-dynamic-function")):"function"==typeof define&&define.amd?define(["exports","js-dynamic-function"],e):e((n=n||self).JsBridgeAdapter={},n.DynamicFunction)}(this,function(n,d){"use strict";var s="default"in d?d.default:d;function p(n,e){if(!(n instanceof e))throw new TypeError("Cannot call a class as a function")}function e(n,e,t){return e in n?Object.defineProperty(n,e,{value:t,enumerable:!0,configurable:!0,writable:!0}):n[e]=t,n}function i(n,e){if(null==n)return{};var t,r,i=function(n,e){if(null==n)return{};var t,r,i={},o=Object.keys(n);for(r=0;r<o.length;r++)t=o[r],0<=e.indexOf(t)||(i[t]=n[t]);return i}(n,e);if(Object.getOwnPropertySymbols){var o=Object.getOwnPropertySymbols(n);for(r=0;r<o.length;r++)t=o[r],0<=e.indexOf(t)||Object.prototype.propertyIsEnumerable.call(n,t)&&(i[t]=n[t])}return i}function g(n){return"function"==typeof n}function a(n){return"string"==typeof n}function v(n){var e=1<arguments.length&&void 0!==arguments[1]?arguments[1]:[],t=2<arguments.length?arguments[2]:void 0;try{var r=(a(e)?e.split("."):e).reduce(function(n,e){return n[e]},n);return function(n){return void 0===n}(r)?t:r}catch(n){return t}}function c(n){var e=1<arguments.length&&void 0!==arguments[1]?arguments[1]:[];e=a(e)?e.split("."):e;for(var t=v(n,e),r=v(n,e.slice(0,-1)),i=arguments.length,o=new Array(2<i?i-2:0),u=2;u<i;u++)o[u-2]=arguments[u];return g(t)?t.call.apply(t,[r].concat(o)):t}function h(){var n=0<arguments.length&&void 0!==arguments[0]?arguments[0]:{};p(this,h);var e=function(n){return"function"==typeof n?{runner:n}:n}(n),t=e.runner,r=void 0===t?h.default.runner:t,i=e.name,o=void 0===i?v(r,"name"):i,u=e.isSupported,a=void 0===u?h.default.isSupported:u,c=e.getRunner,f=void 0===c?d.gen(a,function(n){return n&&g(r)?r:void 0}):c,l=new s({name:o,getExecutor:f});return l.isSupported=l.isExecutable,l.getRunner=l.getExecutor,delete l.isExecutable,delete l.getExecutor,l}e(h,"default",{isSupported:function(){return!0},runner:void 0});function t(){var u=this,n=0<arguments.length&&void 0!==arguments[0]?arguments[0]:"anonymous";p(this,t),e(this,"apis",{}),e(this,"silent",t.default.silent),e(this,"bridgeSupport",function(){return!1}),e(this,"getRunner",function(){return function(){}}),e(this,"log",function(n){if(!u.silent){for(var e=arguments.length,t=new Array(1<e?e-1:0),r=1;r<e;r++)t[r-1]=arguments[r];c.apply(void 0,[console,n].concat(t))}}),e(this,"config",function(){var n=0<arguments.length&&void 0!==arguments[0]?arguments[0]:{},e=n.support,t=n.api,r=i(n,["support","api"]);return Object.assign(u,r,{getRunner:t,bridgeSupport:e})}),e(this,"register",function(){var n=0<arguments.length&&void 0!==arguments[0]?arguments[0]:{};return Object.assign(u.apis,c(n,void 0,u.api)),u}),e(this,"api",function(e){var n=(1<arguments.length&&void 0!==arguments[1]?arguments[1]:{}).getRunner,r=void 0===n?function(){return u.getRunner(e)}:n,t=new h({name:e,runner:function(){for(var n=arguments.length,e=new Array(n),t=0;t<n;t++)e[t]=arguments[t];return c.apply(void 0,[r(),void 0].concat(e))},isSupported:function(){return u.bridgeSupport(e)}});return t.customize=function(n){return u.api(e,{getRunner:function(){return n(r())}})},t}),e(this,"support",function(n){return u.has(n)&&c(u.apis,"".concat(n,".isSupported"))}),e(this,"has",function(n){return n in u.apis}),e(this,"get",function(n){return v(u.apis,n)}),e(this,"call",function(n){for(var e=arguments.length,t=new Array(1<e?e-1:0),r=1;r<e;r++)t[r-1]=arguments[r];var i='[Unregistered] Api "'.concat(n,'" is unregistered in Bridge "').concat(u.name,'"'),o='[Not Supported] Api "'.concat(n,'" is not supported by Bridge "').concat(u.name,'"');return u.has(n)?d.gen(function(){return c(u.apis,"".concat(n,".getRunner"))},function(n){return g(n)?n.apply(void 0,t):u.log("warn",o)})():u.log("warn",i)}),this.name=n}e(t,"Api",h),e(t,"default",{silent:!1});function u(){var n=0<arguments.length&&void 0!==arguments[0]?arguments[0]:"";return"".concat(n,"_").concat(++o,"_").concat(Date.now(),"_").concat(Math.floor(1e6*Math.random()))}function f(){if("undefined"!=typeof self)return self;if("undefined"!=typeof window)return window;if("undefined"!=typeof global)return global;throw new Error("unable to locate global object")}function r(n){var e=1<arguments.length&&void 0!==arguments[1]?arguments[1]:{},t=e.name,r=void 0===t?u("__globalFunction"):t,i=e.once,o=void 0!==i&&i;return a(n)?n:(f()[r]=function(){if(o&&delete f()[r],g(n))return n.apply(void 0,arguments)},r)}var o=0;r.getGlobal=f,n.Api=h,n.Bridge=t,n.default=t,n.globalize=r,n.uniqueId=u,Object.defineProperty(n,"__esModule",{value:!0})});