https://github.com/compulim/memoize-one-with-dispose
A memoization function with cache of one and disposing function
https://github.com/compulim/memoize-one-with-dispose
memoize
Last synced: about 1 month ago
JSON representation
A memoization function with cache of one and disposing function
- Host: GitHub
- URL: https://github.com/compulim/memoize-one-with-dispose
- Owner: compulim
- License: mit
- Created: 2018-11-25T03:29:23.000Z (over 6 years ago)
- Default Branch: master
- Last Pushed: 2022-12-08T06:16:13.000Z (over 2 years ago)
- Last Synced: 2025-03-24T16:02:53.876Z (about 2 months ago)
- Topics: memoize
- Language: JavaScript
- Size: 1.15 MB
- Stars: 3
- Watchers: 1
- Forks: 0
- Open Issues: 12
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- License: LICENSE
Awesome Lists containing this project
README
# memoize-one-with-dispose
A memoization function based on [`memoize-one`](https://npmjs.com/package/memoize-one) with disposing function.
[](https://badge.fury.io/js/memoize-one-with-dispose) [](https://travis-ci.org/compulim/memoize-one-with-dispose) [](https://coveralls.io/github/compulim/memoize-one-with-dispose)
## Background
[`memoize-one`](https://npmjs.com/package/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`](https://npmjs.com/package/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.
## To install
Run `npm install memoize-one memoize-one-with-dispose`.
> This package peer-depends on [`memoize-one`](https://npmjs.com/package/memoize-one).
### Before using `memoize-one-with-dispose`
Before using this package, you will need to write code like this.
```js
import memoize from 'memoize-one';let created, lastValue;
const createOrGetValue = memoize(x => {
created && lastValue.dispose();
created = true;
lastValue = createValue(x);return lastValue;
});
```### After using `memoize-one-with-dispose`
After using this package, you can write shorter and more readable code like this.
```js
import memoizeWithDispose from 'memoize-one-with-dispose';const createOrGetValue = memoize(
x => createValue(x),
undefined, // pass as equalityFn to memoize-one
lastValue => lastValue.dispose()
);
```# Contributions
Like us? [Star](https://github.com/compulim/memoize-one-with-dispose/stargazers) us.
Want to make it better? [File](https://github.com/compulim/memoize-one-with-dispose/issues) us an issue.
Don't like something you see? [Submit](https://github.com/compulim/memoize-one-with-dispose/pulls) a pull request.