server-ts: Convert services/script_context

This commit is contained in:
Elian Doran 2024-04-04 22:29:12 +03:00
parent 15dee4b952
commit 884b6618fb
No known key found for this signature in database
5 changed files with 51 additions and 30 deletions

View File

@ -33,6 +33,7 @@ import BOption = require('../becca/entities/boption');
import { AttributeRow, AttributeType, NoteType } from '../becca/entities/rows';
import Becca from '../becca/becca-interface';
import { NoteParams } from './note-interface';
import { ApiParams } from './backend_script_api_interface';
/**
@ -46,11 +47,6 @@ import { NoteParams } from './note-interface';
* @var {BackendScriptApi} api
*/
interface ApiParams {
startNote: BNote;
originEntity: AbstractBeccaEntity<any>;
}
interface SearchParams {
includeArchivedNotes?: boolean;
ignoreHoistedNote?: boolean;
@ -384,6 +380,7 @@ interface Api {
};
}
// TODO: Convert to class.
/**
* <p>This is the main backend API interface for scripts. All the properties and methods are published in the "api" object
* available in the JS backend notes. You can use e.g. <code>api.log(api.startNote.title);</code></p>
@ -654,4 +651,6 @@ function BackendScriptApi(this: Api, currentNote: BNote, apiParams: ApiParams) {
}
}
export = BackendScriptApi;
export = BackendScriptApi as any as {
new (currentNote: BNote, apiParams: ApiParams): Api
};

View File

@ -0,0 +1,7 @@
import AbstractBeccaEntity = require("../becca/entities/abstract_becca_entity");
import BNote = require("../becca/entities/bnote");
export interface ApiParams {
startNote: BNote;
originEntity: AbstractBeccaEntity<any>;
}

View File

@ -1,4 +1,4 @@
const ScriptContext = require('./script_context.js');
const ScriptContext = require('./script_context');
const cls = require('./cls');
const log = require('./log');
const becca = require('../becca/becca');
@ -76,7 +76,7 @@ function executeScript(script, params, startNoteId, currentNoteId, originEntityN
}
function execute(ctx, script) {
return function() { return eval(`const apiContext = this;\r\n(${script}\r\n)()`); }.call(ctx);
return function () { return eval(`const apiContext = this;\r\n(${script}\r\n)()`); }.call(ctx);
}
function getParams(params) {

View File

@ -1,22 +0,0 @@
const utils = require('./utils');
const BackendScriptApi = require('./backend_script_api');
function ScriptContext(allNotes, apiParams = {}) {
this.modules = {};
this.notes = utils.toObject(allNotes, note => [note.noteId, note]);
this.apis = utils.toObject(allNotes, note => [note.noteId, new BackendScriptApi(note, apiParams)]);
this.require = moduleNoteIds => {
return moduleName => {
const candidates = allNotes.filter(note => moduleNoteIds.includes(note.noteId));
const note = candidates.find(c => c.title === moduleName);
if (!note) {
return require(moduleName);
}
return this.modules[note.noteId].exports;
}
};
}
module.exports = ScriptContext;

View File

@ -0,0 +1,37 @@
import utils = require('./utils');
import BackendScriptApi = require('./backend_script_api');
import BNote = require('../becca/entities/bnote');
import { ApiParams } from './backend_script_api_interface';
type Module = {
exports: any[];
};
class ScriptContext {
modules: Record<string, Module>;
notes: {};
apis: {};
allNotes: BNote[];
constructor(allNotes: BNote[], apiParams: ApiParams) {
this.allNotes = allNotes;
this.modules = {};
this.notes = utils.toObject(allNotes, note => [note.noteId, note]);
this.apis = utils.toObject(allNotes, note => [note.noteId, new BackendScriptApi(note, apiParams)]);
}
require(moduleNoteIds: string[]) {
return (moduleName: string) => {
const candidates = this.allNotes.filter(note => moduleNoteIds.includes(note.noteId));
const note = candidates.find(c => c.title === moduleName);
if (!note) {
return require(moduleName);
}
return this.modules[note.noteId].exports;
}
};
}
export = ScriptContext;