Comparing version 8.0.0 to 8.1.0
@@ -85,2 +85,29 @@ declare type AnyFunction = (...arguments_: any) => any; | ||
/** | ||
@returns A TypeScript decorator which memoizes the given function. | ||
@example | ||
``` | ||
import mem = require('mem'); | ||
class Example { | ||
index = 0 | ||
@mem.decorator() | ||
counter() { | ||
return ++this.index; | ||
} | ||
} | ||
class ExampleWithOptions { | ||
index = 0 | ||
@mem.decorator({maxAge: 1000}) | ||
counter() { | ||
return ++this.index; | ||
} | ||
} | ||
``` | ||
*/ | ||
decorator<FunctionToMemoize_1 extends AnyFunction, CacheKeyType_1>(options?: Options<FunctionToMemoize_1, CacheKeyType_1>): (target: any, propertyKey: string, descriptor: PropertyDescriptor) => void; | ||
/** | ||
Clear all cached data of a memoized function. | ||
@@ -87,0 +114,0 @@ |
@@ -48,12 +48,9 @@ 'use strict'; | ||
data: result, | ||
maxAge: maxAge ? Date.now() + maxAge : Infinity | ||
maxAge: maxAge ? Date.now() + maxAge : Number.POSITIVE_INFINITY | ||
}); | ||
return result; | ||
}; | ||
try { | ||
// The below call will throw in some host environments | ||
// See https://github.com/sindresorhus/mimic-fn/issues/10 | ||
mimicFn(memoized, fn); | ||
} | ||
catch (_a) { } | ||
mimicFn(memoized, fn, { | ||
ignoreNonConfigurable: true | ||
}); | ||
cacheStore.set(memoized, cache); | ||
@@ -63,2 +60,35 @@ return memoized; | ||
/** | ||
@returns A TypeScript decorator which memoizes the given function. | ||
@example | ||
``` | ||
import mem = require('mem'); | ||
class Example { | ||
index = 0 | ||
@mem.decorator() | ||
counter() { | ||
return ++this.index; | ||
} | ||
} | ||
class ExampleWithOptions { | ||
index = 0 | ||
@mem.decorator({maxAge: 1000}) | ||
counter() { | ||
return ++this.index; | ||
} | ||
} | ||
``` | ||
*/ | ||
mem.decorator = (options = {}) => (target, propertyKey, descriptor) => { | ||
const input = target[propertyKey]; | ||
if (typeof input !== 'function') { | ||
throw new TypeError('The decorated value must be a function'); | ||
} | ||
descriptor.value = mem(input, options); | ||
}; | ||
/** | ||
Clear all cached data of a memoized function. | ||
@@ -65,0 +95,0 @@ |
{ | ||
"name": "mem", | ||
"version": "8.0.0", | ||
"version": "8.1.0", | ||
"description": "Memoize functions - An optimization used to speed up consecutive function calls by caching the result of calls with identical input", | ||
@@ -48,3 +48,3 @@ "license": "MIT", | ||
"@types/serialize-javascript": "^4.0.0", | ||
"ava": "^3.13.0", | ||
"ava": "^3.15.0", | ||
"del-cli": "^3.0.1", | ||
@@ -55,3 +55,3 @@ "delay": "^4.4.0", | ||
"typescript": "^4.0.3", | ||
"xo": "^0.33.1" | ||
"xo": "^0.38.2" | ||
}, | ||
@@ -58,0 +58,0 @@ "ava": { |
@@ -1,2 +0,2 @@ | ||
# mem [![Build Status](https://travis-ci.com/sindresorhus/mem.svg?branch=master)](https://travis-ci.com/github/sindresorhus/mem) | ||
# mem | ||
@@ -199,2 +199,34 @@ > [Memoize](https://en.wikipedia.org/wiki/Memoization) functions - An optimization used to speed up consecutive function calls by caching the result of calls with identical input | ||
### mem.decorator(options) | ||
Returns a TypeScript decorator which memoizes the given function. | ||
#### options | ||
Type: `object` | ||
Same as options for `mem()`. | ||
```js | ||
const mem = require('mem'); | ||
class Example { | ||
index = 0 | ||
@mem.decorator() | ||
counter() { | ||
return ++this.index; | ||
} | ||
} | ||
class ExampleWithOptions { | ||
index = 0 | ||
@mem.decorator({maxAge: 1000}) | ||
counter() { | ||
return ++this.index; | ||
} | ||
} | ||
``` | ||
### mem.clear(fn) | ||
@@ -201,0 +233,0 @@ |
16523
188
282