2018-02-27 09:47:34 +08:00
|
|
|
const utils = require('./utils');
|
2018-08-23 16:10:04 +08:00
|
|
|
const BackendScriptApi = require('./backend_script_api');
|
2018-01-28 06:18:19 +08:00
|
|
|
|
2018-08-10 19:30:20 +08:00
|
|
|
function ScriptContext(startNote, allNotes, originEntity = null) {
|
2018-03-07 12:04:35 +08:00
|
|
|
this.modules = {};
|
|
|
|
this.notes = utils.toObject(allNotes, note => [note.noteId, note]);
|
2018-08-23 16:10:04 +08:00
|
|
|
this.apis = utils.toObject(allNotes, note => [note.noteId, new BackendScriptApi(startNote, note, originEntity)]);
|
2018-03-11 00:53:51 +08:00
|
|
|
this.require = moduleNoteIds => {
|
|
|
|
return moduleName => {
|
|
|
|
const candidates = allNotes.filter(note => moduleNoteIds.includes(note.noteId));
|
|
|
|
const note = candidates.find(c => c.title === moduleName);
|
|
|
|
|
|
|
|
if (!note) {
|
|
|
|
throw new Error("Could not find module note " + moduleName);
|
|
|
|
}
|
|
|
|
|
|
|
|
return this.modules[note.noteId].exports;
|
|
|
|
}
|
|
|
|
};
|
2018-03-07 12:04:35 +08:00
|
|
|
}
|
|
|
|
|
2018-01-28 06:18:19 +08:00
|
|
|
module.exports = ScriptContext;
|