A memoization function based on memoize-one
with disposing function.
memoize-one
is a popular utility package for memoization. It is simple and straightforward. But sometimes, you may use it to hold resources that requires a manual release. This package enhance memoize-one
with a disposing function that would call before a new memoized object is created.
Although JavaScript automatically do garbage collection, sometimes you may be working with resources that requires an explicit stop or cancellation.
Run npm install memoize-one memoize-one-with-dispose
.
This package peer-depends on
memoize-one
.
Before using this package, you will need to write code like this.
import memoize from 'memoize-one';
let created, lastValue;
const createOrGetValue = memoize(x => {
created && lastValue.dispose();
created = true;
lastValue = createValue(x);
return lastValue;
});
After using this package, you can write shorter and more readable code like this.
import memoizeWithDispose from 'memoize-one-with-dispose';
const createOrGetValue = memoize(
x => createValue(x),
undefined, // pass as equalityFn to memoize-one
lastValue => lastValue.dispose()
);
Like us? Star us.
Want to make it better? File us an issue.
Don't like something you see? Submit a pull request.