# ampersand-optimistic-sync

> A sync wrapper for Backbone.sync and ampersand-sync that implements optimistic concurrency in HTTP.

Latest version **3.1.1** (published 2015-08-27) · MIT license · 0 weekly downloads

## Install

```sh
npm install ampersand-optimistic-sync
pnpm add ampersand-optimistic-sync
yarn add ampersand-optimistic-sync
bun add ampersand-optimistic-sync
```

## Health

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

Positive: no vulnerabilities.

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

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 3.1.1 |
| Published | 2015-08-27 |
| First published | 2014-09-11 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 1 |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 1 |
| Author | Aaron McCall |
| Maintainers | aaronmccall |
| Keywords | optimistic concurrency, sync, REST, last-modified, if-unmodified-since, etag, if-match, ampersand, backbone |

## Links

- npm: https://www.npmjs.com/package/ampersand-optimistic-sync
- Repository: https://github.com/aaronmccall/ampersand-optimistic-sync
- Issues: https://github.com/aaronmccall/ampersand-optimistic-sync/issues
- npm.io page: https://npm.io/package/ampersand-optimistic-sync

## Dependencies (1)

- [kisslog](https://npm.io/package/kisslog.md) ^2.0.0

## Alternatives

- [promise-throttle](https://npm.io/package/promise-throttle.md) — 44.2K weekly downloads
- [@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

## Recent versions

- 3.1.1 (latest) — 2015-08-27
- 3.1.0 — 2015-08-27
- 3.0.0 — 2014-11-24
- 2.0.0 — 2014-11-12
- 1.2.0 — 2014-10-22
- 1.1.1 — 2014-09-26
- 1.1.0 — 2014-09-26
- 1.0.1 — 2014-09-24
- 1.0.0 — 2014-09-17
- 0.3.0 — 2014-09-11

## README

ampersand-optimistic-sync
=============

A wrapper for Backbone.sync and ampersand-sync that implements optimistic concurrency over HTTP.

Not sure what optimistic concurrency is? [You can read a bit more about it and implementing it in HTTP here.][1]

## How do I use it?

### Default Behavior

For the default behavior, just extend like so:

```javascript
var Model = require('ampersand-model'); // or require('backbone').Model;
var syncMixin = require('ampersand-optimistic-sync');

module.exports = Model.extend(syncMixin(Model));

```

Now your model will check for the ETag header on every request to the server.

If it exists, the ETag will be set on the model as _version and a sync:version (args: model, version) event will fire.

On updates, sync will now automatically include the _version value as an If-Match header.

If the update is successful, the new ETag that the server sends will be set as _version.

If the update request returns a 412 error, a `sync:invalid-version (args: model, version, data)` event is fired.

### Customizing Behavior

The mixin takes an optional second config argument that can be used to modify behavior.


#### config.type

Allowed values; etag (default), last-modified

When config.type is changed to last-modified like so:

```javascript
var Model = require('ampersand-model'); // or require('backbone').Model;
var syncMixin = require('ampersand-optimistic-sync');

module.exports = Model.extend(syncMixin(Model, {type: 'last-modified'}));

```

The model will now store Last-Modified header values as _version and send _version as an If-Unmodified-Since header.

#### config.invalidHandler

If config.invalidHandler is a function, it will be registered as a `sync:invalid-version` handler before your first update request is sent.

[1]: http://looselyconnected.wordpress.com/2010/03/25/the-http-etag-header-and-optimistic-locking-in-rest/ (The HTTP ETag header and optimistic locking in REST)

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