~glacambre/firenvim

firenvim/src/page/proxy.ts -rw-r--r-- 1.2 KiB
db4ee792glacambre README.md: add links to demos 1 year, 16 days 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
30
31
32
33
import { getNeovimFrameFunctions } 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 = getNeovimFrameFunctions({} as any);

type ft = typeof functions;
// The proxy automatically appends the frameId to the request, so we hide that from users
type ArgumentsType<T> = T extends (x: any, ...args: infer U) => any ? U: never;
type Promisify<T> = T extends Promise<infer U> ? T : Promise<T>;

export const page = {} as {
    [k in keyof ft]: (...args: ArgumentsType<ft[k]>) => Promisify<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: [(window as any).frameId].concat(arr),
                funcName: [func],
            },
            funcName: ["messagePage"],
        });
    });
}