Repository navigation
Expand file tree
/
Copy pathadmin_mutation_controller.ts
More file actions
109 lines (94 loc) · 4.27 KB
/
Copy pathadmin_mutation_controller.ts
File metadata and controls
109 lines (94 loc) · 4.27 KB
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
export type MutationKind = "register" | "create" | "enable" | "disable" | "unregister";
export type MutationErrorCode =
| "invalid_request" | "revision_conflict" | "active_jobs_conflict"
| "idempotency_conflict" | "unsupported_runner_version" | "agent_unavailable"
| "operation_indeterminate" | "operation_failed" | "network_error" | "unauthorized";
export class AdminMutationError extends Error {
constructor(readonly status: number, readonly code: MutationErrorCode, readonly activeJobs?: number) {
super(code); this.name = "AdminMutationError";
}
}
type MutationContext = {
kind: MutationKind; target: string; body: Record<string, unknown>; key: string;
generation: number; token: string; controller: AbortController; pending: boolean;
};
type Dependencies = {
request(kind: MutationKind, token: string, body: Record<string, unknown>, signal: AbortSignal): Promise<unknown>;
keyFactory(): string;
refresh(): Promise<void>;
outcome(message: string): void;
error(code: MutationErrorCode, context: MutationContext): void;
pending(target: string, value: boolean): void;
lock(message: string): void;
};
function aborted(error: unknown): boolean {
return Boolean(error && typeof error === "object" && "name" in error && error.name === "AbortError");
}
export class AdminMutationController {
private generation = 0;
private token = "";
private contexts = new Map<string, MutationContext>();
constructor(private readonly deps: Dependencies) {}
beginSession(token: string): void { this.invalidate(); this.token = token; }
lock(): void { this.invalidate(); this.token = ""; }
dispose(): void { this.invalidate(); this.token = ""; }
start(kind: MutationKind, target: string, body: Record<string, unknown>): MutationContext | null {
const existing = this.contexts.get(target);
if (!this.token || existing) return null;
const context: MutationContext = {
kind, target, body: { ...body }, key: this.deps.keyFactory(), generation: this.generation,
token: this.token, controller: new AbortController(), pending: false,
};
this.contexts.set(target, context);
return context;
}
retry(target: string): Promise<void> {
const context = this.contexts.get(target);
return context ? this.submit(context) : Promise.resolve();
}
cancel(target: string): void {
const context = this.contexts.get(target);
if (!context?.pending) this.contexts.delete(target);
}
has(target: string): boolean { return this.contexts.has(target); }
isPending(target: string): boolean { return this.contexts.get(target)?.pending === true; }
async submit(context: MutationContext): Promise<void> {
if (!this.current(context) || context.pending) return;
context.pending = true;
this.deps.pending(context.target, true);
try {
await this.deps.request(context.kind, context.token, { ...context.body, idempotency_key: context.key }, context.controller.signal);
if (!this.current(context)) return;
this.deps.outcome(`${context.kind[0].toUpperCase()}${context.kind.slice(1)} completed.`);
this.contexts.delete(context.target);
await this.deps.refresh();
} catch (error) {
if (!this.current(context) || aborted(error)) return;
const classified = error instanceof AdminMutationError
? error
: new AdminMutationError(0, "network_error");
if (classified.code === "unauthorized") {
this.deps.lock("Administrator authentication required.");
return;
}
this.deps.error(classified.code, context);
if (classified.code === "revision_conflict" || classified.code === "active_jobs_conflict") {
await this.deps.refresh();
}
if (classified.code === "revision_conflict") this.contexts.delete(context.target);
} finally {
if (this.generation === context.generation && this.token === context.token) {
context.pending = false;
this.deps.pending(context.target, false);
}
}
}
private current(context: MutationContext): boolean {
return this.generation === context.generation && this.token === context.token && this.contexts.get(context.target) === context;
}
private invalidate(): void {
this.generation += 1;
for (const context of this.contexts.values()) context.controller.abort();
this.contexts.clear();
}
}