@pear-protocol/chart-sdk
Advanced tools
+0
-1
@@ -6,3 +6,2 @@ import { Connector } from '@pear-protocol/types'; | ||
| private collector; | ||
| private subscriptions; | ||
| private baselinePrices; | ||
@@ -9,0 +8,0 @@ constructor(config?: ChartConfig); |
+6
-18
@@ -6,3 +6,2 @@ import { DataCollector } from './collector'; | ||
| collector; | ||
| subscriptions = /* @__PURE__ */ new Map(); | ||
| baselinePrices = {}; | ||
@@ -66,3 +65,5 @@ constructor(config) { | ||
| } | ||
| const rawListenerId = this.collector.addRealtimeListener(() => { | ||
| const formatTokens = (tokens) => tokens.map((t) => `${t.symbol}:${t.weight}`).join(","); | ||
| const id = `${chartType}-${formatTokens(this.collector.getLongTokens())}-${formatTokens(this.collector.getShortTokens())}`; | ||
| this.collector.addRealtimeListener(id, () => { | ||
| const snapshot = this.collector.getLatestCandles(); | ||
@@ -103,5 +104,2 @@ if (!snapshot) return; | ||
| }); | ||
| const formatTokens = (tokens) => tokens.map((t) => `${t.symbol}:${t.weight}`).join(","); | ||
| const id = `${chartType}-${formatTokens(this.collector.getLongTokens())}-${formatTokens(this.collector.getShortTokens())}`; | ||
| this.subscriptions.set(id, { chartType, callback, rawListenerId }); | ||
| return id; | ||
@@ -114,3 +112,4 @@ } | ||
| } | ||
| const rawListenerId = this.collector.addRealtimeListener(() => { | ||
| const id = `price-${symbol}`; | ||
| this.collector.addRealtimeListener(id, () => { | ||
| const snapshot = this.collector.getLatestCandles(); | ||
@@ -130,14 +129,6 @@ if (!snapshot) return; | ||
| }); | ||
| const id = `price-${symbol}`; | ||
| this.subscriptions.set(id, { chartType: "price", callback, rawListenerId }); | ||
| return id; | ||
| } | ||
| unsubscribeRealtimeBars(id) { | ||
| const sub = this.subscriptions.get(id); | ||
| if (!sub) return; | ||
| this.collector.removeRealtimeListener(sub.rawListenerId); | ||
| this.subscriptions.delete(id); | ||
| if (this.subscriptions.size === 0) { | ||
| this.collector.disconnectWs(); | ||
| } | ||
| this.collector.removeRealtimeListener(id); | ||
| } | ||
@@ -155,5 +146,2 @@ assertTokenExists(symbol) { | ||
| destroy() { | ||
| for (const [id] of this.subscriptions) { | ||
| this.unsubscribeRealtimeBars(id); | ||
| } | ||
| this.collector.destroy(); | ||
@@ -160,0 +148,0 @@ } |
@@ -23,3 +23,3 @@ import { Connector } from '@pear-protocol/types'; | ||
| get isWsConnected(): boolean; | ||
| addRealtimeListener(cb: RealtimeCandleCallback): string; | ||
| addRealtimeListener(id: string, cb: RealtimeCandleCallback): void; | ||
| removeRealtimeListener(id: string): void; | ||
@@ -26,0 +26,0 @@ getLatestCandles(): Record<string, CandleData> | null; |
@@ -27,4 +27,4 @@ import { CandleCache } from '../cache'; | ||
| this.shortTokens = shortTokens; | ||
| this.syncWsSubscriptions(); | ||
| this.syncTokenChanges(); | ||
| this.syncWsSubscriptions(); | ||
| } | ||
@@ -89,6 +89,4 @@ setCandleInterval(interval) { | ||
| } | ||
| addRealtimeListener(cb) { | ||
| const id = Math.random().toString(36).slice(2); | ||
| addRealtimeListener(id, cb) { | ||
| this.realtimeListeners.set(id, cb); | ||
| return id; | ||
| } | ||
@@ -199,6 +197,7 @@ removeRealtimeListener(id) { | ||
| } | ||
| // Helper method to synchronize WebSocket subscriptions based on current tokens and interval | ||
| syncWsSubscriptions() { | ||
| if (!this.candleWs) return; | ||
| const currentSymbols = new Set(this.getAllTokens().map((t) => t.symbol)); | ||
| for (const [symbol] of this.latestCandles) { | ||
| for (const symbol of this.prevTokenSymbols) { | ||
| if (!currentSymbols.has(symbol)) { | ||
@@ -209,2 +208,6 @@ this.candleWs.unsubscribe(symbol, this.candleInterval); | ||
| } | ||
| if (currentSymbols.size === 0) { | ||
| this.destroy(); | ||
| return; | ||
| } | ||
| for (const symbol of currentSymbols) { | ||
@@ -211,0 +214,0 @@ this.candleWs.subscribe(symbol, this.candleInterval); |
+1
-6
@@ -36,7 +36,2 @@ import { Connector } from '@pear-protocol/types'; | ||
| } | ||
| interface RealtimeSubscription { | ||
| chartType: ChartType; | ||
| callback: RealtimeBarCallback; | ||
| rawListenerId: string; | ||
| } | ||
| type RealtimeCandleCallback = (symbol: string, candle: CandleData) => void; | ||
@@ -61,2 +56,2 @@ type ChartType = 'weighted-ratio' | 'price-ratio' | 'performance' | 'price'; | ||
| export type { Bar, CandleData, CandleInterval, ChartConfig, ChartType, HistoricalRange, MessageListener, PerformanceResult, RealtimeBarCallback, RealtimeCandleCallback, RealtimeSubscription, TokenHistoricalPriceData, TokenSelection, WsMessage }; | ||
| export type { Bar, CandleData, CandleInterval, ChartConfig, ChartType, HistoricalRange, MessageListener, PerformanceResult, RealtimeBarCallback, RealtimeCandleCallback, TokenHistoricalPriceData, TokenSelection, WsMessage }; |
+1
-1
| { | ||
| "name": "@pear-protocol/chart-sdk", | ||
| "version": "0.0.6", | ||
| "version": "0.0.7", | ||
| "description": "Pear Protocol Chart SDK", | ||
@@ -5,0 +5,0 @@ "private": false, |
URL strings
Supply chain riskPackage contains fragments of external URLs or IP addresses, which the package may be accessing at runtime.
URL strings
Supply chain riskPackage contains fragments of external URLs or IP addresses, which the package may be accessing at runtime.
60634
-0.94%1770
-0.84%