@gardenfi/react-hooks 2.5.3-beta.3 → 2.5.3-beta.5
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/index3.cjs +1 -1
- package/dist/index3.js +102 -42
- package/package.json +2 -2
package/dist/index3.cjs
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
"use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const
|
|
1
|
+
"use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const g=require("@gardenfi/orderbook"),o=require("react"),R=require("./index6.cjs"),k=(r,u)=>{const[T,m]=o.useState([]),a=o.useRef(null),p=o.useRef(!1),E=o.useRef(!1),I=5e3,f=o.useCallback(()=>{var s,i,l,c,t,n,d,y,P,b;if(!r)return[];const e=new Set;try{[(i=(s=r.htlcs)==null?void 0:s.evm)==null?void 0:i.htlcActorAddress,(c=(l=r.htlcs)==null?void 0:l.sui)==null?void 0:c.htlcActorAddress,(n=(t=r.htlcs)==null?void 0:t.solana)==null?void 0:n.htlcActorAddress,(y=(d=r.htlcs)==null?void 0:d.starknet)==null?void 0:y.htlcActorAddress,(b=(P=r.htlcs)==null?void 0:P.bitcoin)==null?void 0:b.htlcActorAddress].filter(O=>!!O&&O.length>0).forEach(O=>e.add(O.toLowerCase()))}catch(N){console.error("Error getting HTLC addresses:",N)}return Array.from(e)},[r]),S=o.useCallback(async()=>{if(r)try{const e=f();if(e.length===0){console.log("No HTLC addresses found for initial pending orders fetch");return}const s=e.map(async t=>{try{const n=await r.getOrders({address:t,status:g.OrderLifecycle.pending,per_page:500});return n.ok?n.val.data:(console.error(`Failed to fetch orders for address ${t}:`,n.error),[])}catch(n){return console.error(`Error fetching orders for address ${t}:`,n),[]}}),c=(await Promise.all(s)).flat().map(t=>t.order_id);if(c.length>0)try{u.setItem(R.PENDING_ORDERS_STORE,JSON.stringify(c))}catch(t){console.error("Error saving initial pending order IDs to localStorage",t)}}catch(e){console.error("Error fetching initial pending orders:",e)}},[r,u,f]),A=o.useCallback(async()=>{if(r)try{const e=u.getItem(R.PENDING_ORDERS_STORE),s=e?JSON.parse(e):[];if(s.length===0){m([]);return}const i=await Promise.all(s.map(t=>r.getOrder(t))),l=[],c=[];i.forEach((t,n)=>{if(!t.ok){c.push(s[n]);return}const d=t.val;d.status===g.OrderStatus.Redeemed||d.status===g.OrderStatus.Refunded||d.status===g.OrderStatus.Expired||(l.push(d),c.push(s[n]))});try{u.setItem(R.PENDING_ORDERS_STORE,JSON.stringify(c))}catch(t){console.error("Error persisting remaining pending order ids",t)}m(l)}catch(e){console.error("Error fetching pending orders:",e)}},[r,u]),h=o.useMemo(()=>{try{return f().join(",")}catch(e){return console.error("Error computing HTLC addresses:",e),""}},[f]),C=o.useRef("");return o.useEffect(()=>{if(!r||!r.redeemServiceEnabled||!h)return;C.current!==h&&(E.current=!1,C.current=h),E.current||(S(),E.current=!0)},[r,h,S]),o.useEffect(()=>{if(r)if(r.redeemServiceEnabled){let e=!1;const s=()=>{e||(a.current=setTimeout(i,I))},i=async()=>{if(!e){if(p.current){s();return}p.current=!0;try{await A()}finally{p.current=!1}s()}};return i(),()=>{e=!0,a.current&&(clearTimeout(a.current),a.current=null)}}else{const e=s=>m(s);return r.on("onPendingOrdersChanged",e),()=>{r.off("onPendingOrdersChanged",e)}}},[r,A]),{pendingOrders:T}};exports.useOrderbook=k;
|
package/dist/index3.js
CHANGED
|
@@ -1,66 +1,126 @@
|
|
|
1
|
-
import { OrderStatus as
|
|
2
|
-
import { useState as
|
|
3
|
-
import { PENDING_ORDERS_STORE as
|
|
4
|
-
const
|
|
5
|
-
const [
|
|
6
|
-
|
|
1
|
+
import { OrderLifecycle as b, OrderStatus as A } from "@gardenfi/orderbook";
|
|
2
|
+
import { useState as k, useRef as m, useCallback as E, useMemo as D, useEffect as L } from "react";
|
|
3
|
+
import { PENDING_ORDERS_STORE as C } from "./index6.js";
|
|
4
|
+
const x = (e, l) => {
|
|
5
|
+
const [N, p] = k([]), a = m(null), g = m(!1), y = m(!1), w = 5e3, u = E(() => {
|
|
6
|
+
var s, c, d, o, t, n, i, O, I, T;
|
|
7
|
+
if (!e) return [];
|
|
8
|
+
const r = /* @__PURE__ */ new Set();
|
|
9
|
+
try {
|
|
10
|
+
[
|
|
11
|
+
(c = (s = e.htlcs) == null ? void 0 : s.evm) == null ? void 0 : c.htlcActorAddress,
|
|
12
|
+
(o = (d = e.htlcs) == null ? void 0 : d.sui) == null ? void 0 : o.htlcActorAddress,
|
|
13
|
+
(n = (t = e.htlcs) == null ? void 0 : t.solana) == null ? void 0 : n.htlcActorAddress,
|
|
14
|
+
(O = (i = e.htlcs) == null ? void 0 : i.starknet) == null ? void 0 : O.htlcActorAddress,
|
|
15
|
+
(T = (I = e.htlcs) == null ? void 0 : I.bitcoin) == null ? void 0 : T.htlcActorAddress
|
|
16
|
+
].filter((h) => !!h && h.length > 0).forEach((h) => r.add(h.toLowerCase()));
|
|
17
|
+
} catch (v) {
|
|
18
|
+
console.error("Error getting HTLC addresses:", v);
|
|
19
|
+
}
|
|
20
|
+
return Array.from(r);
|
|
21
|
+
}, [e]), S = E(async () => {
|
|
22
|
+
if (e)
|
|
7
23
|
try {
|
|
8
|
-
const
|
|
24
|
+
const r = u();
|
|
9
25
|
if (r.length === 0) {
|
|
10
|
-
|
|
26
|
+
console.log("No HTLC addresses found for initial pending orders fetch");
|
|
11
27
|
return;
|
|
12
28
|
}
|
|
13
|
-
const
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
29
|
+
const s = r.map(async (t) => {
|
|
30
|
+
try {
|
|
31
|
+
const n = await e.getOrders({
|
|
32
|
+
address: t,
|
|
33
|
+
status: b.pending,
|
|
34
|
+
per_page: 500
|
|
35
|
+
});
|
|
36
|
+
return n.ok ? n.val.data : (console.error(
|
|
37
|
+
`Failed to fetch orders for address ${t}:`,
|
|
38
|
+
n.error
|
|
39
|
+
), []);
|
|
40
|
+
} catch (n) {
|
|
41
|
+
return console.error(`Error fetching orders for address ${t}:`, n), [];
|
|
42
|
+
}
|
|
43
|
+
}), o = (await Promise.all(s)).flat().map((t) => t.order_id);
|
|
44
|
+
if (o.length > 0)
|
|
45
|
+
try {
|
|
46
|
+
l.setItem(C, JSON.stringify(o));
|
|
47
|
+
} catch (t) {
|
|
48
|
+
console.error(
|
|
49
|
+
"Error saving initial pending order IDs to localStorage",
|
|
50
|
+
t
|
|
51
|
+
);
|
|
52
|
+
}
|
|
53
|
+
} catch (r) {
|
|
54
|
+
console.error("Error fetching initial pending orders:", r);
|
|
55
|
+
}
|
|
56
|
+
}, [e, l, u]), P = E(async () => {
|
|
57
|
+
if (e)
|
|
58
|
+
try {
|
|
59
|
+
const r = l.getItem(C), s = r ? JSON.parse(r) : [];
|
|
60
|
+
if (s.length === 0) {
|
|
61
|
+
p([]);
|
|
62
|
+
return;
|
|
63
|
+
}
|
|
64
|
+
const c = await Promise.all(s.map((t) => e.getOrder(t))), d = [], o = [];
|
|
65
|
+
c.forEach((t, n) => {
|
|
66
|
+
if (!t.ok) {
|
|
67
|
+
o.push(s[n]);
|
|
17
68
|
return;
|
|
18
69
|
}
|
|
19
|
-
const i =
|
|
20
|
-
i.status ===
|
|
70
|
+
const i = t.val;
|
|
71
|
+
i.status === A.Redeemed || i.status === A.Refunded || i.status === A.Expired || (d.push(i), o.push(s[n]));
|
|
21
72
|
});
|
|
22
73
|
try {
|
|
23
|
-
|
|
24
|
-
} catch (
|
|
25
|
-
console.error("Error persisting remaining pending order ids",
|
|
74
|
+
l.setItem(C, JSON.stringify(o));
|
|
75
|
+
} catch (t) {
|
|
76
|
+
console.error("Error persisting remaining pending order ids", t);
|
|
26
77
|
}
|
|
27
|
-
|
|
28
|
-
} catch (
|
|
29
|
-
console.error("Error fetching pending orders:",
|
|
78
|
+
p(d);
|
|
79
|
+
} catch (r) {
|
|
80
|
+
console.error("Error fetching pending orders:", r);
|
|
30
81
|
}
|
|
31
|
-
}, [
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
82
|
+
}, [e, l]), f = D(() => {
|
|
83
|
+
try {
|
|
84
|
+
return u().join(",");
|
|
85
|
+
} catch (r) {
|
|
86
|
+
return console.error("Error computing HTLC addresses:", r), "";
|
|
87
|
+
}
|
|
88
|
+
}, [u]), R = m("");
|
|
89
|
+
return L(() => {
|
|
90
|
+
if (!e || !e.redeemServiceEnabled || !f) return;
|
|
91
|
+
R.current !== f && (y.current = !1, R.current = f), y.current || (S(), y.current = !0);
|
|
92
|
+
}, [e, f, S]), L(() => {
|
|
93
|
+
if (e)
|
|
94
|
+
if (e.redeemServiceEnabled) {
|
|
95
|
+
let r = !1;
|
|
96
|
+
const s = () => {
|
|
97
|
+
r || (a.current = setTimeout(c, w));
|
|
98
|
+
}, c = async () => {
|
|
99
|
+
if (!r) {
|
|
100
|
+
if (g.current) {
|
|
101
|
+
s();
|
|
42
102
|
return;
|
|
43
103
|
}
|
|
44
|
-
|
|
104
|
+
g.current = !0;
|
|
45
105
|
try {
|
|
46
|
-
await
|
|
106
|
+
await P();
|
|
47
107
|
} finally {
|
|
48
|
-
|
|
108
|
+
g.current = !1;
|
|
49
109
|
}
|
|
50
|
-
|
|
110
|
+
s();
|
|
51
111
|
}
|
|
52
112
|
};
|
|
53
|
-
return
|
|
54
|
-
|
|
113
|
+
return c(), () => {
|
|
114
|
+
r = !0, a.current && (clearTimeout(a.current), a.current = null);
|
|
55
115
|
};
|
|
56
116
|
} else {
|
|
57
|
-
const
|
|
58
|
-
return
|
|
59
|
-
|
|
117
|
+
const r = (s) => p(s);
|
|
118
|
+
return e.on("onPendingOrdersChanged", r), () => {
|
|
119
|
+
e.off("onPendingOrdersChanged", r);
|
|
60
120
|
};
|
|
61
121
|
}
|
|
62
|
-
}, [
|
|
122
|
+
}, [e, P]), { pendingOrders: N };
|
|
63
123
|
};
|
|
64
124
|
export {
|
|
65
|
-
|
|
125
|
+
x as useOrderbook
|
|
66
126
|
};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@gardenfi/react-hooks",
|
|
3
|
-
"version": "2.5.3-beta.
|
|
3
|
+
"version": "2.5.3-beta.5",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"main": "./dist/index.cjs",
|
|
6
6
|
"module": "./dist/index.js",
|
|
@@ -27,7 +27,7 @@
|
|
|
27
27
|
"registry": "https://registry.npmjs.org/"
|
|
28
28
|
},
|
|
29
29
|
"dependencies": {
|
|
30
|
-
"@gardenfi/core": "2.5.3-beta.
|
|
30
|
+
"@gardenfi/core": "2.5.3-beta.5",
|
|
31
31
|
"@gardenfi/orderbook": "2.5.3-beta.4",
|
|
32
32
|
"@gardenfi/utils": "2.5.3-beta.3",
|
|
33
33
|
"react": "^18.3.1",
|