# decorator-synchronized

> function decorator which ensures that calls do not run simultaneously

Latest version **0.6.0** (published 2021-05-18) · ISC license · 0 weekly downloads

## Install

```sh
npm install decorator-synchronized
pnpm add decorator-synchronized
yarn add decorator-synchronized
bun add decorator-synchronized
```

## Health

**Score 15/100 (F)** — status: abandoned.

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support; pre 1.0.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 0.6.0 |
| Published | 2021-05-18 |
| First published | 2016-12-12 |
| Weekly downloads | 0 |
| License | ISC |
| TypeScript types | none |
| Module format | CommonJS |
| Node | >=4 |
| Dependencies | 0 |
| Unpacked size | 15.4 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 0 |
| Author | Pierre Donias |
| Maintainers | julien-f, pdonias |
| Keywords | decorator, function, lock, mutex, queue, synchronise, synchronised, synchronize, synchronized, wrapper |

## Links

- npm: https://www.npmjs.com/package/decorator-synchronized
- Repository: https://github.com/JsCommunity/decorator-synchronized
- Issues: https://github.com/JsCommunity/decorator-synchronized/issues
- npm.io page: https://npm.io/package/decorator-synchronized

## Alternatives

- [cron](https://npm.io/package/cron.md) — 4.9M weekly downloads
- [@vercel/queue](https://npm.io/package/@vercel/queue.md) — 731.6K weekly downloads
- [create-sonicjs](https://npm.io/package/create-sonicjs.md) — 1.6K weekly downloads
- [@exellix/jobs-api](https://npm.io/package/@exellix/jobs-api.md) — 941 weekly downloads
- [@forwardimpact/libskill](https://npm.io/package/@forwardimpact/libskill.md) — 575 weekly downloads

## Recent versions

- 0.6.0 (latest) — 2021-05-18
- 0.5.0 — 2019-03-15
- 0.4.0 — 2019-03-14
- 0.3.0 — 2017-07-20
- 0.2.3 — 2016-12-22
- 0.2.2 — 2016-12-12
- 0.2.1 — 2016-12-12
- 0.2.0 — 2016-12-12
- 0.1.0 — 2016-12-12
- 0.0.0 — 2016-12-12

## README

# decorator-synchronized

[![Package Version](https://badgen.net/npm/v/decorator-synchronized)](https://npmjs.org/package/decorator-synchronized) [![Build Status](https://travis-ci.org/JsCommunity/decorator-synchronized.png?branch=master)](https://travis-ci.org/JsCommunity/decorator-synchronized) [![PackagePhobia](https://badgen.net/packagephobia/install/decorator-synchronized)](https://packagephobia.now.sh/result?p=decorator-synchronized) [![Latest Commit](https://badgen.net/github/last-commit/JsCommunity/decorator-synchronized)](https://github.com/JsCommunity/decorator-synchronized/commits/master)

> Function decorator which ensures that calls do not run simultaneously.

Requires _WeakMap_, it your system does not have it, use a [polyfill](https://github.com/medikoo/es6-weak-map).

## Install

Installation of the [npm package](https://npmjs.org/package/decorator-synchronized):

```
> npm install --save decorator-synchronized
```

## Usage

```js
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`

```js
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:

```js
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](https://github.com/JsCommunity/decorator-synchronized/issues)
  you've encountered;
- fork and create a pull request.

## License

ISC © [Pierre Donias](https://github.com/pdonias)

---
_Source: https://npm.io/package/decorator-synchronized · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
