1+ /**
2+ * A module to provide anonymous function which is callable from
3+ * outside of the plugin (Vim or other plugins.)
4+ *
5+ * @module
6+ */
17import type { Denops } from "https://deno.land/x/[email protected] /mod.ts" ; 28
39// https://github.com/microsoft/TypeScript/issues/26223#issuecomment-674500430
@@ -19,7 +25,36 @@ export type Identifier = string;
1925export type Callback = ( ...args : unknown [ ] ) => Promise < unknown > | unknown ;
2026
2127/**
22- * Add anonymous functions as a denops API and return the identifiers
28+ * Add anonymous functions as denops API and return the identifiers
29+ *
30+ * ```typescript
31+ * import { Denops } from "../mod.ts";
32+ * import * as anonymous from "./mod.ts";
33+ *
34+ * export async function main(denops: Denops): Promise<void> {
35+ * // Add anonymous functions
36+ * const ids = anonymous.add(
37+ * denops,
38+ * () => {
39+ * // Do what ever you want.
40+ * },
41+ * () => {
42+ * // Do what ever you want.
43+ * },
44+ * // You can add as many callbacks as you want...
45+ * );
46+ *
47+ * // Use ids to dispatch added functions from Deno
48+ * await denops.dispatch(denops.name, ids[0]);
49+ * await denops.dispatch(denops.name, ids[1]);
50+ *
51+ * // Or from Vim
52+ * await denops.cmd("call denops#notify(name, id, [])", {
53+ * name: denops.name,
54+ * id: ids[1],
55+ * });
56+ * }
57+ * ```
2358 */
2459export function add < N extends number > (
2560 denops : Denops ,
@@ -36,7 +71,33 @@ export function add<N extends number>(
3671}
3772
3873/**
39- * Add oneshot anonymous functions as a denops API and return the identifiers
74+ * Add oneshot anonymous functions as denops API and return the identifiers
75+ *
76+ * ```typescript
77+ * import { Denops } from "../mod.ts";
78+ * import * as anonymous from "./mod.ts";
79+ *
80+ * export async function main(denops: Denops): Promise<void> {
81+ * // Add anonymous functions
82+ * const ids = anonymous.once(
83+ * denops,
84+ * () => {
85+ * // Do what ever you want.
86+ * },
87+ * () => {
88+ * // Do what ever you want.
89+ * },
90+ * // You can add as many callbacks as you want...
91+ * );
92+ *
93+ * // First calls complete normally
94+ * await denops.dispatch(denops.name, ids[0]);
95+ * await denops.dispatch(denops.name, ids[1]);
96+ *
97+ * // But second call would throw errors
98+ * await denops.dispatch(denops.name, ids[0]);
99+ * }
100+ * ```
40101 */
41102export function once < N extends number > (
42103 denops : Denops ,
@@ -57,7 +118,35 @@ export function once<N extends number>(
57118}
58119
59120/**
60- * Remove anonymous functions identified by names from a denops API
121+ * Remove anonymous functions from denops API identified by the identifiers
122+ *
123+ * ```typescript
124+ * import { Denops } from "../mod.ts";
125+ * import * as anonymous from "./mod.ts";
126+ *
127+ * export async function main(denops: Denops): Promise<void> {
128+ * // Add anonymous functions
129+ * const ids = anonymous.add(
130+ * denops,
131+ * () => {
132+ * // Do what ever you want.
133+ * },
134+ * () => {
135+ * // Do what ever you want.
136+ * },
137+ * // You can add as many callbacks as you want...
138+ * );
139+ *
140+ * // Remove ids[1]
141+ * anonymous.remove(denops, ids[1]);
142+ *
143+ * // Call of ids[0] complete normally
144+ * await denops.dispatch(denops.name, ids[0]);
145+ *
146+ * // But ids[1] is already removed
147+ * await denops.dispatch(denops.name, ids[1]);
148+ * }
149+ * ```
61150 */
62151export function remove < N extends number > (
63152 denops : Denops ,
0 commit comments