JSPM

use-weak-map-memoizer

1.0.0-alpha.0
    • ESM via JSPM
    • ES Module Entrypoint
    • Export Map
    • Keywords
    • License
    • Repository URL
    • TypeScript Types
    • README
    • Created
    • Published
    • Downloads 1
    • Score
      100M100P100Q23403F
    • License MIT

    React-like interface for memoization without binding the order of calls. Based on WeakMap.

    Package Exports

    • use-weak-map-memoizer
    • use-weak-map-memoizer/build/index.js
    • use-weak-map-memoizer/src/index.ts

    This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (use-weak-map-memoizer) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.

    Readme

    🧐 useWeakMapMemoizer 🧐

    Use Weak Map Memoizer is released under the MIT license. Use Weak Map Memoizer passed the testing Current npm package version.

    Provides React-like interface for memorizing values and callbacks without binding the order of calls. Based on WeakMap.

    Installation

    yarn add use-weak-map-memoizer
    # or
    npm install use-weak-map-memoizer 

    Example

    const MyListComponent = ({ list }) => {
      const wkmap = useWeakMapMemoizer(); // #1
      const selectItem = useCallback((item) => {/* some logic */}, [/* deps */])
      const addItem = useCallback((item) => {/* some logic */}, [/* deps */])
      
      return list.map(item => {
        const cache = wkmap(item); // #2
        const mappedItem = cache.memo(1, [], () => ({ ...item /*, some new fields */ })); // #3
        const onPress = cache.callback(2, [selectItem], () => selectItem(mappedItem)); // #4
        const onDoublePress = cache.callback(3, [addItem], () => addItem(mappedItem));
        
        return (
          <MyMemoizedItemComponent
            item={mappedItem}
            onPress={onPress}
            onDoublePress={onDoublePress}
          />
        )
      })
    }

    What's going on here?

    1. Initialize the memoizer
    2. Get the object for caching by reference key
    3. Similar to the working principle of useMemo.
      1. 1 is the caching key within the item object
      2. [] is an array of dependencies. It would be possible to omit this parameter
      3. () => {} — the creator function.
    4. The principle of operation is similar to p. 3, but only an analogy for useCallback

    Use case

    Mapping

    It is convenient to memorize the results of mapping based on a reference.

    const { list } = props;
    const wkmap = useWeakMapMemoizer();
    
    return list.map(item => {
      const mappedItem = wkmap(item).memo(() => ({...item, /*, some new fields */}))
      return <MyMemoizedComponent item={mappedItem} />
    })

    Closure

    It is convenient to make closures in iterative components

    const { list } = props;
    const wkmap = useWeakMapMemoizer();
    const addItem = useCallback((item) => {/* some logic */}, [/* deps */])
    
    return list.map(item => {
      const onClick = wkmap(item).callback(1, [addItem], () => addItem(item))
      return <MyMemoizedComponent item={mappedItem} onClick={onClick}  />
    })

    You can come up with your own situations when it is convenient for you!

    API

    In short:

    const wkmap = useWeakMapMemoizer(); // or createWeakMapMemoizer()
    
    wkmap(obj).memo(creator)
    wkmap(obj).memo(cacheKey, creator)
    wkmap(obj).memo(cacheKey, deps, creator)
    
    wkmap(obj).callback(callback)
    wkmap(obj).callback(cacheKey, callback)
    wkmap(obj).callback(cacheKey, deps, callback)

    ⚠️ By default, deps is an empty array.

    That is, memoization will not compare dependencies and return a memorized result relative the reference object and cache key. This behavior differs from useMemo and useCallback

    Important tips

    Use cache key

    If you need to memorize several results relative the same object, use the caching key.

    const onPress = wkmap(obj).callback(1, [], () => { /* some logic */ }); // cacheKey is 1
    const onDoublePress = wkmap(obj).callback(2, [], () => { /* some logic */ }); // cacheKey is 2

    Only primitive types for cache key

    You don't need to use reference types in caching keys, instead pass them to map(cacheKeyObj)

    👨‍💻 Author

    Sergey Rozhkov

    🎯 Was it helpful?

    Do you like it and find it helpful? You can help this project in the following way:

    • ⭐ Put the star.
    • 💡 Suggest your ideas.
    • 😉 Open a founded issue.

    📄 License

    useWeakMapMemoizer is MIT licensed, as found in the LICENSE file.