2025-01-02 13:47:44 +01:00
|
|
|
import { toObject } from "./utils.js";
|
2024-07-18 21:35:17 +03:00
|
|
|
import BackendScriptApi from "./backend_script_api.js";
|
2025-01-13 23:18:10 +02:00
|
|
|
import type BNote from "../becca/entities/bnote.js";
|
2025-01-09 18:36:24 +02:00
|
|
|
import type { ApiParams } from "./backend_script_api_interface.js";
|
2024-04-04 22:29:12 +03:00
|
|
|
|
|
|
|
type Module = {
|
|
|
|
exports: any[];
|
|
|
|
};
|
|
|
|
|
|
|
|
class ScriptContext {
|
|
|
|
modules: Record<string, Module>;
|
|
|
|
notes: {};
|
|
|
|
apis: {};
|
|
|
|
allNotes: BNote[];
|
2024-12-22 15:42:15 +02:00
|
|
|
|
2024-04-04 22:29:12 +03:00
|
|
|
constructor(allNotes: BNote[], apiParams: ApiParams) {
|
|
|
|
this.allNotes = allNotes;
|
|
|
|
this.modules = {};
|
2025-01-09 18:07:02 +02:00
|
|
|
this.notes = toObject(allNotes, (note) => [note.noteId, note]);
|
|
|
|
this.apis = toObject(allNotes, (note) => [note.noteId, new BackendScriptApi(note, apiParams)]);
|
2024-04-04 22:29:12 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
require(moduleNoteIds: string[]) {
|
|
|
|
return (moduleName: string) => {
|
2025-01-09 18:07:02 +02:00
|
|
|
const candidates = this.allNotes.filter((note) => moduleNoteIds.includes(note.noteId));
|
|
|
|
const note = candidates.find((c) => c.title === moduleName);
|
2024-04-04 22:29:12 +03:00
|
|
|
|
|
|
|
if (!note) {
|
|
|
|
return require(moduleName);
|
|
|
|
}
|
|
|
|
|
|
|
|
return this.modules[note.noteId].exports;
|
2025-01-09 18:07:02 +02:00
|
|
|
};
|
|
|
|
}
|
2024-04-04 22:29:12 +03:00
|
|
|
}
|
|
|
|
|
2024-07-18 21:50:12 +03:00
|
|
|
export default ScriptContext;
|