@webview-bridge/web 1.6.0 → 1.6.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/commonjs/index.cjs +1 -1
- package/dist/module/index.mjs +1 -1
- package/package.json +1 -1
package/dist/commonjs/index.cjs
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
"use strict";var
|
|
1
|
+
"use strict";var b=Object.defineProperty,A=Object.defineProperties,D=Object.getOwnPropertyDescriptor,K=Object.getOwnPropertyDescriptors,J=Object.getOwnPropertyNames,T=Object.getOwnPropertySymbols;var M=Object.prototype.hasOwnProperty,I=Object.prototype.propertyIsEnumerable;var O=(t,e,r)=>e in t?b(t,e,{enumerable:!0,configurable:!0,writable:!0,value:r}):t[e]=r,u=(t,e)=>{for(var r in e||(e={}))M.call(e,r)&&O(t,r,e[r]);if(T)for(var r of T(e))I.call(e,r)&&O(t,r,e[r]);return t},_=(t,e)=>A(t,K(e));var C=(t,e)=>{for(var r in e)b(t,r,{get:e[r],enumerable:!0})},$=(t,e,r,s)=>{if(e&&typeof e=="object"||typeof e=="function")for(let i of J(e))!M.call(t,i)&&i!==r&&b(t,i,{get:()=>e[i],enumerable:!(s=D(e,i))||s.enumerable});return t};var H=t=>$(b({},"__esModule",{value:!0}),t);var p=(t,e,r)=>(O(t,typeof e!="symbol"?e+"":e,r),r);var P=(t,e,r)=>new Promise((s,i)=>{var n=o=>{try{g(r.next(o))}catch(l){i(l)}},d=o=>{try{g(r.throw(o))}catch(l){i(l)}},g=o=>o.done?s(o.value):Promise.resolve(o.value).then(n,d);g((r=r.apply(t,e)).next())});var U={};C(U,{MethodNotFoundError:()=>w,NativeMethodError:()=>h,linkBridge:()=>q,registerWebMethod:()=>G});module.exports=H(U);var w=class extends Error{constructor(e){super(`Method ${e} is not defined`),this.name="MethodNotFoundError"}},h=class extends Error{constructor(e){super(`An error occurred in the native bridge: ${e}`),this.name="NativeMethodError"}};var y=()=>({events:{},emit(t,...e){let r=this.events[t]||[];for(let s=0,i=r.length;s<i;s++)r[s](...e)},on(t,e){var r;return(r=this.events[t])!=null&&r.push(e)||(this.events[t]=[e]),()=>{var s;this.events[t]=(s=this.events[t])==null?void 0:s.filter(i=>e!==i)}}}),k=({emitter:t,evaluate:e,eventId:r,failHandler:s=!1,methodName:i,onFallback:n})=>new Promise((d,g)=>{let o=t.on(`${i}-${r}`,(l,a)=>{o(),a?s instanceof Error?(n==null||n(),g(s)):d(void 0):d(l)});e()});var V="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";var L=(t=21)=>Array.from({length:t},()=>V[Math.floor(Math.random()*V.length)]).join("");var E=(t,e)=>{if(t===e)return!0;if(t&&e&&typeof t=="object"&&typeof e=="object"){let r=Array.isArray(t),s=Array.isArray(e),i,n,d;if(r&&s){if(n=t.length,n!==e.length)return!1;for(i=n;i--!==0;)if(!E(t[i],e[i]))return!1;return!0}if(r!==s)return!1;let g=Object.keys(t);if(n=g.length,n!==Object.keys(e).length)return!1;for(i=n;i--!==0;)if(!Object.prototype.hasOwnProperty.call(e,g[i]))return!1;for(i=n;i--!==0;)if(d=g[i],!E(t[d],e[d]))return!1;return!0}return t!==t&&e!==e};var N=t=>(Object.keys(t).forEach(e=>{t[e]===void 0&&delete t[e]}),t);var j=(t,e=!0)=>new Promise((r,s)=>{setTimeout(()=>{e?s(new Error("Timeout")):r(void 0)},t)});var W=()=>new Proxy({},{get:()=>()=>Promise.resolve()});var R=(t,e={})=>{let r=()=>i,s=o=>{let l=u(u({},i),N(o));if(E(i,l))return;let a=i;i=l,d(i,a)};t.on("bridgeStateChange",o=>{s(o)});let i=u({},e),n=new Set,d=(o,l)=>{for(let a of n)a(o,l)};return{getState:r,subscribe:o=>(n.add(o),()=>n.delete(o))}};var S=(t={})=>{let e=t;return{getState:()=>e,subscribe:()=>()=>{}}};var x=class{constructor(e,r,s=[],i){this._options=e;this._emitter=r;this._bridgeMethods=s;this._nativeInitialState=i;p(this,"_defaultTimeoutMs",2e3);p(this,"_isListenerRegistered",!1);p(this,"store",S());p(this,"loose",W());this._hydrate(s,i)}get isReactNativeWebView(){return!!window.ReactNativeWebView}get isWebViewBridgeAvailable(){return this._bridgeMethods.length>0}isNativeMethodAvailable(e){return typeof e=="string"&&this._bridgeMethods.includes(e)}addEventListener(e,r){return this._emitter.on(`postMessage/${String(e)}`,r)}_postMessage(e,r){var s;(s=window.ReactNativeWebView)==null||s.postMessage(JSON.stringify(r?{type:e,body:r}:{type:e}))}_createNativeMethod(e,r,s,i){return(...n)=>{let d=L();return Promise.race([k({emitter:this._emitter,methodName:e,eventId:d,evaluate:()=>{this._postMessage("bridge",{method:e,eventId:d,args:n})},onFallback:()=>{i==null||i(e,n)},failHandler:r&&new h(e)}),s>0&&j(s,r)].filter(Boolean))}}_willMethodThrowOnError(e){let{throwOnError:r}=this._options;return r===!0||Array.isArray(r)&&r.includes(e)}_createLoose(e){let{timeout:r=this._defaultTimeoutMs,onFallback:s}=this._options;return new Proxy(e,{get:(i,n)=>n in i&&!["isWebViewBridgeAvailable","isNativeMethodAvailable"].includes(n)?i[n]:this._createNativeMethod(n,this._willMethodThrowOnError(n),r,s)})}_hydrate(e,r={}){var l;let{timeout:s=this._defaultTimeoutMs,onFallback:i,initialBridge:n={}}=this._options,d=Object.entries(n).filter(([a,c])=>typeof c=="function"),g=d.map(([a])=>a);Object.defineProperties(this,Object.fromEntries(d.map(([a,c])=>[a,{value:c,writable:!0}]))),this._bridgeMethods=[...e,...g],this._nativeInitialState=r;let o=e.reduce((a,c)=>{if(!this.isReactNativeWebView)return a;let f=this._createNativeMethod(c,this._willMethodThrowOnError(c),s,i);return Object.defineProperty(this,c,{value:f,writable:!1}),_(u({},a),{[c]:f})},n);if(this.loose=this._createLoose(o),this.store=R(this._emitter,u(u({},o),r)),!this._isListenerRegistered){let a=()=>{document.visibilityState==="visible"&&this._postMessage("getBridgeState")};document.addEventListener("visibilitychange",a),this._isListenerRegistered=!0}this._postMessage("getBridgeState");for(let[a,...c]of(l=window.nativeBatchedEvents)!=null?l:[])this._emitter.emit(a,...c);return window.nativeBatchedEvents=[],!0}};var q=(t={timeout:2e3,throwOnError:!1})=>{var o,l,a;if(typeof window=="undefined"){let c=(o=t==null?void 0:t.initialBridge)!=null?o:{},v=Object.entries(c).filter(([m,B])=>typeof B=="function").map(([m])=>m);return{addEventListener:(m,B)=>()=>{},loose:{},isWebViewBridgeAvailable:v.length>0,isNativeMethodAvailable:m=>v.includes(m),store:S(t==null?void 0:t.initialBridge)}}window.ReactNativeWebView||console.warn("[WebViewBridge] Not in a WebView environment");let e=y();window.nativeEmitter||(window.nativeEmitter=e);let r=(l=window.__bridgeMethods__)!=null?l:[],s=(a=window.__bridgeInitialState__)!=null?a:{},i=new x(t,e,r,s);if(r.length===0){let c=e.on("hydrate",({bridgeMethods:f,nativeInitialState:v})=>{i._hydrate(f,v),c()})}let{onFallback:n,onReady:d}=t,g=new Proxy(i,{get:(c,f,v)=>f in c?c[f]:(v._postMessage("fallback",{method:f}),v._willMethodThrowOnError(f)?(...m)=>(n==null||n(f,m),Promise.reject(new w(f))):(console.warn(`[WebViewBridge] ${f} is not defined, using fallback.`),()=>Promise.resolve()))});return d==null||d(g),g};var G=t=>{if(typeof window!="undefined"&&!window.ReactNativeWebView)return console.warn("[WebViewBridge] Not in a WebView environment"),t;let e=Object.entries(t),r=Object.keys(t),s=y();window.webEmitter=s;for(let[n,d]of e){let g=(o,l)=>P(void 0,null,function*(){var a,c;try{let f=yield d(...l);(a=window.ReactNativeWebView)==null||a.postMessage(JSON.stringify({type:"webMethodResponse",body:{funcName:n,eventId:o,value:f}}))}catch(f){(c=window.ReactNativeWebView)==null||c.postMessage(JSON.stringify({type:"webMethodError",body:{funcName:n,eventId:o,error:JSON.stringify(f)}}))}});s.on(n,g)}let i=()=>{var n;(n=window.ReactNativeWebView)==null||n.postMessage(JSON.stringify({type:"registerWebMethod",body:{bridgeNames:r}})),window.removeEventListener("DOMContentLoaded",i)};return window.ReactNativeWebView?(document.addEventListener("visibilitychange",()=>{var n;document.visibilityState==="visible"&&((n=window.ReactNativeWebView)==null||n.postMessage(JSON.stringify({type:"registerWebMethod",body:{bridgeNames:r}})))}),i(),t):(window.addEventListener("DOMContentLoaded",i),t)};0&&(module.exports={MethodNotFoundError,NativeMethodError,linkBridge,registerWebMethod});
|
package/dist/module/index.mjs
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
var
|
|
1
|
+
var W=Object.defineProperty,R=Object.defineProperties;var A=Object.getOwnPropertyDescriptors;var B=Object.getOwnPropertySymbols;var D=Object.prototype.hasOwnProperty,K=Object.prototype.propertyIsEnumerable;var x=(t,e,r)=>e in t?W(t,e,{enumerable:!0,configurable:!0,writable:!0,value:r}):t[e]=r,u=(t,e)=>{for(var r in e||(e={}))D.call(e,r)&&x(t,r,e[r]);if(B)for(var r of B(e))K.call(e,r)&&x(t,r,e[r]);return t},T=(t,e)=>R(t,A(e));var p=(t,e,r)=>(x(t,typeof e!="symbol"?e+"":e,r),r);var M=(t,e,r)=>new Promise((s,i)=>{var n=o=>{try{g(r.next(o))}catch(l){i(l)}},d=o=>{try{g(r.throw(o))}catch(l){i(l)}},g=o=>o.done?s(o.value):Promise.resolve(o.value).then(n,d);g((r=r.apply(t,e)).next())});var w=class extends Error{constructor(e){super(`Method ${e} is not defined`),this.name="MethodNotFoundError"}},h=class extends Error{constructor(e){super(`An error occurred in the native bridge: ${e}`),this.name="NativeMethodError"}};var b=()=>({events:{},emit(t,...e){let r=this.events[t]||[];for(let s=0,i=r.length;s<i;s++)r[s](...e)},on(t,e){var r;return(r=this.events[t])!=null&&r.push(e)||(this.events[t]=[e]),()=>{var s;this.events[t]=(s=this.events[t])==null?void 0:s.filter(i=>e!==i)}}}),_=({emitter:t,evaluate:e,eventId:r,failHandler:s=!1,methodName:i,onFallback:n})=>new Promise((d,g)=>{let o=t.on(`${i}-${r}`,(l,a)=>{o(),a?s instanceof Error?(n==null||n(),g(s)):d(void 0):d(l)});e()});var P="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";var k=(t=21)=>Array.from({length:t},()=>P[Math.floor(Math.random()*P.length)]).join("");var y=(t,e)=>{if(t===e)return!0;if(t&&e&&typeof t=="object"&&typeof e=="object"){let r=Array.isArray(t),s=Array.isArray(e),i,n,d;if(r&&s){if(n=t.length,n!==e.length)return!1;for(i=n;i--!==0;)if(!y(t[i],e[i]))return!1;return!0}if(r!==s)return!1;let g=Object.keys(t);if(n=g.length,n!==Object.keys(e).length)return!1;for(i=n;i--!==0;)if(!Object.prototype.hasOwnProperty.call(e,g[i]))return!1;for(i=n;i--!==0;)if(d=g[i],!y(t[d],e[d]))return!1;return!0}return t!==t&&e!==e};var V=t=>(Object.keys(t).forEach(e=>{t[e]===void 0&&delete t[e]}),t);var L=(t,e=!0)=>new Promise((r,s)=>{setTimeout(()=>{e?s(new Error("Timeout")):r(void 0)},t)});var N=()=>new Proxy({},{get:()=>()=>Promise.resolve()});var j=(t,e={})=>{let r=()=>i,s=o=>{let l=u(u({},i),V(o));if(y(i,l))return;let a=i;i=l,d(i,a)};t.on("bridgeStateChange",o=>{s(o)});let i=u({},e),n=new Set,d=(o,l)=>{for(let a of n)a(o,l)};return{getState:r,subscribe:o=>(n.add(o),()=>n.delete(o))}};var E=(t={})=>{let e=t;return{getState:()=>e,subscribe:()=>()=>{}}};var S=class{constructor(e,r,s=[],i){this._options=e;this._emitter=r;this._bridgeMethods=s;this._nativeInitialState=i;p(this,"_defaultTimeoutMs",2e3);p(this,"_isListenerRegistered",!1);p(this,"store",E());p(this,"loose",N());this._hydrate(s,i)}get isReactNativeWebView(){return!!window.ReactNativeWebView}get isWebViewBridgeAvailable(){return this._bridgeMethods.length>0}isNativeMethodAvailable(e){return typeof e=="string"&&this._bridgeMethods.includes(e)}addEventListener(e,r){return this._emitter.on(`postMessage/${String(e)}`,r)}_postMessage(e,r){var s;(s=window.ReactNativeWebView)==null||s.postMessage(JSON.stringify(r?{type:e,body:r}:{type:e}))}_createNativeMethod(e,r,s,i){return(...n)=>{let d=k();return Promise.race([_({emitter:this._emitter,methodName:e,eventId:d,evaluate:()=>{this._postMessage("bridge",{method:e,eventId:d,args:n})},onFallback:()=>{i==null||i(e,n)},failHandler:r&&new h(e)}),s>0&&L(s,r)].filter(Boolean))}}_willMethodThrowOnError(e){let{throwOnError:r}=this._options;return r===!0||Array.isArray(r)&&r.includes(e)}_createLoose(e){let{timeout:r=this._defaultTimeoutMs,onFallback:s}=this._options;return new Proxy(e,{get:(i,n)=>n in i&&!["isWebViewBridgeAvailable","isNativeMethodAvailable"].includes(n)?i[n]:this._createNativeMethod(n,this._willMethodThrowOnError(n),r,s)})}_hydrate(e,r={}){var l;let{timeout:s=this._defaultTimeoutMs,onFallback:i,initialBridge:n={}}=this._options,d=Object.entries(n).filter(([a,c])=>typeof c=="function"),g=d.map(([a])=>a);Object.defineProperties(this,Object.fromEntries(d.map(([a,c])=>[a,{value:c,writable:!0}]))),this._bridgeMethods=[...e,...g],this._nativeInitialState=r;let o=e.reduce((a,c)=>{if(!this.isReactNativeWebView)return a;let f=this._createNativeMethod(c,this._willMethodThrowOnError(c),s,i);return Object.defineProperty(this,c,{value:f,writable:!1}),T(u({},a),{[c]:f})},n);if(this.loose=this._createLoose(o),this.store=j(this._emitter,u(u({},o),r)),!this._isListenerRegistered){let a=()=>{document.visibilityState==="visible"&&this._postMessage("getBridgeState")};document.addEventListener("visibilitychange",a),this._isListenerRegistered=!0}this._postMessage("getBridgeState");for(let[a,...c]of(l=window.nativeBatchedEvents)!=null?l:[])this._emitter.emit(a,...c);return window.nativeBatchedEvents=[],!0}};var be=(t={timeout:2e3,throwOnError:!1})=>{var o,l,a;if(typeof window=="undefined"){let c=(o=t==null?void 0:t.initialBridge)!=null?o:{},v=Object.entries(c).filter(([m,O])=>typeof O=="function").map(([m])=>m);return{addEventListener:(m,O)=>()=>{},loose:{},isWebViewBridgeAvailable:v.length>0,isNativeMethodAvailable:m=>v.includes(m),store:E(t==null?void 0:t.initialBridge)}}window.ReactNativeWebView||console.warn("[WebViewBridge] Not in a WebView environment");let e=b();window.nativeEmitter||(window.nativeEmitter=e);let r=(l=window.__bridgeMethods__)!=null?l:[],s=(a=window.__bridgeInitialState__)!=null?a:{},i=new S(t,e,r,s);if(r.length===0){let c=e.on("hydrate",({bridgeMethods:f,nativeInitialState:v})=>{i._hydrate(f,v),c()})}let{onFallback:n,onReady:d}=t,g=new Proxy(i,{get:(c,f,v)=>f in c?c[f]:(v._postMessage("fallback",{method:f}),v._willMethodThrowOnError(f)?(...m)=>(n==null||n(f,m),Promise.reject(new w(f))):(console.warn(`[WebViewBridge] ${f} is not defined, using fallback.`),()=>Promise.resolve()))});return d==null||d(g),g};var Se=t=>{if(typeof window!="undefined"&&!window.ReactNativeWebView)return console.warn("[WebViewBridge] Not in a WebView environment"),t;let e=Object.entries(t),r=Object.keys(t),s=b();window.webEmitter=s;for(let[n,d]of e){let g=(o,l)=>M(void 0,null,function*(){var a,c;try{let f=yield d(...l);(a=window.ReactNativeWebView)==null||a.postMessage(JSON.stringify({type:"webMethodResponse",body:{funcName:n,eventId:o,value:f}}))}catch(f){(c=window.ReactNativeWebView)==null||c.postMessage(JSON.stringify({type:"webMethodError",body:{funcName:n,eventId:o,error:JSON.stringify(f)}}))}});s.on(n,g)}let i=()=>{var n;(n=window.ReactNativeWebView)==null||n.postMessage(JSON.stringify({type:"registerWebMethod",body:{bridgeNames:r}})),window.removeEventListener("DOMContentLoaded",i)};return window.ReactNativeWebView?(document.addEventListener("visibilitychange",()=>{var n;document.visibilityState==="visible"&&((n=window.ReactNativeWebView)==null||n.postMessage(JSON.stringify({type:"registerWebMethod",body:{bridgeNames:r}})))}),i(),t):(window.addEventListener("DOMContentLoaded",i),t)};export{w as MethodNotFoundError,h as NativeMethodError,be as linkBridge,Se as registerWebMethod};
|