-
Notifications
You must be signed in to change notification settings - Fork 72
Expand file tree
/
Copy pathhosting.worker.ts
More file actions
103 lines (81 loc) · 2.53 KB
/
Copy pathhosting.worker.ts
File metadata and controls
103 lines (81 loc) · 2.53 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
import { SYNC_CUSTOM_DOMAIN_TIMER_INTERVAL } from '$lib/constants/app.constants';
import { getCustomDomainRegistration } from '$lib/rest/bn.v1.rest';
import type { CustomDomain, CustomDomainName, CustomDomainState } from '$lib/types/custom-domain';
import type { PostMessageDataRequest, PostMessageRequest } from '$lib/types/post-message';
import { isNullish } from '@dfinity/utils';
export const onHostingMessage = async ({ data: dataMsg }: MessageEvent<PostMessageRequest>) => {
const { msg, data } = dataMsg;
switch (msg) {
case 'stopCustomDomainRegistrationTimer':
stopTimer();
return;
case 'startCustomDomainRegistrationTimer':
await startTimer({ data });
}
};
let timer: NodeJS.Timeout | undefined = undefined;
const stopTimer = () => {
if (!timer) {
return;
}
clearInterval(timer);
timer = undefined;
};
const startTimer = async ({ data: { customDomain } }: { data: PostMessageDataRequest }) => {
if (isNullish(customDomain)) {
// No custom domain registration to sync
return;
}
const sync = async () => await syncCustomDomainRegistration({ customDomain });
// We sync the cycles now but also schedule the update afterwards
await sync();
timer = setInterval(sync, SYNC_CUSTOM_DOMAIN_TIMER_INTERVAL);
};
let syncing = false;
const syncCustomDomainRegistration = async ({ customDomain }: { customDomain: CustomDomain }) => {
// We avoid to relaunch a sync while previous sync is not finished
if (syncing) {
return;
}
syncing = true;
try {
const sync = async (): Promise<CustomDomainState> => {
const [domainName] = customDomain;
return await syncCustomDomainRegistrationV1({ domain: domainName });
};
const registrationState = await sync();
emit(registrationState);
// We sync until Available or Failed
if (registrationState === null || ['Available', 'Failed'].includes(registrationState)) {
stopTimer();
}
} catch (err: unknown) {
console.error(err);
emit('failed');
// We sync until Available or Failed
stopTimer();
}
syncing = false;
};
const syncCustomDomainRegistrationV1 = async ({
domain
}: {
domain: CustomDomainName;
}): Promise<CustomDomainState> => {
const response = await getCustomDomainRegistration({ domainName: domain });
if (response?.status === 'success') {
const {
data: { registration_status }
} = response;
return registration_status;
}
return 'failed';
};
// Update ui with registration state
const emit = (registrationState: CustomDomainState | null) =>
postMessage({
msg: 'customDomainRegistrationState',
data: {
registrationState
}
});