|
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'; |
3 | 4 | import { |
4 | 5 | concatMap, |
5 | 6 | exhaustMap, |
6 | 7 | from, |
7 | 8 | mergeMap, |
8 | 9 | Observable, |
| 10 | + pipe, |
9 | 11 | switchMap, |
10 | 12 | } from 'rxjs'; |
11 | 13 | import { match } from 'ts-pattern'; |
@@ -89,33 +91,38 @@ export function onProcess<Types extends Type<Process<any>>[]>( |
89 | 91 | scheduling: ProcessSchedulingStrategy, |
90 | 92 | handler: ProcessHandler<InstanceType<Types[number]>>, |
91 | 93 | ): void { |
| 94 | + const injector = inject(Injector); |
| 95 | + |
92 | 96 | const scheduler = match(scheduling) |
93 | 97 | .with(ProcessSchedulingStrategy.Sequential, () => concatMap) |
94 | 98 | .with(ProcessSchedulingStrategy.Concurrent, () => mergeMap) |
95 | 99 | .with(ProcessSchedulingStrategy.Preemptive, () => switchMap) |
96 | 100 | .with(ProcessSchedulingStrategy.Blocking, () => exhaustMap) |
97 | 101 | .run(); |
98 | 102 |
|
99 | | - registerCommandHandler(types, ($) => |
100 | | - $.pipe( |
| 103 | + onCommand( |
| 104 | + types, |
| 105 | + pipe( |
101 | 106 | scheduler( |
102 | 107 | (process) => |
103 | 108 | new Observable<ProcessEvent<InstanceType<Types[number]>>>( |
104 | 109 | (observer) => { |
105 | 110 | 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 | + }); |
119 | 126 | }, |
120 | 127 | ), |
121 | 128 | ), |
|
0 commit comments