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

expirable-synchronized

v1.1.7

Published

A decorator to make any function that returns promise atomic with expiration

Downloads

9

Readme

expirable-synchronized

Build Status Test Coverage npm npm GitHub

A JavaScript decorator to atomize any function that returns promise. The waiting to resolve can expire after some time.

Similar to Java's @synchronized, this decorator can atomize a function!

If you add this decorator to a function that returns a promise, and call this function multiple times, the function calls will be executed in a certain way. There are two modes:

  • Fair: @expirableSynchronized or @expirableSynchronizedFair

    Queue up all function calls. The next function call in the queue can only start after the previous one finishes. The function acts like a fair lock added.

  • Exclusive: @expirableSynchronizedExclusive

    Drop all other function calls when there is a function call that hasn't finished.

The function to apply this decorator must return a promise

Notice that this decorator doesn't affect the promise itself, just set when / whether to execute them.

Use Case:

When you want to use it & Solutions if you don't really need it:

  • The function has asynchronous jobs and the order matters. (Otherwise, you don't need to do anything.)
  • The function is not called by your program, or it's hard to manage the function caller. A typical example is event listener. (Otherwise, you should refactor your code to better call your function.)
  • You need the expiration of waiting to resolve. (Otherwise, consider using RxJS's asyncScheduler, schedule in a subscription.)
  • If RxJS is too big for your project to include. (Otherwise, RxJS is your answer if you don't need the expiration of waiting to resolve.)

How it works:

Fair:

It connects all function calls into a promise chain. It's safe because you can set an expiry time to stop waiting for the promise to resolve. If one call in the chain takes too long to resolve / reject, the next in the chain will be executed.

It uses a pointer to build promise chain. The promise is saved in the function's target(who calls the function). This target is the class instance in most cases.

Exclusive:

It locks the function when a function call starts, release it when it's done. No other function call by the same caller can run when this function is locked, and won't run later.

Installation

Using npm:

npm install --save expirable-synchronized

Using yarn:

yarn add expirable-synchronized

Requirements

  • An environment that supports stage-0 of the decorators spec.
  • Node 8+.

If you use transpiler

How to Use

Just add @expirableSynchronized() or @expirableSynchronizedExclusive() above the function that returns a promise. According to current stage of decorator proposal, the function has to be a class member.

class Example {
    @expirableSynchronized()
    anyFunctionThatReturnsAPromise() {
        // ...
    }

    @expirableSynchronized(2000)
    anyFunctionThatReturnsAPromise2() {
        // ...
    }

    @expirableSynchronized(2000, 'your-custom-prefix-')
    anyFunctionThatReturnsAPromise3() {
        // ...
    }

    @expirableSynchronizedExclusive()
    anyFunctionThatReturnsAPromise4() {
        // ...
    }

    @expirableSynchronizedExclusive(2000)
    anyFunctionThatReturnsAPromise5() {
        // ...
    }

    @expirableSynchronizedExclusive(2000, 'your-custom-prefix-')
    anyFunctionThatReturnsAPromise6() {
        // ...
    }
}

Options

| Parameter | Description | Type | Default Value | |-----------|-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|--------|---------------------------| | life | The promise life limit. After this amount of time, the next promise in chain will be executed anyway in Fair mode, lock will be released anyway in Exclusive mode Default value is 5 seconds (other packages like jest has 5s as timeout also) | number | 5000 | | prefix | The prefix for the name of lock / promise pointer living in the caller object (class instance). Only set this parameter in case that the default prefix has conflict with your existing object key | string | expirable-synchronized- |