@@ -28,7 +28,8 @@ export class StreamConsumer implements Consumer {
28
28
public offset : Offset
29
29
private clientLocalOffset : Offset
30
30
private creditsHandler : ConsumerCreditPolicy
31
- readonly handle : ConsumerFunc
31
+ private consumerHandle : ConsumerFunc
32
+ private closed : boolean
32
33
33
34
constructor (
34
35
handle : ConsumerFunc ,
@@ -50,10 +51,12 @@ export class StreamConsumer implements Consumer {
50
51
this . clientLocalOffset = this . offset . clone ( )
51
52
this . connection . incrRefCount ( )
52
53
this . creditsHandler = params . creditPolicy || defaultCreditPolicy
53
- this . handle = this . wrapHandle ( handle , params . offset )
54
+ this . consumerHandle = handle
55
+ this . closed = false
54
56
}
55
57
56
58
async close ( manuallyClose : boolean ) : Promise < void > {
59
+ this . closed = true
57
60
this . connection . decrRefCount ( )
58
61
if ( ConnectionPool . removeIfUnused ( this . connection ) ) {
59
62
await this . connection . close ( { closingCode : 0 , closingReason : "" , manuallyClose } )
@@ -79,31 +82,10 @@ export class StreamConsumer implements Consumer {
79
82
return this . clientLocalOffset . clone ( )
80
83
}
81
84
82
- private wrapHandle ( handle : ConsumerFunc , offset : Offset ) {
83
- const updateLocalOffsetHandle = this . updateLocalOffsetHandle ( handle )
84
- return this . addOffsetFilterToHandle ( updateLocalOffsetHandle , offset )
85
- }
86
-
87
- private updateLocalOffsetHandle ( handle : ConsumerFunc ) {
88
- const wrapped = ( message : Message ) => {
89
- const result = handle ( message )
90
- if ( message . offset !== undefined ) this . clientLocalOffset = Offset . offset ( message . offset )
91
- return result
92
- }
93
- return wrapped
94
- }
95
-
96
- private addOffsetFilterToHandle ( handle : ConsumerFunc , offset : Offset ) {
97
- if ( offset . type === "numeric" ) {
98
- const handlerWithFilter = ( message : Message ) => {
99
- if ( message . offset !== undefined && message . offset < offset . value ! ) {
100
- return
101
- }
102
- handle ( message )
103
- }
104
- return handlerWithFilter
105
- }
106
- return handle
85
+ public handle ( message : Message ) {
86
+ if ( this . closed || this . isMessageOffsetLessThanConsumers ( message ) ) return
87
+ this . consumerHandle ( message )
88
+ this . maybeUpdateLocalOffset ( message )
107
89
}
108
90
109
91
public get streamName ( ) : string {
@@ -117,4 +99,13 @@ export class StreamConsumer implements Consumer {
117
99
public get creditPolicy ( ) {
118
100
return this . creditsHandler
119
101
}
102
+
103
+ private maybeUpdateLocalOffset ( message : Message ) {
104
+ if ( message . offset !== undefined ) this . clientLocalOffset = Offset . offset ( message . offset )
105
+ }
106
+
107
+ // TODO -- Find better name?
108
+ private isMessageOffsetLessThanConsumers ( message : Message ) {
109
+ return this . offset . type === "numeric" && message . offset !== undefined && message . offset < this . offset . value !
110
+ }
120
111
}
0 commit comments