-
Notifications
You must be signed in to change notification settings - Fork 176
Expand file tree
/
Copy pathstore.ts
More file actions
113 lines (92 loc) · 3.13 KB
/
store.ts
File metadata and controls
113 lines (92 loc) · 3.13 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
110
111
112
113
import {setInterval} from "timers";
import {EventEmitter} from "vscode";
import {GitHubRepoContext} from "../git/repository";
import {log, logDebug} from "../log";
import * as model from "../model";
import {WorkflowRun} from "./workflowRun";
export interface RunStoreEvent {
run: WorkflowRun;
}
type Updater = {
intervalMs: number;
remainingAttempts: number;
repoContext: GitHubRepoContext;
runId: number;
handle: NodeJS.Timeout | undefined;
};
export class RunStore extends EventEmitter<RunStoreEvent> {
private runs = new Map<number, WorkflowRun>();
private updaters = new Map<number, Updater>();
private _isFocused = true;
private _isViewVisible = true;
setFocused(focused: boolean) {
this._isFocused = focused;
logDebug(`[Store]: Focus state changed to ${String(focused)}`);
}
setViewVisible(visible: boolean) {
this._isViewVisible = visible;
logDebug(`[Store]: View visibility changed to ${String(visible)}`);
}
getRun(runId: number): WorkflowRun | undefined {
return this.runs.get(runId);
}
addRun(gitHubRepoContext: GitHubRepoContext, runData: model.WorkflowRun): WorkflowRun {
let run = this.runs.get(runData.id);
if (!run) {
run = new WorkflowRun(gitHubRepoContext, runData);
logDebug("[Store]: adding run: ", runData.id, runData.updated_at);
} else {
run.updateRun(runData);
logDebug("[Store]: updating run: ", runData.id, runData.updated_at);
}
this.runs.set(runData.id, run);
this.fire({run});
return run;
}
/**
* Start polling for updates for the given run
*/
pollRun(runId: number, repoContext: GitHubRepoContext, intervalMs: number, attempts = 10) {
log(`Starting polling for run ${runId} every ${intervalMs}ms for ${attempts} attempts`);
const existingUpdater: Updater | undefined = this.updaters.get(runId);
if (existingUpdater && existingUpdater.handle) {
clearInterval(existingUpdater.handle);
}
const updater: Updater = {
intervalMs,
repoContext,
runId,
remainingAttempts: attempts,
handle: undefined
};
updater.handle = setInterval(() => void this.fetchRun(updater), intervalMs);
this.updaters.set(runId, updater);
}
private async fetchRun(updater: Updater) {
if (!this._isFocused || !this._isViewVisible) {
return;
}
log(`Fetching run update: ${updater.runId}. Remaining attempts: ${updater.remainingAttempts}`);
updater.remainingAttempts--;
if (updater.remainingAttempts === 0) {
if (updater.handle) {
clearInterval(updater.handle);
}
this.updaters.delete(updater.runId);
}
const result = await updater.repoContext.client.actions.getWorkflowRun({
owner: updater.repoContext.owner,
repo: updater.repoContext.name,
run_id: updater.runId
});
const run = result.data;
log(`Polled run: ${run.id} Status: ${run.status || "null"} Conclusion: ${run.conclusion || "null"}`);
this.addRun(updater.repoContext, run);
if (run.status === "completed") {
if (updater.handle) {
clearInterval(updater.handle);
}
this.updaters.delete(updater.runId);
}
}
}