-
Notifications
You must be signed in to change notification settings - Fork 272
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
42 changed files
with
238 additions
and
1 deletion.
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
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export * from './memoize.js'; |
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,104 @@ | ||
import { sleep } from '../sleep/index.js'; | ||
import { memoize } from './memoize.js'; | ||
|
||
class Memoized { | ||
public readonly counters: Record<string, number> = {}; | ||
|
||
@memoize | ||
voidPublic() { | ||
this.inc('voidPublic'); | ||
} | ||
|
||
@memoize | ||
synchPublic() { | ||
this.inc('synchPublic'); | ||
return this.counters['synchPublic'] + 10; | ||
} | ||
|
||
callSyncPrivate() { | ||
this.synchPrivate(); | ||
this.synchPrivate(); | ||
} | ||
|
||
@memoize | ||
private synchPrivate() { | ||
this.inc('synchPrivate'); | ||
return this.counters['synchPrivate'] + 20; | ||
} | ||
|
||
@memoize | ||
async asyncPublic() { | ||
this.inc('asyncPublic'); | ||
await sleep(1); | ||
return this.counters['asyncPublic'] + 30; | ||
} | ||
|
||
async callAsyncPrivate() { | ||
await this.asyncPrivate(); | ||
await this.asyncPrivate(); | ||
} | ||
|
||
@memoize | ||
private async asyncPrivate() { | ||
this.inc('asyncPrivate'); | ||
await sleep(1); | ||
return this.counters['asyncPrivate'] + 40; | ||
} | ||
|
||
@memoize | ||
async asyncVoid() { | ||
this.inc('asyncVoid'); | ||
await sleep(1); | ||
} | ||
|
||
private inc(name: string) { | ||
if (!(name in this.counters)) { | ||
this.counters[name] = 0; | ||
} | ||
this.counters[name]++; | ||
} | ||
} | ||
|
||
describe('memoize', () => { | ||
let memoized: Memoized; | ||
|
||
beforeEach(() => { | ||
memoized = new Memoized(); | ||
}); | ||
|
||
it('memoizes public void', () => { | ||
memoized.voidPublic(); | ||
memoized.voidPublic(); | ||
expect(memoized.counters['voidPublic']).toBe(1); | ||
}); | ||
|
||
it('memoizes public synchronous', () => { | ||
expect(memoized.synchPublic()).toBe(11); | ||
expect(memoized.synchPublic()).toBe(11); | ||
expect(memoized.counters['synchPublic']).toBe(1); | ||
}); | ||
|
||
it('memoizes private synchronous', () => { | ||
memoized.callSyncPrivate(); | ||
memoized.callSyncPrivate(); | ||
expect(memoized.counters['synchPrivate']).toBe(1); | ||
}); | ||
|
||
it('memoizes public asynchronous', async () => { | ||
expect(await memoized.asyncPublic()).toBe(31); | ||
expect(await memoized.asyncPublic()).toBe(31); | ||
expect(memoized.counters['asyncPublic']).toBe(1); | ||
}); | ||
|
||
it('memoizes private asynchronous', async () => { | ||
await memoized.callAsyncPrivate(); | ||
await memoized.callAsyncPrivate(); | ||
expect(memoized.counters['asyncPrivate']).toBe(1); | ||
}); | ||
|
||
it('memoizes void asynchronous', async () => { | ||
await memoized.asyncVoid(); | ||
await memoized.asyncVoid(); | ||
expect(memoized.counters['asyncVoid']).toBe(1); | ||
}); | ||
}); |
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,11 @@ | ||
export function memoize<This extends object, Result>(fn: () => Result, context: ClassMethodDecoratorContext) { | ||
return function (this: This) { | ||
const key = `__${String(context.name)}_value`; | ||
const thisWithKey = this as { [key: string]: Result }; | ||
if (!(key in this)) { | ||
const result = fn.call(this); | ||
thisWithKey[key] = result; | ||
} | ||
return thisWithKey[key]; | ||
}; | ||
} |
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
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
Oops, something went wrong.