@@ -58,12 +58,11 @@ export class StateMachine<I, O> implements RestateStreamConsumer {
58
58
// Suspension timeout that gets set and cleared based on completion messages;
59
59
private suspensionTimeout ?: NodeJS . Timeout ;
60
60
61
- private readonly suspensionMillis = 30_000 ;
62
-
63
61
constructor (
64
62
private readonly connection : Connection ,
65
63
private readonly invocation : Invocation < I , O > ,
66
- private readonly protocolMode : ProtocolMode
64
+ private readonly protocolMode : ProtocolMode ,
65
+ private readonly suspensionMillis : number = 30_000
67
66
) {
68
67
this . localStateStore = invocation . localStateStore ;
69
68
@@ -383,11 +382,15 @@ export class StateMachine<I, O> implements RestateStreamConsumer {
383
382
"Scheduling suspension in " + delay + " ms"
384
383
) ;
385
384
386
- // Set a new suspension with a new timeout
387
- // The suspension will only be sent if the timeout is not canceled due to a completion.
388
- this . suspensionTimeout = setTimeout ( ( ) => {
389
- this . suspend ( ) ;
390
- } , delay ) ;
385
+ if ( delay >= 0 ) {
386
+ // Set a new suspension with a new timeout
387
+ // The suspension will only be sent if the timeout is not canceled due to a completion.
388
+ // In case the delay is 0 we still schedule a timeout in order to process the suspension on the next process tick,
389
+ // without interrupting the current work.
390
+ this . suspensionTimeout = setTimeout ( ( ) => {
391
+ this . suspend ( ) ;
392
+ } , delay ) ;
393
+ }
391
394
}
392
395
393
396
// Suspension timeouts:
0 commit comments