@orderly.network/hooks 1.0.28-alpha.16 → 1.0.28-alpha.17

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/index.d.mts CHANGED
@@ -24,7 +24,7 @@ declare global {
24
24
  };
25
25
  }
26
26
  }
27
- declare const _default: "1.0.28-alpha.16";
27
+ declare const _default: "1.0.28-alpha.17";
28
28
 
29
29
  type useQueryOptions<T> = SWRConfiguration & {
30
30
  formatter?: (data: any) => T;
@@ -277,8 +277,7 @@ declare const usePositionStream: (symbol?: string, options?: SWRConfiguration &
277
277
  }, any, {
278
278
  readonly loading: false;
279
279
  readonly error: any;
280
- readonly loadMore: () => void;
281
- readonly refresh: () => void;
280
+ readonly refresh: swr__internal.KeyedMutator<API.PositionInfo>;
282
281
  }];
283
282
 
284
283
  type Params = {
@@ -290,10 +289,11 @@ type Params = {
290
289
  declare const useOrderStream: (params: Params) => readonly [any[] | null, {
291
290
  readonly total: any;
292
291
  readonly isLoading: boolean;
292
+ readonly refresh: swr__internal.KeyedMutator<any[]>;
293
293
  readonly loadMore: () => void;
294
294
  readonly cancelAllOrders: () => void;
295
295
  readonly updateOrder: (orderId: string, order: OrderEntity) => Promise<any>;
296
- readonly cancelOrder: (orderId: string, symbol?: string) => Promise<any>;
296
+ readonly cancelOrder: (orderId: number, symbol?: string) => Promise<any>;
297
297
  readonly errors: {
298
298
  readonly cancelOrder: unknown;
299
299
  readonly updateOrder: unknown;
@@ -424,7 +424,11 @@ declare const useSettleSubscription: (options?: {
424
424
  onMessage?: ((data: any) => void) | undefined;
425
425
  } | undefined) => swr_subscription.SWRSubscriptionResponse<any, any>;
426
426
 
427
- declare const usePrivateDataObserver: () => void;
427
+ type getKeyFunction = (index: number, prevData: any) => string | null;
428
+
429
+ declare const usePrivateDataObserver: (options: {
430
+ getKeysMap: (type: string) => Map<string, getKeyFunction>;
431
+ }) => void;
428
432
 
429
433
  declare function useMediaQuery(query: string): boolean;
430
434
 
package/dist/index.d.ts CHANGED
@@ -24,7 +24,7 @@ declare global {
24
24
  };
25
25
  }
26
26
  }
27
- declare const _default: "1.0.28-alpha.16";
27
+ declare const _default: "1.0.28-alpha.17";
28
28
 
29
29
  type useQueryOptions<T> = SWRConfiguration & {
30
30
  formatter?: (data: any) => T;
@@ -277,8 +277,7 @@ declare const usePositionStream: (symbol?: string, options?: SWRConfiguration &
277
277
  }, any, {
278
278
  readonly loading: false;
279
279
  readonly error: any;
280
- readonly loadMore: () => void;
281
- readonly refresh: () => void;
280
+ readonly refresh: swr__internal.KeyedMutator<API.PositionInfo>;
282
281
  }];
283
282
 
284
283
  type Params = {
@@ -290,10 +289,11 @@ type Params = {
290
289
  declare const useOrderStream: (params: Params) => readonly [any[] | null, {
291
290
  readonly total: any;
292
291
  readonly isLoading: boolean;
292
+ readonly refresh: swr__internal.KeyedMutator<any[]>;
293
293
  readonly loadMore: () => void;
294
294
  readonly cancelAllOrders: () => void;
295
295
  readonly updateOrder: (orderId: string, order: OrderEntity) => Promise<any>;
296
- readonly cancelOrder: (orderId: string, symbol?: string) => Promise<any>;
296
+ readonly cancelOrder: (orderId: number, symbol?: string) => Promise<any>;
297
297
  readonly errors: {
298
298
  readonly cancelOrder: unknown;
299
299
  readonly updateOrder: unknown;
@@ -424,7 +424,11 @@ declare const useSettleSubscription: (options?: {
424
424
  onMessage?: ((data: any) => void) | undefined;
425
425
  } | undefined) => swr_subscription.SWRSubscriptionResponse<any, any>;
426
426
 
427
- declare const usePrivateDataObserver: () => void;
427
+ type getKeyFunction = (index: number, prevData: any) => string | null;
428
+
429
+ declare const usePrivateDataObserver: (options: {
430
+ getKeysMap: (type: string) => Map<string, getKeyFunction>;
431
+ }) => void;
428
432
 
429
433
  declare function useMediaQuery(query: string): boolean;
430
434
 
package/dist/index.js CHANGED
@@ -28,9 +28,9 @@ var useSWRSubscription__default = /*#__PURE__*/_interopDefault(useSWRSubscriptio
28
28
  // src/version.ts
29
29
  if (typeof window !== "undefined") {
30
30
  window.__ORDERLY_VERSION__ = window.__ORDERLY_VERSION__ || {};
31
- window.__ORDERLY_VERSION__["@orderly.network/hooks"] = "1.0.28-alpha.16";
31
+ window.__ORDERLY_VERSION__["@orderly.network/hooks"] = "1.0.28-alpha.17";
32
32
  }
33
- var version_default = "1.0.28-alpha.16";
33
+ var version_default = "1.0.28-alpha.17";
34
34
  var fetcher = (url, init = {}, queryOptions) => net.get(url, init, queryOptions?.formatter);
35
35
  var OrderlyContext = React.createContext({
36
36
  // configStore: new MemoryConfigStore(),
@@ -444,31 +444,28 @@ var useWS = () => {
444
444
  });
445
445
  return ws;
446
446
  };
447
- var usePrivateDataObserver = () => {
447
+ var usePrivateDataObserver = (options) => {
448
448
  const ws = useWS();
449
449
  const { mutate: mutate2 } = useSWR.useSWRConfig();
450
450
  const ee = useEventEmitter();
451
451
  const { state } = useAccount();
452
- const updateOrders = useDebounce.useDebouncedCallback(() => {
453
- mutate2(
454
- useSWRInfinite.unstable_serialize(() => [
455
- `/v1/orders?size=100&page=1&status=${types.OrderStatus.INCOMPLETE}`,
456
- state.accountId
457
- ])
458
- );
459
- mutate2(
460
- useSWRInfinite.unstable_serialize(() => [
461
- `/v1/orders?size=100&page=1&status=${types.OrderStatus.NEW}`,
462
- state.accountId
463
- ])
464
- );
452
+ const updateOrders = useDebounce.useDebouncedCallback((data) => {
453
+ const map = options.getKeysMap("orders");
454
+ map.forEach((getKey, key) => {
455
+ mutate2(
456
+ useSWRInfinite.unstable_serialize((index, prevData) => [
457
+ getKey(index, prevData),
458
+ state.accountId
459
+ ])
460
+ );
461
+ });
465
462
  }, 500);
466
463
  React.useEffect(() => {
467
464
  if (!state.accountId)
468
465
  return;
469
466
  const unsubscribe = ws.privateSubscribe("executionreport", {
470
467
  onMessage: (data) => {
471
- updateOrders();
468
+ updateOrders(data);
472
469
  ee.emit("orders:changed", data);
473
470
  }
474
471
  });
@@ -532,15 +529,31 @@ var usePrivateDataObserver = () => {
532
529
  var DataCenterContext = React.createContext(
533
530
  {}
534
531
  );
532
+ var useDataCenterContext = () => React.useContext(DataCenterContext);
535
533
  var DataCenterProvider = ({ children }) => {
536
534
  const { error, done } = usePreLoadData();
537
- usePrivateDataObserver();
535
+ const getKeyHandlerMapRef = React.useRef(/* @__PURE__ */ new Map());
536
+ usePrivateDataObserver({
537
+ getKeysMap(type) {
538
+ return getKeyHandlerMapRef.current;
539
+ }
540
+ });
538
541
  if (error) {
539
542
  return /* @__PURE__ */ jsxRuntime.jsx("div", { children: "Data load failed" });
540
543
  }
541
544
  if (!done)
542
545
  return null;
543
- return /* @__PURE__ */ jsxRuntime.jsx(DataCenterContext.Provider, { value: {}, children });
546
+ return /* @__PURE__ */ jsxRuntime.jsx(
547
+ DataCenterContext.Provider,
548
+ {
549
+ value: {
550
+ regesterKeyHandler: (key, fun) => {
551
+ getKeyHandlerMapRef.current.set(key, fun);
552
+ }
553
+ },
554
+ children
555
+ }
556
+ );
544
557
  };
545
558
  var OrderlyConfigProvider = (props) => {
546
559
  const [account5, setAccount] = React__default.default.useState(null);
@@ -1271,7 +1284,7 @@ var usePositionStream = (symbol, options) => {
1271
1284
  const {
1272
1285
  data,
1273
1286
  error,
1274
- mutate: updatePositions
1287
+ mutate: refreshPositions
1275
1288
  } = usePrivateQuery(`/v1/positions`, {
1276
1289
  // revalidateOnFocus: false,
1277
1290
  // revalidateOnReconnect: false,
@@ -1368,7 +1381,16 @@ var usePositionStream = (symbol, options) => {
1368
1381
  unsettledPnL: unsettlementPnL_total.toNumber()
1369
1382
  }
1370
1383
  ];
1371
- }, [data?.rows, symbolInfo, accountInfo, markPrices, priceMode, tickerPrices, symbol, holding]);
1384
+ }, [
1385
+ data?.rows,
1386
+ symbolInfo,
1387
+ accountInfo,
1388
+ markPrices,
1389
+ priceMode,
1390
+ tickerPrices,
1391
+ symbol,
1392
+ holding
1393
+ ]);
1372
1394
  const [totalCollateral, totalValue, totalUnrealizedROI] = React.useMemo(() => {
1373
1395
  if (!holding || !markPrices) {
1374
1396
  return [utils.zero, utils.zero, 0];
@@ -1441,10 +1463,8 @@ var usePositionStream = (symbol, options) => {
1441
1463
  loading: false,
1442
1464
  // showSymbol,
1443
1465
  error,
1444
- loadMore: () => {
1445
- },
1446
- refresh: () => {
1447
- }
1466
+ // loadMore: () => {},
1467
+ refresh: refreshPositions
1448
1468
  }
1449
1469
  ];
1450
1470
  };
@@ -1505,6 +1525,7 @@ var useHoldingStream = () => {
1505
1525
  var useOrderStream = (params) => {
1506
1526
  const { status, symbol, side, size = 100 } = params;
1507
1527
  const { data: markPrices = {} } = useMarkPricesStream();
1528
+ const { regesterKeyHandler } = useDataCenterContext();
1508
1529
  const [
1509
1530
  doCancelOrder,
1510
1531
  { error: cancelOrderError, isMutating: cancelMutating }
@@ -1513,45 +1534,56 @@ var useOrderStream = (params) => {
1513
1534
  doUpdateOrder,
1514
1535
  { error: updateOrderError, isMutating: updateMutating }
1515
1536
  ] = useMutation("/v1/order", "PUT");
1516
- const ordersResponse = usePrivateInfiniteQuery(
1517
- (pageIndex, previousPageData) => {
1518
- if (previousPageData && !previousPageData.rows?.length)
1519
- return null;
1520
- const search = new URLSearchParams([
1521
- ["size", size.toString()],
1522
- ["page", `${pageIndex + 1}`]
1523
- ]);
1524
- if (status) {
1525
- search.set(`status`, status);
1526
- }
1527
- if (symbol) {
1528
- search.set(`symbol`, symbol);
1529
- }
1530
- if (side) {
1531
- search.set(`side`, side);
1532
- }
1533
- return `/v1/orders?${search.toString()}`;
1534
- },
1535
- {
1536
- initialSize: 1,
1537
- // revalidateFirstPage: false,
1538
- // onError: (err) => {
1539
- // console.error("fetch failed::::", err);
1540
- // },
1541
- formatter: (data) => data
1537
+ const getKey = (pageIndex, previousPageData) => {
1538
+ if (previousPageData && !previousPageData.rows?.length)
1539
+ return null;
1540
+ const search = new URLSearchParams([
1541
+ ["size", size.toString()],
1542
+ ["page", `${pageIndex + 1}`]
1543
+ ]);
1544
+ if (status) {
1545
+ search.set(`status`, status);
1542
1546
  }
1543
- );
1544
- const orders = React.useMemo(() => {
1547
+ if (symbol) {
1548
+ search.set(`symbol`, symbol);
1549
+ }
1550
+ if (side) {
1551
+ search.set(`side`, side);
1552
+ }
1553
+ return `/v1/orders?${search.toString()}`;
1554
+ };
1555
+ React.useEffect(() => {
1556
+ const key = `orders:${status}:${symbol}:${side}`;
1557
+ regesterKeyHandler(key, getKey);
1558
+ }, [status, symbol, side]);
1559
+ const ordersResponse = usePrivateInfiniteQuery(getKey, {
1560
+ initialSize: 1,
1561
+ // revalidateFirstPage: false,
1562
+ // onError: (err) => {
1563
+ // console.error("fetch failed::::", err);
1564
+ // },
1565
+ formatter: (data) => data
1566
+ });
1567
+ const flattenOrders = React.useMemo(() => {
1545
1568
  if (!ordersResponse.data) {
1546
1569
  return null;
1547
1570
  }
1548
- return ordersResponse.data?.map((item) => item.rows)?.flat().map((item) => {
1571
+ return ordersResponse.data?.map((item) => item.rows)?.flat();
1572
+ }, [ordersResponse.data]);
1573
+ const orders = React.useMemo(() => {
1574
+ if (!flattenOrders) {
1575
+ return null;
1576
+ }
1577
+ if (status !== types.OrderStatus.NEW) {
1578
+ return flattenOrders;
1579
+ }
1580
+ return flattenOrders.map((item) => {
1549
1581
  return {
1550
1582
  ...item,
1551
1583
  mark_price: markPrices[item.symbol] ?? 0
1552
1584
  };
1553
1585
  });
1554
- }, [ordersResponse.data, markPrices]);
1586
+ }, [flattenOrders, markPrices, status]);
1555
1587
  const total = React.useMemo(() => {
1556
1588
  return ordersResponse.data?.[0]?.meta?.total || 0;
1557
1589
  }, [ordersResponse.data?.[0]?.meta?.total]);
@@ -1564,7 +1596,7 @@ var useOrderStream = (params) => {
1564
1596
  return doCancelOrder(null, {
1565
1597
  order_id: orderId,
1566
1598
  symbol: symbol2,
1567
- source: "mweb"
1599
+ source: `SDK_${version_default}`
1568
1600
  }).then((res) => {
1569
1601
  if (res.success) {
1570
1602
  return res;
@@ -1581,6 +1613,7 @@ var useOrderStream = (params) => {
1581
1613
  {
1582
1614
  total,
1583
1615
  isLoading: ordersResponse.isLoading,
1616
+ refresh: ordersResponse.mutate,
1584
1617
  loadMore,
1585
1618
  cancelAllOrders,
1586
1619
  updateOrder,