-
Notifications
You must be signed in to change notification settings - Fork 10
/
index.js
41 lines (36 loc) · 896 Bytes
/
index.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
import { UINode } from './lib/node.js';
export { UINode } from './lib/node.js';
export function get(predicate) {
return new Promise((resolve, reject) => {
let tries = 0;
function tryResolve() {
ObjC.schedule(ObjC.mainQueue, () => {
const { UIWindow } = getApi();
const window = UIWindow.keyWindow();
const layout = new UINode(window);
const node = layout.find(predicate);
if (node !== null) {
resolve(node);
return;
}
// TODO: configurable timeout and retry interval
tries++;
if (tries < 40) {
setTimeout(tryResolve, 500);
} else {
reject(new Error('Timed out'));
}
});
}
tryResolve();
});
}
let _api = null;
function getApi() {
if (_api === null) {
_api = {
UIWindow: ObjC.classes.UIWindow
};
}
return _api;
}