|
| 1 | +import { sleep } from '../sleep/index.js'; |
| 2 | +import { memoize } from './memoize.js'; |
| 3 | + |
| 4 | +class Memoized { |
| 5 | + public readonly counters: Record<string, number> = {}; |
| 6 | + |
| 7 | + @memoize |
| 8 | + voidPublic() { |
| 9 | + this.inc('voidPublic'); |
| 10 | + } |
| 11 | + |
| 12 | + @memoize |
| 13 | + synchPublic() { |
| 14 | + this.inc('synchPublic'); |
| 15 | + return this.counters['synchPublic'] + 10; |
| 16 | + } |
| 17 | + |
| 18 | + callSyncPrivate() { |
| 19 | + this.synchPrivate(); |
| 20 | + this.synchPrivate(); |
| 21 | + } |
| 22 | + |
| 23 | + @memoize |
| 24 | + private synchPrivate() { |
| 25 | + this.inc('synchPrivate'); |
| 26 | + return this.counters['synchPrivate'] + 20; |
| 27 | + } |
| 28 | + |
| 29 | + @memoize |
| 30 | + async asyncPublic() { |
| 31 | + this.inc('asyncPublic'); |
| 32 | + await sleep(1); |
| 33 | + return this.counters['asyncPublic'] + 30; |
| 34 | + } |
| 35 | + |
| 36 | + async callAsyncPrivate() { |
| 37 | + await this.asyncPrivate(); |
| 38 | + await this.asyncPrivate(); |
| 39 | + } |
| 40 | + |
| 41 | + @memoize |
| 42 | + private async asyncPrivate() { |
| 43 | + this.inc('asyncPrivate'); |
| 44 | + await sleep(1); |
| 45 | + return this.counters['asyncPrivate'] + 40; |
| 46 | + } |
| 47 | + |
| 48 | + @memoize |
| 49 | + async asyncVoid() { |
| 50 | + this.inc('asyncVoid'); |
| 51 | + await sleep(1); |
| 52 | + } |
| 53 | + |
| 54 | + private inc(name: string) { |
| 55 | + if (!(name in this.counters)) { |
| 56 | + this.counters[name] = 0; |
| 57 | + } |
| 58 | + this.counters[name]++; |
| 59 | + } |
| 60 | +} |
| 61 | + |
| 62 | +describe('memoize', () => { |
| 63 | + let memoized: Memoized; |
| 64 | + |
| 65 | + beforeEach(() => { |
| 66 | + memoized = new Memoized(); |
| 67 | + }); |
| 68 | + |
| 69 | + it('memoizes public void', () => { |
| 70 | + memoized.voidPublic(); |
| 71 | + memoized.voidPublic(); |
| 72 | + expect(memoized.counters['voidPublic']).toBe(1); |
| 73 | + }); |
| 74 | + |
| 75 | + it('memoizes public synchronous', () => { |
| 76 | + expect(memoized.synchPublic()).toBe(11); |
| 77 | + expect(memoized.synchPublic()).toBe(11); |
| 78 | + expect(memoized.counters['synchPublic']).toBe(1); |
| 79 | + }); |
| 80 | + |
| 81 | + it('memoizes private synchronous', () => { |
| 82 | + memoized.callSyncPrivate(); |
| 83 | + memoized.callSyncPrivate(); |
| 84 | + expect(memoized.counters['synchPrivate']).toBe(1); |
| 85 | + }); |
| 86 | + |
| 87 | + it('memoizes public asynchronous', async () => { |
| 88 | + expect(await memoized.asyncPublic()).toBe(31); |
| 89 | + expect(await memoized.asyncPublic()).toBe(31); |
| 90 | + expect(memoized.counters['asyncPublic']).toBe(1); |
| 91 | + }); |
| 92 | + |
| 93 | + it('memoizes private asynchronous', async () => { |
| 94 | + await memoized.callAsyncPrivate(); |
| 95 | + await memoized.callAsyncPrivate(); |
| 96 | + expect(memoized.counters['asyncPrivate']).toBe(1); |
| 97 | + }); |
| 98 | + |
| 99 | + it('memoizes void asynchronous', async () => { |
| 100 | + await memoized.asyncVoid(); |
| 101 | + await memoized.asyncVoid(); |
| 102 | + expect(memoized.counters['asyncVoid']).toBe(1); |
| 103 | + }); |
| 104 | +}); |
0 commit comments