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

memd

v0.3.13

Published

Cache data and memoize, debounce, throttle and queue methods

Downloads

248

Readme

Memd


Build Status NPM version

  • Memoize, debounce, throttle and queue methods
  • Cache Handler
  • Persistence: (File, LocalStorage, Custom)
  • NodeJS/Browser

import { deco } from 'memd';

class Foo {
    @deco.memoize()
    someMethod () {}

    @deco.memoize()
    get someProp () {}

    @deco.debounce()
    bar () {}

    @deco.throttle()
    qux () {}

    @deco.queue()
    async dex () {}
}

Cache

import { Cache } from 'memd'

interface ICacheOpts {
    maxAge?: number
    monitors?: ICacheChangeEventMonitor[]
    keyResolver?: (...args) => string
}
interface ICacheChangeEventMonitor {
    on (event: 'change', fn: Function)
    off (event: 'change', fn: Function)
}

const cache = new Cache(<ICacheOpts> { maxAge: 60 });
.get (key: string): T
.set (key: string, val: T): T
.clear (key?: string)
.destroy ()

memoize

interface IMemoizeOpts {
    // Per default method or getter returns are cached for all instances of a class.
    // Use `perInstance` to cache per instance: this could be useful when the method reads `this.` values.
    perInstance?: boolean

    // When a promise is memoized, and gets rejected. Clear also the cache, so that
    // the next time it wont hit the cache and is reavaluated.
    clearOnReject?: boolean
}
.memoize(opts?: ICacheOpts & IMemoizeOpts)

debounce

When ms is 0 or undefined then requestAnimationFrame or setImmediate is used.

.debounce(ms: number = 0)

throttle

.throttle(ms: number, shouldCallLater?: boolean)

queued

Calls method only when the previous promise is resolved. Use trimQueue: true to ensure the queue consists of max 1 listener.

.queued(opts: { trimQueue?: boolean, timeout?: number, throttle?: number })

Transport and Store for Cache/Memoize

Transport

Persist all cached data to the backed store, to be able to restore on app restarts

(NodeJS) Files
import memd from 'memd'
const fs = new memd.FsTransport({ path: './lorem.json' });
class Foo {

    @memd.deco.memoize({ transport: fs })
    foo (bar) {
        // do smth
    }
}
(Browser) localStorage
import memd from 'memd'

const storage = new memd.LocalStorageTransport({ key: 'foo' });
class Foo {

    @memd.deco.memoize({ transport: storage })
    foo (bar) {
        // do smth
    }
}

Store

Read and Save single values from store

import memd from 'memd'

const store = {
    getAsync (key: string, ...args) {
        // get cached value if any
        return {
            value: 'bar'
        }
    },
    saveAsync (key: string, entry: { value: any, timestamp: number }) {
        // save cached value
    }
}
class Foo {

    @memd.deco.memoize({ store })
    foo (bar) {
        // do smth
    }
}

Atma.js Project