@webview-bridge/web 1.6.0 → 1.6.1-rc.0

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.
@@ -1 +1 @@
1
- "use strict";var w=Object.defineProperty,W=Object.defineProperties,R=Object.getOwnPropertyDescriptor,A=Object.getOwnPropertyDescriptors,D=Object.getOwnPropertyNames,x=Object.getOwnPropertySymbols;var O=Object.prototype.hasOwnProperty,K=Object.prototype.propertyIsEnumerable;var S=(t,e,r)=>e in t?w(t,e,{enumerable:!0,configurable:!0,writable:!0,value:r}):t[e]=r,f=(t,e)=>{for(var r in e||(e={}))O.call(e,r)&&S(t,r,e[r]);if(x)for(var r of x(e))K.call(e,r)&&S(t,r,e[r]);return t},T=(t,e)=>W(t,A(e));var J=(t,e)=>{for(var r in e)w(t,r,{get:e[r],enumerable:!0})},I=(t,e,r,s)=>{if(e&&typeof e=="object"||typeof e=="function")for(let i of D(e))!O.call(t,i)&&i!==r&&w(t,i,{get:()=>e[i],enumerable:!(s=R(e,i))||s.enumerable});return t};var C=t=>I(w({},"__esModule",{value:!0}),t);var m=(t,e,r)=>(S(t,typeof e!="symbol"?e+"":e,r),r);var B=(t,e,r)=>new Promise((s,i)=>{var n=o=>{try{l(r.next(o))}catch(g){i(g)}},c=o=>{try{l(r.throw(o))}catch(g){i(g)}},l=o=>o.done?s(o.value):Promise.resolve(o.value).then(n,c);l((r=r.apply(t,e)).next())});var q={};J(q,{MethodNotFoundError:()=>v,NativeMethodError:()=>p,linkBridge:()=>$,registerWebMethod:()=>H});module.exports=C(q);var v=class extends Error{constructor(e){super(`Method ${e} is not defined`),this.name="MethodNotFoundError"}},p=class extends Error{constructor(e){super(`An error occurred in the native bridge: ${e}`),this.name="NativeMethodError"}};var h=()=>({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)}}}),M=({emitter:t,evaluate:e,eventId:r,failHandler:s=!1,methodName:i,onFallback:n})=>new Promise((c,l)=>{let o=t.on(`${i}-${r}`,(g,a)=>{o(),a?s instanceof Error?(n==null||n(),l(s)):c(void 0):c(g)});e()});var _="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";var P=(t=21)=>Array.from({length:t},()=>_[Math.floor(Math.random()*_.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,c;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 l=Object.keys(t);if(n=l.length,n!==Object.keys(e).length)return!1;for(i=n;i--!==0;)if(!Object.prototype.hasOwnProperty.call(e,l[i]))return!1;for(i=n;i--!==0;)if(c=l[i],!y(t[c],e[c]))return!1;return!0}return t!==t&&e!==e};var k=t=>(Object.keys(t).forEach(e=>{t[e]===void 0&&delete t[e]}),t);var V=(t,e=!0)=>new Promise((r,s)=>{setTimeout(()=>{e?s(new Error("Timeout")):r(void 0)},t)});var L=()=>new Proxy({},{get:()=>()=>Promise.resolve()});var j=(t,e={})=>{let r=()=>i,s=o=>{let g=f(f({},i),k(o));if(y(i,g))return;let a=i;i=g,c(i,a)};t.on("bridgeStateChange",o=>{s(o)});let i=f({},e),n=new Set,c=(o,g)=>{for(let a of n)a(o,g)};return{getState:r,subscribe:o=>(n.add(o),()=>n.delete(o))}};var b=(t={})=>{let e=t;return{getState:()=>e,subscribe:()=>()=>{}}};var E=class{constructor(e,r,s=[],i){this._options=e;this._emitter=r;this._bridgeMethods=s;this._nativeInitialState=i;m(this,"_defaultTimeoutMs",2e3);m(this,"_isListenerRegistered",!1);m(this,"store",b());m(this,"loose",L());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 c=P();return Promise.race([M({emitter:this._emitter,methodName:e,eventId:c,evaluate:()=>{this._postMessage("bridge",{method:e,eventId:c,args:n})},onFallback:()=>{i==null||i(e,n)},failHandler:r&&new p(e)}),s>0&&V(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 g;let{timeout:s=this._defaultTimeoutMs,onFallback:i,initialBridge:n={}}=this._options,c=Object.entries(n).filter(([a,d])=>typeof d=="function"),l=c.map(([a])=>a);Object.defineProperties(this,Object.fromEntries(c.map(([a,d])=>[a,{value:d,writable:!0}]))),this._bridgeMethods=[...e,...l],this._nativeInitialState=r;let o=e.reduce((a,d)=>{if(!this.isReactNativeWebView)return a;let u=this._createNativeMethod(d,this._willMethodThrowOnError(d),s,i);return Object.defineProperty(this,d,{value:u,writable:!1}),T(f({},a),{[d]:u})},n);if(this.loose=this._createLoose(o),this.store=j(this._emitter,f(f({},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,...d]of(g=window.nativeBatchedEvents)!=null?g:[])this._emitter.emit(a,...d);return window.nativeBatchedEvents=[],!0}};var $=(t={timeout:2e3,throwOnError:!1})=>{var o,g;if(typeof window=="undefined")return{store:b(t==null?void 0:t.initialBridge)};window.ReactNativeWebView||console.warn("[WebViewBridge] Not in a WebView environment");let e=h();window.nativeEmitter||(window.nativeEmitter=e);let r=(o=window.__bridgeMethods__)!=null?o:[],s=(g=window.__bridgeInitialState__)!=null?g:{},i=new E(t,e,r,s);if(r.length===0){let a=e.on("hydrate",({bridgeMethods:d,nativeInitialState:u})=>{i._hydrate(d,u),a()})}let{onFallback:n,onReady:c}=t,l=new Proxy(i,{get:(a,d,u)=>d in a?a[d]:(u._postMessage("fallback",{method:d}),u._willMethodThrowOnError(d)?(...N)=>(n==null||n(d,N),Promise.reject(new v(d))):(console.warn(`[WebViewBridge] ${d} is not defined, using fallback.`),()=>Promise.resolve()))});return c==null||c(l),l};var H=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=h();window.webEmitter=s;for(let[n,c]of e){let l=(o,g)=>B(void 0,null,function*(){var a,d;try{let u=yield c(...g);(a=window.ReactNativeWebView)==null||a.postMessage(JSON.stringify({type:"webMethodResponse",body:{funcName:n,eventId:o,value:u}}))}catch(u){(d=window.ReactNativeWebView)==null||d.postMessage(JSON.stringify({type:"webMethodError",body:{funcName:n,eventId:o,error:JSON.stringify(u)}}))}});s.on(n,l)}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});
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});
@@ -1 +1 @@
1
- var j=Object.defineProperty,N=Object.defineProperties;var W=Object.getOwnPropertyDescriptors;var S=Object.getOwnPropertySymbols;var R=Object.prototype.hasOwnProperty,A=Object.prototype.propertyIsEnumerable;var E=(t,e,r)=>e in t?j(t,e,{enumerable:!0,configurable:!0,writable:!0,value:r}):t[e]=r,f=(t,e)=>{for(var r in e||(e={}))R.call(e,r)&&E(t,r,e[r]);if(S)for(var r of S(e))A.call(e,r)&&E(t,r,e[r]);return t},x=(t,e)=>N(t,W(e));var m=(t,e,r)=>(E(t,typeof e!="symbol"?e+"":e,r),r);var O=(t,e,r)=>new Promise((s,i)=>{var n=o=>{try{l(r.next(o))}catch(g){i(g)}},c=o=>{try{l(r.throw(o))}catch(g){i(g)}},l=o=>o.done?s(o.value):Promise.resolve(o.value).then(n,c);l((r=r.apply(t,e)).next())});var v=class extends Error{constructor(e){super(`Method ${e} is not defined`),this.name="MethodNotFoundError"}},p=class extends Error{constructor(e){super(`An error occurred in the native bridge: ${e}`),this.name="NativeMethodError"}};var w=()=>({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)}}}),T=({emitter:t,evaluate:e,eventId:r,failHandler:s=!1,methodName:i,onFallback:n})=>new Promise((c,l)=>{let o=t.on(`${i}-${r}`,(g,a)=>{o(),a?s instanceof Error?(n==null||n(),l(s)):c(void 0):c(g)});e()});var B="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";var M=(t=21)=>Array.from({length:t},()=>B[Math.floor(Math.random()*B.length)]).join("");var h=(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,c;if(r&&s){if(n=t.length,n!==e.length)return!1;for(i=n;i--!==0;)if(!h(t[i],e[i]))return!1;return!0}if(r!==s)return!1;let l=Object.keys(t);if(n=l.length,n!==Object.keys(e).length)return!1;for(i=n;i--!==0;)if(!Object.prototype.hasOwnProperty.call(e,l[i]))return!1;for(i=n;i--!==0;)if(c=l[i],!h(t[c],e[c]))return!1;return!0}return t!==t&&e!==e};var _=t=>(Object.keys(t).forEach(e=>{t[e]===void 0&&delete t[e]}),t);var P=(t,e=!0)=>new Promise((r,s)=>{setTimeout(()=>{e?s(new Error("Timeout")):r(void 0)},t)});var k=()=>new Proxy({},{get:()=>()=>Promise.resolve()});var V=(t,e={})=>{let r=()=>i,s=o=>{let g=f(f({},i),_(o));if(h(i,g))return;let a=i;i=g,c(i,a)};t.on("bridgeStateChange",o=>{s(o)});let i=f({},e),n=new Set,c=(o,g)=>{for(let a of n)a(o,g)};return{getState:r,subscribe:o=>(n.add(o),()=>n.delete(o))}};var y=(t={})=>{let e=t;return{getState:()=>e,subscribe:()=>()=>{}}};var b=class{constructor(e,r,s=[],i){this._options=e;this._emitter=r;this._bridgeMethods=s;this._nativeInitialState=i;m(this,"_defaultTimeoutMs",2e3);m(this,"_isListenerRegistered",!1);m(this,"store",y());m(this,"loose",k());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 c=M();return Promise.race([T({emitter:this._emitter,methodName:e,eventId:c,evaluate:()=>{this._postMessage("bridge",{method:e,eventId:c,args:n})},onFallback:()=>{i==null||i(e,n)},failHandler:r&&new p(e)}),s>0&&P(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 g;let{timeout:s=this._defaultTimeoutMs,onFallback:i,initialBridge:n={}}=this._options,c=Object.entries(n).filter(([a,d])=>typeof d=="function"),l=c.map(([a])=>a);Object.defineProperties(this,Object.fromEntries(c.map(([a,d])=>[a,{value:d,writable:!0}]))),this._bridgeMethods=[...e,...l],this._nativeInitialState=r;let o=e.reduce((a,d)=>{if(!this.isReactNativeWebView)return a;let u=this._createNativeMethod(d,this._willMethodThrowOnError(d),s,i);return Object.defineProperty(this,d,{value:u,writable:!1}),x(f({},a),{[d]:u})},n);if(this.loose=this._createLoose(o),this.store=V(this._emitter,f(f({},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,...d]of(g=window.nativeBatchedEvents)!=null?g:[])this._emitter.emit(a,...d);return window.nativeBatchedEvents=[],!0}};var we=(t={timeout:2e3,throwOnError:!1})=>{var o,g;if(typeof window=="undefined")return{store:y(t==null?void 0:t.initialBridge)};window.ReactNativeWebView||console.warn("[WebViewBridge] Not in a WebView environment");let e=w();window.nativeEmitter||(window.nativeEmitter=e);let r=(o=window.__bridgeMethods__)!=null?o:[],s=(g=window.__bridgeInitialState__)!=null?g:{},i=new b(t,e,r,s);if(r.length===0){let a=e.on("hydrate",({bridgeMethods:d,nativeInitialState:u})=>{i._hydrate(d,u),a()})}let{onFallback:n,onReady:c}=t,l=new Proxy(i,{get:(a,d,u)=>d in a?a[d]:(u._postMessage("fallback",{method:d}),u._willMethodThrowOnError(d)?(...L)=>(n==null||n(d,L),Promise.reject(new v(d))):(console.warn(`[WebViewBridge] ${d} is not defined, using fallback.`),()=>Promise.resolve()))});return c==null||c(l),l};var be=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=w();window.webEmitter=s;for(let[n,c]of e){let l=(o,g)=>O(void 0,null,function*(){var a,d;try{let u=yield c(...g);(a=window.ReactNativeWebView)==null||a.postMessage(JSON.stringify({type:"webMethodResponse",body:{funcName:n,eventId:o,value:u}}))}catch(u){(d=window.ReactNativeWebView)==null||d.postMessage(JSON.stringify({type:"webMethodError",body:{funcName:n,eventId:o,error:JSON.stringify(u)}}))}});s.on(n,l)}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{v as MethodNotFoundError,p as NativeMethodError,we as linkBridge,be as registerWebMethod};
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};
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@webview-bridge/web",
3
3
  "type": "module",
4
- "version": "1.6.0",
4
+ "version": "1.6.1-rc.0",
5
5
  "description": "Fully Type-Safe Integration for React Native WebView and Web",
6
6
  "publishConfig": {
7
7
  "access": "public"