|
| 1 | +/** |
| 2 | + * A module to provide lambda function that is callable from the outside of the plugin. |
| 3 | + * |
| 4 | + * Use `denops#callback#register()` and `denops#callback#call()` functions instead if you'd like |
| 5 | + * to create a lambda function of Vim script that is callable from Deno. |
| 6 | + * |
| 7 | + * ```typescript |
| 8 | + * import { Denops } from "../mod.ts"; |
| 9 | + * import * as lambda from "./mod.ts"; |
| 10 | + * |
| 11 | + * export async function main(denops: Denops): Promise<void> { |
| 12 | + * // Add lambda function |
| 13 | + * const id = lambda.register( |
| 14 | + * denops, |
| 15 | + * () => { |
| 16 | + * // Do what ever you want. |
| 17 | + * }, |
| 18 | + * ); |
| 19 | + * |
| 20 | + * // Use id to dispatch added function from Deno |
| 21 | + * await denops.dispatch(denops.name, id); |
| 22 | + * |
| 23 | + * // Or from Vim |
| 24 | + * await denops.cmd("call denops#notify(name, id, [])", { |
| 25 | + * name: denops.name, |
| 26 | + * id, |
| 27 | + * }); |
| 28 | + * } |
| 29 | + * ``` |
| 30 | + * |
| 31 | + * @module |
| 32 | + */ |
| 33 | + |
| 34 | +import type { Denops } from "https://deno.land/x/denops_core@v3.4.1/mod.ts"; |
| 35 | + |
| 36 | +/** |
| 37 | + * Lambda function identifier |
| 38 | + */ |
| 39 | +export type Identifier = string; |
| 40 | + |
| 41 | +/** |
| 42 | + * Lambda function |
| 43 | + */ |
| 44 | +export type Fn = (...args: unknown[]) => unknown; |
| 45 | + |
| 46 | +/** |
| 47 | + * Register options |
| 48 | + */ |
| 49 | +export interface Options { |
| 50 | + /** Register the function as a one-time lambda function that will be removed when the function has called */ |
| 51 | + once?: boolean; |
| 52 | +} |
| 53 | + |
| 54 | +/** |
| 55 | + * Register a lambda function as a denops API and return the identifier. |
| 56 | + * |
| 57 | + * ```typescript |
| 58 | + * import { Denops } from "../mod.ts"; |
| 59 | + * import * as lambda from "./mod.ts"; |
| 60 | + * |
| 61 | + * export async function main(denops: Denops): Promise<void> { |
| 62 | + * // Add lambda function |
| 63 | + * const id = lambda.register( |
| 64 | + * denops, |
| 65 | + * () => { |
| 66 | + * // Do what ever you want. |
| 67 | + * }, |
| 68 | + * ); |
| 69 | + * |
| 70 | + * // Use id to dispatch added function from Deno |
| 71 | + * await denops.dispatch(denops.name, id); |
| 72 | + * |
| 73 | + * // Or from Vim |
| 74 | + * await denops.cmd("call denops#notify(name, id, [])", { |
| 75 | + * name: denops.name, |
| 76 | + * id, |
| 77 | + * }); |
| 78 | + * } |
| 79 | + * ``` |
| 80 | + * |
| 81 | + * If you need an one-time lambda function, use `once` option like |
| 82 | + * |
| 83 | + * ```typescript |
| 84 | + * import { Denops } from "../mod.ts"; |
| 85 | + * import * as lambda from "./mod.ts"; |
| 86 | + * |
| 87 | + * export async function main(denops: Denops): Promise<void> { |
| 88 | + * // Add lambda function |
| 89 | + * const id = lambda.register( |
| 90 | + * denops, |
| 91 | + * () => { |
| 92 | + * // Do what ever you want. |
| 93 | + * }, |
| 94 | + * { |
| 95 | + * once: true, |
| 96 | + * }, |
| 97 | + * ); |
| 98 | + * |
| 99 | + * // Use id to dispatch added function from Deno |
| 100 | + * await denops.dispatch(denops.name, id); |
| 101 | + * |
| 102 | + * // Second call would throw error |
| 103 | + * await denops.dispatch(denops.name, id); |
| 104 | + * } |
| 105 | + * ``` |
| 106 | + */ |
| 107 | +export function register( |
| 108 | + denops: Denops, |
| 109 | + fn: Fn, |
| 110 | + options: Options = {}, |
| 111 | +): Identifier { |
| 112 | + const uuid = crypto.randomUUID(); |
| 113 | + const id = `lambda:${denops.name}:${uuid}`; |
| 114 | + if (options.once) { |
| 115 | + denops.dispatcher[id] = async (...args: unknown[]) => { |
| 116 | + try { |
| 117 | + return await fn(...args); |
| 118 | + } finally { |
| 119 | + unregister(denops, id); |
| 120 | + } |
| 121 | + }; |
| 122 | + } else { |
| 123 | + denops.dispatcher[id] = async (...args: unknown[]) => { |
| 124 | + return await fn(...args); |
| 125 | + }; |
| 126 | + } |
| 127 | + return id; |
| 128 | +} |
| 129 | + |
| 130 | +/** |
| 131 | + * Unregister a lambda function from a denops API identified by the identifier |
| 132 | + * |
| 133 | + * It returns `true` if the lambda function is unregistered. Otherwise it returns `false`. |
| 134 | + */ |
| 135 | +export function unregister( |
| 136 | + denops: Denops, |
| 137 | + id: Identifier, |
| 138 | +): boolean { |
| 139 | + if (id in denops.dispatcher) { |
| 140 | + delete denops.dispatcher[id]; |
| 141 | + return true; |
| 142 | + } |
| 143 | + return false; |
| 144 | +} |
0 commit comments