|
| 1 | +# anonymous |
| 2 | + |
| 3 | +`anonymous` is a module to provide anonymous function which is callable from |
| 4 | +outside of the plugin (Vim or other plugins.) |
| 5 | + |
| 6 | +- [API documentation](https://doc.deno.land/https/deno.land/x/denops_std/anonymous/mod.ts) |
| 7 | + |
| 8 | +## Usage |
| 9 | + |
| 10 | +### add |
| 11 | + |
| 12 | +Use `add()` to add new anonymous functions and use return value as unique IDs to |
| 13 | +call added functions later like: |
| 14 | + |
| 15 | +```typescript |
| 16 | +import { Denops } from "https://deno.land/x/denops_std/mod.ts"; |
| 17 | +import * as anonymous from "https://deno.land/x/denops_std/anonymous/mod.ts"; |
| 18 | + |
| 19 | +export async function main(denops: Denops): Promise<void> { |
| 20 | + // Add anonymous functions |
| 21 | + const ids = anonymous.add( |
| 22 | + denops, |
| 23 | + () => { |
| 24 | + // Do what ever you want. |
| 25 | + }, |
| 26 | + () => { |
| 27 | + // Do what ever you want. |
| 28 | + }, |
| 29 | + // You can add as many callbacks as you want... |
| 30 | + ); |
| 31 | + |
| 32 | + // Use ids to dispatch added functions from Deno |
| 33 | + await denops.dispatch(denops.name, ids[0]); |
| 34 | + await denops.dispatch(denops.name, ids[1]); |
| 35 | + |
| 36 | + // Or from Vim |
| 37 | + await denops.cmd("call denops#notify(name, id, [])", { |
| 38 | + name: denops.name, |
| 39 | + id: ids[1], |
| 40 | + }); |
| 41 | +} |
| 42 | +``` |
| 43 | + |
| 44 | +### once |
| 45 | + |
| 46 | +If you need one-time anonymous function, use `once()` instead like: |
| 47 | + |
| 48 | +```typescript |
| 49 | +import { Denops } from "https://deno.land/x/denops_std/mod.ts"; |
| 50 | +import * as anonymous from "https://deno.land/x/denops_std/anonymous/mod.ts"; |
| 51 | + |
| 52 | +export async function main(denops: Denops): Promise<void> { |
| 53 | + // Add anonymous functions |
| 54 | + const ids = anonymous.once( |
| 55 | + denops, |
| 56 | + () => { |
| 57 | + // Do what ever you want. |
| 58 | + }, |
| 59 | + () => { |
| 60 | + // Do what ever you want. |
| 61 | + }, |
| 62 | + // You can add as many callbacks as you want... |
| 63 | + ); |
| 64 | + |
| 65 | + // First calls complete normally |
| 66 | + await denops.dispatch(denops.name, ids[0]); |
| 67 | + await denops.dispatch(denops.name, ids[1]); |
| 68 | + |
| 69 | + // But second call would throw errors |
| 70 | + await denops.dispatch(denops.name, ids[0]); |
| 71 | +} |
| 72 | +``` |
| 73 | + |
| 74 | +### remove |
| 75 | + |
| 76 | +When you need to remove callback, use `remove()` like: |
| 77 | + |
| 78 | +```typescript |
| 79 | +import { Denops } from "https://deno.land/x/denops_std/mod.ts"; |
| 80 | +import * as anonymous from "https://deno.land/x/denops_std/anonymous/mod.ts"; |
| 81 | + |
| 82 | +export async function main(denops: Denops): Promise<void> { |
| 83 | + // Add anonymous functions |
| 84 | + const ids = anonymous.add( |
| 85 | + denops, |
| 86 | + () => { |
| 87 | + // Do what ever you want. |
| 88 | + }, |
| 89 | + () => { |
| 90 | + // Do what ever you want. |
| 91 | + }, |
| 92 | + // You can add as many callbacks as you want... |
| 93 | + ); |
| 94 | + |
| 95 | + // Remove ids[1] |
| 96 | + anonymous.remove(denops, ids[1]); |
| 97 | + |
| 98 | + // Call of ids[0] complete normally |
| 99 | + await denops.dispatch(denops.name, ids[0]); |
| 100 | + |
| 101 | + // But ids[1] is already removed |
| 102 | + await denops.dispatch(denops.name, ids[1]); |
| 103 | +} |
| 104 | +``` |
0 commit comments