# synchronizer

> utility for exclusive access control

Latest version **0.1.0** (published 2015-07-03) · MIT license · 0 weekly downloads

## Install

```sh
npm install synchronizer
pnpm add synchronizer
yarn add synchronizer
bun add synchronizer
```

## 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.1.0 |
| Published | 2015-07-03 |
| First published | 2015-07-03 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 0 |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 1 |
| Author | Nao Yonamine |
| Maintainers | mohayonao |
| Keywords | promise, synchronized |

## Links

- npm: https://www.npmjs.com/package/synchronizer
- Repository: https://github.com/mohayonao/synchronizer
- Homepage: https://github.com/mohayonao/synchronizer/
- Issues: https://github.com/mohayonao/synchronizer/issues
- npm.io page: https://npm.io/package/synchronizer

## Alternatives

- [@commercetools/sync-actions](https://npm.io/package/@commercetools/sync-actions.md) — 25.1K weekly downloads
- [cwait](https://npm.io/package/cwait.md) — 21.4K weekly downloads
- [@ledgerhq/hw-app-cosmos](https://npm.io/package/@ledgerhq/hw-app-cosmos.md) — 4.2K weekly downloads
- [@financial-times/o-loading](https://npm.io/package/@financial-times/o-loading.md) — 2.8K weekly downloads
- [fa](https://npm.io/package/fa.md) — 185 weekly downloads

## Recent versions

- 0.1.0 (latest) — 2015-07-03

## README

# SYNCHRONIZER
[![Build Status](http://img.shields.io/travis/mohayonao/synchronizer.svg?style=flat-square)](https://travis-ci.org/mohayonao/synchronizer)
[![NPM Version](http://img.shields.io/npm/v/synchronizer.svg?style=flat-square)](https://www.npmjs.org/package/synchronizer)
[![License](http://img.shields.io/badge/license-MIT-brightgreen.svg?style=flat-square)](http://mohayonao.mit-license.org/)

> utility for exclusive access control

## Installation

```
npm install synchronizer
```

## API

- `synchronizer.create(object: any): function`
  - return a synchronized decorator function

## Examples
For example, the below function needs exclusive access control.

```js
function asyncFuncNeedsExclusiveAccessControl(message) {
  if (asyncFuncNeedsExclusiveAccessControl.LOCKED) {
    console.error("FATAL ERROR: THIS FUNCTION IS LOCKED!!");
    return process.exit(1);
  }
  asyncFuncNeedsExclusiveAccessControl.LOCKED = true;

  return new Promise(function(resolve, reject) {
    setTimeout(function() {
      console.log(message);

      asyncFuncNeedsExclusiveAccessControl.LOCKED = false;

      resolve(message);
    }, 500);
  });
}
```

The above function locks a single resource. So you cannot execute the below code.

```js
var object = { name: "alice" };

asyncFuncNeedsExclusiveAccessControl(object.name + "!!").then(function(res) {
  console.log("-->", res);
});

asyncFuncNeedsExclusiveAccessControl(object.name + "??").then(function(res) {
  console.log("-->", res);
});
```

In using `synchronizer`, you can execute that code using synchronized decorator.

```js
var object = { name: "alice" };
var synchronized = synchronizer.create(object);

synchronized(function(object) {
  return asyncFuncNeedsExclusiveAccessControl(object.name + "!!");
}).then(function(res) {
  console.log("-->", res);
});

synchronized(function(object) {
  return asyncFuncNeedsExclusiveAccessControl(object.name + "??");
}).then(function(res) {
  console.log("-->", res);
});
```

## LICENSE
MIT

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