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
@@ -132,9 +132,9 @@ export function register(
132
132
}
133
133
134
134
/**
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
136
136
*
137
- * It returns `true` if the lambda function is unregistered. Otherwise it returns `false`.
137
+ * Returns `true` if successfully unregistered, otherwise `false`.
138
138
*/
139
139
export function unregister (
140
140
denops : Denops ,
@@ -147,11 +147,19 @@ export function unregister(
147
147
return false ;
148
148
}
149
149
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
+ */
150
155
export interface Lambda extends Disposable {
156
+ /**
157
+ * The identifier of the registered lambda function
158
+ */
151
159
readonly id : Identifier ;
152
160
153
161
/**
154
- * Create a Vim script expression to notify the lambda function
162
+ * Generates a Vim script expression to notify the lambda function
155
163
*
156
164
* ```typescript
157
165
* import type { Entrypoint } from "jsr:@denops/std";
@@ -168,7 +176,7 @@ export interface Lambda extends Disposable {
168
176
notify ( ...args : unknown [ ] ) : Expression ;
169
177
170
178
/**
171
- * Create a Vim script expression to request the lambda function
179
+ * Generates a Vim script expression to request the lambda function
172
180
*
173
181
* ```typescript
174
182
* import type { Entrypoint } from "jsr:@denops/std";
@@ -185,7 +193,7 @@ export interface Lambda extends Disposable {
185
193
request ( ...args : unknown [ ] ) : Expression ;
186
194
187
195
/**
188
- * Dispose the lambda function
196
+ * Disposes the lambda function
189
197
*
190
198
* ```typescript
191
199
* import type { Entrypoint } from "jsr:@denops/std";
@@ -195,23 +203,28 @@ export interface Lambda extends Disposable {
195
203
* const a = lambda.add(denops, () => {
196
204
* // Do what ever you want.
197
205
* });
198
- * // Dispose the lambda function manually
199
- * a.dispose();
206
+ * // Dispose the lambda function manually
207
+ * a.dispose();
200
208
* }
201
209
* ```
202
210
*/
203
211
dispose ( ) : void ;
204
212
}
205
213
206
214
/**
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.
208
221
*
209
222
* ```typescript
210
223
* import type { Entrypoint } from "jsr:@denops/std";
211
224
* import * as lambda from "jsr:@denops/std/lambda";
212
225
*
213
226
* export const main: Entrypoint = async (denops) => {
214
- * // Add lambda function
227
+ * // Add a lambda function
215
228
* const lo = lambda.add(
216
229
* denops,
217
230
* () => {
@@ -227,9 +240,12 @@ export interface Lambda extends Disposable {
227
240
* }
228
241
* ```
229
242
*
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}.
233
249
*
234
250
* ```typescript
235
251
* import type { Denops } from "jsr:@denops/std";
0 commit comments