Skip to content

Commit

Permalink
Merge 4b3f914 into 670af8a
Browse files Browse the repository at this point in the history
  • Loading branch information
spalladino authored Oct 2, 2024
2 parents 670af8a + 4b3f914 commit 6970095
Show file tree
Hide file tree
Showing 42 changed files with 238 additions and 1 deletion.
3 changes: 3 additions & 0 deletions yarn-project/accounts/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,9 @@
"parser": {
"syntax": "typescript",
"decorators": true
},
"transform": {
"decoratorVersion": "2022-03"
}
}
}
Expand Down
3 changes: 3 additions & 0 deletions yarn-project/archiver/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,9 @@
"parser": {
"syntax": "typescript",
"decorators": true
},
"transform": {
"decoratorVersion": "2022-03"
}
}
}
Expand Down
3 changes: 3 additions & 0 deletions yarn-project/aztec-faucet/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,9 @@
"parser": {
"syntax": "typescript",
"decorators": true
},
"transform": {
"decoratorVersion": "2022-03"
}
}
}
Expand Down
3 changes: 3 additions & 0 deletions yarn-project/aztec-node/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,9 @@
"parser": {
"syntax": "typescript",
"decorators": true
},
"transform": {
"decoratorVersion": "2022-03"
}
}
}
Expand Down
3 changes: 3 additions & 0 deletions yarn-project/aztec.js/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,9 @@
"parser": {
"syntax": "typescript",
"decorators": true
},
"transform": {
"decoratorVersion": "2022-03"
}
}
}
Expand Down
3 changes: 3 additions & 0 deletions yarn-project/aztec/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,9 @@
"parser": {
"syntax": "typescript",
"decorators": true
},
"transform": {
"decoratorVersion": "2022-03"
}
}
}
Expand Down
3 changes: 3 additions & 0 deletions yarn-project/bb-prover/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,9 @@
"parser": {
"syntax": "typescript",
"decorators": true
},
"transform": {
"decoratorVersion": "2022-03"
}
}
}
Expand Down
3 changes: 3 additions & 0 deletions yarn-project/bot/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@
"parser": {
"syntax": "typescript",
"decorators": true
},
"transform": {
"decoratorVersion": "2022-03"
}
}
}
Expand Down
3 changes: 3 additions & 0 deletions yarn-project/builder/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,9 @@
"parser": {
"syntax": "typescript",
"decorators": true
},
"transform": {
"decoratorVersion": "2022-03"
}
}
}
Expand Down
3 changes: 3 additions & 0 deletions yarn-project/circuit-types/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,9 @@
"parser": {
"syntax": "typescript",
"decorators": true
},
"transform": {
"decoratorVersion": "2022-03"
}
}
}
Expand Down
3 changes: 3 additions & 0 deletions yarn-project/circuits.js/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,9 @@
"parser": {
"syntax": "typescript",
"decorators": true
},
"transform": {
"decoratorVersion": "2022-03"
}
}
}
Expand Down
3 changes: 3 additions & 0 deletions yarn-project/cli-wallet/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,9 @@
"parser": {
"syntax": "typescript",
"decorators": true
},
"transform": {
"decoratorVersion": "2022-03"
}
}
}
Expand Down
3 changes: 3 additions & 0 deletions yarn-project/cli/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,9 @@
"parser": {
"syntax": "typescript",
"decorators": true
},
"transform": {
"decoratorVersion": "2022-03"
}
}
}
Expand Down
3 changes: 3 additions & 0 deletions yarn-project/end-to-end/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,9 @@
"parser": {
"syntax": "typescript",
"decorators": true
},
"transform": {
"decoratorVersion": "2022-03"
}
}
}
Expand Down
3 changes: 3 additions & 0 deletions yarn-project/entrypoints/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,9 @@
"parser": {
"syntax": "typescript",
"decorators": true
},
"transform": {
"decoratorVersion": "2022-03"
}
}
}
Expand Down
3 changes: 3 additions & 0 deletions yarn-project/ethereum/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,9 @@
"parser": {
"syntax": "typescript",
"decorators": true
},
"transform": {
"decoratorVersion": "2022-03"
}
}
}
Expand Down
6 changes: 5 additions & 1 deletion yarn-project/foundation/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
"./collection": "./dest/collection/index.js",
"./config": "./dest/config/index.js",
"./crypto": "./dest/crypto/index.js",
"./decorators": "./dest/decorators/index.js",
"./error": "./dest/error/index.js",
"./eth-address": "./dest/eth-address/index.js",
"./eth-signature": "./dest/eth-signature/index.js",
Expand Down Expand Up @@ -68,6 +69,9 @@
"parser": {
"syntax": "typescript",
"decorators": true
},
"transform": {
"decoratorVersion": "2022-03"
}
}
}
Expand Down Expand Up @@ -156,4 +160,4 @@
"engines": {
"node": ">=18"
}
}
}
1 change: 1 addition & 0 deletions yarn-project/foundation/src/decorators/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from './memoize.js';
104 changes: 104 additions & 0 deletions yarn-project/foundation/src/decorators/memoize.test.ts
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);
});
});
11 changes: 11 additions & 0 deletions yarn-project/foundation/src/decorators/memoize.ts
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];
};
}
3 changes: 3 additions & 0 deletions yarn-project/ivc-integration/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,9 @@
"parser": {
"syntax": "typescript",
"decorators": true
},
"transform": {
"decoratorVersion": "2022-03"
}
}
}
Expand Down
3 changes: 3 additions & 0 deletions yarn-project/key-store/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,9 @@
"parser": {
"syntax": "typescript",
"decorators": true
},
"transform": {
"decoratorVersion": "2022-03"
}
}
}
Expand Down
3 changes: 3 additions & 0 deletions yarn-project/kv-store/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,9 @@
"parser": {
"syntax": "typescript",
"decorators": true
},
"transform": {
"decoratorVersion": "2022-03"
}
}
}
Expand Down
3 changes: 3 additions & 0 deletions yarn-project/merkle-tree/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,9 @@
"parser": {
"syntax": "typescript",
"decorators": true
},
"transform": {
"decoratorVersion": "2022-03"
}
}
}
Expand Down
3 changes: 3 additions & 0 deletions yarn-project/noir-contracts.js/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,9 @@
"parser": {
"syntax": "typescript",
"decorators": true
},
"transform": {
"decoratorVersion": "2022-03"
}
}
}
Expand Down
3 changes: 3 additions & 0 deletions yarn-project/noir-protocol-circuits-types/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,9 @@
"parser": {
"syntax": "typescript",
"decorators": true
},
"transform": {
"decoratorVersion": "2022-03"
}
}
}
Expand Down
3 changes: 3 additions & 0 deletions yarn-project/p2p-bootstrap/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,9 @@
"parser": {
"syntax": "typescript",
"decorators": true
},
"transform": {
"decoratorVersion": "2022-03"
}
}
}
Expand Down
3 changes: 3 additions & 0 deletions yarn-project/p2p/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,9 @@
"parser": {
"syntax": "typescript",
"decorators": true
},
"transform": {
"decoratorVersion": "2022-03"
}
}
}
Expand Down
3 changes: 3 additions & 0 deletions yarn-project/package.common.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,9 @@
"parser": {
"syntax": "typescript",
"decorators": true
},
"transform": {
"decoratorVersion": "2022-03"
}
}
}
Expand Down
Loading

0 comments on commit 6970095

Please sign in to comment.