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

@vlsergey/react-indexdb-cache

v0.13.1

Published

IndexedDb-backed cache for react application

Downloads

3

Readme

react-indexdb-cache

Flexible indexeddb-backed cache for react application

  • Uses IndexedDb to store cached data, thus it can be used even if user reloads page or reopen browser window.
  • Tries to reduce number of component render calls as much as possible. For example changing key order in arguments from ['first', 'second'] to ['second', 'first'] will return same Record<string,...> object instance. Thus it's safe to use cache result as useMemo or React.memo argument.

NPM version Build Status Downloads

Usage

Cache definition

Create new file and define loader, cache instance and access helpers (hooks and components):

import {cacheValueProviderFactory, cacheValuesProviderFactory, CacheWithIndexedDb,
  cacheValueHookFactory, cacheValuesHookFactory} from '@vlsergey/react-indexdb-cache';

type Key = string;
type Value = number;

async function loader(key : Key) : Promise<Value | undefined> {
  // place actual key to value resolving here
  return 42;
}

export const cache = new CacheWithIndexedDb<string, number, number>({
  loader,
  databaseName: 'testDatabase', // name of IndexedDb database name
});

export const CacheValueProvider = cacheValueProviderFactory(testCache, true);
export const CacheValuesProvider = cacheValuesProviderFactory(testCache);
export const useCacheValue = cacheValueHookFactory(propertiesDataCache);
export const useCacheValues = cacheValuesHookFactory(propertiesDataCache);

export default cache

It is advised to use batching function (like one from @vlsergey/batcher) to group multiple async calls into batches.

import Batcher from '@vlsergey/batcher';

async function batchLoader(keys : Key[]) : Promise<(Value | undefined)[]> {
  // place actual key to value resolving here
  return keys.map( key => 42 );
}

const batcher = new Batcher<string, number>(batchLoader, {
  maxBatchSize: 50
});

export const cache = new CacheWithIndexedDb<string, number, number>({
  loader: key => batcher.queue(key),
  databaseName: 'testDatabase', // name of IndexedDb database name
});

Cache usage

Use cache values in functional of class components.

import {CacheValueProvider, CacheValuesProvider, useCacheValue, useCacheValues} from "./myCache.ts"
interface MyComponentProps {
  entityId?: string
}

function MyComponent({
  entityId
} : MyComponentProps) => {
  const valueFromCache : number | undefined = useCacheValue( entityId );
  return valueFromCache ? <span>{valueFromCache}</span> : <span>Loading...</span>
}
interface MyComponentProps {
  entityId?: string
}

function MyComponent({
  entityId
} : MyComponentProps) => <CacheValueProvider cacheKey={entityId}>{ (valueFromCache : number | undefined) =>
  valueFromCache ? <span>{valueFromCache}</span> : <span>Loading...</span>
}</CacheValueProvider>
interface MyComponentProps {
  entityIds: readonly string[]
}

function MyComponent({
  entityIds
} : MyComponentProps) => {
  const valuesFromCache : Record<string, number> = useCacheValues( entityIds );
  return JSON.stringify(valuesFromCache)
}
interface MyComponentProps {
  entityIds: readonly string[]
}

function MyComponent({
  entityIds
} : MyComponentProps) => <CacheValuesProvider cacheKeys={entityIds}>{ (valuesFromCache : Record<string, number>) =>
  valueFromCache ? <span>{valueFromCache}</span> : <span>Loading...</span>
}</CacheValuesProvider>