-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Migrate
@emotion/weak-memoize
to TypeScript (#2429)
* [weak-memoize] Convert to TypeScript * Add changeset * tweak a bunch of stuff Co-authored-by: Mateusz Burzyński <mateuszburzynski@gmail.com>
- Loading branch information
1 parent
7f8db2d
commit b1d16b0
Showing
8 changed files
with
29 additions
and
27 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,5 @@ | ||
--- | ||
'@emotion/weak-memoize': minor | ||
--- | ||
|
||
Source code has been migrated to TypeScript. From now on type declarations will be emitted based on that, instead of being hand-written. |
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 was deleted.
Oops, something went wrong.
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,17 @@ | ||
let weakMemoize = function <Arg extends object, Return>( | ||
func: (arg: Arg) => Return | ||
): (arg: Arg) => Return { | ||
let cache = new WeakMap<Arg, Return>() | ||
return (arg: Arg) => { | ||
if (cache.has(arg)) { | ||
// Use non-null assertion because we just checked that the cache `has` it | ||
// This allows us to remove `undefined` from the return value | ||
return cache.get(arg)! | ||
} | ||
let ret = func(arg) | ||
cache.set(arg, ret) | ||
return ret | ||
} | ||
} | ||
|
||
export default weakMemoize |
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,3 @@ | ||
// TypeScript Version: 2.2 | ||
|
||
type UnaryFn<Arg, Return> = (arg: Arg) => Return | ||
|
||
export default function weakMemoize<Arg extends object, Return>( | ||
func: UnaryFn<Arg, Return> | ||
): UnaryFn<Arg, Return> | ||
export { default } from '../src' |
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,4 +1,4 @@ | ||
import weakMemoize from '@emotion/weak-memoize' | ||
import weakMemoize from '../src' | ||
|
||
interface Foo { | ||
bar: 'xyz' | ||
|
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