fixed script support

This commit is contained in:
zadam 2020-08-18 23:32:50 +02:00
parent 7335844ae3
commit 5741b380f0
3 changed files with 22 additions and 16 deletions

View File

@ -176,11 +176,9 @@ function FrontendScriptApi(startNote, currentNote, originEntity = null, $contain
* @returns {Promise<NoteShort[]>} * @returns {Promise<NoteShort[]>}
*/ */
this.searchForNotes = async searchString => { this.searchForNotes = async searchString => {
const noteIds = await this.runOnBackend(async searchString => { const noteIds = await this.runOnBackend(
const notes = await api.searchForNotes(searchString); searchString => api.searchForNotes(searchString).map(note => note.noteId),
[searchString]);
return notes.map(note => note.noteId);
}, [searchString]);
return await treeCache.getNotes(noteIds); return await treeCache.getNotes(noteIds);
}; };

View File

@ -5,10 +5,18 @@ const attributeService = require('../../services/attributes');
const repository = require('../../services/repository'); const repository = require('../../services/repository');
const syncService = require('../../services/sync'); const syncService = require('../../services/sync');
function exec(req) { async function exec(req) {
try { try {
const result = scriptService.executeScript(req.body.script, req.body.params, req.body.startNoteId, const {body} = req;
req.body.currentNoteId, req.body.originEntityName, req.body.originEntityId);
const result = await scriptService.executeScript(
body.script,
body.params,
body.startNoteId,
body.currentNoteId,
body.originEntityName,
body.originEntityId
);
return { return {
success: true, success: true,
@ -21,10 +29,10 @@ function exec(req) {
} }
} }
function run(req) { async function run(req) {
const note = repository.getNote(req.params.noteId); const note = repository.getNote(req.params.noteId);
const result = scriptService.executeNote(note, { originEntity: note }); const result = await scriptService.executeNote(note, { originEntity: note });
return { executionResult: result }; return { executionResult: result };
} }

View File

@ -34,12 +34,12 @@ async function executeBundle(bundle, apiParams = {}) {
cls.set('sourceId', 'script'); cls.set('sourceId', 'script');
// last \r\n is necessary if script contains line comment on its last line // last \r\n is necessary if script contains line comment on its last line
const script = "function() {\r\n" + bundle.script + "\r\n}"; const script = "async function() {\r\n" + bundle.script + "\r\n}";
const ctx = new ScriptContext(bundle.allNotes, apiParams); const ctx = new ScriptContext(bundle.allNotes, apiParams);
try { try {
return execute(ctx, script); return await execute(ctx, script);
} }
catch (e) { catch (e) {
log.error(`Execution of script "${bundle.note.title}" (${bundle.note.noteId}) failed with error: ${e.message}`); log.error(`Execution of script "${bundle.note.title}" (${bundle.note.noteId}) failed with error: ${e.message}`);
@ -52,7 +52,7 @@ async function executeBundle(bundle, apiParams = {}) {
* This method preserves frontend startNode - that's why we start execution from currentNote and override * This method preserves frontend startNode - that's why we start execution from currentNote and override
* bundle's startNote. * bundle's startNote.
*/ */
function executeScript(script, params, startNoteId, currentNoteId, originEntityName, originEntityId) { async function executeScript(script, params, startNoteId, currentNoteId, originEntityName, originEntityId) {
const startNote = repository.getNote(startNoteId); const startNote = repository.getNote(startNoteId);
const currentNote = repository.getNote(currentNoteId); const currentNote = repository.getNote(currentNoteId);
const originEntity = repository.getEntityFromName(originEntityName, originEntityId); const originEntity = repository.getEntityFromName(originEntityName, originEntityId);
@ -63,11 +63,11 @@ function executeScript(script, params, startNoteId, currentNoteId, originEntityN
const bundle = getScriptBundle(currentNote); const bundle = getScriptBundle(currentNote);
return executeBundle(bundle, { startNote, originEntity }); return await executeBundle(bundle, { startNote, originEntity });
} }
function execute(ctx, script) { async function execute(ctx, script) {
return (function() { return eval(`const apiContext = this;\r\n(${script}\r\n)()`); }.call(ctx)); return await (function() { return eval(`const apiContext = this;\r\n(${script}\r\n)()`); }.call(ctx));
} }
function getParams(params) { function getParams(params) {