forked from credential-handler/web-credential-handler
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCredentialEventProxy.js
50 lines (42 loc) · 1.3 KB
/
CredentialEventProxy.js
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
/*!
* Copyright (c) 2017-2018 Digital Bazaar, Inc. All rights reserved.
*/
/* global window */
'use strict';
import {WebApp} from 'web-request-rpc';
const PROXY_EVENT_TIMEOUT = 60000;
export class CredentialEventProxy extends WebApp {
constructor() {
super(window.location.origin);
}
async receive() {
const self = this;
await self.connect();
// this promise resolves once the event is received
return new Promise((resolveReceive, rejectReceive) => {
const timeoutId = setTimeout(() => {
rejectReceive(new Error('Timed out waiting to receive event.'));
}, PROXY_EVENT_TIMEOUT);
self.server.define('credentialEventProxy', {
// called by credential handler to send event to UI window
async send(event) {
// event received, clear timeout
resolveReceive(event);
clearTimeout(timeoutId);
// this promise resolves when the promise that the UI passes
// to `event.respondWith` resolves
return new Promise((resolveSend, rejectSend) => {
event.respondWith = promise => {
try {
resolveSend(promise);
} catch(e) {
rejectSend(e);
}
};
});
}
});
self.ready();
});
}
}