npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@smart-hook/react-hook-retention-cache

v0.0.13

Published

React hooks to automatic cache retention.

Downloads

40

Readme

Overview

This package provides an easy way to cache objects during the lifetime of a React component with additional retention time. You can create cache with retentionCache and use it with useCache.

Basic Usage

// global
const cache = retentionCache({
  generator: (documentId: string) => new CertainObject(documentId),
  retentionTime: 1000
});
// in react functional component
const obj: CertainObject = useCache(cache, documentId);

With Store

Store is a interaface that provides a way to resolve streaming resource (such as websocket) into a value. This package includes convenient functions to work with Store useStore, useStoreCache, createStoreCacheHook.

export interface Store<C> {
  current: C;
  on(handler: (content: C) => void): Unsubscriber;
}

// global
const cache = retentionCache({
  generator: (documentId: string) => new CertainStore<C>(documentId),
});
// in react functional component
const data: C = useStoreCache(cache, documentId);

Destructive change

  • = v0.0.9. useStoreCache and hooks generated by createStoreCacheHook become to accept null / undefined as params and return empty object({}) at that time.

API

retentionCache

export declare function retentionCache<V, P = undefined>(params: {
    generator: (param: P) => V;
     // clean up function. (default: no operation)
    cleanUp: (value: V) => void;
    // retention time after component is unmounted. (default: 0)
    retentionTime: number | undefined; 
    // serializer to determine cache key. (default: stringify function like json-stable-stringify)
    serializer?: ((param: P) => string) | undefined;
}): RetentionCache<V, P>;

// example
const cache = retentionCache({
  generator: ({ userId, groupId }) => new CertainObject(documentId),
  cleanUp: (obj: CertainObject) => obj.close(),
  serializer: ({ userId, groupId }) => `${userId}:${groupId}`
  retentionTime: 1000
});

Usually (at least in React component), you don't have to use RetentionCache directly. Use it with useCache.

useCache

export declare const useCache: <V, P>(
  // param to use generate object (via generator) and to determine cache (via serializer).
  param: P | undefined | null, 
  // RetentionCache object
  cache: RetentionCache<V, P>
) => V | undefined;

// example
// in react functional component
const obj: CertainObject = useCache(cache, { userId, groupId });

useStore

export interface Store<C> {
    current: C;
    on(handler: (content: C) => void): Unsubscriber;
}
export declare const useStore: <C>(store: Store<C>) => C;

useStore is a helper independent with RetentionCache. It resolve streaming value.

useStoreCache

declare function useStoreCache<S extends Store<unknown>, P>(cache: RetentionCache<S, P>, params: P | undefined | null): ContentOfStore<S> | undefined;

useStoreCache is syntax sugar of useStore(useCache(params, cache)). If you want to use only content of Store in RetentionCache. It is convenient to use useStoreCache.

createStoreCacheHook

declare function createStoreCacheHook<S extends Store<unknown>, P, V>(cache: RetentionCache<S, P>, defaultValue: V): (params: P | undefined | null) => V | ContentOfStore<S>;

createStoreCacheHook creates partial application of useStoreCache which RetentionCache is binded with. By this solution, you don't have to be aware of RetentionCache in application code.

Hack: If you want the only store in cache, create a retention cache with non-null parameter (for example true) and bind the same value.

const cache = retentionCache({
  generator: (_param: true) => new SomeStore<D>(),
  cleanUp: (v) => v.close(),
  retentionTime,
});
createStoreCacheHook(cache, {} as never).bind(null, true)