Change to registerFunction space script API (backwards compatibility added)
parent
7e82a1b278
commit
cdefe7351a
|
@ -2,13 +2,31 @@ import { System } from "../lib/plugos/system.ts";
|
|||
import { ScriptObject } from "../plugs/index/script.ts";
|
||||
import { AppCommand, CommandDef } from "./hooks/command.ts";
|
||||
|
||||
type FunctionDef = {
|
||||
name: string;
|
||||
};
|
||||
|
||||
export class ScriptEnvironment {
|
||||
functions: Record<string, (...args: any[]) => any> = {};
|
||||
commands: Record<string, AppCommand> = {};
|
||||
|
||||
// Public API
|
||||
registerFunction(name: string, fn: (...args: any[]) => any) {
|
||||
this.functions[name] = fn;
|
||||
|
||||
// Register function
|
||||
registerFunction(def: FunctionDef, fn: (...args: any[]) => any): void;
|
||||
// Legacy invocation
|
||||
registerFunction(name: string, fn: (...args: any[]) => any): void;
|
||||
registerFunction(
|
||||
arg: string | FunctionDef,
|
||||
fn: (...args: any[]) => any,
|
||||
): void {
|
||||
if (typeof arg === "string") {
|
||||
console.warn(
|
||||
"registerFunction with string is deprecated, use `{name: string}` instead",
|
||||
);
|
||||
arg = { name: arg };
|
||||
}
|
||||
this.functions[arg.name] = fn;
|
||||
}
|
||||
|
||||
registerCommand(command: CommandDef, fn: (...args: any[]) => any) {
|
||||
|
|
|
@ -41,8 +41,6 @@ shortcuts:
|
|||
key: "Alt-x"
|
||||
- command: "{[Upload: File]}"
|
||||
priority: 1 # Make sure this appears at the top of the list in the command palette
|
||||
- command: "{[Open Command Palette]}"
|
||||
key: "Ctrl-."
|
||||
|
||||
# Defines files to ignore in a format compatible with .gitignore
|
||||
spaceIgnore: |
|
||||
|
|
|
@ -13,7 +13,7 @@ Space scripts are defined by simply using `space-script` fenced code blocks in y
|
|||
Here is a trivial example:
|
||||
|
||||
```space-script
|
||||
silverbullet.registerFunction("helloYeller", (name) => {
|
||||
silverbullet.registerFunction({name: "helloYeller"}, (name) => {
|
||||
return `Hello ${name}!`.toUpperCase();
|
||||
})
|
||||
```
|
||||
|
@ -35,7 +35,7 @@ While not very secure, some effort is put into running this code in a clean Java
|
|||
|
||||
Depending on where code is run (client or server), a slightly different JavaScript API will be available. However, code should ideally primarily rely on the following explicitly exposed APIs:
|
||||
|
||||
* `silverbullet.registerFunction(name, callback)`: registers a custom function (see [[#Custom functions]]).
|
||||
* `silverbullet.registerFunction(definition, callback)`: registers a custom function (see [[#Custom functions]]).
|
||||
* `silverbullet.registerCommand(definition, callback)`: registers a custom command (see [[#Custom commands]]).
|
||||
* `syscall(name, args...)`: invoke a syscall (see [[#Syscalls]]).
|
||||
|
||||
|
@ -51,14 +51,15 @@ Since template rendering happens on the server (except in [[Client Modes#Synced
|
|||
|
||||
The `silverbullet.registerFunction` API takes two arguments:
|
||||
|
||||
* `name`: the function name to register
|
||||
* `options`: with currently just one option:
|
||||
* `name`: the name of the function to register
|
||||
* `callback`: the callback function to invoke (can be `async` or not)
|
||||
|
||||
## Example
|
||||
Even though a [[Functions#readPage(name)]] function already exist, you could implement it in space script as follows (let’s name it `myReadPage`) using the `syscall` API (detailed further in [[#Syscalls]]):
|
||||
|
||||
```space-script
|
||||
silverbullet.registerFunction("myReadPage", async (name) => {
|
||||
silverbullet.registerFunction({name: "myReadPage"}, async (name) => {
|
||||
const pageContent = await syscall("space.readPage", name);
|
||||
return pageContent;
|
||||
})
|
||||
|
|
Loading…
Reference in New Issue