Skip to content

Commit 1b5bb2d

Browse files
committed
Clarify the dispatch and dispatcher types with examples
1 parent d5fbafe commit 1b5bb2d

File tree

1 file changed

+57
-4
lines changed

1 file changed

+57
-4
lines changed

type.ts

Lines changed: 57 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,13 @@
77
*/
88

99
/**
10-
* API dispatcher
10+
* API dispatcher that maps method names to functions.
11+
*
12+
* Each method must accept `unknown[]` arguments and return `unknown`.
13+
* Use type validation libraries like [jsr:@core/unknownutil] to properly
14+
* validate and narrow argument types at runtime.
15+
*
16+
* [jsr:@core/unknownutil]: https://jsr.io/@core/unknownutil
1117
*/
1218
export type Dispatcher = {
1319
[key: string]: (...args: unknown[]) => unknown;
@@ -70,6 +76,34 @@ export interface Denops {
7076

7177
/**
7278
* User-defined API name and method map used to dispatch API requests.
79+
*
80+
* Each method must accept `unknown[]` arguments and return `unknown` values.
81+
* Use type validation libraries like [jsr:@core/unknownutil] to validate
82+
* argument types at runtime. These methods can be called from other plugins
83+
* using `denops.dispatch()`.
84+
*
85+
* [jsr:@core/unknownutil]: https://jsr.io/@core/unknownutil
86+
*
87+
* @example
88+
* ```ts
89+
* import { is, assert } from "jsr:@core/unknownutil";
90+
*
91+
* denops.dispatcher = {
92+
* "hello": (name: unknown) => {
93+
* assert(name, is.String);
94+
* return `Hello, ${name}!`;
95+
* },
96+
* "add": (a: unknown, b: unknown) => {
97+
* assert(a, is.Number);
98+
* assert(b, is.Number);
99+
* return a + b;
100+
* },
101+
* "fetchData": async (id: unknown) => {
102+
* assert(id, is.String);
103+
* return await fetch(`/api/data/${id}`);
104+
* },
105+
* };
106+
* ```
73107
*/
74108
dispatcher: Dispatcher;
75109

@@ -126,9 +160,28 @@ export interface Denops {
126160
/**
127161
* Dispatch an arbitrary function of an arbitrary plugin and return the result.
128162
*
129-
* @param name: A plugin registration name.
130-
* @param fn: A function name in the API registration.
131-
* @param args: Arguments of the function.
163+
* @param name A plugin registration name.
164+
* @param fn A function name in the dispatcher of the target plugin.
165+
* @param args Arguments to pass to the function. Arguments are passed as `unknown` types.
166+
* @returns A Promise that resolves to the return value of the dispatched function.
167+
* The return type is `unknown` and should be validated if needed.
168+
*
169+
* @example
170+
* ```ts
171+
* import { is } from "jsr:@core/unknownutil";
172+
*
173+
* // Call method and validate return type
174+
* const result = await denops.dispatch("myPlugin", "hello", "world");
175+
* if (is.String(result)) {
176+
* console.log(`Greeting: ${result}`);
177+
* }
178+
*
179+
* // Call method with multiple arguments
180+
* const sum = await denops.dispatch("calculator", "add", 5, 3);
181+
* if (is.Number(sum)) {
182+
* console.log(`Sum: ${sum}`);
183+
* }
184+
* ```
132185
*/
133186
dispatch(name: string, fn: string, ...args: unknown[]): Promise<unknown>;
134187
}

0 commit comments

Comments
 (0)