Skip to content

Commit 4a275c5

Browse files
authored
Merge pull request #285 from vim-denops/lambda-doc
📝 Improve documentation for lambda module
2 parents e52df7d + ce6fe13 commit 4a275c5

File tree

1 file changed

+45
-29
lines changed

1 file changed

+45
-29
lines changed

lambda/mod.ts

Lines changed: 45 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,26 @@
11
/**
2-
* A module to provide lambda function that is callable from the outside of the plugin.
2+
* A module to provide lambda functions that is callable from outside of the plugin.
33
*
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.
4+
* If you want to create a Vim script lambda functions callable from Deno,
5+
* use `denops#callback#register()` and `denops#callback#call()` instead.
66
*
77
* ```typescript
88
* import type { Entrypoint } from "jsr:@denops/std";
99
* import * as lambda from "jsr:@denops/std/lambda";
1010
*
1111
* export const main: Entrypoint = async (denops) => {
12-
* // Add lambda function
12+
* // Add a lambda function
1313
* const lo = lambda.add(
1414
* denops,
1515
* () => {
1616
* // Do what ever you want.
1717
* },
1818
* );
1919
*
20-
* // Use id to dispatch added function from Deno
20+
* // Dispatch the function from Deno using its id
2121
* await denops.dispatch(denops.name, lo.id);
2222
*
23-
* // Or from Vim
23+
* // Or call it from Vim
2424
* await denops.cmd(`call ${lo.notify()}`);
2525
*
2626
* // Dispose the lambda object
@@ -41,58 +41,58 @@ import { stringify } from "../eval/stringify.ts";
4141
import { expr, type Expression } from "../eval/expression.ts";
4242

4343
/**
44-
* Lambda function identifier
44+
* A unique identifier for a registered lambda function.
4545
*/
4646
export type Identifier = string;
4747

4848
/**
49-
* Lambda function
49+
* The type signature for a lambda function.
5050
*/
5151
export type Fn = (...args: unknown[]) => unknown;
5252

5353
/**
54-
* Register options
54+
* Options for registering a lambda function.
5555
*/
5656
export interface Options {
57-
/** Register the function as a one-time lambda function that will be removed when the function has called */
57+
/** If true, the lambda function will be unregistered when it is called once */
5858
once?: boolean;
5959
}
6060

6161
/**
62-
* Register a lambda function as a denops API and return the identifier.
62+
* Registers a lambda function as a denops API and returns its identifier
6363
*
6464
* ```typescript
6565
* import type { Entrypoint } from "jsr:@denops/std";
6666
* import * as lambda from "jsr:@denops/std/lambda";
6767
*
6868
* export const main: Entrypoint = async (denops) => {
69-
* // Add lambda function
69+
* // Add a lambda function
7070
* const id = lambda.register(
7171
* denops,
7272
* () => {
7373
* // Do what ever you want.
7474
* },
7575
* );
7676
*
77-
* // Use id to dispatch added function from Deno
77+
* // Dispatch the function from Deno using its id
7878
* await denops.dispatch(denops.name, id);
7979
*
80-
* // Or from Vim
80+
* // Or call it from Vim
8181
* await denops.cmd("call denops#notify(name, id, [])", {
8282
* name: denops.name,
8383
* id,
8484
* });
8585
* }
8686
* ```
8787
*
88-
* If you need an one-time lambda function, use `once` option like
88+
* To register a lambda function that is executed only once, use the {@linkcode Options.once|once} option:
8989
*
9090
* ```typescript
9191
* import type { Entrypoint } from "jsr:@denops/std";
9292
* import * as lambda from "jsr:@denops/std/lambda";
9393
*
9494
* export const main: Entrypoint = async (denops) => {
95-
* // Add lambda function
95+
* // Add a lambda function
9696
* const id = lambda.register(
9797
* denops,
9898
* () => {
@@ -103,7 +103,7 @@ export interface Options {
103103
* },
104104
* );
105105
*
106-
* // Use id to dispatch added function from Deno
106+
* // Dispatch the function from Deno using its id
107107
* await denops.dispatch(denops.name, id);
108108
*
109109
* // Second call would throw error
@@ -132,9 +132,9 @@ export function register(
132132
}
133133

134134
/**
135-
* Unregister a lambda function from a denops API identified by the identifier
135+
* Unregisters a lambda function registered via {@linkcode register} function using its identifier
136136
*
137-
* It returns `true` if the lambda function is unregistered. Otherwise it returns `false`.
137+
* Returns `true` if successfully unregistered, otherwise `false`.
138138
*/
139139
export function unregister(
140140
denops: Denops,
@@ -147,11 +147,19 @@ export function unregister(
147147
return false;
148148
}
149149

150+
/**
151+
* An object representing a registered lambda function in the denops API
152+
*
153+
* Instances of this interface are returned by {@linkcode add} function.
154+
*/
150155
export interface Lambda extends Disposable {
156+
/**
157+
* The identifier of the registered lambda function
158+
*/
151159
readonly id: Identifier;
152160

153161
/**
154-
* Create a Vim script expression to notify the lambda function
162+
* Generates a Vim script expression to notify the lambda function
155163
*
156164
* ```typescript
157165
* import type { Entrypoint } from "jsr:@denops/std";
@@ -168,7 +176,7 @@ export interface Lambda extends Disposable {
168176
notify(...args: unknown[]): Expression;
169177

170178
/**
171-
* Create a Vim script expression to request the lambda function
179+
* Generates a Vim script expression to request the lambda function
172180
*
173181
* ```typescript
174182
* import type { Entrypoint } from "jsr:@denops/std";
@@ -185,7 +193,7 @@ export interface Lambda extends Disposable {
185193
request(...args: unknown[]): Expression;
186194

187195
/**
188-
* Dispose the lambda function
196+
* Disposes the lambda function
189197
*
190198
* ```typescript
191199
* import type { Entrypoint } from "jsr:@denops/std";
@@ -195,23 +203,28 @@ export interface Lambda extends Disposable {
195203
* const a = lambda.add(denops, () => {
196204
* // Do what ever you want.
197205
* });
198-
* // Dispose the lambda function manually
199-
* a.dispose();
206+
* // Dispose the lambda function manually
207+
* a.dispose();
200208
* }
201209
* ```
202210
*/
203211
dispose(): void;
204212
}
205213

206214
/**
207-
* Add a lambda function to a denops API and return the lambda object
215+
* Adds a lambda function as a denops API and returns a {@linkcode Lambda} object
216+
*
217+
* The returned {@linkcode Lambda} object provides methods to generate Vim script expressions for
218+
* invoking the registered lambda function, either as a {@linkcode Lambda.notify|notify} or as
219+
* a {@linkcode Lambda.request|request}. It also provides a method to {@linkcode Lambda.dispose|dispose}
220+
* the lambda function.
208221
*
209222
* ```typescript
210223
* import type { Entrypoint } from "jsr:@denops/std";
211224
* import * as lambda from "jsr:@denops/std/lambda";
212225
*
213226
* export const main: Entrypoint = async (denops) => {
214-
* // Add lambda function
227+
* // Add a lambda function
215228
* const lo = lambda.add(
216229
* denops,
217230
* () => {
@@ -227,9 +240,12 @@ export interface Lambda extends Disposable {
227240
* }
228241
* ```
229242
*
230-
* You can pass JSON serializable values, {@linkcode Expression} or
231-
* {@linkcode [eval].RawString|RawString} for {@linkcode [lambda].notify|notify}
232-
* or {@linkcode [lambda].request|request} arguments.
243+
* The {@linkcode Lambda.notify|notify} and {@linkcode Lambda.request|request} methods accept JSON
244+
* serializable values, {@linkcode [eval].Expression|Expression} or {@linkcode [eval].RawString|RawString}
245+
* as arguments. The registered lambda function will be invoked with the arguments.
246+
*
247+
* The lambda function itself can return any of the serializable values as described above.
248+
* The return value will be sent back to Vim when using {@linkcode Lambda.request|request}.
233249
*
234250
* ```typescript
235251
* import type { Denops } from "jsr:@denops/std";

0 commit comments

Comments
 (0)