-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Updated main function to handle generics
- Loading branch information
Showing
4 changed files
with
29 additions
and
14 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
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,9 +1,24 @@ | ||
import { InputProps, readInput, writeOutput } from './stdin' | ||
import { writeOutput } from './stdin' | ||
|
||
export async function entryMain<T>( | ||
cb: (input: InputProps<T>) => Promise<object> | ||
): Promise<void> { | ||
const input = readInput<T>() | ||
const result = await cb(input) | ||
type EntryCallback<T extends object> = () => T | ||
type EntryCallbackAsync<T extends object> = () => Promise<T> | ||
|
||
export function main<T extends object>(cb: EntryCallback<T>): T | ||
export async function main<T extends object>(cb: EntryCallbackAsync<T>): Promise<T> | ||
export async function main<T extends object>(cb: EntryCallback<T> | EntryCallbackAsync<T>): Promise<T> { | ||
if (isPromiseCallback(cb)) { | ||
const result = await cb() | ||
writeOutput(result) | ||
|
||
return result | ||
} | ||
|
||
const result = cb() | ||
writeOutput(result) | ||
|
||
return result | ||
} | ||
|
||
function isPromiseCallback<T extends object>(cb: EntryCallback<T> | EntryCallbackAsync<T>): cb is EntryCallbackAsync<T> { | ||
return typeof cb === 'function' && cb.length === 0; | ||
} |
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