-
Notifications
You must be signed in to change notification settings - Fork 52
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
37 additions
and
63 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,8 @@ | ||
import { MaybeAsync } from "@polywrap/core-js"; | ||
import { Command as Program, Argument } from "commander"; | ||
|
||
export { Program, Argument }; | ||
|
||
export interface Command { | ||
setup: (program: Program) => void; | ||
setup: (program: Program) => MaybeAsync<void>; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,40 +1,45 @@ | ||
import { | ||
MaybeAsync, | ||
executeMaybeAsyncFunction, | ||
isPromise, | ||
} from ".."; | ||
import { MaybeAsync, isPromise } from ".."; | ||
|
||
class ClassInstance { | ||
interface IClassInterface { | ||
normalMethod(arg: string): MaybeAsync<string>; | ||
asyncMethod(arg: string): MaybeAsync<string>; | ||
} | ||
|
||
class ClassInstance implements IClassInterface { | ||
constructor(private _prop: string) {} | ||
|
||
normalMethod(arg: string): string { | ||
return this._prop + arg; | ||
} | ||
|
||
async asyncMethod(arg: string): Promise<string> { | ||
await new Promise((resolve) => | ||
setTimeout(resolve, 200) | ||
); | ||
await new Promise((resolve) => setTimeout(resolve, 200)); | ||
|
||
return this._prop + arg; | ||
} | ||
} | ||
|
||
describe("MaybeAsync", () => { | ||
const promise: MaybeAsync<string> = | ||
new Promise<string>((resolve, reject) => { return "" }); | ||
const testFunction = (args: unknown[]) => { return "foo" }; | ||
const testFunctionReturnPromise = (args: unknown[]) => new Promise<string>((resolve) => { resolve("foo") }); | ||
const promise: MaybeAsync<string> = new Promise<string>((resolve, reject) => { | ||
return ""; | ||
}); | ||
const testFunction = (): MaybeAsync<string> => { | ||
return "foo"; | ||
}; | ||
const testFunctionReturnPromise = (): MaybeAsync<string> => | ||
new Promise<string>((resolve) => { | ||
resolve("foo"); | ||
}); | ||
|
||
it("sanity", async () => { | ||
expect(isPromise(promise)).toBe(true); | ||
expect(await executeMaybeAsyncFunction(testFunction)).toBe("foo"); | ||
expect(await executeMaybeAsyncFunction(testFunctionReturnPromise)).toBe("foo"); | ||
expect(await testFunction()).toBe("foo"); | ||
expect(await testFunctionReturnPromise()).toBe("foo"); | ||
}); | ||
|
||
it("works with class instances", async () => { | ||
const instance = new ClassInstance("bar"); | ||
expect(await executeMaybeAsyncFunction(instance.normalMethod.bind(instance, "foo"))).toBe("barfoo") | ||
expect(await executeMaybeAsyncFunction(instance.asyncMethod.bind(instance, "foo"))).toBe("barfoo") | ||
}) | ||
const instance: IClassInterface = new ClassInstance("bar"); | ||
expect(await instance.normalMethod("foo")).toBe("barfoo"); | ||
expect(await instance.asyncMethod("foo")).toBe("barfoo"); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,18 +1 @@ | ||
export type MaybeAsync<T> = Promise<T> | T; | ||
|
||
export const isPromise = <T extends unknown>( | ||
test?: MaybeAsync<T> | ||
): test is Promise<T> => | ||
!!test && typeof (test as Promise<T>).then === "function"; | ||
|
||
// eslint-disable-next-line @typescript-eslint/no-explicit-any, @typescript-eslint/explicit-module-boundary-types | ||
export const executeMaybeAsyncFunction = async <T extends unknown>( | ||
func: (...args: unknown[]) => Promise<T> | T, | ||
...args: unknown[] | ||
): Promise<T> => { | ||
let result = func(...args); | ||
if (isPromise(result)) { | ||
result = await result; | ||
} | ||
return result; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters