-
Notifications
You must be signed in to change notification settings - Fork 20
/
Copy pathshared.ts
71 lines (62 loc) · 2.1 KB
/
shared.ts
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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
function importedBlocksIterate(
importedBlocks: any[],
action: {
(k: string, importedModule: any): void;
}
) {
importedBlocks.forEach((importedModule) => {
Object.keys(importedModule)
.filter((item) => !item.includes("_init_blocks"))
.forEach((k) => {
action(k, importedModule);
});
});
}
export const addImportedBlocks = (importedBlocks: any[]) =>
importedBlocksIterate(importedBlocks, (k, importedModule) => {
globalThis[k] = importedModule[k];
});
export const removeImportedBlocks = (importedBlocks: any[]) =>
importedBlocksIterate(importedBlocks, (k) => {
delete globalThis[k];
});
export interface RunError {
exception: Error;
blockId: string;
}
export type Function = (...args: any[]) => any;
export type AsyncFunction = (...args: any[]) => Promise<any>;
// Get constructor of an async function and use it to eval the source
// It is like doing Function(source) but is async
export const makeAsyncFn: (source: string) => AsyncFunction =
Object.getPrototypeOf(async function () {}).constructor;
export const makeHighlightBlock = (
highlighter: (id: string | undefined) => void,
globalThisIdKey: string
) =>
async function (id: string, f: Function) {
const prevId = globalThis[globalThisIdKey];
highlighter(id);
const ret = await f();
highlighter(prevId);
return ret;
};
export const addGlobals = (globals: Record<string, any>) =>
Object.entries(globals).forEach(([k, v]) => {
globalThis[k] = v;
});
export const removeGlobals = (globals: Record<string, any>) =>
Object.keys(globals).forEach((k) => {
delete globalThis[k];
});
export const serializeError = <T extends Error>(error: T) =>
Object.fromEntries(
Array.from(
new Set([
...Object.getOwnPropertyNames(Object.getPrototypeOf(error)),
...Object.getOwnPropertyNames(error)
])
)
.filter((k) => typeof error[k] !== "function")
.map((k) => [k, error[k]])
) as T;