Skip to content

Commit 37d1b27

Browse files
authored
refactor(core): remove redundant type assertions in Subscription (#293)
* chore(core): remove redundant type assertions in Subscription logic --------- Signed-off-by: Alberto Ricart <[email protected]>
1 parent 154fa70 commit 37d1b27

File tree

4 files changed

+8
-10
lines changed

4 files changed

+8
-10
lines changed

core/src/protocol.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -299,7 +299,7 @@ export class Subscriptions {
299299
add(s: SubscriptionImpl): SubscriptionImpl {
300300
this.sidCounter++;
301301
s.sid = this.sidCounter;
302-
this.subs.set(s.sid, s as SubscriptionImpl);
302+
this.subs.set(s.sid, s);
303303
return s;
304304
}
305305

@@ -705,7 +705,7 @@ export class ProtocolHandler implements Dispatcher<ParserEvent> {
705705
return;
706706
}
707707

708-
const sub = this.subscriptions.get(msg.sid) as SubscriptionImpl;
708+
const sub = this.subscriptions.get(msg.sid);
709709
if (!sub) {
710710
return;
711711
}
@@ -1010,7 +1010,7 @@ export class ProtocolHandler implements Dispatcher<ParserEvent> {
10101010
sendSubscriptions(): void {
10111011
const cmds: string[] = [];
10121012
this.subscriptions.all().forEach((s) => {
1013-
const sub = s as SubscriptionImpl;
1013+
const sub = s;
10141014
if (sub.queue) {
10151015
cmds.push(`SUB ${sub.subject} ${sub.queue} ${sub.sid}${CR_LF}`);
10161016
} else {

core/tests/basics_test.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -194,8 +194,8 @@ Deno.test("basics - subscriptions pass exact subject to cb", async () => {
194194
Deno.test("basics - subscribe returns Subscription", async () => {
195195
const { ns, nc } = await setup();
196196
const subj = createInbox();
197-
const sub = nc.subscribe(subj) as SubscriptionImpl;
198-
assertEquals(sub.sid, 1);
197+
const sub = nc.subscribe(subj);
198+
assertEquals(sub.getID(), 1);
199199
await cleanup(ns, nc);
200200
});
201201

jetstream/src/consumer.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@ import type {
2121
QueuedIterator,
2222
Status,
2323
Subscription,
24-
SubscriptionImpl,
2524
} from "@nats-io/nats-core/internal";
2625
import {
2726
backoff,
@@ -232,7 +231,7 @@ export class PullConsumerMessagesImpl extends QueuedIteratorImpl<JsMsg>
232231
: msg.subject === this.inbox;
233232

234233
if (isProtocol) {
235-
if (msg.subject !== (this.sub as SubscriptionImpl).subject) {
234+
if (msg.subject !== this.sub.getSubject()) {
236235
// this is a stale message - was not sent to the current inbox
237236
return;
238237
}
@@ -437,7 +436,7 @@ export class PullConsumerMessagesImpl extends QueuedIteratorImpl<JsMsg>
437436

438437
this.sub.closed.then(() => {
439438
// for ordered consumer we cannot break the iterator
440-
if ((this.sub as SubscriptionImpl).draining) {
439+
if (this.sub.isDraining()) {
441440
// @ts-ignore: we are pushing the pull fn
442441
this._push(() => {
443442
this.stop();

jetstream/src/pushconsumer.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,6 @@ import type {
4444
QueuedIterator,
4545
Status,
4646
Subscription,
47-
SubscriptionImpl,
4847
} from "@nats-io/nats-core/internal";
4948
import { JetStreamStatus } from "./jserrors.ts";
5049

@@ -293,7 +292,7 @@ export class PushConsumerMessagesImpl extends QueuedIteratorImpl<JsMsg>
293292
: msg.subject === subject;
294293

295294
if (isProtocol) {
296-
if (msg.subject !== (this.sub as SubscriptionImpl).subject) {
295+
if (msg.subject !== this.sub.getSubject()) {
297296
// this is a stale message - was not sent to the current inbox
298297
return;
299298
}

0 commit comments

Comments
 (0)