Skip to content

Commit

Permalink
feat: Memoise.all()
Browse files Browse the repository at this point in the history
  • Loading branch information
Alorel committed Mar 18, 2019
1 parent 1e65368 commit 02ae821
Show file tree
Hide file tree
Showing 5 changed files with 174 additions and 78 deletions.
16 changes: 16 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ An ES7 decorator for memoising (caching) a method's response
- [Using Instance Methods as Cache Key Generators](#using-instance-methods-as-cache-key-generators)
- [Typescript](#typescript)
- [Babel](#babel)
- [Memoising all method calls disregarding parameters](#memoising-all-method-calls-disregarding-parameters)

<!-- END doctoc generated TOC please keep comment here to allow auto update -->

Expand Down Expand Up @@ -124,3 +125,18 @@ class MyClass {
}
}
```

## Memoising all method calls disregarding parameters

This might be useful for methods that don't accept parameters in the first place.

```javascript
import {Memoise} from '@aloreljs/memoise-decorator';

class MyClass {
@Memoise.all()
method() {
return 'foo';
}
}
```
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@
"@types/chai": "~4.1.7",
"@types/lodash": "^4.14.121",
"@types/mocha": "~5.2.6",
"@types/node": "~11.11.0",
"@types/node": "~11.11.3",
"chai": "~4.2.0",
"concurrently": "~4.1.0",
"coveralls": "~3.0.3",
Expand All @@ -68,4 +68,4 @@
"types": "index.d.ts",
"module": "_bundle/fesm5.js",
"typings": "index.d.ts"
}
}
9 changes: 9 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,16 @@ export function Memoise(serialiser: Memoise.ArgSerialiserFn = stdSerialiser): Me
};
}

function constantFn(): '' {
return '';
}

export module Memoise {
/** Serialiser function for computing cache keys. This accepts the method arguments. */
export type ArgSerialiserFn = (...args: any[]) => PropertyKey;

/** Memoise the output disregarding method parameters */
export function all(): MethodDecorator {
return Memoise(constantFn);
}
}
25 changes: 25 additions & 0 deletions test/Test.js
Original file line number Diff line number Diff line change
Expand Up @@ -231,6 +231,31 @@ describe(_.startCase(TEST_TYPE), () => {
});
});

describe('Memoise.all', () => {
class Clazz {
static gets = 0;

@Memoise.all()
meth(a) {
Clazz.gets++;

return a;
}
}

const inst = new Clazz();

it('First call should increment gets, return foo', () => {
expect(inst.meth('foo')).to.eq('foo', 'return');
expect(Clazz.gets).to.eq(1, 'gets');
});

it('Second call should not increment gets, should return foo', () => {
expect(inst.meth('bar')).to.eq('foo', 'return');
expect(Clazz.gets).to.eq(1, 'gets');
});
});

describe('Errors', () => {
it('Should throw on non-method decoration', () => {
expect(() => {
Expand Down
Loading

0 comments on commit 02ae821

Please sign in to comment.