-
Notifications
You must be signed in to change notification settings - Fork 125
/
Copy pathscript_context.ts
37 lines (30 loc) · 1.09 KB
/
script_context.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
import { toObject } from "./utils.js";
import BackendScriptApi from "./backend_script_api.js";
import type BNote from "../becca/entities/bnote.js";
import type { ApiParams } from "./backend_script_api_interface.js";
type Module = {
exports: any[];
};
class ScriptContext {
modules: Record<string, Module>;
notes: {};
apis: {};
allNotes: BNote[];
constructor(allNotes: BNote[], apiParams: ApiParams) {
this.allNotes = allNotes;
this.modules = {};
this.notes = toObject(allNotes, (note) => [note.noteId, note]);
this.apis = toObject(allNotes, (note) => [note.noteId, new BackendScriptApi(note, apiParams)]);
}
require(moduleNoteIds: string[]) {
return (moduleName: string) => {
const candidates = this.allNotes.filter((note) => moduleNoteIds.includes(note.noteId));
const note = candidates.find((c) => c.title === moduleName);
if (!note) {
return require(moduleName);
}
return this.modules[note.noteId].exports;
};
}
}
export default ScriptContext;