@cowprotocol/widget-react 0.7.1 → 0.8.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/index.js +1 -1
- package/index.mjs +77 -73
- package/package.json +2 -2
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
|
|
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=_;
|
package/index.mjs
CHANGED
|
@@ -1,10 +1,10 @@
|
|
|
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 f = (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
|
-
import { useState as M, useRef as m, useCallback as H, useEffect as
|
|
5
|
+
import { useState as M, useRef as m, useCallback as H, useEffect as g } from "react";
|
|
6
6
|
const W = "cowSwapWidget";
|
|
7
|
-
function
|
|
7
|
+
function P(e, t, n) {
|
|
8
8
|
const s = {
|
|
9
9
|
key: W,
|
|
10
10
|
method: t,
|
|
@@ -16,7 +16,7 @@ function g(e, t, n) {
|
|
|
16
16
|
// TODO: Change to CoW specific origin in production. https://github.com/cowprotocol/cowswap/issues/3828
|
|
17
17
|
);
|
|
18
18
|
}
|
|
19
|
-
function
|
|
19
|
+
function R(e, t, n) {
|
|
20
20
|
const r = (s) => {
|
|
21
21
|
!F(s.data) || s.data.key !== W || s.data.method !== t || n(s.data);
|
|
22
22
|
};
|
|
@@ -31,7 +31,7 @@ function k(e, t, n) {
|
|
|
31
31
|
function D(e, t) {
|
|
32
32
|
e.removeEventListener("message", t);
|
|
33
33
|
}
|
|
34
|
-
var
|
|
34
|
+
var E = /* @__PURE__ */ ((e) => (e.ACTIVATE = "ACTIVATE", e.UPDATE_HEIGHT = "UPDATE_HEIGHT", e.EMIT_COW_EVENT = "EMIT_COW_EVENT", e.PROVIDER_RPC_REQUEST = "PROVIDER_RPC_REQUEST", e))(E || {}), w = /* @__PURE__ */ ((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))(w || {}), O = /* @__PURE__ */ ((e) => (e.SWAP = "swap", e.LIMIT = "limit", e.ADVANCED = "advanced", e))(O || {});
|
|
35
35
|
const ae = [
|
|
36
36
|
"primary",
|
|
37
37
|
"background",
|
|
@@ -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
|
+
f(this, "ethereumProvider", null);
|
|
58
58
|
/** Stored JSON-RPC requests, to queue them when disconnected. */
|
|
59
|
-
|
|
60
|
-
|
|
59
|
+
f(this, "requestWaitingForConnection", {});
|
|
60
|
+
f(this, "processRpcCallFromWindow", ({ rpcRequest: t }) => {
|
|
61
61
|
if (!this.ethereumProvider) {
|
|
62
62
|
t.id && (this.requestWaitingForConnection[t.id] = t);
|
|
63
63
|
return;
|
|
@@ -70,14 +70,14 @@ class x {
|
|
|
70
70
|
* Disconnects the JSON-RPC bridge from the Ethereum provider.
|
|
71
71
|
*/
|
|
72
72
|
disconnect() {
|
|
73
|
-
this.ethereumProvider = null, k(window,
|
|
73
|
+
this.ethereumProvider = null, k(window, E.PROVIDER_RPC_REQUEST, this.processRpcCallFromWindow);
|
|
74
74
|
}
|
|
75
75
|
/**
|
|
76
76
|
* Handles the 'connect' event and sets up event listeners for Ethereum provider events.
|
|
77
77
|
* @param newProvider - The Ethereum provider to connect.
|
|
78
78
|
*/
|
|
79
79
|
onConnect(t) {
|
|
80
|
-
this.ethereumProvider ? this.disconnect() :
|
|
80
|
+
this.ethereumProvider ? this.disconnect() : R(window, E.PROVIDER_RPC_REQUEST, this.processRpcCallFromWindow), this.ethereumProvider = t, this.processPendingRequests(), G.forEach((n) => {
|
|
81
81
|
t.on(n, (r) => this.onProviderEvent(n, r));
|
|
82
82
|
});
|
|
83
83
|
}
|
|
@@ -95,17 +95,17 @@ class x {
|
|
|
95
95
|
if (!this.ethereumProvider || !n)
|
|
96
96
|
return;
|
|
97
97
|
(s === "enable" ? this.ethereumProvider.enable() : this.ethereumProvider.request({ ...t, id: n })).then(
|
|
98
|
-
(
|
|
99
|
-
rpcResponse: { jsonrpc: r, id: n, result:
|
|
98
|
+
(d) => this.forwardRpcResponseToIframe({
|
|
99
|
+
rpcResponse: { jsonrpc: r, id: n, result: d }
|
|
100
100
|
})
|
|
101
101
|
).catch(
|
|
102
|
-
(
|
|
103
|
-
rpcResponse: { jsonrpc: r, id: n, error:
|
|
102
|
+
(d) => this.forwardRpcResponseToIframe({
|
|
103
|
+
rpcResponse: { jsonrpc: r, id: n, error: d }
|
|
104
104
|
})
|
|
105
105
|
);
|
|
106
106
|
}
|
|
107
107
|
onProviderEvent(t, n) {
|
|
108
|
-
|
|
108
|
+
P(this.iframeWidow, w.PROVIDER_ON_EVENT, {
|
|
109
109
|
event: t,
|
|
110
110
|
params: n
|
|
111
111
|
});
|
|
@@ -114,7 +114,7 @@ class x {
|
|
|
114
114
|
* Forward a JSON-RPC message to the content window.
|
|
115
115
|
*/
|
|
116
116
|
forwardRpcResponseToIframe(t) {
|
|
117
|
-
|
|
117
|
+
P(this.iframeWidow, w.PROVIDER_RPC_RESPONSE, t);
|
|
118
118
|
}
|
|
119
119
|
}
|
|
120
120
|
function Q(e) {
|
|
@@ -126,24 +126,24 @@ function $(e) {
|
|
|
126
126
|
return t + "/#" + n + "?" + y(e);
|
|
127
127
|
}
|
|
128
128
|
function L(e) {
|
|
129
|
-
const { chainId: t = 1, sell: n, buy: r, tradeType: s = O.SWAP } = e,
|
|
130
|
-
return `/${t}/widget/${s}/${
|
|
129
|
+
const { chainId: t = 1, sell: n, buy: r, tradeType: s = O.SWAP } = e, c = [(n == null ? void 0 : n.asset) || A, (r == null ? void 0 : r.asset) || A].map(encodeURIComponent).join("/");
|
|
130
|
+
return `/${t}/widget/${s}/${c}`;
|
|
131
131
|
}
|
|
132
132
|
function y(e) {
|
|
133
133
|
const t = new URLSearchParams();
|
|
134
|
-
return B(
|
|
134
|
+
return q(B(t, e), e);
|
|
135
135
|
}
|
|
136
|
-
function
|
|
136
|
+
function B(e, t) {
|
|
137
137
|
const { sell: n, buy: r } = t;
|
|
138
138
|
return n != null && n.amount && e.append("sellAmount", n.amount), r != null && r.amount && e.append("buyAmount", r.amount), e;
|
|
139
139
|
}
|
|
140
|
-
function
|
|
140
|
+
function q(e, t) {
|
|
141
141
|
const n = t.theme;
|
|
142
|
-
return n
|
|
142
|
+
return n ? (Q(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);
|
|
143
143
|
}
|
|
144
144
|
class J {
|
|
145
145
|
constructor() {
|
|
146
|
-
|
|
146
|
+
f(this, "subscriptions", {});
|
|
147
147
|
}
|
|
148
148
|
on(t) {
|
|
149
149
|
const { event: n, handler: r } = t;
|
|
@@ -159,12 +159,12 @@ class J {
|
|
|
159
159
|
}
|
|
160
160
|
class j {
|
|
161
161
|
constructor(t, n = []) {
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
this.contentWindow = t, this.updateListeners(n), this.widgetListener =
|
|
162
|
+
f(this, "eventEmitter", new J());
|
|
163
|
+
f(this, "listeners", []);
|
|
164
|
+
f(this, "widgetListener");
|
|
165
|
+
this.contentWindow = t, this.updateListeners(n), this.widgetListener = R(
|
|
166
166
|
this.contentWindow,
|
|
167
|
-
|
|
167
|
+
E.EMIT_COW_EVENT,
|
|
168
168
|
(r) => this.eventEmitter.emit(r.event, r.payload)
|
|
169
169
|
);
|
|
170
170
|
}
|
|
@@ -181,7 +181,7 @@ class j {
|
|
|
181
181
|
}
|
|
182
182
|
class K {
|
|
183
183
|
constructor(t, n) {
|
|
184
|
-
|
|
184
|
+
f(this, "forwardSdkMessage");
|
|
185
185
|
this.appWindow = t, this.iframeWidow = n, this.forwardSdkMessage = (r) => {
|
|
186
186
|
Y(r.data) && (z(r.data) ? this.appWindow.parent.postMessage(r.data, "*") : X(r.data) && this.iframeWidow.postMessage(r.data, "*"));
|
|
187
187
|
}, this.startListening();
|
|
@@ -205,28 +205,28 @@ function X(e) {
|
|
|
205
205
|
const U = "640px", Z = "450px", ee = 20;
|
|
206
206
|
function C(e, t) {
|
|
207
207
|
const { params: n, provider: r, listeners: s } = t;
|
|
208
|
-
let
|
|
208
|
+
let c = r, d = n;
|
|
209
209
|
const o = te(n);
|
|
210
210
|
e.innerHTML = "", e.appendChild(o);
|
|
211
|
-
const { contentWindow:
|
|
212
|
-
if (!
|
|
211
|
+
const { contentWindow: p } = o;
|
|
212
|
+
if (!p)
|
|
213
213
|
throw console.error("Iframe does not contain a window", o), new Error("Iframe does not contain a window!");
|
|
214
|
-
const
|
|
215
|
-
|
|
216
|
-
const
|
|
217
|
-
let
|
|
218
|
-
o.addEventListener("load", () => S(
|
|
219
|
-
const
|
|
214
|
+
const l = [];
|
|
215
|
+
l.push(ne(p, n.appCode)), l.push(re(o, n.height));
|
|
216
|
+
const u = new j(window, s);
|
|
217
|
+
let h = I(p, null, c);
|
|
218
|
+
o.addEventListener("load", () => S(p, d, c));
|
|
219
|
+
const a = new K(window, p);
|
|
220
220
|
return {
|
|
221
221
|
updateParams: (i) => {
|
|
222
|
-
|
|
222
|
+
d = i, S(p, d, c);
|
|
223
223
|
},
|
|
224
|
-
updateListeners: (i) =>
|
|
224
|
+
updateListeners: (i) => u.updateListeners(i),
|
|
225
225
|
updateProvider: (i) => {
|
|
226
|
-
|
|
226
|
+
c = i, h = I(p, h, i);
|
|
227
227
|
},
|
|
228
228
|
destroy: () => {
|
|
229
|
-
|
|
229
|
+
h.disconnect(), u.stopListeningIframe(), l.forEach((i) => window.removeEventListener("message", i)), a.stopListening(), e.removeChild(o);
|
|
230
230
|
}
|
|
231
231
|
};
|
|
232
232
|
}
|
|
@@ -240,77 +240,81 @@ function te(e) {
|
|
|
240
240
|
return r.src = $(e), r.width = t, r.height = n, r.style.border = "0", r;
|
|
241
241
|
}
|
|
242
242
|
function S(e, t, n) {
|
|
243
|
-
const r = !!n, s = L(t),
|
|
244
|
-
|
|
243
|
+
const r = !!n, s = L(t), c = y(t).toString(), { theme: d, ...o } = t;
|
|
244
|
+
P(e, w.UPDATE_PARAMS, {
|
|
245
245
|
urlParams: {
|
|
246
246
|
pathname: s,
|
|
247
|
-
search:
|
|
247
|
+
search: c
|
|
248
248
|
},
|
|
249
249
|
appParams: o,
|
|
250
250
|
hasProvider: r
|
|
251
251
|
});
|
|
252
252
|
}
|
|
253
253
|
function ne(e, t) {
|
|
254
|
-
const n =
|
|
255
|
-
D(window, n),
|
|
254
|
+
const n = R(window, E.ACTIVATE, () => {
|
|
255
|
+
D(window, n), P(e, w.UPDATE_APP_DATA, {
|
|
256
256
|
metaData: t ? { appCode: t } : void 0
|
|
257
257
|
});
|
|
258
258
|
});
|
|
259
259
|
return n;
|
|
260
260
|
}
|
|
261
261
|
function re(e, t = U) {
|
|
262
|
-
return
|
|
262
|
+
return R(window, E.UPDATE_HEIGHT, (n) => {
|
|
263
263
|
e.style.height = n.height ? `${n.height + ee}px` : t;
|
|
264
264
|
});
|
|
265
265
|
}
|
|
266
266
|
function ce(e) {
|
|
267
|
-
const { params: t, provider: n, listeners: r } = e, [s,
|
|
268
|
-
(
|
|
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), h = H(
|
|
268
|
+
(a, i) => {
|
|
269
269
|
try {
|
|
270
|
-
console.log(`[WIDGET] ${
|
|
270
|
+
console.log(`[WIDGET] ${a}`), i();
|
|
271
271
|
} catch (T) {
|
|
272
|
-
const _ = `Error ${
|
|
273
|
-
console.error(`[WIDGET] ${_}`, T),
|
|
272
|
+
const _ = `Error ${a.toLowerCase()}`;
|
|
273
|
+
console.error(`[WIDGET] ${_}`, T), c({ message: _, error: T });
|
|
274
274
|
}
|
|
275
275
|
},
|
|
276
|
-
[
|
|
276
|
+
[c]
|
|
277
277
|
);
|
|
278
|
-
return
|
|
279
|
-
|
|
278
|
+
return g(() => () => {
|
|
279
|
+
d.current = null, o.current = void 0, p.current = void 0;
|
|
280
|
+
const a = u.current;
|
|
281
|
+
a && (h("💥 Destroy widget", () => a.destroy()), u.current = null);
|
|
282
|
+
}, []), g(() => {
|
|
283
|
+
if (!l.current || JSON.stringify(d.current) === JSON.stringify(t))
|
|
280
284
|
return;
|
|
281
|
-
const
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
}) :
|
|
285
|
-
}, [t]),
|
|
286
|
-
if (!
|
|
285
|
+
const a = l.current, i = u.current;
|
|
286
|
+
d.current = t, i === null ? h("Creating a new widget", () => {
|
|
287
|
+
u.current = C(a, { params: t, provider: o.current, listeners: r }), p.current = r;
|
|
288
|
+
}) : h("Updating the widget", () => i.updateParams(t));
|
|
289
|
+
}, [t]), g(() => {
|
|
290
|
+
if (!u.current || o.current === n)
|
|
287
291
|
return;
|
|
288
292
|
o.current = n;
|
|
289
|
-
const
|
|
290
|
-
|
|
293
|
+
const a = l.current;
|
|
294
|
+
a && h("Updating the provider", () => {
|
|
291
295
|
var i;
|
|
292
|
-
(i =
|
|
296
|
+
(i = u.current) == null || i.destroy(), u.current = C(a, { params: t, provider: o.current, listeners: r });
|
|
293
297
|
});
|
|
294
|
-
}, [n]),
|
|
295
|
-
if (!
|
|
298
|
+
}, [n]), g(() => {
|
|
299
|
+
if (!u.current || p.current === r)
|
|
296
300
|
return;
|
|
297
|
-
const
|
|
298
|
-
|
|
301
|
+
const a = u.current;
|
|
302
|
+
h("Updating the listeners", () => a.updateListeners(r));
|
|
299
303
|
}, [r]), s ? /* @__PURE__ */ b("div", { style: { color: "#ff3a3a" }, children: [
|
|
300
304
|
s.message,
|
|
301
305
|
s.error.message && /* @__PURE__ */ v("pre", { style: { whiteSpace: "pre-wrap", fontSize: "0.75em" }, children: s.error.message })
|
|
302
|
-
] }) : /* @__PURE__ */ v("div", { ref:
|
|
306
|
+
] }) : /* @__PURE__ */ v("div", { ref: l, style: { width: "100%" } });
|
|
303
307
|
}
|
|
304
308
|
export {
|
|
305
309
|
ce as CowSwapWidget,
|
|
306
310
|
O as TradeType,
|
|
307
311
|
ae as WIDGET_PALETTE_COLORS,
|
|
308
|
-
|
|
312
|
+
E as WidgetMethodsEmit,
|
|
309
313
|
w as WidgetMethodsListen,
|
|
310
314
|
C as createCowSwapWidget,
|
|
311
315
|
Q as isCowSwapWidgetPalette,
|
|
312
|
-
|
|
313
|
-
|
|
316
|
+
R as listenToMessageFromWindow,
|
|
317
|
+
P as postMessageToWindow,
|
|
314
318
|
k as stopListeningToMessageFromWindow,
|
|
315
319
|
D as stopListeningWindowListener
|
|
316
320
|
};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@cowprotocol/widget-react",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.8.1",
|
|
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.10.0"
|
|
26
26
|
}
|
|
27
27
|
}
|