import { remote } from '../proto/runtime/v1alpha1/remote'; import { ApplicationError } from '../runtime/_errors'; export type SupportedTypes = string | boolean | number | object | null | undefined; export type JetroutineFunctionReturnTypes = SupportedTypes | Array | void; export type JetroutineFunction = (...args: SupportedTypes[]) => JetroutineFunctionReturnTypes | Promise; export type RegisteredFunctions = { [x: string]: JetroutineFunction; }; export type RegisteredCronjobs = { [x: string]: remote.CronJob; }; class PrivateSymbolTable { registeredFunctions: RegisteredFunctions; registeredCronjobs: RegisteredCronjobs; constructor() { this.registeredFunctions = {}; this.registeredCronjobs = {}; } register(symbol: string, func: JetroutineFunction) { if (this.registeredFunctions[symbol]) { // Collision detected. throw new ApplicationError(`Symbol name must be unique: \ more than one jetroutine/cronjob detected for symbol ${symbol}.`); } this.registeredFunctions[symbol] = func; } registerCron(symbol: string, cron: remote.CronJob) { if (this.registeredCronjobs[symbol]) { // Collision detected. throw new ApplicationError(`Symbol name must be unique: \ more than one cronjobs detected for symbol ${symbol}.`); } this.registeredCronjobs[symbol] = cron; } definedSymbols() { return Object.keys(this.registeredFunctions); } getRegisteredFunction(symbol: string) { return this.registeredFunctions[symbol]; } getAllCronjobs() { return Object.values(this.registeredCronjobs); } } export const SymbolTable = new PrivateSymbolTable();