Important: This documentation covers Yarn 1 (Classic).
For Yarn 2+ docs and migration guide, see yarnpkg.com.

Package detail

@gmod/abortable-promise-cache

GMOD1.9kMIT2.0.1TypeScript support: included

add AbortController support to a cache of async requests

readme

abortable-promise-cache

NPM version Coverage Status Build Status

Adds AbortController/AbortSignal semantics to a cache of promises. Each get from the cache can optionally take an AbortSignal object that lets that request be aborted.

Cached fill requests will be aborted and evicted from the cache if all the incoming requests for it are aborted before the promise settles.

If the fill request has already settled before all the requests for it have been aborted, it will stay in the cache.

Install

$ npm install --save abortable-promise-cache

Usage

import AbortablePromiseCache from 'abortable-promise-cache'
import QuickLRU from 'quick-lru'

const cache = new AbortablePromiseCache({
  // QuickLRU is a good backing cache to use, but you can use any
  // cache as long as it supports `get`, `set`, `delete`, and `keys`.
  cache: new QuickLRU({ maxSize: 1000 }),

  // the `fill` callback will be called for a cache miss
  async fill(requestData, abortSignal) {
    // do some long-running thing
    return longRunningThing(requestData, abortSignal)
  },
})

// Make a cached request. The returned promise will abort with the given abort signal if
// there is not already a cached copy that has been resolved.
// Fill requests will be signaled to abort if all the requests for them
// so far have been aborted.
const aborter = new AbortController()
const result = await cache.get('some key', { ...anyStuff }, aborter.signal)

// deleting and clearing will abort any outstanding requests
cache.delete('some key')
cache.clear()

API

Table of Contents

Academic Use

This package was written with funding from the NHGRI as part of the JBrowse project. If you use it in an academic project that you publish, please cite the most recent JBrowse paper, which will be linked from jbrowse.org.

License

MIT © Robert Buels

changelog

v1.5.1

  • Update esm bundle
  • Update README

v1.5.0

  • Add esm bundle
  • Fix typescript definition of .get() to return undefined|T

v1.4.0

  • Only evict if not already settled

v1.3.0

  • Add statusCallback

v1.2.0

  • Avoid caching failed requests

v1.1.3

  • Update typescript typings for abortsignal being optional

v1.1.2

  • Fix typescript declaration on abortsignal ponyfill

v1.1.1

  • Improve typescript typings

v1.1.0

  • Add has method
  • Add typescript typings

v1.0.1

  • Fix abortcontroller polyfilling

v1.0.0

  • Initial release