Skip to content

Commit

Permalink
change swift order subscriber
Browse files Browse the repository at this point in the history
  • Loading branch information
NourAlharithi committed Jan 14, 2025
1 parent 8449a4b commit 1edde10
Showing 1 changed file with 19 additions and 10 deletions.
29 changes: 19 additions & 10 deletions sdk/src/swift/swiftOrderSubscriber.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,15 +31,14 @@ export class SwiftOrderSubscriber {
private ws: WebSocket | null = null;
private driftClient: DriftClient;
private userMap: UserMap;
private onOrder: (
orderMessageRaw: any,
swiftOrderParamsMessage: SwiftOrderParamsMessage
) => Promise<void>;

subscribed = false;

constructor(
private config: SwiftOrderSubscriberConfig,
private onOrder: (
orderMessageRaw: any,
swiftOrderParamsMessage: SwiftOrderParamsMessage
) => Promise<void>
) {
constructor(private config: SwiftOrderSubscriberConfig) {
this.driftClient = config.driftClient;
this.userMap = config.userMap;
}
Expand Down Expand Up @@ -91,7 +90,14 @@ export class SwiftOrderSubscriber {
}
}

async subscribe(): Promise<void> {
async subscribe(
onOrder: (
orderMessageRaw: any,
swiftOrderParamsMessage: SwiftOrderParamsMessage
) => Promise<void>
): Promise<void> {
this.onOrder = onOrder;

const endpoint =
this.config.endpoint || this.config.driftEnv === 'devnet'
? 'wss://master.swift.drift.trade/ws'
Expand Down Expand Up @@ -132,7 +138,7 @@ export class SwiftOrderSubscriber {
return;
}

this.onOrder(order, swiftOrderParamsMessage);
onOrder(order, swiftOrderParamsMessage);
}
});

Expand Down Expand Up @@ -193,6 +199,9 @@ export class SwiftOrderSubscriber {
if (this.heartbeatTimeout) {
clearTimeout(this.heartbeatTimeout);
}
if (!this.onOrder) {
throw new Error('onOrder callback function must be set');
}
this.heartbeatTimeout = setTimeout(() => {
console.warn('No heartbeat received within 30 seconds, reconnecting...');
this.reconnect();
Expand All @@ -207,7 +216,7 @@ export class SwiftOrderSubscriber {

console.log('Reconnecting to WebSocket...');
setTimeout(() => {
this.subscribe();
this.subscribe(this.onOrder);
}, 1000);
}
}

0 comments on commit 1edde10

Please sign in to comment.