@hyperix/hooks 0.1.4 → 0.1.6

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/README.md CHANGED
@@ -6,20 +6,6 @@ React hooks for the local workspace.
6
6
 
7
7
  This package is intended for source-first usage inside the monorepo. Consumers should reference it with `"workspace:*"` and let the app bundler resolve the TypeScript source.
8
8
 
9
- ## Example
10
-
11
- ```tsx
12
- import { useState } from "react";
13
- import { useDebouncedValue } from "@bunstack/hooks";
14
-
15
- function Search() {
16
- const [query, setQuery] = useState("");
17
- const debouncedQuery = useDebouncedValue(query, 300);
18
-
19
- return <input value={query} onChange={(e) => setQuery(e.target.value)} />;
20
- }
21
- ```
22
-
23
9
  ## Tests
24
10
 
25
11
  ```bash
package/dist/index.d.ts CHANGED
@@ -1,6 +1,5 @@
1
- import * as _outofgas_react_stream from '@outofgas/react-stream';
2
-
3
- declare function useDebouncedValue<T>(value: T, delayMs?: number): T;
1
+ import { UseSubscribeState } from '@outofgas/react-stream';
2
+ import { TradesEvent } from '@nktkas/hyperliquid/api/subscription';
4
3
 
5
4
  type L2BookLevel = [price: number, size: number, cumulativeSize: number];
6
5
  type L2Book = {
@@ -18,10 +17,19 @@ type UseL2BookOptions = {
18
17
  depth?: number;
19
18
  nSigFigs?: number;
20
19
  };
20
+ declare const EMPTY_L2_BOOK: L2Book;
21
21
  declare function calcOrderbookLevels(price: number): {
22
22
  nSigFigs: number;
23
23
  tick: string;
24
24
  }[];
25
- declare const useL2Book: (coin: string, options?: UseL2BookOptions) => _outofgas_react_stream.UseSubscribeState<L2Book>;
25
+ declare const useL2Book: (coin: string, options?: UseL2BookOptions) => UseSubscribeState<L2Book>;
26
+
27
+ type Trade = TradesEvent[number];
28
+ type UseTradesOptions = {
29
+ limit?: number;
30
+ enabled?: boolean;
31
+ };
32
+ declare const EMPTY_TRADES: Trade[];
33
+ declare function useTrades(coin: string, options?: UseTradesOptions): UseSubscribeState<Trade[]>;
26
34
 
27
- export { type L2Book, type L2BookLevel, type UseL2BookOptions, calcOrderbookLevels, useDebouncedValue, useL2Book };
35
+ export { EMPTY_L2_BOOK, EMPTY_TRADES, type L2Book, type L2BookLevel, type Trade, type UseL2BookOptions, type UseTradesOptions, calcOrderbookLevels, useL2Book, useTrades };
package/dist/index.js CHANGED
@@ -1,17 +1,4 @@
1
- // src/use-debounced-value.ts
2
- import { useEffect, useState } from "react";
3
- function useDebouncedValue(value, delayMs = 300) {
4
- const [debouncedValue, setDebouncedValue] = useState(value);
5
- useEffect(() => {
6
- const timeout = setTimeout(() => {
7
- setDebouncedValue(value);
8
- }, delayMs);
9
- return () => clearTimeout(timeout);
10
- }, [value, delayMs]);
11
- return debouncedValue;
12
- }
13
-
14
- // ../../node_modules/.bun/decimal.js@10.6.0/node_modules/decimal.js/decimal.mjs
1
+ // ../../node_modules/decimal.js/decimal.mjs
15
2
  var EXP_LIMIT = 9e15;
16
3
  var MAX_DIGITS = 1e9;
17
4
  var NUMERALS = "0123456789abcdef";
@@ -2266,10 +2253,41 @@ var useL2Book = (coin, options = {}) => {
2266
2253
  }
2267
2254
  });
2268
2255
  };
2256
+
2257
+ // src/use-trades.ts
2258
+ import { useSubscribe as useSubscribe2 } from "@outofgas/react-stream";
2259
+ var EMPTY_TRADES = [];
2260
+ function formatTrades(previousTrades, incomingTrades, limit) {
2261
+ const uniqueTrades = Array.from(
2262
+ new Map([...incomingTrades, ...previousTrades].map((trade) => [trade.tid, trade])).values()
2263
+ );
2264
+ return uniqueTrades.sort((a, b) => b.time - a.time).slice(0, limit);
2265
+ }
2266
+ function useTrades(coin, options = {}) {
2267
+ const { limit = 60, enabled: enabledOverride } = options;
2268
+ const enabled = enabledOverride ?? Boolean(coin);
2269
+ return useSubscribe2({
2270
+ key: ["trades", coin, limit],
2271
+ enabled,
2272
+ initialData: EMPTY_TRADES,
2273
+ subscribe: async ({ onData }) => {
2274
+ let trades = EMPTY_TRADES;
2275
+ const subscription = await wsClient.trades({ coin }, (msg) => {
2276
+ trades = formatTrades(trades, msg, limit);
2277
+ onData(trades);
2278
+ });
2279
+ return {
2280
+ unsubscribe: () => subscription.unsubscribe()
2281
+ };
2282
+ }
2283
+ });
2284
+ }
2269
2285
  export {
2286
+ EMPTY_L2_BOOK,
2287
+ EMPTY_TRADES,
2270
2288
  calcOrderbookLevels,
2271
- useDebouncedValue,
2272
- useL2Book
2289
+ useL2Book,
2290
+ useTrades
2273
2291
  };
2274
2292
  /*! Bundled license information:
2275
2293
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@hyperix/hooks",
3
- "version": "0.1.4",
3
+ "version": "0.1.6",
4
4
  "type": "module",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",