2018-03-25 21:29:35 -04:00
|
|
|
import ScriptContext from "./script_context.js";
|
2018-03-26 22:29:14 -04:00
|
|
|
import server from "./server.js";
|
2018-08-17 11:31:42 +02:00
|
|
|
import infoService from "./info.js";
|
2018-03-25 21:16:57 -04:00
|
|
|
|
2018-08-10 13:30:20 +02:00
|
|
|
async function getAndExecuteBundle(noteId, originEntity = null) {
|
2018-07-29 16:06:13 +02:00
|
|
|
const bundle = await server.get('script/bundle/' + noteId);
|
|
|
|
|
2018-08-10 13:30:20 +02:00
|
|
|
await executeBundle(bundle, originEntity);
|
2018-07-29 16:06:13 +02:00
|
|
|
}
|
|
|
|
|
2019-05-19 21:52:28 +02:00
|
|
|
async function executeBundle(bundle, originEntity, tabContext) {
|
|
|
|
const apiContext = await ScriptContext(bundle.noteId, bundle.allNoteIds, originEntity, tabContext);
|
2018-03-25 21:16:57 -04:00
|
|
|
|
2018-08-17 11:31:42 +02:00
|
|
|
try {
|
|
|
|
return await (function () {
|
|
|
|
return eval(`const apiContext = this; (async function() { ${bundle.script}\r\n})()`);
|
|
|
|
}.call(apiContext));
|
|
|
|
}
|
|
|
|
catch (e) {
|
2019-03-07 20:51:55 +01:00
|
|
|
infoService.showAndLogError(`Execution of ${bundle.noteId} failed with error: ${e.message}`);
|
2018-08-17 11:31:42 +02:00
|
|
|
}
|
2018-03-25 21:16:57 -04:00
|
|
|
}
|
|
|
|
|
2018-03-26 22:29:14 -04:00
|
|
|
async function executeStartupBundles() {
|
|
|
|
const scriptBundles = await server.get("script/startup");
|
|
|
|
|
|
|
|
for (const bundle of scriptBundles) {
|
|
|
|
await executeBundle(bundle);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-05-29 21:48:48 +02:00
|
|
|
async function executeRelationBundles(note, relationName, tabContext) {
|
2019-01-16 22:52:32 +01:00
|
|
|
note.bundleCache = note.bundleCache || {};
|
2018-07-29 18:39:10 +02:00
|
|
|
|
2019-01-16 22:52:32 +01:00
|
|
|
if (!note.bundleCache[relationName]) {
|
|
|
|
note.bundleCache[relationName] = await server.get("script/relation/" + note.noteId + "/" + relationName);
|
|
|
|
}
|
|
|
|
|
|
|
|
for (const bundle of note.bundleCache[relationName]) {
|
2019-05-29 21:48:48 +02:00
|
|
|
await executeBundle(bundle, note, tabContext);
|
2018-07-29 18:39:10 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-03-25 21:16:57 -04:00
|
|
|
export default {
|
2018-03-26 22:29:14 -04:00
|
|
|
executeBundle,
|
2018-07-29 16:06:13 +02:00
|
|
|
getAndExecuteBundle,
|
2018-07-29 18:39:10 +02:00
|
|
|
executeStartupBundles,
|
|
|
|
executeRelationBundles
|
2018-03-25 21:16:57 -04:00
|
|
|
}
|