package
Not invasive, performant, and garbage collected storage for React components (and more)
npm install component-mapGitHub-flavored package documentation.
Across all versions.
Download trend unavailable.
Runtime metadata.
Tagged topics.
Not invasive, performant, and garbage collected storage for React components (and more)
ComponentMap allows for most operations WeakMap provides, but it's not a 1:1 shim. In particular, to simplify the implementation it doesn't accept iterable object as a constructor parameter.
ComponentMap it is preconfigured for performant mapping of React classes, but can be easily customized.
ComponentMap promises following:
Unfortunately given first promise, it's not possible to prevent memory pollution in case WeakMap is provided. For this reason ComponentMap can be used without WeakMap option only in short-lived sessions, like development or tests.
Component Map supports subset of WeakMap API.
new ComponentMap(options: object) with following options:
WeakMap: WeakMap - the WeakMap implementation to use, or undefined, recommendedgetBucketName: (object) => string - see Configuraton section below, optional#get(key: object): any - returns the value that key corresponds to the key or undefined.#has(key: object): boolean - tells whether there exists a value with given key#set(key: object, value: any) - sets key to given value. The key must be an object.#delete(key: object): boolean - removes the value and returns true if there was a value to delete.The key option must be an object, by default ComponentMap is optimized to store React components.
class MyComponent { render() { return <div>Hello world</div> } }
const components = new ComponentMap({ WeakMap })
components.set(MyComponent, { metadata: 'something' })
// somewhere else
const meta = components.get(MyComponent).metadataIf you wish to use objects other than React components as keys, you can use getBucketName. It allows for specifying custom bucket names. See how ComponentMap is a subclass of BucketMap:
const object = { type: 'MyObject' }
class myMap = new ComponentMap({ getBucketName: (key => key.type) })
myMap.set(MyObject, 'something')By default it tries to store components directly into passed WeakMap, if provided. Otherwise it uses custom storage that puts objects into buckets instead of single map, improving retrieval performance a lot. Something like buckets[getBucketName(key)] = value.
ComponentMap doesn't automatically detect WeakMap implementation, but rather allows you to pass it as a constructor parameter. It uses bucketed storage implementation only in case WeakMap is not provided.
MIT