Skip to content

Commit fea2b9e

Browse files
committed
👍 Add gather function on batch module
1 parent bd00963 commit fea2b9e

File tree

2 files changed

+127
-0
lines changed

2 files changed

+127
-0
lines changed

denops_std/batch/gather.ts

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
import { Context, Denops, Dispatcher, Meta } from "../deps.ts";
2+
3+
class GatherHelper implements Denops {
4+
#denops: Denops;
5+
#calls: [string, ...unknown[]][];
6+
7+
constructor(denops: Denops) {
8+
this.#denops = denops;
9+
this.#calls = [];
10+
}
11+
12+
static getCalls(helper: GatherHelper): [string, ...unknown[]][] {
13+
return helper.#calls;
14+
}
15+
16+
get name(): string {
17+
return this.#denops.name;
18+
}
19+
20+
get meta(): Meta {
21+
return this.#denops.meta;
22+
}
23+
24+
get dispatcher(): Dispatcher {
25+
return this.#denops.dispatcher;
26+
}
27+
28+
set dispatcher(dispatcher: Dispatcher) {
29+
this.#denops.dispatcher = dispatcher;
30+
}
31+
32+
call(fn: string, ...args: unknown[]): Promise<unknown> {
33+
this.#calls.push([fn, ...args]);
34+
return Promise.resolve();
35+
}
36+
37+
batch(..._calls: [string, ...unknown[]][]): Promise<unknown[]> {
38+
throw new Error("The 'batch' method is not available on GatherHelper.");
39+
}
40+
41+
cmd(cmd: string, ctx: Context = {}): Promise<void> {
42+
this.call("denops#api#cmd", cmd, ctx);
43+
return Promise.resolve();
44+
}
45+
46+
eval(expr: string, ctx: Context = {}): Promise<unknown> {
47+
this.call("denops#api#eval", expr, ctx);
48+
return Promise.resolve();
49+
}
50+
51+
dispatch(name: string, fn: string, ...args: unknown[]): Promise<unknown> {
52+
return this.#denops.dispatch(name, fn, ...args);
53+
}
54+
}
55+
56+
/**
57+
* Perform gather call
58+
*/
59+
export async function gather(
60+
denops: Denops,
61+
main: (helper: GatherHelper) => Promise<void>,
62+
): Promise<unknown[]> {
63+
const helper = new GatherHelper(denops);
64+
await main(helper);
65+
const calls = GatherHelper.getCalls(helper);
66+
return await denops.batch(...calls);
67+
}
68+
69+
export type { GatherHelper };

denops_std/batch/gather_test.ts

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
import { assertEquals, assertThrowsAsync, test } from "../deps_test.ts";
2+
import { gather } from "./gather.ts";
3+
4+
test({
5+
mode: "any",
6+
name: "gather() sequentially execute 'denops.call()'.",
7+
fn: async (denops) => {
8+
const results = await gather(denops, async (denops) => {
9+
await denops.call("range", 0);
10+
await denops.call("range", 1);
11+
await denops.call("range", 2);
12+
});
13+
assertEquals(results, [[], [0], [0, 1]]);
14+
},
15+
});
16+
test({
17+
mode: "any",
18+
name: "gather() throws an error when 'denops.cmd()' is called.",
19+
fn: async (denops) => {
20+
await denops.cmd("let g:denops_gather_test = 0");
21+
await denops.cmd("command! DenopsGatherTest let g:denops_gather_test += 1");
22+
const results = await gather(denops, async (denops) => {
23+
await denops.cmd("DenopsGatherTest");
24+
await denops.cmd("DenopsGatherTest");
25+
await denops.cmd("DenopsGatherTest");
26+
});
27+
assertEquals(results, [0, 0, 0]);
28+
assertEquals(await denops.eval("g:denops_gather_test") as number, 3);
29+
},
30+
});
31+
test({
32+
mode: "any",
33+
name: "gather() sequentially execute 'denops.eval()'.",
34+
fn: async (denops) => {
35+
await denops.cmd("let g:denops_gather_test = 10");
36+
const results = await gather(denops, async (denops) => {
37+
await denops.eval("g:denops_gather_test + 1");
38+
await denops.eval("g:denops_gather_test - 1");
39+
await denops.eval("g:denops_gather_test * 10");
40+
});
41+
assertEquals(results, [11, 9, 100]);
42+
},
43+
});
44+
test({
45+
mode: "any",
46+
name: "gather() throws an error when 'denops.batch()' is called.",
47+
fn: async (denops) => {
48+
await assertThrowsAsync(
49+
async () => {
50+
await gather(denops, async (denops) => {
51+
await denops.batch();
52+
});
53+
},
54+
undefined,
55+
"method is not available",
56+
);
57+
},
58+
});

0 commit comments

Comments
 (0)