-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathpolling.ts
More file actions
54 lines (46 loc) · 1.42 KB
/
polling.ts
File metadata and controls
54 lines (46 loc) · 1.42 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
/*
* Copyright (c) 2025 Ping Identity Corporation. All rights reserved.
*
* This software may be modified and distributed under the terms
* of the MIT license. See the LICENSE file for details.
*/
import type {
InternalErrorResponse,
NodeStates,
PollingCollector,
Poller,
} from '@forgerock/davinci-client/types';
function isInternalErrorResponse(
value: NodeStates | InternalErrorResponse,
): value is InternalErrorResponse {
return 'type' in value && value.type === 'internal_error';
}
export default function pollingComponent(
formEl: HTMLFormElement,
collector: PollingCollector,
poll: Poller,
onNode: (node: NodeStates) => void,
) {
const button = document.createElement('button');
button.type = 'button';
button.value = collector.output.key;
button.textContent = 'Start polling';
formEl.appendChild(button);
const controller = new AbortController();
button.onclick = async () => {
button.disabled = true;
const status = document.createElement('p');
status.textContent = 'Polling...';
formEl.appendChild(status);
const result = await poll({ signal: controller.signal });
if (isInternalErrorResponse(result)) {
console.error(result.error?.message);
const errEl = document.createElement('p');
errEl.textContent = 'Polling error: ' + result.error?.message;
formEl.appendChild(errEl);
button.disabled = false;
return;
}
onNode(result);
};
}