memoize-one-with-dispose

1.0.0 • Public • Published

memoize-one-with-dispose

A memoization function based on memoize-one with disposing function.

npm version Build Status Coverage Status

Background

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.

To install

Run npm install memoize-one memoize-one-with-dispose.

This package peer-depends on memoize-one.

Before using memoize-one-with-dispose

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 memoize-one-with-dispose

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()
);

Contributions

Like us? Star us.

Want to make it better? File us an issue.

Don't like something you see? Submit a pull request.

Package Sidebar

Install

npm i memoize-one-with-dispose

Weekly Downloads

4

Version

1.0.0

License

MIT

Unpacked Size

9.92 kB

Total Files

10

Last publish

Collaborators

  • compulim