@cowprotocol/widget-react 0.8.1 → 0.9.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.
- package/index.js +1 -1
- package/index.mjs +19 -19
- package/package.json +3 -3
package/index.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
"use strict";var V=Object.defineProperty;var b=(e,t,n)=>t in e?V(e,t,{enumerable:!0,configurable:!0,writable:!0,value:n}):e[t]=n;var l=(e,t,n)=>(b(e,typeof t!="symbol"?t+"":t,n),n);Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const P=require("react/jsx-runtime"),f=require("react"),D="cowSwapWidget";function g(e,t,n){const s={key:D,method:t,...typeof n=="object"?n:{}};e.postMessage(s,"*")}function R(e,t,n){const r=s=>{!N(s.data)||s.data.key!==D||s.data.method!==t||n(s.data)};return e.addEventListener("message",r),r}function N(e){return typeof e=="object"&&e!==null&&"key"in e&&"method"in e&&typeof e.key=="string"&&typeof e.method=="string"}function L(e,t,n){e.removeEventListener("message",n)}function _(e,t){e.removeEventListener("message",t)}var w=(e=>(e.ACTIVATE="ACTIVATE",e.UPDATE_HEIGHT="UPDATE_HEIGHT",e.EMIT_COW_EVENT="EMIT_COW_EVENT",e.PROVIDER_RPC_REQUEST="PROVIDER_RPC_REQUEST",e))(w||{}),m=(e=>(e.UPDATE_PARAMS="UPDATE_PARAMS",e.UPDATE_APP_DATA="UPDATE_APP_DATA",e.PROVIDER_RPC_RESPONSE="PROVIDER_RPC_RESPONSE",e.PROVIDER_ON_EVENT="PROVIDER_ON_EVENT",e))(m||{}),v=(e=>(e.SWAP="swap",e.LIMIT="limit",e.ADVANCED="advanced",e))(v||{});const F=["primary","background","paper","text","danger","warning","alert","info","success"],H=["connect","disconnect","close","chainChanged","accountsChanged"];class k{constructor(t){l(this,"ethereumProvider",null);l(this,"requestWaitingForConnection",{});l(this,"processRpcCallFromWindow",({rpcRequest:t})=>{if(!this.ethereumProvider){t.id&&(this.requestWaitingForConnection[t.id]=t);return}this.processRpcRequest(t)});this.iframeWidow=t}disconnect(){this.ethereumProvider=null,L(window,w.PROVIDER_RPC_REQUEST,this.processRpcCallFromWindow)}onConnect(t){this.ethereumProvider?this.disconnect():R(window,w.PROVIDER_RPC_REQUEST,this.processRpcCallFromWindow),this.ethereumProvider=t,this.processPendingRequests(),H.forEach(n=>{t.on(n,r=>this.onProviderEvent(n,r))})}processPendingRequests(){Object.keys(this.requestWaitingForConnection).forEach(t=>{this.processRpcRequest(this.requestWaitingForConnection[t])}),this.requestWaitingForConnection={}}processRpcRequest(t){const{id:n,jsonrpc:r,method:s}=t;if(!this.ethereumProvider||!n)return;(s==="enable"?this.ethereumProvider.enable():this.ethereumProvider.request({...t,id:n})).then(d=>this.forwardRpcResponseToIframe({rpcResponse:{jsonrpc:r,id:n,result:d}})).catch(d=>this.forwardRpcResponseToIframe({rpcResponse:{jsonrpc:r,id:n,error:d}}))}onProviderEvent(t,n){g(this.iframeWidow,m.PROVIDER_ON_EVENT,{event:t,params:n})}forwardRpcResponseToIframe(t){g(this.iframeWidow,m.PROVIDER_RPC_RESPONSE,t)}}function O(e){return!!(e&&typeof e=="object")}const A="_";function G(e){const t=typeof e.baseUrl=="string"?e.baseUrl:"https://swap.cow.fi",n=y(e);return t+"/#"+n+"?"+U(e)}function y(e){const{chainId:t=1,sell:n,buy:r,tradeType:s=v.SWAP}=e,c=[(n==null?void 0:n.asset)||A,(r==null?void 0:r.asset)||A].map(encodeURIComponent).join("/");return`/${t}/widget/${s}/${c}`}function U(e){const t=new URLSearchParams;return Q(x(t,e),e)}function x(e,t){const{sell:n,buy:r}=t;return n!=null&&n.amount&&e.append("sellAmount",n.amount),r!=null&&r.amount&&e.append("buyAmount",r.amount),e}function Q(e,t){const n=t.theme;return n?(O(n)?(e.append("palette",encodeURIComponent(JSON.stringify(n))),e.append("theme",n.baseTheme)):(e.append("palette","null"),e.append("theme",n)),e):(e.append("palette","null"),e)}class ${constructor(){l(this,"subscriptions",{})}on(t){const{event:n,handler:r}=t;this.subscriptions[n]||(this.subscriptions[n]=[]),this.subscriptions[n].push(r)}off(t){const{event:n,handler:r}=t;this.subscriptions[n]&&(this.subscriptions[n]=this.subscriptions[n].filter(s=>s!==r))}emit(t,n){this.subscriptions[t]&&this.subscriptions[t].forEach(r=>r(n))}}class j{constructor(t,n=[]){l(this,"eventEmitter",new $);l(this,"listeners",[]);l(this,"widgetListener");this.contentWindow=t,this.updateListeners(n),this.widgetListener=R(this.contentWindow,w.EMIT_COW_EVENT,r=>this.eventEmitter.emit(r.event,r.payload))}stopListeningIframe(){_(this.contentWindow,this.widgetListener)}updateListeners(t){for(const n of this.listeners)this.eventEmitter.off(n);this.listeners=t||[];for(const n of this.listeners)this.eventEmitter.on(n)}}class q{constructor(t,n){l(this,"forwardSdkMessage");this.appWindow=t,this.iframeWidow=n,this.forwardSdkMessage=r=>{B(r.data)&&(J(r.data)?this.appWindow.parent.postMessage(r.data,"*"):K(r.data)&&this.iframeWidow.postMessage(r.data,"*"))},this.startListening()}startListening(){this.appWindow.addEventListener("message",this.forwardSdkMessage)}stopListening(){this.appWindow.removeEventListener("message",this.forwardSdkMessage)}}function B(e){return typeof e=="object"&&e!==null&&"id"in e&&typeof e.id=="string"}function J(e){return"method"in e&&typeof e.method=="string"&&"params"in e&&"env"in e&&typeof e.env=="object"&&e.env!==null&&"sdkVersion"in e.env}function K(e){return"success"in e&&typeof e.success=="boolean"&&"version"in e&&typeof e.version=="string"}const M="640px",Y="450px",z=20;function T(e,t){const{params:n,provider:r,listeners:s}=t;let c=r,d=n;const o=X(n);e.innerHTML="",e.appendChild(o);const{contentWindow:p}=o;if(!p)throw console.error("Iframe does not contain a window",o),new Error("Iframe does not contain a window!");const E=[];E.push(Z(p,n.appCode)),E.push(ee(o,n.height));const u=new j(window,s);let h=C(p,null,c);o.addEventListener("load",()=>I(p,d,c));const a=new q(window,p);return{updateParams:i=>{d=i,I(p,d,c)},updateListeners:i=>u.updateListeners(i),updateProvider:i=>{c=i,h=C(p,h,i)},destroy:()=>{h.disconnect(),u.stopListeningIframe(),E.forEach(i=>window.removeEventListener("message",i)),a.stopListening(),e.removeChild(o)}}}function C(e,t,n){t&&t.disconnect();const r=t||new k(e);return n&&r.onConnect(n),r}function X(e){const{width:t=Y,height:n=M}=e,r=document.createElement("iframe");return r.src=G(e),r.width=t,r.height=n,r.style.border="0",r}function I(e,t,n){const r=!!n,s=y(t),c=U(t).toString(),{theme:d,...o}=t;g(e,m.UPDATE_PARAMS,{urlParams:{pathname:s,search:c},appParams:o,hasProvider:r})}function Z(e,t){const n=R(window,w.ACTIVATE,()=>{_(window,n),g(e,m.UPDATE_APP_DATA,{metaData:t?{appCode:t}:void 0})});return n}function ee(e,t=M){return R(window,w.UPDATE_HEIGHT,n=>{e.style.height=n.height?`${n.height+z}px`:t})}function te(e){const{params:t,provider:n,listeners:r}=e,[s,c]=f.useState(null),d=f.useRef(null),o=f.useRef(n),p=f.useRef(r),E=f.useRef(null),u=f.useRef(null),h=f.useCallback((a,i)=>{try{console.log(`[WIDGET] ${a}`),i()}catch(W){const S=`Error ${a.toLowerCase()}`;console.error(`[WIDGET] ${S}`,W),c({message:S,error:W})}},[c]);return f.useEffect(()=>()=>{d.current=null,o.current=void 0,p.current=void 0;const a=u.current;a&&(h("💥 Destroy widget",()=>a.destroy()),u.current=null)},[]),f.useEffect(()=>{if(!E.current||JSON.stringify(d.current)===JSON.stringify(t))return;const a=E.current,i=u.current;d.current=t,i===null?h("Creating a new widget",()=>{u.current=T(a,{params:t,provider:o.current,listeners:r}),p.current=r}):h("Updating the widget",()=>i.updateParams(t))},[t]),f.useEffect(()=>{if(!u.current||o.current===n)return;o.current=n;const a=E.current;a&&h("Updating the provider",()=>{var i;(i=u.current)==null||i.destroy(),u.current=T(a,{params:t,provider:o.current,listeners:r})})},[n]),f.useEffect(()=>{if(!u.current||p.current===r)return;const a=u.current;h("Updating the listeners",()=>a.updateListeners(r))},[r]),s?P.jsxs("div",{style:{color:"#ff3a3a"},children:[s.message,s.error.message&&P.jsx("pre",{style:{whiteSpace:"pre-wrap",fontSize:"0.75em"},children:s.error.message})]}):P.jsx("div",{ref:E,style:{width:"100%"}})}exports.CowSwapWidget=te;exports.TradeType=v;exports.WIDGET_PALETTE_COLORS=F;exports.WidgetMethodsEmit=w;exports.WidgetMethodsListen=m;exports.createCowSwapWidget=T;exports.isCowSwapWidgetPalette=O;exports.listenToMessageFromWindow=R;exports.postMessageToWindow=g;exports.stopListeningToMessageFromWindow=L;exports.stopListeningWindowListener=_;
|
|
1
|
+
"use strict";var V=Object.defineProperty;var b=(e,t,n)=>t in e?V(e,t,{enumerable:!0,configurable:!0,writable:!0,value:n}):e[t]=n;var l=(e,t,n)=>(b(e,typeof t!="symbol"?t+"":t,n),n);Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const P=require("react/jsx-runtime"),f=require("react"),D="cowSwapWidget";function g(e,t,n){const s={key:D,method:t,...typeof n=="object"?n:{}};e.postMessage(s,"*")}function R(e,t,n){const r=s=>{!N(s.data)||s.data.key!==D||s.data.method!==t||n(s.data)};return e.addEventListener("message",r),r}function N(e){return typeof e=="object"&&e!==null&&"key"in e&&"method"in e&&typeof e.key=="string"&&typeof e.method=="string"}function L(e,t,n){e.removeEventListener("message",n)}function _(e,t){e.removeEventListener("message",t)}var w=(e=>(e.ACTIVATE="ACTIVATE",e.UPDATE_HEIGHT="UPDATE_HEIGHT",e.EMIT_COW_EVENT="EMIT_COW_EVENT",e.PROVIDER_RPC_REQUEST="PROVIDER_RPC_REQUEST",e))(w||{}),m=(e=>(e.UPDATE_PARAMS="UPDATE_PARAMS",e.UPDATE_APP_DATA="UPDATE_APP_DATA",e.PROVIDER_RPC_RESPONSE="PROVIDER_RPC_RESPONSE",e.PROVIDER_ON_EVENT="PROVIDER_ON_EVENT",e))(m||{}),v=(e=>(e.SWAP="swap",e.LIMIT="limit",e.ADVANCED="advanced",e))(v||{});const F=["primary","background","paper","text","danger","warning","alert","info","success"],H=["connect","disconnect","close","chainChanged","accountsChanged"];class k{constructor(t){l(this,"ethereumProvider",null);l(this,"requestWaitingForConnection",{});l(this,"processRpcCallFromWindow",({rpcRequest:t})=>{if(!this.ethereumProvider){t.id&&(this.requestWaitingForConnection[t.id]=t);return}this.processRpcRequest(t)});this.iframeWidow=t}disconnect(){this.ethereumProvider=null,L(window,w.PROVIDER_RPC_REQUEST,this.processRpcCallFromWindow)}onConnect(t){this.ethereumProvider?this.disconnect():R(window,w.PROVIDER_RPC_REQUEST,this.processRpcCallFromWindow),this.ethereumProvider=t,this.processPendingRequests(),H.forEach(n=>{t.on(n,r=>this.onProviderEvent(n,r))})}processPendingRequests(){Object.keys(this.requestWaitingForConnection).forEach(t=>{this.processRpcRequest(this.requestWaitingForConnection[t])}),this.requestWaitingForConnection={}}processRpcRequest(t){const{id:n,jsonrpc:r,method:s}=t;if(!this.ethereumProvider||!n)return;(s==="enable"?this.ethereumProvider.enable():this.ethereumProvider.request({...t,id:n})).then(d=>this.forwardRpcResponseToIframe({rpcResponse:{jsonrpc:r,id:n,result:d}})).catch(d=>this.forwardRpcResponseToIframe({rpcResponse:{jsonrpc:r,id:n,error:d}}))}onProviderEvent(t,n){g(this.iframeWidow,m.PROVIDER_ON_EVENT,{event:t,params:n})}forwardRpcResponseToIframe(t){g(this.iframeWidow,m.PROVIDER_RPC_RESPONSE,t)}}function O(e){return!!(e&&typeof e=="object")}const A="_";function G(e){const t=typeof e.baseUrl=="string"?e.baseUrl:"https://swap.cow.fi",n=y(e);return t+"/#"+n+"?"+U(e)}function y(e){const{chainId:t=1,sell:n,buy:r,tradeType:s=v.SWAP}=e,c=[(n==null?void 0:n.asset)||A,(r==null?void 0:r.asset)||A].map(encodeURIComponent).join("/");return`/${t}/widget/${s}/${c}`}function U(e){const t=new URLSearchParams;return Q(x(t,e),e)}function x(e,t){const{sell:n,buy:r}=t;return n!=null&&n.amount&&e.append("sellAmount",n.amount),r!=null&&r.amount&&e.append("buyAmount",r.amount),e}function Q(e,t){const n=t.theme;return n?(O(n)?(e.append("palette",encodeURIComponent(JSON.stringify(n))),e.append("theme",n.baseTheme)):(e.append("palette","null"),e.append("theme",n)),e):(e.append("palette","null"),e)}class ${constructor(){l(this,"subscriptions",{})}on(t){const{event:n,handler:r}=t;this.subscriptions[n]||(this.subscriptions[n]=[]),this.subscriptions[n].push(r)}off(t){const{event:n,handler:r}=t;this.subscriptions[n]&&(this.subscriptions[n]=this.subscriptions[n].filter(s=>s!==r))}emit(t,n){this.subscriptions[t]&&this.subscriptions[t].forEach(r=>r(n))}}class j{constructor(t,n=[]){l(this,"eventEmitter",new $);l(this,"listeners",[]);l(this,"widgetListener");this.contentWindow=t,this.updateListeners(n),this.widgetListener=R(this.contentWindow,w.EMIT_COW_EVENT,r=>this.eventEmitter.emit(r.event,r.payload))}stopListeningIframe(){_(this.contentWindow,this.widgetListener)}updateListeners(t){for(const n of this.listeners)this.eventEmitter.off(n);this.listeners=t||[];for(const n of this.listeners)this.eventEmitter.on(n)}}class q{constructor(t,n){l(this,"forwardSdkMessage");this.appWindow=t,this.iframeWidow=n,this.forwardSdkMessage=r=>{B(r.data)&&r.origin!==window.location.origin&&(J(r.data)?this.appWindow.parent.postMessage(r.data,"*"):K(r.data)&&this.iframeWidow.postMessage(r.data,"*"))},this.startListening()}startListening(){this.appWindow.addEventListener("message",this.forwardSdkMessage)}stopListening(){this.appWindow.removeEventListener("message",this.forwardSdkMessage)}}function B(e){return typeof e=="object"&&e!==null&&"id"in e&&typeof e.id=="string"}function J(e){return"method"in e&&typeof e.method=="string"&&"params"in e&&"env"in e&&typeof e.env=="object"&&e.env!==null&&"sdkVersion"in e.env}function K(e){return"success"in e&&typeof e.success=="boolean"&&"version"in e&&typeof e.version=="string"}const M="640px",Y="450px",z=20;function T(e,t){const{params:n,provider:r,listeners:s}=t;let c=r,d=n;const o=X(n);e.innerHTML="",e.appendChild(o);const{contentWindow:p}=o;if(!p)throw console.error("Iframe does not contain a window",o),new Error("Iframe does not contain a window!");const E=[];E.push(Z(p,n.appCode)),E.push(ee(o,n.height));const u=new j(window,s);let h=C(p,null,c);o.addEventListener("load",()=>I(p,d,c));const a=new q(window,p);return{updateParams:i=>{d=i,I(p,d,c)},updateListeners:i=>u.updateListeners(i),updateProvider:i=>{c=i,h=C(p,h,i)},destroy:()=>{h.disconnect(),u.stopListeningIframe(),E.forEach(i=>window.removeEventListener("message",i)),a.stopListening(),e.removeChild(o)}}}function C(e,t,n){t&&t.disconnect();const r=t||new k(e);return n&&r.onConnect(n),r}function X(e){const{width:t=Y,height:n=M}=e,r=document.createElement("iframe");return r.src=G(e),r.width=t,r.height=n,r.style.border="0",r}function I(e,t,n){const r=!!n,s=y(t),c=U(t).toString(),{theme:d,...o}=t;g(e,m.UPDATE_PARAMS,{urlParams:{pathname:s,search:c},appParams:o,hasProvider:r})}function Z(e,t){const n=R(window,w.ACTIVATE,()=>{_(window,n),g(e,m.UPDATE_APP_DATA,{metaData:t?{appCode:t}:void 0})});return n}function ee(e,t=M){return R(window,w.UPDATE_HEIGHT,n=>{e.style.height=n.height?`${n.height+z}px`:t})}function te(e){const{params:t,provider:n,listeners:r}=e,[s,c]=f.useState(null),d=f.useRef(null),o=f.useRef(n),p=f.useRef(r),E=f.useRef(null),u=f.useRef(null),h=f.useCallback((a,i)=>{try{console.log(`[WIDGET] ${a}`),i()}catch(W){const S=`Error ${a.toLowerCase()}`;console.error(`[WIDGET] ${S}`,W),c({message:S,error:W})}},[c]);return f.useEffect(()=>()=>{d.current=null,o.current=void 0,p.current=void 0;const a=u.current;a&&(h("💥 Destroy widget",()=>a.destroy()),u.current=null)},[]),f.useEffect(()=>{if(!E.current||JSON.stringify(d.current)===JSON.stringify(t))return;const a=E.current,i=u.current;d.current=t,i===null?h("Creating a new widget",()=>{u.current=T(a,{params:t,provider:o.current,listeners:r}),p.current=r}):h("Updating the widget",()=>i.updateParams(t))},[t]),f.useEffect(()=>{if(!u.current||o.current===n)return;o.current=n;const a=E.current;a&&h("Updating the provider",()=>{var i;(i=u.current)==null||i.destroy(),u.current=T(a,{params:t,provider:o.current,listeners:r})})},[n]),f.useEffect(()=>{if(!u.current||p.current===r)return;const a=u.current;h("Updating the listeners",()=>a.updateListeners(r))},[r]),s?P.jsxs("div",{style:{color:"#ff3a3a"},children:[s.message,s.error.message&&P.jsx("pre",{style:{whiteSpace:"pre-wrap",fontSize:"0.75em"},children:s.error.message})]}):P.jsx("div",{ref:E,style:{width:"100%"}})}exports.CowSwapWidget=te;exports.TradeType=v;exports.WIDGET_PALETTE_COLORS=F;exports.WidgetMethodsEmit=w;exports.WidgetMethodsListen=m;exports.createCowSwapWidget=T;exports.isCowSwapWidgetPalette=O;exports.listenToMessageFromWindow=R;exports.postMessageToWindow=g;exports.stopListeningToMessageFromWindow=L;exports.stopListeningWindowListener=_;
|
package/index.mjs
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
var V = Object.defineProperty;
|
|
2
2
|
var N = (e, t, n) => t in e ? V(e, t, { enumerable: !0, configurable: !0, writable: !0, value: n }) : e[t] = n;
|
|
3
|
-
var
|
|
3
|
+
var h = (e, t, n) => (N(e, typeof t != "symbol" ? t + "" : t, n), n);
|
|
4
4
|
import { jsxs as b, jsx as v } from "react/jsx-runtime";
|
|
5
5
|
import { useState as M, useRef as m, useCallback as H, useEffect as g } from "react";
|
|
6
6
|
const W = "cowSwapWidget";
|
|
@@ -54,10 +54,10 @@ class x {
|
|
|
54
54
|
* The Ethereum provider instance.
|
|
55
55
|
* When is null the JSON-RPC bridge is disconnected from the Ethereum provider.
|
|
56
56
|
* */
|
|
57
|
-
|
|
57
|
+
h(this, "ethereumProvider", null);
|
|
58
58
|
/** Stored JSON-RPC requests, to queue them when disconnected. */
|
|
59
|
-
|
|
60
|
-
|
|
59
|
+
h(this, "requestWaitingForConnection", {});
|
|
60
|
+
h(this, "processRpcCallFromWindow", ({ rpcRequest: t }) => {
|
|
61
61
|
if (!this.ethereumProvider) {
|
|
62
62
|
t.id && (this.requestWaitingForConnection[t.id] = t);
|
|
63
63
|
return;
|
|
@@ -143,7 +143,7 @@ function q(e, t) {
|
|
|
143
143
|
}
|
|
144
144
|
class J {
|
|
145
145
|
constructor() {
|
|
146
|
-
|
|
146
|
+
h(this, "subscriptions", {});
|
|
147
147
|
}
|
|
148
148
|
on(t) {
|
|
149
149
|
const { event: n, handler: r } = t;
|
|
@@ -159,9 +159,9 @@ class J {
|
|
|
159
159
|
}
|
|
160
160
|
class j {
|
|
161
161
|
constructor(t, n = []) {
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
162
|
+
h(this, "eventEmitter", new J());
|
|
163
|
+
h(this, "listeners", []);
|
|
164
|
+
h(this, "widgetListener");
|
|
165
165
|
this.contentWindow = t, this.updateListeners(n), this.widgetListener = R(
|
|
166
166
|
this.contentWindow,
|
|
167
167
|
E.EMIT_COW_EVENT,
|
|
@@ -181,9 +181,9 @@ class j {
|
|
|
181
181
|
}
|
|
182
182
|
class K {
|
|
183
183
|
constructor(t, n) {
|
|
184
|
-
|
|
184
|
+
h(this, "forwardSdkMessage");
|
|
185
185
|
this.appWindow = t, this.iframeWidow = n, this.forwardSdkMessage = (r) => {
|
|
186
|
-
Y(r.data) && (z(r.data) ? this.appWindow.parent.postMessage(r.data, "*") : X(r.data) && this.iframeWidow.postMessage(r.data, "*"));
|
|
186
|
+
Y(r.data) && r.origin !== window.location.origin && (z(r.data) ? this.appWindow.parent.postMessage(r.data, "*") : X(r.data) && this.iframeWidow.postMessage(r.data, "*"));
|
|
187
187
|
}, this.startListening();
|
|
188
188
|
}
|
|
189
189
|
startListening() {
|
|
@@ -214,7 +214,7 @@ function C(e, t) {
|
|
|
214
214
|
const l = [];
|
|
215
215
|
l.push(ne(p, n.appCode)), l.push(re(o, n.height));
|
|
216
216
|
const u = new j(window, s);
|
|
217
|
-
let
|
|
217
|
+
let f = I(p, null, c);
|
|
218
218
|
o.addEventListener("load", () => S(p, d, c));
|
|
219
219
|
const a = new K(window, p);
|
|
220
220
|
return {
|
|
@@ -223,10 +223,10 @@ function C(e, t) {
|
|
|
223
223
|
},
|
|
224
224
|
updateListeners: (i) => u.updateListeners(i),
|
|
225
225
|
updateProvider: (i) => {
|
|
226
|
-
c = i,
|
|
226
|
+
c = i, f = I(p, f, i);
|
|
227
227
|
},
|
|
228
228
|
destroy: () => {
|
|
229
|
-
|
|
229
|
+
f.disconnect(), u.stopListeningIframe(), l.forEach((i) => window.removeEventListener("message", i)), a.stopListening(), e.removeChild(o);
|
|
230
230
|
}
|
|
231
231
|
};
|
|
232
232
|
}
|
|
@@ -264,7 +264,7 @@ function re(e, t = U) {
|
|
|
264
264
|
});
|
|
265
265
|
}
|
|
266
266
|
function ce(e) {
|
|
267
|
-
const { params: t, provider: n, listeners: r } = e, [s, c] = M(null), d = m(null), o = m(n), p = m(r), l = m(null), u = m(null),
|
|
267
|
+
const { params: t, provider: n, listeners: r } = e, [s, c] = M(null), d = m(null), o = m(n), p = m(r), l = m(null), u = m(null), f = H(
|
|
268
268
|
(a, i) => {
|
|
269
269
|
try {
|
|
270
270
|
console.log(`[WIDGET] ${a}`), i();
|
|
@@ -278,20 +278,20 @@ function ce(e) {
|
|
|
278
278
|
return g(() => () => {
|
|
279
279
|
d.current = null, o.current = void 0, p.current = void 0;
|
|
280
280
|
const a = u.current;
|
|
281
|
-
a && (
|
|
281
|
+
a && (f("💥 Destroy widget", () => a.destroy()), u.current = null);
|
|
282
282
|
}, []), g(() => {
|
|
283
283
|
if (!l.current || JSON.stringify(d.current) === JSON.stringify(t))
|
|
284
284
|
return;
|
|
285
285
|
const a = l.current, i = u.current;
|
|
286
|
-
d.current = t, i === null ?
|
|
286
|
+
d.current = t, i === null ? f("Creating a new widget", () => {
|
|
287
287
|
u.current = C(a, { params: t, provider: o.current, listeners: r }), p.current = r;
|
|
288
|
-
}) :
|
|
288
|
+
}) : f("Updating the widget", () => i.updateParams(t));
|
|
289
289
|
}, [t]), g(() => {
|
|
290
290
|
if (!u.current || o.current === n)
|
|
291
291
|
return;
|
|
292
292
|
o.current = n;
|
|
293
293
|
const a = l.current;
|
|
294
|
-
a &&
|
|
294
|
+
a && f("Updating the provider", () => {
|
|
295
295
|
var i;
|
|
296
296
|
(i = u.current) == null || i.destroy(), u.current = C(a, { params: t, provider: o.current, listeners: r });
|
|
297
297
|
});
|
|
@@ -299,7 +299,7 @@ function ce(e) {
|
|
|
299
299
|
if (!u.current || p.current === r)
|
|
300
300
|
return;
|
|
301
301
|
const a = u.current;
|
|
302
|
-
|
|
302
|
+
f("Updating the listeners", () => a.updateListeners(r));
|
|
303
303
|
}, [r]), s ? /* @__PURE__ */ b("div", { style: { color: "#ff3a3a" }, children: [
|
|
304
304
|
s.message,
|
|
305
305
|
s.error.message && /* @__PURE__ */ v("pre", { style: { whiteSpace: "pre-wrap", fontSize: "0.75em" }, children: s.error.message })
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@cowprotocol/widget-react",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.9.0",
|
|
4
4
|
"type": "commonjs",
|
|
5
5
|
"description": "CoW Swap Widget Library. Allows you to easily embed a CoW Swap widget on your React application.",
|
|
6
6
|
"main": "index.js",
|
|
@@ -22,6 +22,6 @@
|
|
|
22
22
|
"react"
|
|
23
23
|
],
|
|
24
24
|
"dependencies": {
|
|
25
|
-
"@cowprotocol/widget-lib": "^0.
|
|
25
|
+
"@cowprotocol/widget-lib": "^0.11.0"
|
|
26
26
|
}
|
|
27
|
-
}
|
|
27
|
+
}
|