Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

decorator-synchronized

Package Overview
Dependencies
Maintainers
2
Versions
10
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

decorator-synchronized

function decorator which ensures that calls do not run simultaneously

  • 0.6.0
  • latest
  • Source
  • npm
  • Socket score

Version published
Maintainers
2
Created
Source

decorator-synchronized

Package Version Build Status PackagePhobia Latest Commit

Function decorator which ensures that calls do not run simultaneously.

Requires WeakMap, it your system does not have it, use a polyfill.

Install

Installation of the npm package:

> npm install --save decorator-synchronized

Usage

import { synchronized } from "decorator-synchronized";

let i = 0;

const fn = synchronized(() => {
  console.log(i);
  return Promise.resolve().then(() => {
    i++;
  });
});

Promise.all([fn(), fn()]);
// => Prints 0 then 1

// Create a dedicated synchronizer which will be shared amongst
// multiple functions.
//
// Useful when functions work on the same resource.
const counterSynchronized = synchronized();

const increment = counterSynchronized(async () => {
  const i = 1 + (await db.getCounter());
  await db.setCounter(i);
  return i;
});

const decrement = counterSynchronized(async () => {
  const i = -1 + (await db.getCounter());
  await db.setCounter(i);
  return i;
});

increment().then(console.log); // prints 1
decrement().then(console.log); // prints 0

withKey

import { synchronized } from "decorator-synchronized";

const updateUser = synchronized.withKey()(async (userId, props) => {
  const user = await db.getUser(userId);
  await db.setUser(userId, { ...user, ...props });
});

// will correctly update the user without race conditions
updaterUser("wq1567e", { foo: 3.14 });
updaterUser("wq1567e", { bar: 42 });

// different users are still updated in parallel
updateUser("ct356tv", { baz: 2.72 });

The key is deduced from the first argument, if you need something else, just provide a key function:

const fn = synchronized.withKey((_, secondArg) => secondArg)(
  (firstArg, secondArg) => {
    // TODO
  }
);

Development

# Install dependencies
> yarn

# Run the tests
> yarn test

# Continuously compile
> yarn dev

# Continuously run the tests
> yarn dev-test

# Build for production
> yarn build

Contributions

Contributions are very welcomed, either on the documentation or on the code.

You may:

  • report any issue you've encountered;
  • fork and create a pull request.

License

ISC © Pierre Donias

Keywords

FAQs

Package last updated on 18 May 2021

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc