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

@thermopylae/lib.async

v1.0.1

Published

Utilities for async operations.

Downloads

2

Readme

Utilities for async operations.

Install

npm install @thermopylae/lib.async

Description

This package contains utilities for async operations. More details can be found in API doc.

Usage

Below is a use case example of how LabeledConditionalVariableManager can be used.

import { 
    LabeledConditionalVariableManager, 
    LockedOperationType, 
    AwaiterRole 
} from '@thermopylae/lib.async';
import {
    AsyncFunction
} from '@thermopylae/core.declarations';

class Cache {
    // acts as Read-Write Lock, i.e. shared for Reads, exclusive for Writes
    private readonly conditionalVariable: LabeledConditionalVariableManager<string, string>;

    private readonly entries: Map<string, string>; // assuming they will expire somehow

    private readonly storageReader: AsyncFunction<string, string>;

    private readonly storageWriter: AsyncFunction<string, void>;

    public constructor(storageReader: AsyncFunction<string, string>, storageWriter: AsyncFunction<string, void>) {
        this.conditionalVariable = new LabeledConditionalVariableManager();
        this.entries = new Map<string, string>();
        this.storageReader = storageReader;
        this.storageWriter = storageWriter;
    }

    public async get(key: string): Promise<string | undefined> {
        // acquire read-write lock
        // in case it is acquired already by `set` operation, wait will throw
        const lock = await this.conditionalVariable.wait(key, LockedOperationType.READ);

        if (lock.role === AwaiterRole.CONSUMER) {
            // lock has been acquired already by someone who initiated `get` operation for this key
            // just return promise and wait until PRODUCER will resolve/reject it
            return lock.promise;
        }

        let valueOrError = this.entries.get(key);
        if (valueOrError === undefined) {
            try {
                valueOrError = await this.storageReader(key);
                this.entries.set(key, valueOrError);
            } catch (e) {
                valueOrError = e;
            }
        }

        // we are the PRODUCER, so we need to notify ourself and other consumers with value of the key or error that occurred
        // also the lock needs to be released, so that `set` operation can acquire it
        this.conditionalVariable.notifyAll(key, valueOrError);
        return lock.promise;
    }

    public async set(key: string, value: string): Promise<void> {
        // acquire exclusive lock
        // in case it is acquired already by `set` or `get` cache operations, wait will throw
        await this.conditionalVariable.wait(key, LockedOperationType.WRITE);

        // there is no need to check for producer consumer roles,
        // because WRITE lock is either acquired, or an error is thrown

        try {
            await this.storageWriter(key, value);
            this.entries.set(key, value);
        } finally {
            // release exclusive lock, so it can be used by either `set` or `get` cache operations
            this.conditionalVariable.notifyAll(key);
        }
    }
}

PromiseExecutor

import { PromiseExecutor } from '@thermopylae/lib.async';

function fetchUserDetails(accountId: string): Promise<object> {
    return fetch(`http://localhost:8080/user/${accountId}`);
}

// will process id's in batches of 2 elements, 
// i.e. will make up to 2 network calls simultaneusly 
let results = PromiseExecutor.run(
    fetchUserDetails,
    [1, 2, 3, 4, 5],
    2
);
console.log(results);

// will process all id's in parallel, 
// i.e. will make 5 network calls simultaneusly 
results = PromiseExecutor.run(
    fetchUserDetails,
    [1, 2, 3, 4, 5],
    PromiseExecutor.PARALLEL
);
console.log(results);

// will process id's in sequential order, 
// i.e. will 1 network call simultaneusly 
results = PromiseExecutor.run(
    fetchUserDetails,
    [1, 2, 3, 4, 5],
    PromiseExecutor.SEQUENTIAL
);
console.log(results);

API Reference

API documentation is available here.

It can also be generated by issuing the following commands:

git clone [email protected]:marinrusu1997/thermopylae.git
cd thermopylae
yarn install
yarn workspace @thermopylae/lib.async run doc

Author

👤 Rusu Marin

📝 License

Copyright © 2021 Rusu Marin. This project is MIT licensed.