|
| 1 | +import type { |
| 2 | + Context, |
| 3 | + Denops, |
| 4 | + Dispatcher, |
| 5 | + Meta, |
| 6 | +} from "https://deno.land/x/denops_core@v4.0.0/mod.ts"; |
| 7 | + |
| 8 | +type VimVoid<T> = T extends void ? 0 : T; |
| 9 | + |
| 10 | +type Collect<T extends readonly unknown[] | []> = { |
| 11 | + -readonly [P in keyof T]: VimVoid<Awaited<T[P]>>; |
| 12 | +}; |
| 13 | + |
| 14 | +class CollectHelper implements Denops { |
| 15 | + #denops: Denops; |
| 16 | + #calls: [string, ...unknown[]][]; |
| 17 | + #closed: boolean; |
| 18 | + |
| 19 | + constructor(denops: Denops) { |
| 20 | + this.#denops = denops; |
| 21 | + this.#calls = []; |
| 22 | + this.#closed = false; |
| 23 | + } |
| 24 | + |
| 25 | + static getCalls(helper: CollectHelper): [string, ...unknown[]][] { |
| 26 | + return helper.#calls; |
| 27 | + } |
| 28 | + |
| 29 | + static close(helper: CollectHelper): void { |
| 30 | + helper.#closed = true; |
| 31 | + } |
| 32 | + |
| 33 | + get name(): string { |
| 34 | + return this.#denops.name; |
| 35 | + } |
| 36 | + |
| 37 | + get meta(): Meta { |
| 38 | + return this.#denops.meta; |
| 39 | + } |
| 40 | + |
| 41 | + get context(): Record<string | number | symbol, unknown> { |
| 42 | + return this.#denops.context; |
| 43 | + } |
| 44 | + |
| 45 | + get dispatcher(): Dispatcher { |
| 46 | + return this.#denops.dispatcher; |
| 47 | + } |
| 48 | + |
| 49 | + set dispatcher(dispatcher: Dispatcher) { |
| 50 | + this.#denops.dispatcher = dispatcher; |
| 51 | + } |
| 52 | + |
| 53 | + redraw(_force?: boolean): Promise<void> { |
| 54 | + throw new Error("The 'redraw' method is not available on CollectHelper."); |
| 55 | + } |
| 56 | + |
| 57 | + call(fn: string, ...args: unknown[]): Promise<unknown> { |
| 58 | + if (this.#closed) { |
| 59 | + throw new Error( |
| 60 | + "CollectHelper instance is not available outside of 'collect' block", |
| 61 | + ); |
| 62 | + } |
| 63 | + this.#calls.push([fn, ...args]); |
| 64 | + return Promise.resolve(); |
| 65 | + } |
| 66 | + |
| 67 | + batch(..._calls: [string, ...unknown[]][]): Promise<unknown[]> { |
| 68 | + throw new Error("The 'batch' method is not available on CollectHelper."); |
| 69 | + } |
| 70 | + |
| 71 | + cmd(_cmd: string, _ctx: Context = {}): Promise<void> { |
| 72 | + throw new Error("The 'cmd' method is not available on CollectHelper."); |
| 73 | + } |
| 74 | + |
| 75 | + eval(expr: string, ctx: Context = {}): Promise<unknown> { |
| 76 | + if (this.#closed) { |
| 77 | + throw new Error( |
| 78 | + "CollectHelper instance is not available outside of 'collect' block", |
| 79 | + ); |
| 80 | + } |
| 81 | + this.call("denops#api#eval", expr, ctx); |
| 82 | + return Promise.resolve(); |
| 83 | + } |
| 84 | + |
| 85 | + dispatch(name: string, fn: string, ...args: unknown[]): Promise<unknown> { |
| 86 | + return this.#denops.dispatch(name, fn, ...args); |
| 87 | + } |
| 88 | +} |
| 89 | + |
| 90 | +/** |
| 91 | + * Call multiple denops functions sequentially without RPC overhead and return values |
| 92 | + * |
| 93 | + * ```typescript |
| 94 | + * import { Denops } from "../mod.ts"; |
| 95 | + * import { collect } from "./collect.ts"; |
| 96 | + * |
| 97 | + * export async function main(denops: Denops): Promise<void> { |
| 98 | + * const results = await collect(denops, (denops) => [ |
| 99 | + * denops.eval("&modifiable"), |
| 100 | + * denops.eval("&modified"), |
| 101 | + * denops.eval("&filetype"), |
| 102 | + * ]); |
| 103 | + * // results contains the value of modifiable, modified, and filetype |
| 104 | + * } |
| 105 | + * ``` |
| 106 | + * |
| 107 | + * Not like `batch`, the function can NOT be nested. |
| 108 | + * |
| 109 | + * Note that `denops.call()` or `denops.eval()` always return falsy value in |
| 110 | + * `collect()`, indicating that you **cannot** write code like below: |
| 111 | + * |
| 112 | + * ```typescript |
| 113 | + * import { Denops } from "../mod.ts"; |
| 114 | + * import { collect } from "./collect.ts"; |
| 115 | + * |
| 116 | + * export async function main(denops: Denops): Promise<void> { |
| 117 | + * const results = await collect(denops, (denops) => { |
| 118 | + * // !!! DON'T DO THIS !!! |
| 119 | + * (async () => { |
| 120 | + * if (await denops.call("has", "nvim")) { |
| 121 | + * // deno-lint-ignore no-explicit-any |
| 122 | + * await (denops.call("api_info") as any).version; |
| 123 | + * } else { |
| 124 | + * await denops.eval("v:version"); |
| 125 | + * } |
| 126 | + * })(); |
| 127 | + * return []; |
| 128 | + * }); |
| 129 | + * } |
| 130 | + * ``` |
| 131 | + * |
| 132 | + * The `denops` instance passed to the `collect` block is NOT available outside of |
| 133 | + * the block. An error is thrown when `denops.call()`, `denops.cmd()`, or |
| 134 | + * `denops.eval()` is called. |
| 135 | + * |
| 136 | + * Note that `denops.redraw()` and `denops.cmd()` cannot be called within `collect()`. |
| 137 | + * If it is called, an error is raised. |
| 138 | + */ |
| 139 | +export async function collect<T extends readonly unknown[] | []>( |
| 140 | + denops: Denops, |
| 141 | + executor: (helper: CollectHelper) => T, |
| 142 | +): Promise<Collect<T>> { |
| 143 | + const helper = new CollectHelper(denops); |
| 144 | + try { |
| 145 | + await Promise.all(executor(helper)); |
| 146 | + } finally { |
| 147 | + CollectHelper.close(helper); |
| 148 | + } |
| 149 | + const calls = CollectHelper.getCalls(helper); |
| 150 | + const results = await denops.batch(...calls); |
| 151 | + return results as Collect<T>; |
| 152 | +} |
0 commit comments