-
-
Notifications
You must be signed in to change notification settings - Fork 40
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: lazy loading some modules (#178)
- Loading branch information
1 parent
3731a59
commit 3806c65
Showing
4 changed files
with
54 additions
and
31 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,7 @@ | ||
export default memoize; | ||
/** | ||
* @template T | ||
* @param fn {(function(): any) | undefined} | ||
* @returns {function(): T} | ||
*/ | ||
declare function memoize<T>(fn: (() => any) | undefined): () => T; |
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 |
---|---|---|
@@ -0,0 +1,26 @@ | ||
/** | ||
* @template T | ||
* @param fn {(function(): any) | undefined} | ||
* @returns {function(): T} | ||
*/ | ||
const memoize = (fn) => { | ||
let cache = false; | ||
/** @type {T} */ | ||
let result; | ||
|
||
return () => { | ||
if (cache) { | ||
return result; | ||
} | ||
result = /** @type {function(): any} */ (fn)(); | ||
cache = true; | ||
// Allow to clean up memory for fn | ||
// and all dependent resources | ||
// eslint-disable-next-line no-undefined, no-param-reassign | ||
fn = undefined; | ||
|
||
return result; | ||
}; | ||
}; | ||
|
||
export default memoize; |
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