@drift-labs/sdk 2.43.0-beta.12 → 2.43.0-beta.13

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/VERSION CHANGED
@@ -1 +1 @@
1
- 2.43.0-beta.12
1
+ 2.43.0-beta.13
@@ -13,6 +13,7 @@ export declare class WebSocketAccountSubscriber<T> implements AccountSubscriber<
13
13
  onChange: (data: T) => void;
14
14
  listenerId?: number;
15
15
  resubTimeoutMs?: number;
16
+ isUnsubscribing: boolean;
16
17
  timeoutId?: NodeJS.Timeout;
17
18
  receivingData: boolean;
18
19
  constructor(accountName: string, program: Program, accountPublicKey: PublicKey, decodeBuffer?: (buffer: Buffer) => T, resubTimeoutMs?: number);
@@ -4,6 +4,7 @@ exports.WebSocketAccountSubscriber = void 0;
4
4
  const utils_1 = require("./utils");
5
5
  class WebSocketAccountSubscriber {
6
6
  constructor(accountName, program, accountPublicKey, decodeBuffer, resubTimeoutMs) {
7
+ this.isUnsubscribing = false;
7
8
  this.accountName = accountName;
8
9
  this.program = program;
9
10
  this.accountPublicKey = accountPublicKey;
@@ -12,7 +13,7 @@ class WebSocketAccountSubscriber {
12
13
  this.receivingData = false;
13
14
  }
14
15
  async subscribe(onChange) {
15
- if (this.listenerId) {
16
+ if (this.listenerId || this.isUnsubscribing) {
16
17
  return;
17
18
  }
18
19
  this.onChange = onChange;
@@ -49,6 +50,10 @@ class WebSocketAccountSubscriber {
49
50
  throw new Error('onChange callback function must be set');
50
51
  }
51
52
  this.timeoutId = setTimeout(async () => {
53
+ if (this.isUnsubscribing) {
54
+ // If we are in the process of unsubscribing, do not attempt to resubscribe
55
+ return;
56
+ }
52
57
  if (this.receivingData) {
53
58
  console.log(`No ws data from ${this.accountName} in ${this.resubTimeoutMs}ms, resubscribing`);
54
59
  await this.unsubscribe();
@@ -108,11 +113,21 @@ class WebSocketAccountSubscriber {
108
113
  }
109
114
  }
110
115
  unsubscribe() {
116
+ this.isUnsubscribing = true;
117
+ clearTimeout(this.timeoutId);
118
+ this.timeoutId = undefined;
111
119
  if (this.listenerId) {
112
- const promise = this.program.provider.connection.removeAccountChangeListener(this.listenerId);
113
- this.listenerId = undefined;
120
+ const promise = this.program.provider.connection
121
+ .removeAccountChangeListener(this.listenerId)
122
+ .then(() => {
123
+ this.listenerId = undefined;
124
+ this.isUnsubscribing = false;
125
+ });
114
126
  return promise;
115
127
  }
128
+ else {
129
+ this.isUnsubscribing = false;
130
+ }
116
131
  }
117
132
  }
118
133
  exports.WebSocketAccountSubscriber = WebSocketAccountSubscriber;
@@ -15,6 +15,7 @@ export declare class WebSocketProgramAccountSubscriber<T> implements ProgramAcco
15
15
  onChange: (accountId: PublicKey, data: T, context: Context) => void;
16
16
  listenerId?: number;
17
17
  resubTimeoutMs?: number;
18
+ isUnsubscribing: boolean;
18
19
  timeoutId?: NodeJS.Timeout;
19
20
  options: {
20
21
  filters: MemcmpFilter[];
@@ -5,6 +5,7 @@ class WebSocketProgramAccountSubscriber {
5
5
  constructor(subscriptionName, accountDiscriminator, program, decodeBufferFn, options = {
6
6
  filters: [],
7
7
  }, resubTimeoutMs) {
8
+ this.isUnsubscribing = false;
8
9
  this.receivingData = false;
9
10
  this.subscriptionName = subscriptionName;
10
11
  this.accountDiscriminator = accountDiscriminator;
@@ -16,7 +17,7 @@ class WebSocketProgramAccountSubscriber {
16
17
  }
17
18
  async subscribe(onChange) {
18
19
  var _a;
19
- if (this.listenerId) {
20
+ if (this.listenerId || this.isUnsubscribing) {
20
21
  return;
21
22
  }
22
23
  this.onChange = onChange;
@@ -40,6 +41,10 @@ class WebSocketProgramAccountSubscriber {
40
41
  throw new Error('onChange callback function must be set');
41
42
  }
42
43
  this.timeoutId = setTimeout(async () => {
44
+ if (this.isUnsubscribing) {
45
+ // If we are in the process of unsubscribing, do not attempt to resubscribe
46
+ return;
47
+ }
43
48
  if (this.receivingData) {
44
49
  console.log(`No ws data from ${this.subscriptionName} in ${this.resubTimeoutMs}ms, resubscribing`);
45
50
  await this.unsubscribe();
@@ -89,11 +94,21 @@ class WebSocketProgramAccountSubscriber {
89
94
  }
90
95
  }
91
96
  unsubscribe() {
97
+ this.isUnsubscribing = true;
98
+ clearTimeout(this.timeoutId);
99
+ this.timeoutId = undefined;
92
100
  if (this.listenerId) {
93
- const promise = this.program.provider.connection.removeAccountChangeListener(this.listenerId);
94
- this.listenerId = undefined;
101
+ const promise = this.program.provider.connection
102
+ .removeAccountChangeListener(this.listenerId)
103
+ .then(() => {
104
+ this.listenerId = undefined;
105
+ this.isUnsubscribing = false;
106
+ });
95
107
  return promise;
96
108
  }
109
+ else {
110
+ this.isUnsubscribing = false;
111
+ }
97
112
  }
98
113
  }
99
114
  exports.WebSocketProgramAccountSubscriber = WebSocketProgramAccountSubscriber;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@drift-labs/sdk",
3
- "version": "2.43.0-beta.12",
3
+ "version": "2.43.0-beta.13",
4
4
  "main": "lib/index.js",
5
5
  "types": "lib/index.d.ts",
6
6
  "author": "crispheaney",
@@ -14,6 +14,8 @@ export class WebSocketAccountSubscriber<T> implements AccountSubscriber<T> {
14
14
  onChange: (data: T) => void;
15
15
  listenerId?: number;
16
16
  resubTimeoutMs?: number;
17
+ isUnsubscribing = false;
18
+
17
19
  timeoutId?: NodeJS.Timeout;
18
20
 
19
21
  receivingData: boolean;
@@ -34,7 +36,7 @@ export class WebSocketAccountSubscriber<T> implements AccountSubscriber<T> {
34
36
  }
35
37
 
36
38
  async subscribe(onChange: (data: T) => void): Promise<void> {
37
- if (this.listenerId) {
39
+ if (this.listenerId || this.isUnsubscribing) {
38
40
  return;
39
41
  }
40
42
 
@@ -80,6 +82,11 @@ export class WebSocketAccountSubscriber<T> implements AccountSubscriber<T> {
80
82
  throw new Error('onChange callback function must be set');
81
83
  }
82
84
  this.timeoutId = setTimeout(async () => {
85
+ if (this.isUnsubscribing) {
86
+ // If we are in the process of unsubscribing, do not attempt to resubscribe
87
+ return;
88
+ }
89
+
83
90
  if (this.receivingData) {
84
91
  console.log(
85
92
  `No ws data from ${this.accountName} in ${this.resubTimeoutMs}ms, resubscribing`
@@ -154,13 +161,20 @@ export class WebSocketAccountSubscriber<T> implements AccountSubscriber<T> {
154
161
  }
155
162
 
156
163
  unsubscribe(): Promise<void> {
164
+ this.isUnsubscribing = true;
165
+ clearTimeout(this.timeoutId);
166
+ this.timeoutId = undefined;
167
+
157
168
  if (this.listenerId) {
158
- const promise =
159
- this.program.provider.connection.removeAccountChangeListener(
160
- this.listenerId
161
- );
162
- this.listenerId = undefined;
169
+ const promise = this.program.provider.connection
170
+ .removeAccountChangeListener(this.listenerId)
171
+ .then(() => {
172
+ this.listenerId = undefined;
173
+ this.isUnsubscribing = false;
174
+ });
163
175
  return promise;
176
+ } else {
177
+ this.isUnsubscribing = false;
164
178
  }
165
179
  }
166
180
  }
@@ -21,6 +21,7 @@ export class WebSocketProgramAccountSubscriber<T>
21
21
  onChange: (accountId: PublicKey, data: T, context: Context) => void;
22
22
  listenerId?: number;
23
23
  resubTimeoutMs?: number;
24
+ isUnsubscribing = false;
24
25
  timeoutId?: NodeJS.Timeout;
25
26
  options: { filters: MemcmpFilter[]; commitment?: Commitment };
26
27
 
@@ -48,7 +49,7 @@ export class WebSocketProgramAccountSubscriber<T>
48
49
  async subscribe(
49
50
  onChange: (accountId: PublicKey, data: T, context: Context) => void
50
51
  ): Promise<void> {
51
- if (this.listenerId) {
52
+ if (this.listenerId || this.isUnsubscribing) {
52
53
  return;
53
54
  }
54
55
 
@@ -81,6 +82,11 @@ export class WebSocketProgramAccountSubscriber<T>
81
82
  throw new Error('onChange callback function must be set');
82
83
  }
83
84
  this.timeoutId = setTimeout(async () => {
85
+ if (this.isUnsubscribing) {
86
+ // If we are in the process of unsubscribing, do not attempt to resubscribe
87
+ return;
88
+ }
89
+
84
90
  if (this.receivingData) {
85
91
  console.log(
86
92
  `No ws data from ${this.subscriptionName} in ${this.resubTimeoutMs}ms, resubscribing`
@@ -140,13 +146,20 @@ export class WebSocketProgramAccountSubscriber<T>
140
146
  }
141
147
 
142
148
  unsubscribe(): Promise<void> {
149
+ this.isUnsubscribing = true;
150
+ clearTimeout(this.timeoutId);
151
+ this.timeoutId = undefined;
152
+
143
153
  if (this.listenerId) {
144
- const promise =
145
- this.program.provider.connection.removeAccountChangeListener(
146
- this.listenerId
147
- );
148
- this.listenerId = undefined;
154
+ const promise = this.program.provider.connection
155
+ .removeAccountChangeListener(this.listenerId)
156
+ .then(() => {
157
+ this.listenerId = undefined;
158
+ this.isUnsubscribing = false;
159
+ });
149
160
  return promise;
161
+ } else {
162
+ this.isUnsubscribing = false;
150
163
  }
151
164
  }
152
165
  }