1
1
/**
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.
3
3
*
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 .
6
6
*
7
7
* ```typescript
8
8
* import type { Entrypoint } from "jsr:@denops/std";
9
9
* import * as lambda from "jsr:@denops/std/lambda";
10
10
*
11
11
* export const main: Entrypoint = async (denops) => {
12
- * // Add lambda function
12
+ * // Add a lambda function
13
13
* const lo = lambda.add(
14
14
* denops,
15
15
* () => {
16
16
* // Do what ever you want.
17
17
* },
18
18
* );
19
19
*
20
- * // Use id to dispatch added function from Deno
20
+ * // Dispatch the function from Deno using its id
21
21
* await denops.dispatch(denops.name, lo.id);
22
22
*
23
- * // Or from Vim
23
+ * // Or call it from Vim
24
24
* await denops.cmd(`call ${lo.notify()}`);
25
25
*
26
26
* // Dispose the lambda object
@@ -41,58 +41,58 @@ import { stringify } from "../eval/stringify.ts";
41
41
import { expr , type Expression } from "../eval/expression.ts" ;
42
42
43
43
/**
44
- * Lambda function identifier
44
+ * A unique identifier for a registered lambda function.
45
45
*/
46
46
export type Identifier = string ;
47
47
48
48
/**
49
- * Lambda function
49
+ * The type signature for a lambda function.
50
50
*/
51
51
export type Fn = ( ...args : unknown [ ] ) => unknown ;
52
52
53
53
/**
54
- * Register options
54
+ * Options for registering a lambda function.
55
55
*/
56
56
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 */
58
58
once ?: boolean ;
59
59
}
60
60
61
61
/**
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
63
63
*
64
64
* ```typescript
65
65
* import type { Entrypoint } from "jsr:@denops/std";
66
66
* import * as lambda from "jsr:@denops/std/lambda";
67
67
*
68
68
* export const main: Entrypoint = async (denops) => {
69
- * // Add lambda function
69
+ * // Add a lambda function
70
70
* const id = lambda.register(
71
71
* denops,
72
72
* () => {
73
73
* // Do what ever you want.
74
74
* },
75
75
* );
76
76
*
77
- * // Use id to dispatch added function from Deno
77
+ * // Dispatch the function from Deno using its id
78
78
* await denops.dispatch(denops.name, id);
79
79
*
80
- * // Or from Vim
80
+ * // Or call it from Vim
81
81
* await denops.cmd("call denops#notify(name, id, [])", {
82
82
* name: denops.name,
83
83
* id,
84
84
* });
85
85
* }
86
86
* ```
87
87
*
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:
89
89
*
90
90
* ```typescript
91
91
* import type { Entrypoint } from "jsr:@denops/std";
92
92
* import * as lambda from "jsr:@denops/std/lambda";
93
93
*
94
94
* export const main: Entrypoint = async (denops) => {
95
- * // Add lambda function
95
+ * // Add a lambda function
96
96
* const id = lambda.register(
97
97
* denops,
98
98
* () => {
@@ -103,7 +103,7 @@ export interface Options {
103
103
* },
104
104
* );
105
105
*
106
- * // Use id to dispatch added function from Deno
106
+ * // Dispatch the function from Deno using its id
107
107
* await denops.dispatch(denops.name, id);
108
108
*
109
109
* // Second call would throw error
@@ -135,9 +135,9 @@ export function register(
135
135
}
136
136
137
137
/**
138
- * Unregister a lambda function from a denops API identified by the identifier
138
+ * Unregisters a lambda function registered via { @linkcode register} function using its identifier
139
139
*
140
- * It returns `true` if the lambda function is unregistered. Otherwise it returns `false`.
140
+ * Returns `true` if successfully unregistered, otherwise `false`.
141
141
*/
142
142
export function unregister (
143
143
denops : Denops ,
@@ -150,11 +150,19 @@ export function unregister(
150
150
return false ;
151
151
}
152
152
153
+ /**
154
+ * An object representing a registered lambda function in the denops API
155
+ *
156
+ * Instances of this interface are returned by {@linkcode add} function.
157
+ */
153
158
export interface Lambda extends Disposable {
159
+ /**
160
+ * The identifier of the registered lambda function
161
+ */
154
162
readonly id : Identifier ;
155
163
156
164
/**
157
- * Create a Vim script expression to notify the lambda function
165
+ * Generates a Vim script expression to notify the lambda function
158
166
*
159
167
* ```typescript
160
168
* import type { Entrypoint } from "jsr:@denops/std";
@@ -171,7 +179,7 @@ export interface Lambda extends Disposable {
171
179
notify ( ...args : unknown [ ] ) : Expression ;
172
180
173
181
/**
174
- * Create a Vim script expression to request the lambda function
182
+ * Generates a Vim script expression to request the lambda function
175
183
*
176
184
* ```typescript
177
185
* import type { Entrypoint } from "jsr:@denops/std";
@@ -188,7 +196,7 @@ export interface Lambda extends Disposable {
188
196
request ( ...args : unknown [ ] ) : Expression ;
189
197
190
198
/**
191
- * Dispose the lambda function
199
+ * Disposes the lambda function
192
200
*
193
201
* ```typescript
194
202
* import type { Entrypoint } from "jsr:@denops/std";
@@ -198,23 +206,28 @@ export interface Lambda extends Disposable {
198
206
* const a = lambda.add(denops, () => {
199
207
* // Do what ever you want.
200
208
* });
201
- * // Dispose the lambda function manually
202
- * a.dispose();
209
+ * // Dispose the lambda function manually
210
+ * a.dispose();
203
211
* }
204
212
* ```
205
213
*/
206
214
dispose ( ) : void ;
207
215
}
208
216
209
217
/**
210
- * Add a lambda function to a denops API and return the lambda object
218
+ * Adds a lambda function as a denops API and returns a {@linkcode Lambda} object
219
+ *
220
+ * The returned {@linkcode Lambda} object provides methods to generate Vim script expressions for
221
+ * invoking the registered lambda function, either as a {@linkcode Lambda.notify|notify} or as
222
+ * a {@linkcode Lambda.request|request}. It also provides a method to {@linkcode Lambda.dispose|dispose}
223
+ * the lambda function.
211
224
*
212
225
* ```typescript
213
226
* import type { Entrypoint } from "jsr:@denops/std";
214
227
* import * as lambda from "jsr:@denops/std/lambda";
215
228
*
216
229
* export const main: Entrypoint = async (denops) => {
217
- * // Add lambda function
230
+ * // Add a lambda function
218
231
* const lo = lambda.add(
219
232
* denops,
220
233
* () => {
@@ -230,9 +243,12 @@ export interface Lambda extends Disposable {
230
243
* }
231
244
* ```
232
245
*
233
- * You can pass JSON serializable values, {@linkcode Expression} or
234
- * {@linkcode [eval].RawString|RawString} for {@linkcode [lambda].notify|notify}
235
- * or {@linkcode [lambda].request|request} arguments.
246
+ * The {@linkcode Lambda.notify|notify} and {@linkcode Lambda.request|request} methods accept JSON
247
+ * serializable values, {@linkcode [eval].Expression|Expression} or {@linkcode [eval].RawString|RawString}
248
+ * as arguments. The registered lambda function will be invoked with the arguments.
249
+ *
250
+ * The lambda function itself can return any of the serializable values as described above.
251
+ * The return value will be sent back to Vim when using {@linkcode Lambda.request|request}.
236
252
*
237
253
* ```typescript
238
254
* import type { Denops } from "jsr:@denops/std";
0 commit comments