Skip to content

Commit db21bf5

Browse files
committed
Better error handling
1 parent 00cea56 commit db21bf5

File tree

3 files changed

+25
-18
lines changed

3 files changed

+25
-18
lines changed

packages/sdk/src/algebraic_type.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -363,7 +363,10 @@ export class AlgebraicType {
363363
}
364364
static createTimestampType(): AlgebraicType {
365365
return this.createProductType([
366-
new ProductTypeElement('__timestamp_micros_since_unix_epoch__', this.createI64Type()),
366+
new ProductTypeElement(
367+
'__timestamp_micros_since_unix_epoch__',
368+
this.createI64Type()
369+
),
367370
]);
368371
}
369372
static createAddressType(): AlgebraicType {
@@ -408,7 +411,7 @@ export class AlgebraicType {
408411
this.product.elements[0].name === tag
409412
);
410413
}
411-
414+
412415
isTimestamp(): boolean {
413416
return this.#isBytesNewtype('__timestamp_micros_since_unix_epoch__');
414417
}

packages/sdk/src/subscription_builder_impl.ts

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -129,22 +129,13 @@ export class SubscriptionManager {
129129
> = new Map();
130130
}
131131

132-
type Result<T = undefined> =
133-
| {
134-
tag: 'Ok';
135-
value: T;
136-
}
137-
| {
138-
tag: 'Err';
139-
value: Error;
140-
};
141-
142132
export class SubscriptionHandleImpl<
143133
DBView = any,
144134
Reducers = any,
145135
SetReducerFlags = any,
146136
> {
147137
#queryId: number;
138+
#unsubscribeCalled: boolean = false;
148139
#endedState: boolean = false;
149140
#activeState: boolean = false;
150141
#emitter: EventEmitter<SubscribeEvent, (...args: any[]) => void> =
@@ -195,10 +186,13 @@ export class SubscriptionHandleImpl<
195186
* removing this query from the client's set of subscribed queries.
196187
* It is only valid to call this method if `is_active()` is `true`.
197188
*/
198-
unsubscribe(): Result {
189+
unsubscribe(): void {
190+
if (this.#unsubscribeCalled) {
191+
throw new Error('Unsubscribe has already been called');
192+
}
193+
this.#unsubscribeCalled = true;
199194
this.db.unregisterSubscription(this.#queryId);
200195
this.db['unsubscribe'](this.#queryId);
201-
return { tag: 'Ok', value: undefined };
202196
}
203197

204198
/**
@@ -215,7 +209,14 @@ export class SubscriptionHandleImpl<
215209
onEnd: (
216210
ctx: SubscriptionEventContextInterface<DBView, Reducers, SetReducerFlags>
217211
) => void
218-
): Result {
212+
): void {
213+
if (this.#endedState) {
214+
throw new Error('Subscription has already ended');
215+
}
216+
if (this.#unsubscribeCalled) {
217+
throw new Error('Unsubscribe has already been called');
218+
}
219+
this.#unsubscribeCalled = true;
219220
this.#emitter.on(
220221
'end',
221222
(
@@ -230,7 +231,6 @@ export class SubscriptionHandleImpl<
230231
onEnd(ctx);
231232
}
232233
);
233-
return { tag: 'Ok', value: undefined };
234234
}
235235

236236
/**

packages/sdk/tests/db_connection.test.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,11 @@ import { AlgebraicType } from '../src/algebraic_type';
1111
import { parseValue } from '../src/algebraic_value';
1212
import BinaryWriter from '../src/binary_writer';
1313
import * as ws from '../src/client_api';
14-
import { ReducerEvent, TimeDuration, Timestamp } from '../src/db_connection_impl';
14+
import {
15+
ReducerEvent,
16+
TimeDuration,
17+
Timestamp,
18+
} from '../src/db_connection_impl';
1519
import { Identity } from '../src/identity';
1620
import WebsocketTestAdapter from '../src/websocket_test_adapter';
1721

@@ -617,7 +621,7 @@ describe('DBConnection', () => {
617621
],
618622
},
619623
],
620-
}),
624+
}),
621625
timestamp: new Timestamp(BigInt(1681391805281203)),
622626
callerIdentity: anIdentity,
623627
callerAddress: Address.random(),

0 commit comments

Comments
 (0)