14
14
import { Message } from "./types/types" ;
15
15
import { HostedGrpcServiceMethod } from "./types/grpc" ;
16
16
import {
17
+ Failure ,
17
18
PollInputStreamEntryMessage ,
18
19
StartMessage ,
19
20
} from "./generated/proto/protocol" ;
@@ -37,6 +38,10 @@ enum State {
37
38
Complete = 3 ,
38
39
}
39
40
41
+ type InvocationValue =
42
+ | { kind : "value" ; value : Buffer }
43
+ | { kind : "failure" ; failure : Failure } ;
44
+
40
45
export class InvocationBuilder < I , O > implements RestateStreamConsumer {
41
46
private readonly complete = new CompletablePromise < void > ( ) ;
42
47
@@ -46,7 +51,7 @@ export class InvocationBuilder<I, O> implements RestateStreamConsumer {
46
51
private replayEntries = new Map < number , Message > ( ) ;
47
52
private id ?: Buffer = undefined ;
48
53
private debugId ?: string = undefined ;
49
- private invocationValue ?: Buffer = undefined ;
54
+ private invocationValue ?: InvocationValue = undefined ;
50
55
private nbEntriesToReplay ?: number = undefined ;
51
56
private localStateStore ?: LocalStateStore ;
52
57
@@ -67,6 +72,8 @@ export class InvocationBuilder<I, O> implements RestateStreamConsumer {
67
72
POLL_INPUT_STREAM_ENTRY_MESSAGE_TYPE ,
68
73
m
69
74
) ;
75
+
76
+ this . handlePollInputStreamEntry ( m ) ;
70
77
this . addReplayEntry ( m ) ;
71
78
break ;
72
79
@@ -100,6 +107,28 @@ export class InvocationBuilder<I, O> implements RestateStreamConsumer {
100
107
}
101
108
}
102
109
110
+ private handlePollInputStreamEntry ( m : Message ) {
111
+ const pollInputStreamMessage = m . message as PollInputStreamEntryMessage ;
112
+
113
+ if ( pollInputStreamMessage . value !== undefined ) {
114
+ this . invocationValue = {
115
+ kind : "value" ,
116
+ value : pollInputStreamMessage . value ,
117
+ } ;
118
+ } else if ( pollInputStreamMessage . failure !== undefined ) {
119
+ this . invocationValue = {
120
+ kind : "failure" ,
121
+ failure : pollInputStreamMessage . failure ,
122
+ } ;
123
+ } else {
124
+ throw new Error (
125
+ `PollInputStreamEntry neither contains value nor failure: ${ printMessageAsJson (
126
+ m
127
+ ) } `
128
+ ) ;
129
+ }
130
+ }
131
+
103
132
public handleStreamError ( e : Error ) : void {
104
133
this . complete . reject ( e ) ;
105
134
}
@@ -120,10 +149,6 @@ export class InvocationBuilder<I, O> implements RestateStreamConsumer {
120
149
}
121
150
122
151
private addReplayEntry ( m : Message ) : InvocationBuilder < I , O > {
123
- if ( m . messageType === POLL_INPUT_STREAM_ENTRY_MESSAGE_TYPE ) {
124
- this . invocationValue = ( m . message as PollInputStreamEntryMessage ) . value ;
125
- }
126
-
127
152
// Will be retrieved when the user code reaches this point
128
153
this . replayEntries . set ( this . runtimeReplayIndex , m ) ;
129
154
this . incrementRuntimeReplayIndex ( ) ;
@@ -164,7 +189,7 @@ export class Invocation<I, O> {
164
189
public readonly debugId : string ,
165
190
public readonly nbEntriesToReplay : number ,
166
191
public readonly replayEntries : Map < number , Message > ,
167
- public readonly invocationValue : Buffer ,
192
+ public readonly invocationValue : InvocationValue ,
168
193
public readonly localStateStore : LocalStateStore
169
194
) {
170
195
this . logPrefix = `[${ makeFqServiceName (
0 commit comments