Skip to content

Commit d7f2fa1

Browse files
committed
feat(command-flow): register processes as pending tasks
1 parent cfb558c commit d7f2fa1

1 file changed

Lines changed: 24 additions & 17 deletions

File tree

packages/command-flow/process-flow/src/handler.ts

Lines changed: 24 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
1-
import { Type } from '@angular/core';
2-
import { registerCommandHandler } from '@angularity/command-flow';
1+
import { inject, Injector, Type } from '@angular/core';
2+
import { pendingUntilEvent } from '@angular/core/rxjs-interop';
3+
import { onCommand } from '@angularity/command-flow';
34
import {
45
concatMap,
56
exhaustMap,
67
from,
78
mergeMap,
89
Observable,
10+
pipe,
911
switchMap,
1012
} from 'rxjs';
1113
import { match } from 'ts-pattern';
@@ -89,33 +91,38 @@ export function onProcess<Types extends Type<Process<any>>[]>(
8991
scheduling: ProcessSchedulingStrategy,
9092
handler: ProcessHandler<InstanceType<Types[number]>>,
9193
): void {
94+
const injector = inject(Injector);
95+
9296
const scheduler = match(scheduling)
9397
.with(ProcessSchedulingStrategy.Sequential, () => concatMap)
9498
.with(ProcessSchedulingStrategy.Concurrent, () => mergeMap)
9599
.with(ProcessSchedulingStrategy.Preemptive, () => switchMap)
96100
.with(ProcessSchedulingStrategy.Blocking, () => exhaustMap)
97101
.run();
98102

99-
registerCommandHandler(types, ($) =>
100-
$.pipe(
103+
onCommand(
104+
types,
105+
pipe(
101106
scheduler(
102107
(process) =>
103108
new Observable<ProcessEvent<InstanceType<Types[number]>>>(
104109
(observer) => {
105110
observer.next(new ProcessStarted(process));
106-
return from(handler(process)).subscribe({
107-
next: (result) => {
108-
observer.next(new ProcessCompleted(process, result));
109-
observer.complete();
110-
},
111-
error: (error) => {
112-
observer.next(new ProcessFailed(process, error));
113-
observer.complete();
114-
},
115-
complete: () => {
116-
observer.complete();
117-
},
118-
});
111+
return from(handler(process))
112+
.pipe(pendingUntilEvent(injector))
113+
.subscribe({
114+
next: (result) => {
115+
observer.next(new ProcessCompleted(process, result));
116+
observer.complete();
117+
},
118+
error: (error) => {
119+
observer.next(new ProcessFailed(process, error));
120+
observer.complete();
121+
},
122+
complete: () => {
123+
observer.complete();
124+
},
125+
});
119126
},
120127
),
121128
),

0 commit comments

Comments
 (0)