|
| 1 | +# batch |
| 2 | + |
| 3 | +`batch` is a module to provide `denops.batch()` helper functions. |
| 4 | + |
| 5 | +- [API documentation](https://doc.deno.land/https/deno.land/x/denops_std/batch/mod.ts) |
| 6 | + |
| 7 | +## Usage |
| 8 | + |
| 9 | +### batch |
| 10 | + |
| 11 | +Use `batch()` to call multiple denops functions sequentially without overhead |
| 12 | +like: |
| 13 | + |
| 14 | +```typescript |
| 15 | +import { Denops } from "https://deno.land/x/denops_std/mod.ts"; |
| 16 | +import { batch } from "https://deno.land/x/denops_std/batch/mod.ts"; |
| 17 | + |
| 18 | +export async function main(denops: Denops): Promise<void> { |
| 19 | + await batch(denops, async (denops) => { |
| 20 | + await denops.cmd("setlocal modifiable"); |
| 21 | + await denops.cmd("setlocal noswap"); |
| 22 | + await denops.cmd("setlocal nobackup"); |
| 23 | + }); |
| 24 | +} |
| 25 | +``` |
| 26 | + |
| 27 | +The function can be nested thus users can use functions which may internally use |
| 28 | +`batch()` like: |
| 29 | + |
| 30 | +```typescript |
| 31 | +import { Denops } from "https://deno.land/x/denops_std/mod.ts"; |
| 32 | +import { batch } from "https://deno.land/x/denops_std/batch/mod.ts"; |
| 33 | + |
| 34 | +async function replace(denops: Denops, content: string): Promise<void> { |
| 35 | + await batch(denops, (denops) => { |
| 36 | + await denops.cmd("setlocal modifiable"); |
| 37 | + await denops.cmd("setlocal foldmethod=manual"); |
| 38 | + await denops.call("setline", 1, content.split(/\r?\n/)); |
| 39 | + await denops.cmd("setlocal nomodifiable"); |
| 40 | + await denops.cmd("setlocal nomodified"); |
| 41 | + }); |
| 42 | +} |
| 43 | + |
| 44 | +export async function main(denops: Denops): Promise<void> { |
| 45 | + await batch(denops, async (denops) => { |
| 46 | + await denops.cmd("edit Hello"); |
| 47 | + await replace(denops, "Hello Darkness My Old Friend"); |
| 48 | + }); |
| 49 | +} |
| 50 | +``` |
| 51 | + |
| 52 | +Note that `denops.call()`, `denops.batch()`, or `denops.eval()` always return |
| 53 | +falsy value in `batch()`, indicating that you **cannot** write code like below: |
| 54 | + |
| 55 | +```typescript |
| 56 | +import { Denops } from "https://deno.land/x/denops_std/mod.ts"; |
| 57 | +import { batch } from "https://deno.land/x/denops_std/batch/mod.ts"; |
| 58 | + |
| 59 | +export async function main(denops: Denops): Promise<void> { |
| 60 | + await batch(denops, async (denops) => { |
| 61 | + // !!! DON'T DO THIS !!! |
| 62 | + if (await denops.eval("&verbose")) { |
| 63 | + await denops.cmd("echomsg 'VERBOSE'"); |
| 64 | + } |
| 65 | + await denops.cmd("echomsg 'Hello world'"); |
| 66 | + }); |
| 67 | +} |
| 68 | +``` |
| 69 | + |
| 70 | +### gather |
| 71 | + |
| 72 | +Use `gather()` to call multiple denops functions sequentially without overhead |
| 73 | +and return values like: |
| 74 | + |
| 75 | +```typescript |
| 76 | +import { Denops } from "https://deno.land/x/denops_std/mod.ts"; |
| 77 | +import { gather } from "https://deno.land/x/denops_std/batch/mod.ts"; |
| 78 | + |
| 79 | +export async function main(denops: Denops): Promise<void> { |
| 80 | + const results = await gather(denops, async (denops) => { |
| 81 | + await denops.eval("&modifiable"); |
| 82 | + await denops.eval("&modified"); |
| 83 | + await denops.eval("&filetype"); |
| 84 | + }); |
| 85 | + // results contains the value of modifiable, modified, and filetype |
| 86 | +} |
| 87 | +``` |
| 88 | + |
| 89 | +Not like `batch`, the function can NOT be nested. |
| 90 | + |
| 91 | +Note that `denops.call()`, `denops.batch()`, or `denops.eval()` always return |
| 92 | +falsy value in `gather()`, indicating that you **cannot** write code like below: |
| 93 | + |
| 94 | +```typescript |
| 95 | +import { Denops } from "https://deno.land/x/denops_std/mod.ts"; |
| 96 | +import { batch } from "https://deno.land/x/denops_std/batch/mod.ts"; |
| 97 | + |
| 98 | +export async function main(denops: Denops): Promise<void> { |
| 99 | + const results = await gather(denops, async (denops) => { |
| 100 | + // !!! DON'T DO THIS !!! |
| 101 | + if (await denops.call("has", "nvim")) { |
| 102 | + await denops.call("api_info").version; |
| 103 | + } else { |
| 104 | + await denops.eval("v:version"); |
| 105 | + } |
| 106 | + }); |
| 107 | +} |
| 108 | +``` |
0 commit comments