-
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.
- Loading branch information
Showing
6 changed files
with
108 additions
and
74 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
import { getSpinner } from "./util.ts"; | ||
|
||
/** Creates a logging spinner using Ora for progress on a function */ | ||
export function SpinnerLog( | ||
messages: { start: string; success: string; fail: string }, | ||
) { | ||
// deno-lint-ignore no-explicit-any | ||
return function (fn: any, _ctx: ClassMethodDecoratorContext) { | ||
return function (...args: unknown[]) { | ||
const spinner = getSpinner().start(messages.start); | ||
try { | ||
// @ts-ignore need to apply this function call but unable to type "this" | ||
const v = fn.apply(this, ...args); | ||
|
||
if (v instanceof Promise) { | ||
return v.then((r) => { | ||
spinner.succeed(messages.success); | ||
return r; | ||
}); | ||
} | ||
spinner.succeed(messages.success); | ||
return v; | ||
} catch (e) { | ||
spinner.fail(messages.fail); | ||
throw e; | ||
} | ||
}; | ||
}; | ||
} | ||
|
||
export function Memoize() { | ||
const cache = new Map<string, unknown>(); | ||
|
||
// deno-lint-ignore no-explicit-any | ||
return function (fn: any, _ctx: ClassMethodDecoratorContext) { | ||
return function (...args: unknown[]) { | ||
const key = JSON.stringify(args); | ||
|
||
if (cache.has(key)) { | ||
return cache.get(key); | ||
} | ||
|
||
// @ts-ignore need to apply this function call but unable to type "this" | ||
const result = fn.apply(this, args); | ||
|
||
// If the method is async, wait for the promise to resolve | ||
if (result instanceof Promise) { | ||
return result.then((resolvedResult) => { | ||
cache.set(key, resolvedResult); | ||
return resolvedResult; | ||
}); | ||
} | ||
|
||
cache.set(key, 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
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