From 5741b380f0174148765dc440c9c91ebd7454be9c Mon Sep 17 00:00:00 2001 From: zadam Date: Tue, 18 Aug 2020 23:32:50 +0200 Subject: [PATCH] fixed script support --- src/public/app/services/frontend_script_api.js | 8 +++----- src/routes/api/script.js | 18 +++++++++++++----- src/services/script.js | 12 ++++++------ 3 files changed, 22 insertions(+), 16 deletions(-) diff --git a/src/public/app/services/frontend_script_api.js b/src/public/app/services/frontend_script_api.js index b12b04382..f7e3c07a0 100644 --- a/src/public/app/services/frontend_script_api.js +++ b/src/public/app/services/frontend_script_api.js @@ -176,11 +176,9 @@ function FrontendScriptApi(startNote, currentNote, originEntity = null, $contain * @returns {Promise} */ this.searchForNotes = async searchString => { - const noteIds = await this.runOnBackend(async searchString => { - const notes = await api.searchForNotes(searchString); - - return notes.map(note => note.noteId); - }, [searchString]); + const noteIds = await this.runOnBackend( + searchString => api.searchForNotes(searchString).map(note => note.noteId), + [searchString]); return await treeCache.getNotes(noteIds); }; diff --git a/src/routes/api/script.js b/src/routes/api/script.js index 9c85af430..53b6ca56c 100644 --- a/src/routes/api/script.js +++ b/src/routes/api/script.js @@ -5,10 +5,18 @@ const attributeService = require('../../services/attributes'); const repository = require('../../services/repository'); const syncService = require('../../services/sync'); -function exec(req) { +async function exec(req) { try { - const result = scriptService.executeScript(req.body.script, req.body.params, req.body.startNoteId, - req.body.currentNoteId, req.body.originEntityName, req.body.originEntityId); + const {body} = req; + + const result = await scriptService.executeScript( + body.script, + body.params, + body.startNoteId, + body.currentNoteId, + body.originEntityName, + body.originEntityId + ); return { success: true, @@ -21,10 +29,10 @@ function exec(req) { } } -function run(req) { +async function run(req) { 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 }; } diff --git a/src/services/script.js b/src/services/script.js index b588fdfcd..eb9c92f83 100644 --- a/src/services/script.js +++ b/src/services/script.js @@ -34,12 +34,12 @@ async function executeBundle(bundle, apiParams = {}) { cls.set('sourceId', 'script'); // 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); try { - return execute(ctx, script); + return await execute(ctx, script); } catch (e) { 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 * 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 currentNote = repository.getNote(currentNoteId); const originEntity = repository.getEntityFromName(originEntityName, originEntityId); @@ -63,11 +63,11 @@ function executeScript(script, params, startNoteId, currentNoteId, originEntityN const bundle = getScriptBundle(currentNote); - return executeBundle(bundle, { startNote, originEntity }); + return await executeBundle(bundle, { startNote, originEntity }); } -function execute(ctx, script) { - return (function() { return eval(`const apiContext = this;\r\n(${script}\r\n)()`); }.call(ctx)); +async function execute(ctx, script) { + return await (function() { return eval(`const apiContext = this;\r\n(${script}\r\n)()`); }.call(ctx)); } function getParams(params) {