2018-03-26 09:29:35 +08:00
|
|
|
import ScriptContext from "./script_context.js";
|
2018-03-27 10:29:14 +08:00
|
|
|
import server from "./server.js";
|
2018-03-26 09:16:57 +08:00
|
|
|
|
2018-08-01 16:12:54 +08:00
|
|
|
async function getAndExecuteBundle(noteId, workNote = null) {
|
2018-07-29 22:06:13 +08:00
|
|
|
const bundle = await server.get('script/bundle/' + noteId);
|
|
|
|
|
2018-08-01 16:12:54 +08:00
|
|
|
await executeBundle(bundle, workNote);
|
2018-07-29 22:06:13 +08:00
|
|
|
}
|
|
|
|
|
2018-08-01 16:12:54 +08:00
|
|
|
async function executeBundle(bundle, workNote) {
|
|
|
|
const apiContext = ScriptContext(bundle.note, bundle.allNotes, workNote);
|
2018-03-26 09:16:57 +08:00
|
|
|
|
|
|
|
return await (function () {
|
|
|
|
return eval(`const apiContext = this; (async function() { ${bundle.script}\r\n})()`);
|
|
|
|
}.call(apiContext));
|
|
|
|
}
|
|
|
|
|
2018-03-27 10:29:14 +08:00
|
|
|
async function executeStartupBundles() {
|
|
|
|
const scriptBundles = await server.get("script/startup");
|
|
|
|
|
|
|
|
for (const bundle of scriptBundles) {
|
|
|
|
await executeBundle(bundle);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-30 00:39:10 +08:00
|
|
|
async function executeRelationBundles(note, relationName) {
|
|
|
|
const bundlesToRun = await server.get("script/relation/" + note.noteId + "/" + relationName);
|
|
|
|
|
|
|
|
for (const bundle of bundlesToRun) {
|
|
|
|
await executeBundle(bundle, note);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-03-26 09:16:57 +08:00
|
|
|
export default {
|
2018-03-27 10:29:14 +08:00
|
|
|
executeBundle,
|
2018-07-29 22:06:13 +08:00
|
|
|
getAndExecuteBundle,
|
2018-07-30 00:39:10 +08:00
|
|
|
executeStartupBundles,
|
|
|
|
executeRelationBundles
|
2018-03-26 09:16:57 +08:00
|
|
|
}
|