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

promise-decorators

v0.0.9

Published

set of promise methods decorators

Downloads

3

Readme

promise-decorators

This library contains a set of useful ES6 Promise Decorators

installation

npm install --save promise-decorators

available decorators

@PromiseOnce

This method allows multiple concurrent calls to the same asynchronous method executing the content of the call only once.

Each call after the first, and until the Promise wasn't resolved, gets the previous instance of promise

When the Promise will be resolved or rejected then that will be removed and the next call gets a new instance of promise.

Signature

@PromiseOnce /* this decorator doesn't have any parameters.*/

Example

import { PromiseOnce } from "promise-decorators";

class MyClass {
    public CallCounter = 0;

    @PromiseOnce
    async MyPromiseMethod(): Promise<boolean> {
        this.CallCounter++;
        return true;
    }
}

var x = new MyClass(); // this.CallCounter === 0; // no calls
let p1 = x.MyPromiseMethod(); // x.CallCounter === 1 (first call)
let p2 = x.MyPromiseMethod(); // x.CallCounter === 1 (2nd call)
let p3 = x.MyPromiseMethod(); // x.CallCounter === 1 (3rd call)
let p4 = x.MyPromiseMethod(); // x.CallCounter === 1 (4th call)
Promise.all([p1,p2,p3,p4]).then(values => {
    // values is Array<boolean> 
    // values.length === 4;
    // values[i] === true; // i: from 0 to 3
});

@PromiseCache

This Method enables a very simple cache for a Promise methods.

NOTE: all parameters must be serializable by JSON.stringify because the cache depends by calling parameters

Signature

// Without parameters:
@PromiseCache
async MyMethod(): Promise<boolean> { return true; }
// OR:
@PromiseCache()
async MyMethod(): Promise<boolean> { return true; }
// With "key" parameter:
@PromiseCache("myCacheKey")
async MyMethod(): Promise<boolean> { return true; }

Parameters

If no parameters are passed to the PromiseCache function then the decorator generates an internal key serializing all the parameters

If a key parameter is passed to the PromiseCache then the cache system uses this value to store the results and no other parameters will be used or serialized.

When using a custom key the cache is shared between each method in the same class (static or not) that uses that key.

Example

import { PromiseCache } from "../dist/promise-decorators";

let values: Array<Array<boolean>> = [
    [true, true, true],
    [false, false, false],
    [true, false, true],
];

class ClassCache {
    public GetItems1Counter: number = 0;
    @PromiseCache
    GetItems1(index: number): Promise<Array<boolean>> {
        return new Promise<Array<boolean>>((resolve, reject) => {
            this.GetItems1Counter++;
            resolve(values[index]);
        });
    }
    public static GetItems2Counter: number = 0;
    @PromiseCache
    static async GetItems2(index: number): Promise<Array<boolean>> {
        ClassCache.GetItems2Counter++;
        return values[index];
    }
    public static SharedCount: number = 0;
    @PromiseCache("shared")
    static async GetItemsSharedStatic(index: number): Promise<Array<boolean>> {
        ClassCache.SharedCount++;
        return values[index];
    }
    @PromiseCache("shared")
    async GetItemsShared(index: number): Promise<Array<boolean>> {
        ClassCache.SharedCount++;
        return values[index];
    }
}

async function tests() {
    let cc: ClassCache = new ClassCache();
    await cc.GetItems1(0);
    // cc.GetItems1Counter == 1;
    await cc.GetItems1(0);
    // cc.GetItems1Counter == 1;
    await cc.GetItems1(1);
    // cc.GetItems1Counter == 2;
    await ClassCache.GetItems2(0);
    // cc.GetItems2Counter == 1;
    await ClassCache.GetItems2(1);
    // cc.GetItems2Counter == 2;
    await ClassCache.GetItems2(1);
    // cc.GetItems2Counter == 2;
    await ClassCache.GetItemsSharedStatic(0);
    // cc.SharedCount == 1;
    await ClassCache.GetItemsSharedStatic(1);
    // cc.SharedCount == 1;
    await ClassCache.GetItemsSharedStatic(2);
    // cc.SharedCount == 1;
    await cc.GetItemsShared(0);
    // cc.SharedCount == 1;
    await cc.GetItemsShared(1);
    // cc.SharedCount == 1;
    await cc.GetItemsShared(2);
    // cc.SharedCount == 1;
}