~glacambre/firenvim

ref: eca09fa8c73a947934c2d71ea8fb944c71db8932 firenvim/src/page/proxy.ts -rw-r--r-- 984 bytes
eca09fa8glacambre Add tslint-sonarts, fix lots of errors it detected, fix proxy 2 years ago
                                                                                
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
import { getFunctions } from "./functions";

// We don't need to give real values to getFunctions since we're only trying to
// get the name of functions that exist in the page.
const functions = getFunctions({} as any);

type ft = typeof functions;
type ArgumentsType<T> = T extends  (...args: infer U) => any ? U: never;

export const page = {} as { [k in keyof ft]: (...args: ArgumentsType<ft[k]>) => Promise<ReturnType<ft[k]>> };

let funcName: keyof typeof functions;
for (funcName in functions) {
    if (!functions.hasOwnProperty(funcName)) { // Make tslint happy
        continue;
    }
    // We need to declare func here because funcName is a global and would not
    // be captured in the closure otherwise
    const func = funcName;
    page[func] = ((...arr: any[]) => {
        return browser.runtime.sendMessage({
            args: {
                args: arr,
                function: func,
            },
            function: "messageOwnTab",
        });
    });
}