support for backend jobs and other script API changes

This commit is contained in:
azivner
2018-02-24 14:42:52 -05:00
parent 5dd93e4cdc
commit a555b6319c
8 changed files with 106 additions and 11 deletions

View File

@@ -29,7 +29,7 @@ async function getNoteStartingWith(parentNoteId, startsWith) {
AND note_tree.isDeleted = 0`, [parentNoteId]);
}
async function getRootNoteId() {
async function getRootCalendarNoteId() {
let rootNoteId = await sql.getValue(`SELECT notes.noteId FROM notes JOIN attributes USING(noteId)
WHERE attributes.name = '${CALENDAR_ROOT_ATTRIBUTE}' AND notes.isDeleted = 0`);
@@ -91,7 +91,7 @@ async function getMonthNoteId(dateTimeStr, rootNoteId) {
async function getDateNoteId(dateTimeStr, rootNoteId = null) {
if (!rootNoteId) {
rootNoteId = await getRootNoteId();
rootNoteId = await getRootCalendarNoteId();
}
const dateStr = dateTimeStr.substr(0, 10);
@@ -119,7 +119,7 @@ async function getDateNoteId(dateTimeStr, rootNoteId = null) {
}
module.exports = {
getRootNoteId,
getRootCalendarNoteId,
getYearNoteId,
getMonthNoteId,
getDateNoteId

View File

@@ -18,7 +18,40 @@ async function executeScript(dataKey, script, params) {
return ret;
}
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];
}
}
async function setJob(opts) {
clearExistingJob(opts.name);
if (opts.runEveryMs && opts.runEveryMs > 0) {
intervals[opts.name] = setInterval(() => executeScript(null, opts.job, opts.params), opts.runEveryMs);
}
if (opts.initialRunAfterMs && opts.initialRunAfterMs > 0) {
timeouts[opts.name] = setTimeout(() => executeScript(null, opts.job, opts.params), opts.initialRunAfterMs);
}
}
function getParams(params) {
if (!params) {
return params;
}
return params.map(p => {
if (typeof p === "string" && p.startsWith("!@#Function: ")) {
return p.substr(13);
@@ -30,5 +63,6 @@ function getParams(params) {
}
module.exports = {
executeScript
executeScript,
setJob
};

View File

@@ -23,10 +23,10 @@ function ScriptContext(noteId, dataKey) {
return notes.length > 0 ? notes[0] : null;
};
this.createNote = async function (parentNoteId, name, jsonContent, extraOptions = {}) {
this.createNote = async function (parentNoteId, title, content = "", extraOptions = {}) {
const note = {
title: name,
content: extraOptions.json ? JSON.stringify(jsonContent, null, '\t') : jsonContent,
title: title,
content: extraOptions.json ? JSON.stringify(content, null, '\t') : content,
target: 'into',
isProtected: extraOptions.isProtected !== undefined ? extraOptions.isProtected : false,
type: extraOptions.type,
@@ -58,10 +58,9 @@ function ScriptContext(noteId, dataKey) {
this.updateEntity = this.repository.updateEntity;
this.log = function(message) {
log.info(`Script: ${message}`);
};
this.log = message => log.info(`Script: ${message}`);
this.getRootCalendarNoteId = date_notes.getRootCalendarNoteId;
this.getDateNoteId = date_notes.getDateNoteId;
}