Skip to content

Commit b875e8c

Browse files
authored
Merge pull request #283 from vim-denops/lambda-unregister-correctly
🐛 Fix lambda.unregister incorrectly removing non-lambda methods
2 parents 9275154 + 1ea9c44 commit b875e8c

File tree

2 files changed

+35
-3
lines changed

2 files changed

+35
-3
lines changed

lambda/mod.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,7 @@ export function unregister(
140140
denops: Denops,
141141
id: Identifier,
142142
): boolean {
143-
if (id in denops.dispatcher) {
143+
if (isRegisteredFnId(id) && id in denops.dispatcher) {
144144
delete denops.dispatcher[id];
145145
return true;
146146
}
@@ -290,3 +290,7 @@ const isFnWrapperArgs = isTupleOf([
290290
isLiteralOneOf(["notify", "request"] as const),
291291
isArray,
292292
]) satisfies Predicate<FnWrapperArgs>;
293+
294+
function isRegisteredFnId(id: unknown): id is Identifier {
295+
return typeof id === "string" && id.startsWith("lambda:");
296+
}

lambda/mod_test.ts

Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,7 @@ test({
158158
await t.step("does not unregister lambda functions", async () => {
159159
const fn = spy(returnsNext(["foo"]));
160160
const id = lambda.register(denops, fn);
161-
lambda.unregister(denops, "not-registered-id");
161+
lambda.unregister(denops, "lambda:not-registered-id");
162162

163163
assertSpyCalls(fn, 0);
164164
assertEquals(
@@ -169,7 +169,35 @@ test({
169169
assertSpyCalls(fn, 1);
170170
});
171171
await t.step("returns `false`", () => {
172-
assertEquals(lambda.unregister(denops, "not-registered-id"), false);
172+
assertEquals(
173+
lambda.unregister(denops, "lambda:not-registered-id"),
174+
false,
175+
);
176+
});
177+
});
178+
await t.step("if 'id' is not a lambda identifier", async (t) => {
179+
await t.step("does not unregister method", async () => {
180+
const fn = spy(returnsNext(["foo"]));
181+
const notLambdaId = "not-lambda-id";
182+
denops.dispatcher = {
183+
[notLambdaId]: fn,
184+
};
185+
lambda.unregister(denops, notLambdaId);
186+
187+
assertSpyCalls(fn, 0);
188+
assertEquals(
189+
await denops.dispatch(denops.name, notLambdaId),
190+
"foo",
191+
"The method is available",
192+
);
193+
assertSpyCalls(fn, 1);
194+
});
195+
await t.step("returns `false`", () => {
196+
const notLambdaId = "not-lambda-id";
197+
denops.dispatcher = {
198+
[notLambdaId]: () => "foo",
199+
};
200+
assertEquals(lambda.unregister(denops, notLambdaId), false);
173201
});
174202
});
175203
});

0 commit comments

Comments
 (0)