trilium/src/services/script.js

159 lines
4.3 KiB
JavaScript
Raw Normal View History

const sql = require('./sql');
const ScriptContext = require('./script_context');
async function executeNote(note) {
if (note.isProtected || !note.isJavaScript()) {
return;
}
2018-03-03 22:11:41 +08:00
const manualTransactionHandling = (await note.getAttributeMap()).manual_transaction_handling !== undefined;
2018-03-04 23:32:53 +08:00
const modules = await getModules([note], []);
// last \r\n is necessary if script contains line comment on its last line
const script = "async function() {\r\n" + modules.script + "\r\n}";
const ctx = new ScriptContext(null, note, module.allModules);
if (manualTransactionHandling) {
return await execute(ctx, script, '');
}
else {
return await sql.doInTransaction(async () => execute(ctx, script, ''));
}
}
async function executeScript(dataKey, script, params) {
const ctx = new ScriptContext(dataKey);
const paramsStr = getParams(params);
2018-02-25 11:44:45 +08:00
return await sql.doInTransaction(async () => execute(ctx, script, paramsStr));
}
2018-02-25 11:44:45 +08:00
async function execute(ctx, script, paramsStr) {
2018-03-04 23:32:53 +08:00
console.log(`const api = this; (${script})(${paramsStr})`);
return await (function() { return eval(`const api = this;\r\n(${script})(${paramsStr})`); }.call(ctx));
}
const timeouts = {};
const intervals = {};
function clearExistingJob(name) {
if (timeouts[name]) {
clearTimeout(timeouts[name]);
delete timeouts[name];
}
if (intervals[name]) {
clearInterval(intervals[name]);
delete intervals[name];
}
}
2018-02-25 11:44:45 +08:00
async function executeJob(script, params, manualTransactionHandling) {
const ctx = new ScriptContext();
const paramsStr = getParams(params);
if (manualTransactionHandling) {
return await execute(ctx, script, paramsStr);
}
else {
return await sql.doInTransaction(async () => execute(ctx, script, paramsStr));
}
}
async function setJob(opts) {
2018-02-25 11:44:45 +08:00
const { name, runEveryMs, initialRunAfterMs } = opts;
clearExistingJob(name);
const jobFunc = () => executeJob(opts.job, opts.params, opts.manualTransactionHandling);
2018-02-25 11:44:45 +08:00
if (runEveryMs && runEveryMs > 0) {
intervals[name] = setInterval(jobFunc, runEveryMs);
}
2018-02-25 11:44:45 +08:00
if (initialRunAfterMs && initialRunAfterMs > 0) {
timeouts[name] = setTimeout(jobFunc, initialRunAfterMs);
}
}
function getParams(params) {
if (!params) {
return params;
}
return params.map(p => {
if (typeof p === "string" && p.startsWith("!@#Function: ")) {
return p.substr(13);
}
else {
return JSON.stringify(p);
}
}).join(",");
}
2018-03-04 23:32:53 +08:00
async function getRenderScript(note) {
const subTreeScripts = await getModules(note, [note.noteId]);
2018-03-03 22:11:41 +08:00
// last \r\n is necessary if script contains line comment on its last line
return "async function() {" + subTreeScripts + note.content + "\r\n}";
}
2018-03-04 23:32:53 +08:00
async function getNoteScript(note) {
}
2018-03-03 22:11:41 +08:00
/**
* @param includedNoteIds - if multiple child note scripts reference same dependency (child note),
* it will be included just once
*/
2018-03-04 23:32:53 +08:00
async function getModules(children, includedNoteIds) {
const modules = [];
let allModules = [];
let script = '';
for (const child of children) {
if (!child.isJavaScript()) {
continue;
}
modules.push(child);
2018-03-03 22:11:41 +08:00
2018-03-04 23:32:53 +08:00
if (includedNoteIds.includes(child.noteId)) {
2018-03-03 22:11:41 +08:00
continue;
}
includedNoteIds.push(child.noteId);
2018-03-04 23:32:53 +08:00
const children = await getModules(await child.getChildren(), includedNoteIds);
allModules = allModules.concat(children.allModules);
2018-03-03 22:11:41 +08:00
2018-03-04 23:32:53 +08:00
script += children.script;
script += `
api.__modules['${child.noteId}'] = {};
await (async function(module, api, startNote, currentNote` + (children.modules.length > 0 ? ', ' : '') +
children.modules.map(child => child.title).join(', ') + `) {
${child.content}
})(api.__modules['${child.noteId}'], api, api.__startNote, api.__notes['${child.noteId}']` + (children.modules.length > 0 ? ', ' : '') +
children.modules.map(child => `api.__modules['${child.noteId}'].exports`).join(', ') + `);
`;
2018-03-03 22:11:41 +08:00
}
2018-03-04 23:32:53 +08:00
allModules = allModules.concat(modules);
return { script, modules, allModules };
2018-03-03 22:11:41 +08:00
}
module.exports = {
executeNote,
executeScript,
2018-03-03 22:11:41 +08:00
setJob,
2018-03-04 23:32:53 +08:00
getNoteScript,
getRenderScript
};