Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions deno.jsonc
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@
]
},
"imports": {
"@core/asyncutil": "jsr:@core/asyncutil@^1.2.0",
"@core/unknownutil": "jsr:@core/unknownutil@^4.3.0",
"@denops/core": "jsr:@denops/core@^7.0.0",
"@denops/test": "jsr:@denops/test@^3.0.1",
Expand Down
7 changes: 2 additions & 5 deletions lambda/mod.ts
Original file line number Diff line number Diff line change
Expand Up @@ -120,11 +120,8 @@ export function register(
const id = `lambda:${denops.name}:${uuid}`;
if (options.once) {
denops.dispatcher[id] = async (...args: unknown[]) => {
try {
return await fn(...args);
} finally {
unregister(denops, id);
}
unregister(denops, id);
return await fn(...args);
};
} else {
denops.dispatcher[id] = async (...args: unknown[]) => {
Expand Down
18 changes: 13 additions & 5 deletions lambda/mod_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import {
returnsNext,
spy,
} from "@std/testing/mock";
import { flushPromises } from "@core/asyncutil";
import { assert, is } from "@core/unknownutil";
import { test } from "@denops/test";
import { expr, rawString } from "../eval/mod.ts";
Expand Down Expand Up @@ -72,16 +73,23 @@ test({
});
await t.step("if 'once' option is specified", async (t) => {
await t.step("registers an oneshot lambda function", async (t) => {
const fn = spy(returnsNext(["foo"]));
const waiter = Promise.withResolvers<void>();
const fn = spy(resolvesNext([waiter.promise.then(() => "foo")]));
const id = lambda.register(denops, fn, { once: true });
assertSpyCalls(fn, 0);
assertEquals(await denops.dispatch(denops.name, id), "foo");

// Call the lambda function twice before the first call resolves
const firstCall = denops.dispatch(denops.name, id);
const secondCall = denops.dispatch(denops.name, id);
secondCall.catch(NOOP);
await flushPromises();
waiter.resolve();

assertEquals(await firstCall, "foo");
assertSpyCalls(fn, 1);

await t.step("which will be removed if called once", async () => {
const error = await assertRejects(
() => denops.dispatch(denops.name, id),
);
const error = await assertRejects(() => secondCall);
assertStringIncludes(
error as string,
"denops.dispatcher[name] is not a function",
Expand Down