@@ -785,6 +785,13 @@ class OperationBase extends EventEmitter {
785
785
* @return the operation's underlying event stream binding object
786
786
*/
787
787
getStream ( ) : eventstream . ClientStream { return this . stream ; }
788
+
789
+ /**
790
+ * Set this operation state to be "Ended" so that closing the operation will not send a terminate message.
791
+ */
792
+ setStateEnded ( ) {
793
+ this . state = OperationState . Ended ;
794
+ }
788
795
}
789
796
790
797
/**
@@ -837,6 +844,11 @@ export class RequestResponseOperation<RequestType, ResponseType> extends EventEm
837
844
await this . operation . activate ( requestMessage ) ;
838
845
839
846
let message : eventstream . Message = await responsePromise ;
847
+
848
+ // If the server terminated the stream, then set the operation to be ended immediately
849
+ if ( ( message . flags ?? 0 ) & eventstream . MessageFlags . TerminateStream ) {
850
+ this . operation . setStateEnded ( ) ;
851
+ }
840
852
let response : ResponseType = deserializeResponse ( this . serviceModel , this . operationConfig . name , message ) ;
841
853
842
854
resolve ( response ) ;
@@ -928,6 +940,15 @@ export class StreamingOperation<RequestType, ResponseType, OutboundMessageType,
928
940
let message : eventstream . Message = await responsePromise ;
929
941
let response : ResponseType = deserializeResponse ( this . serviceModel , this . operationConfig . name , message ) ;
930
942
943
+ // If the server terminated the stream, then set the operation to be ended immediately
944
+ if ( ( message . flags ?? 0 ) & eventstream . MessageFlags . TerminateStream ) {
945
+ this . operation . setStateEnded ( ) ;
946
+ // Server hung up on us. Immediately cleanup the operation state.
947
+ // Do this before resolving the promise so that any user-initiated
948
+ // requests will see the correct state, which is that the operation is closed.
949
+ await this . close ( ) ;
950
+ }
951
+
931
952
resolve ( response ) ;
932
953
} catch ( e ) {
933
954
await this . close ( ) ;
0 commit comments