Skip to content

Commit

Permalink
refactor: calls can be async
Browse files Browse the repository at this point in the history
  • Loading branch information
shah committed Aug 22, 2024
1 parent c54aa69 commit e0721a9
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 13 deletions.
9 changes: 8 additions & 1 deletion lib/reflect/callable.ts
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,14 @@ export function callables<T, R>(instance: T) {
filter,
},
callable: name,
call: (...args: unknown[]) => {
call: async (...args: unknown[]) => {
const method = searched[name] as unknown as (...args: unknown[]) => R;
if (nature === CallablesNature.CLASS) {
return await method.apply(instance, args);
}
return await method(...args);
},
callSync: (...args: unknown[]) => {
const method = searched[name] as unknown as (...args: unknown[]) => R;
if (nature === CallablesNature.CLASS) {
return method.apply(instance, args);
Expand Down
24 changes: 12 additions & 12 deletions lib/reflect/callable_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ Deno.test("callables - should return method names for an object", () => {
ta.assertEquals(result.nature, CallablesNature.OBJECT);
});

Deno.test("callables - should filter methods based on include and exclude criteria", () => {
Deno.test("callables - should filter methods based on include and exclude criteria", async () => {
const instance = new TestClass();
const result = callables(instance);

Expand All @@ -47,29 +47,29 @@ Deno.test("callables - should filter methods based on include and exclude criter

ta.assertEquals(filteredMethods.length, 1);
ta.assertEquals(filteredMethods[0].callable, "foo");
ta.assertEquals(filteredMethods[0].call(), 42);
ta.assertEquals(await filteredMethods[0].call(), 42);

filteredMethods = result.filter({ include: "bar" });

ta.assertEquals(filteredMethods.length, 1);
ta.assertEquals(filteredMethods[0].callable, "bar");
ta.assertEquals(
filteredMethods[0].call("bar"),
filteredMethods[0].callSync("bar"),
"Hello, bar (instanceVarValue)",
);
});

Deno.test("callables - should handle standalone object functions correctly", () => {
Deno.test("callables - should handle standalone object functions correctly", async () => {
const result = callables(testObject);

const filteredMethods = result.filter();

ta.assertEquals(filteredMethods.length, 2);
ta.assertEquals(filteredMethods[0].callable, "greet");
ta.assertEquals(filteredMethods[0].call("Alice"), "Hi, Alice");
ta.assertEquals(await filteredMethods[0].call("Alice"), "Hi, Alice");

ta.assertEquals(filteredMethods[1].callable, "add");
ta.assertEquals(filteredMethods[1].call(2, 3), 5);
ta.assertEquals(filteredMethods[1].callSync(2, 3), 5);
});

Deno.test("callables - should throw an error for invalid instance types", () => {
Expand Down Expand Up @@ -136,7 +136,7 @@ Deno.test("callables - should handle complex object with nested functions", () =

ta.assertEquals(filteredMethods.length, 1);
ta.assertEquals(filteredMethods[0].callable, "describe");
ta.assertEquals(filteredMethods[0].call(), "I am a complex object");
ta.assertEquals(filteredMethods[0].callSync(), "I am a complex object");
});

Deno.test("callables - should ensure `this` context is correctly handled in class methods", () => {
Expand All @@ -155,7 +155,7 @@ Deno.test("callables - should ensure `this` context is correctly handled in clas

ta.assertEquals(filteredMethods.length, 1);
ta.assertEquals(filteredMethods[0].callable, "getName");
ta.assertEquals(filteredMethods[0].call(), "Deno");
ta.assertEquals(filteredMethods[0].callSync(), "Deno");
});

Deno.test("callablesCollection - should return combined callables and allow filtering across multiple instances", () => {
Expand Down Expand Up @@ -207,8 +207,8 @@ Deno.test("callablesCollection - should return combined callables and allow filt
ta.assertEquals(filtered[3].callable, "cell4");

// Assert the callable methods return the expected results
ta.assertEquals(filtered[0].call(), "Notebook 1 - Cell 1");
ta.assertEquals(filtered[1].call(), "Notebook 1 - Cell 2");
ta.assertEquals(filtered[2].call(), "Notebook 2 - Cell 3");
ta.assertEquals(filtered[3].call(), "Notebook 2 - Cell 4");
ta.assertEquals(filtered[0].callSync(), "Notebook 1 - Cell 1");
ta.assertEquals(filtered[1].callSync(), "Notebook 1 - Cell 2");
ta.assertEquals(filtered[2].callSync(), "Notebook 2 - Cell 3");
ta.assertEquals(filtered[3].callSync(), "Notebook 2 - Cell 4");
});

0 comments on commit e0721a9

Please sign in to comment.