-
-
Notifications
You must be signed in to change notification settings - Fork 197
/
Copy pathhmr-status-service.ts
98 lines (82 loc) · 2.59 KB
/
hmr-status-service.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
import { cache } from "../common/decorators";
import { HmrConstants } from "../common/constants";
export class HmrStatusService implements IHmrStatusService {
public static HMR_STATUS_LOG_REGEX = /([a-z A-Z]*) hmr hash ([a-z0-9]*)\./;
public static STARTED_MESSAGE = "Checking for updates to the bundle with";
public static SUCCESS_MESSAGE = "Successfully applied update with";
public static FAILED_MESSAGE = "Cannot apply update with";
private hashOperationStatuses: IDictionary<any> = {};
private intervals: IDictionary<any> = {};
constructor(private $logParserService: ILogParserService,
private $processService: IProcessService,
private $logger: ILogger) {
this.$processService.attachToProcessExitSignals(this, this.dispose);
}
public getHmrStatus(deviceId: string, operationHash: string): Promise<number> {
return new Promise((resolve, reject) => {
const key = `${deviceId}${operationHash}`;
let retryCount = 40;
this.intervals[key] = setInterval(() => {
const status = this.getStatusByKey(key);
if (status || retryCount === 0) {
clearInterval(this.intervals[key]);
this.intervals[key] = null;
resolve(status);
} else {
retryCount--;
}
}, 250);
});
}
@cache()
public attachToHmrStatusEvent(): void {
this.$logParserService.addParseRule({
regex: HmrStatusService.HMR_STATUS_LOG_REGEX,
handler: this.handleHmrStatusFound.bind(this),
name: "hmrStatus"
});
}
private handleHmrStatusFound(matches: RegExpMatchArray, deviceId: string): void {
const message = matches[1].trim();
const hash = matches[2];
let status;
switch (message) {
case HmrStatusService.SUCCESS_MESSAGE: {
status = HmrConstants.HMR_SUCCESS_STATUS;
break;
}
case HmrStatusService.FAILED_MESSAGE: {
status = HmrConstants.HMR_ERROR_STATUS;
break;
}
default: {
status = null;
break;
}
}
this.$logger.trace("Found hmr status.", { status, hash });
if (status) {
this.setData(status, hash, deviceId);
}
}
private getStatusByKey(key: string): number {
if (this.hashOperationStatuses[key]) {
return this.hashOperationStatuses[key].status;
}
return null;
}
private setData(status: Number, operationHash: string, deviceId: string): void {
const key = `${deviceId}${operationHash}`;
if (!this.hashOperationStatuses[key]) {
this.hashOperationStatuses[key] = <any>{};
}
this.hashOperationStatuses[key].status = status;
}
private dispose() {
_.forEach(this.intervals, (value, key) => {
clearInterval(value);
this.intervals[key] = null;
});
}
}
$injector.register("hmrStatusService", HmrStatusService);