6
6
* found in the LICENSE file at https://angular.io/license
7
7
*
8
8
*/
9
- import { Observable , Observer , Subject , Subscription , from , isObservable } from 'rxjs' ;
9
+ import { Observable , Observer , Subject , Subscription , from , isObservable , of } from 'rxjs' ;
10
10
import { switchMap , tap } from 'rxjs/operators' ;
11
11
import { BaseException } from '../../exception/index' ;
12
12
import { JsonValue } from '../../json/index' ;
@@ -72,8 +72,17 @@ export function createJobHandler<A extends JsonValue, I extends JsonValue, O ext
72
72
let subscription : Subscription ;
73
73
74
74
return new Observable < JobOutboundMessage < O > > ( subject => {
75
+ function complete ( ) {
76
+ if ( subscription ) {
77
+ subscription . unsubscribe ( ) ;
78
+ }
79
+ subject . next ( { kind : JobOutboundMessageKind . End , description } ) ;
80
+ subject . complete ( ) ;
81
+ inputChannel . complete ( ) ;
82
+ }
83
+
75
84
// Handle input.
76
- inboundBus . subscribe ( message => {
85
+ const inboundSub = inboundBus . subscribe ( message => {
77
86
switch ( message . kind ) {
78
87
case JobInboundMessageKind . Ping :
79
88
subject . next ( { kind : JobOutboundMessageKind . Pong , description, id : message . id } ) ;
@@ -82,13 +91,7 @@ export function createJobHandler<A extends JsonValue, I extends JsonValue, O ext
82
91
case JobInboundMessageKind . Stop :
83
92
// There's no way to cancel a promise or a synchronous function, but we do cancel
84
93
// observables where possible.
85
- if ( subscription ) {
86
- subscription . unsubscribe ( ) ;
87
- }
88
- subject . next ( { kind : JobOutboundMessageKind . End , description } ) ;
89
- subject . complete ( ) ;
90
- // Close all channels.
91
- channels . forEach ( x => x . complete ( ) ) ;
94
+ complete ( ) ;
92
95
break ;
93
96
94
97
case JobInboundMessageKind . Input :
@@ -99,7 +102,7 @@ export function createJobHandler<A extends JsonValue, I extends JsonValue, O ext
99
102
100
103
// Configure a logger to pass in as additional context.
101
104
const logger = new Logger ( 'job' ) ;
102
- logger . subscribe ( entry => {
105
+ const logSub = logger . subscribe ( entry => {
103
106
subject . next ( {
104
107
kind : JobOutboundMessageKind . Log ,
105
108
description,
@@ -108,8 +111,6 @@ export function createJobHandler<A extends JsonValue, I extends JsonValue, O ext
108
111
} ) ;
109
112
110
113
// Execute the function with the additional context.
111
- subject . next ( { kind : JobOutboundMessageKind . Start , description } ) ;
112
-
113
114
const channels = new Map < string , Subject < JsonValue > > ( ) ;
114
115
115
116
const newContext : SimpleJobHandlerContext < A , I , O > = {
@@ -121,7 +122,7 @@ export function createJobHandler<A extends JsonValue, I extends JsonValue, O ext
121
122
throw new ChannelAlreadyExistException ( name ) ;
122
123
}
123
124
const channelSubject = new Subject < JsonValue > ( ) ;
124
- channelSubject . subscribe (
125
+ const channelSub = channelSubject . subscribe (
125
126
message => {
126
127
subject . next ( {
127
128
kind : JobOutboundMessageKind . ChannelMessage , description, name, message,
@@ -140,36 +141,32 @@ export function createJobHandler<A extends JsonValue, I extends JsonValue, O ext
140
141
) ;
141
142
142
143
channels . set ( name , channelSubject ) ;
144
+ if ( subscription ) {
145
+ subscription . add ( channelSub ) ;
146
+ }
143
147
144
148
return channelSubject ;
145
149
} ,
146
150
} ;
147
151
148
- const result = fn ( argument , newContext ) ;
152
+ subject . next ( { kind : JobOutboundMessageKind . Start , description } ) ;
153
+ let result = fn ( argument , newContext ) ;
149
154
// If the result is a promise, simply wait for it to complete before reporting the result.
150
155
if ( isPromise ( result ) ) {
151
- result . then ( result => {
152
- subject . next ( { kind : JobOutboundMessageKind . Output , description, value : result } ) ;
153
- subject . next ( { kind : JobOutboundMessageKind . End , description } ) ;
154
- subject . complete ( ) ;
155
- } , err => subject . error ( err ) ) ;
156
- } else if ( isObservable ( result ) ) {
157
- subscription = ( result as Observable < O > ) . subscribe (
158
- ( value : O ) => subject . next ( { kind : JobOutboundMessageKind . Output , description, value } ) ,
159
- error => subject . error ( error ) ,
160
- ( ) => {
161
- subject . next ( { kind : JobOutboundMessageKind . End , description } ) ;
162
- subject . complete ( ) ;
163
- } ,
164
- ) ;
165
-
166
- return subscription ;
167
- } else {
168
- // If it's a scalar value, report it synchronously.
169
- subject . next ( { kind : JobOutboundMessageKind . Output , description, value : result as O } ) ;
170
- subject . next ( { kind : JobOutboundMessageKind . End , description } ) ;
171
- subject . complete ( ) ;
156
+ result = from ( result ) ;
157
+ } else if ( ! isObservable ( result ) ) {
158
+ result = of ( result as O ) ;
172
159
}
160
+
161
+ subscription = ( result as Observable < O > ) . subscribe (
162
+ ( value : O ) => subject . next ( { kind : JobOutboundMessageKind . Output , description, value } ) ,
163
+ error => subject . error ( error ) ,
164
+ ( ) => complete ( ) ,
165
+ ) ;
166
+ subscription . add ( inboundSub ) ;
167
+ subscription . add ( logSub ) ;
168
+
169
+ return subscription ;
173
170
} ) ;
174
171
} ;
175
172
0 commit comments