From 19d8947123a9e9becfe57af9922614c10ae61eb7 Mon Sep 17 00:00:00 2001 From: azivner Date: Wed, 16 Jan 2019 22:52:32 +0100 Subject: [PATCH] runOnNoteChange fires also on the frontend, closes #340 --- src/public/javascripts/services/bundle.js | 8 ++++++-- src/public/javascripts/services/note_detail.js | 3 +++ src/routes/api/script.js | 5 +++++ src/services/script.js | 2 +- 4 files changed, 15 insertions(+), 3 deletions(-) diff --git a/src/public/javascripts/services/bundle.js b/src/public/javascripts/services/bundle.js index 2f6f6bd87..4d67496ba 100644 --- a/src/public/javascripts/services/bundle.js +++ b/src/public/javascripts/services/bundle.js @@ -30,9 +30,13 @@ async function executeStartupBundles() { } async function executeRelationBundles(note, relationName) { - const bundlesToRun = await server.get("script/relation/" + note.noteId + "/" + relationName); + note.bundleCache = note.bundleCache || {}; - for (const bundle of bundlesToRun) { + if (!note.bundleCache[relationName]) { + note.bundleCache[relationName] = await server.get("script/relation/" + note.noteId + "/" + relationName); + } + + for (const bundle of note.bundleCache[relationName]) { await executeBundle(bundle, note); } } diff --git a/src/public/javascripts/services/note_detail.js b/src/public/javascripts/services/note_detail.js index 07e3057f3..4f750b99b 100644 --- a/src/public/javascripts/services/note_detail.js +++ b/src/public/javascripts/services/note_detail.js @@ -131,6 +131,9 @@ async function saveNote() { } $savedIndicator.fadeIn(); + + // run async + bundleService.executeRelationBundles(getCurrentNote(), 'runOnNoteChange'); } async function saveNoteIfChanged() { diff --git a/src/routes/api/script.js b/src/routes/api/script.js index 87927e612..72ca8ec92 100644 --- a/src/routes/api/script.js +++ b/src/routes/api/script.js @@ -54,6 +54,11 @@ async function getRelationBundles(req) { for (const noteId of uniqueNoteIds) { const note = await repository.getNote(noteId); + + if (!note.isJavaScript() || note.getScriptEnv() !== 'frontend') { + continue; + } + const bundle = await scriptService.getScriptBundleForFrontend(note); if (bundle) { diff --git a/src/services/script.js b/src/services/script.js index 2c3a7855e..628510c6d 100644 --- a/src/services/script.js +++ b/src/services/script.js @@ -6,7 +6,7 @@ const sourceIdService = require('./source_id'); const log = require('./log'); async function executeNote(note, originEntity) { - if (!note.isJavaScript() || !note.isContentAvailable) { + if (!note.isJavaScript() || note.getScriptEnv() !== 'backend' || !note.isContentAvailable) { return; }