-
Notifications
You must be signed in to change notification settings - Fork 12k
/
Copy pathstrategy.ts
160 lines (141 loc) · 4.38 KB
/
strategy.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
/**
* @license
* Copyright Google LLC All Rights Reserved.
*
* Use of this source code is governed by an MIT-style license that can be
* found in the LICENSE file at https://angular.dev/license
*/
import { JsonObject, JsonValue, isJsonObject } from '@angular-devkit/core';
import {
Observable,
Subject,
concat,
finalize,
ignoreElements,
of,
share,
shareReplay,
tap,
} from 'rxjs';
import {
JobDescription,
JobHandler,
JobHandlerContext,
JobInboundMessage,
JobOutboundMessage,
JobOutboundMessageKind,
} from './api';
export type JobStrategy<
A extends JsonValue = JsonValue,
I extends JsonValue = JsonValue,
O extends JsonValue = JsonValue,
> = (
handler: JobHandler<A, I, O>,
options?: Partial<Readonly<JobDescription>>,
) => JobHandler<A, I, O>;
/**
* Creates a JobStrategy that serializes every call. This strategy can be mixed between jobs.
*/
export function serialize<
A extends JsonValue = JsonValue,
I extends JsonValue = JsonValue,
O extends JsonValue = JsonValue,
>(): JobStrategy<A, I, O> {
let latest: Observable<JobOutboundMessage<O>> = of();
return (handler, options) => {
const newHandler = (argument: A, context: JobHandlerContext<A, I, O>) => {
const previous = latest;
latest = concat(
previous.pipe(ignoreElements()),
new Observable<JobOutboundMessage<O>>((o) => handler(argument, context).subscribe(o)),
).pipe(shareReplay(0));
return latest;
};
return Object.assign(newHandler, {
jobDescription: Object.assign({}, handler.jobDescription, options),
});
};
}
/**
* Creates a JobStrategy that will always reuse a running job, and restart it if the job ended.
* @param replayMessages Replay ALL messages if a job is reused, otherwise just hook up where it
* is.
*/
export function reuse<
A extends JsonValue = JsonValue,
I extends JsonValue = JsonValue,
O extends JsonValue = JsonValue,
>(replayMessages = false): JobStrategy<A, I, O> {
let inboundBus = new Subject<JobInboundMessage<I>>();
let run: Observable<JobOutboundMessage<O>> | null = null;
let state: JobOutboundMessage<O> | null = null;
return (handler, options) => {
const newHandler = (argument: A, context: JobHandlerContext<A, I, O>) => {
// Forward inputs.
const subscription = context.inboundBus.subscribe(inboundBus);
if (run) {
return concat(
// Update state.
of(state),
run,
).pipe(finalize(() => subscription.unsubscribe()));
}
run = handler(argument, { ...context, inboundBus: inboundBus.asObservable() }).pipe(
tap(
(message) => {
if (
message.kind == JobOutboundMessageKind.Start ||
message.kind == JobOutboundMessageKind.OnReady ||
message.kind == JobOutboundMessageKind.End
) {
state = message;
}
},
undefined,
() => {
subscription.unsubscribe();
inboundBus = new Subject<JobInboundMessage<I>>();
run = null;
},
),
replayMessages ? shareReplay() : share(),
);
return run;
};
return Object.assign(newHandler, handler, options || {});
};
}
/**
* Creates a JobStrategy that will reuse a running job if the argument matches.
* @param replayMessages Replay ALL messages if a job is reused, otherwise just hook up where it
* is.
*/
export function memoize<
A extends JsonValue = JsonValue,
I extends JsonValue = JsonValue,
O extends JsonValue = JsonValue,
>(replayMessages = false): JobStrategy<A, I, O> {
const runs = new Map<string, Observable<JobOutboundMessage<O>>>();
return (handler, options) => {
const newHandler = (argument: A, context: JobHandlerContext<A, I, O>) => {
const argumentJson = JSON.stringify(
isJsonObject(argument)
? Object.keys(argument)
.sort()
.reduce((result, key) => {
result[key] = argument[key];
return result;
}, {} as JsonObject)
: argument,
);
const maybeJob = runs.get(argumentJson);
if (maybeJob) {
return maybeJob;
}
const run = handler(argument, context).pipe(replayMessages ? shareReplay() : share());
runs.set(argumentJson, run);
return run;
};
return Object.assign(newHandler, handler, options || {});
};
}