Skip to content

Commit

Permalink
Merge pull request #151 from Kinto/113-expose-sync-strategies
Browse files Browse the repository at this point in the history
Fixes #113 - Expose synchronization strategy constants.
  • Loading branch information
n1k0 committed Sep 8, 2015
2 parents 9036e8d + 8049611 commit 26888cf
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 12 deletions.
6 changes: 3 additions & 3 deletions docs/api.md
Original file line number Diff line number Diff line change
Expand Up @@ -238,9 +238,9 @@ Synopsis:
- The developer has to handle them manually using [`#resolve()`](#resolving-conflicts), and call `#sync()` again when done;
3. If everything went fine, publish local changes;
- Fail on any publication conflict detected;
* If `strategy` is set to `Collection.strategy.SERVER_WINS`, no client data will overwrite the remote data;
* If `strategy` is set to `Collection.strategy.CLIENT_WINS`, conflicting server records will be overwritten with local changes;
* If `strategy` is set to `Collection.strategy.MANUAL`, both incoming and outgoing conflicts will be reported in a dedicated array.
* If `strategy` is set to `Kinto.syncStrategy.SERVER_WINS`, no client data will overwrite the remote data;
* If `strategy` is set to `Kinto.syncStrategy.CLIENT_WINS`, conflicting server records will be overwritten with local changes;
* If `strategy` is set to `Kinto.syncStrategy.MANUAL`, both incoming and outgoing conflicts will be reported in a dedicated array.

```js
articles.sync()
Expand Down
31 changes: 24 additions & 7 deletions src/collection.js
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,15 @@ export default class Collection {
return this._lastModified;
}

/**
* Synchronization strategies. Available strategies are:
*
* - `MANUAL`: Conflicts will be reported in a dedicated array.
* - `SERVER_WINS`: Conflicts are resolved using remote data.
* - `CLIENT_WINS`: Conflicts are resolved using local data.
*
* @return {Object}
*/
static get strategy() {
return {
CLIENT_WINS: "client_wins",
Expand Down Expand Up @@ -506,13 +515,7 @@ export default class Collection {
*
* Options:
* - {Object} headers: HTTP headers to attach to outgoing requests.
* - {Collection.strategy} strategy: The synchronization strategy:
* * `Collection.strategy.SERVER_WINS`:
* No remote data override will be performed by the server.
* * `Collection.strategy.CLIENT_WINS`:
* Conflicting server records will be overriden with local changes.
* * `Collection.strategy.MANUAL`:
* Conflicts will be reported in a dedicated array.
* - {Collection.strategy} strategy: See `Collection.strategy`.
* - {Boolean} ignoreBackoff: Force synchronization even if server is currently
* backed off.
*
Expand All @@ -531,12 +534,26 @@ export default class Collection {
.then(_ => this.pullChanges(result, options))
.then(result => {
if (!result.ok) {
// if strategy is MANUAL
return result;
// else if strategy is CLIENT_WINS
// override incoming conflicts with local versions (ignore imports)
// and return sync() again
// else if strategy is SERVER_WINS
// override incoming conflicts with remote versions (force import all)
// and return sync() again
}
return this.pushChanges(result, options)
.then(result => {
if (!result.ok || result.published.length === 0) {
// if strategy is MANUAL
return result;
// else if strategy is CLIENT_WINS
// override outgoing conflicts with local versions (ignore imports)
// and return sync() again
// else if strategy is SERVER_WINS
// override outgoing conflicts with remote versions (force import all)
// and return sync() again
}
return this.pullChanges(result, options);
});
Expand Down
13 changes: 13 additions & 0 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,19 @@ export default class Kinto {
};
}

/**
* Synchronization strategies. Available strategies are:
*
* - `MANUAL`: Conflicts will be reported in a dedicated array.
* - `SERVER_WINS`: Conflicts are resolved using remote data.
* - `CLIENT_WINS`: Conflicts are resolved using local data.
*
* @return {Object}
*/
static get syncStrategy() {
return Collection.strategy;
}

/**
* Creates a remote transformer constructor, the ES5 way.
*
Expand Down
11 changes: 9 additions & 2 deletions test/index_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import sinon from "sinon";
import { EventEmitter } from "events";
import { SUPPORTED_PROTOCOL_VERSION as SPV } from "../src/api";

import Collection from "../src/collection";
import BaseAdapter from "../src/adapters/base";
import LocalStorage from "../src/adapters/LocalStorage";
import IDB from "../src/adapters/IDB";
Expand Down Expand Up @@ -56,14 +57,20 @@ describe("Kinto", () => {
});

describe("get transformers()", () => {
it("should provide an transformers static getter", () => {
it("should provide a transformers static getter", () => {
expect(Kinto.transformers).to.be.an("object");
});

it("should provide an transformers.BaseAdapter getter", () => {
it("should provide a transformers.BaseAdapter getter", () => {
expect(Kinto.transformers.RemoteTransformer).to.eql(RemoteTransformer);
});
});

describe("get syncStrategy()", () => {
it("should provide a syncStrategy static getter", () => {
expect(Kinto.syncStrategy).eql(Collection.strategy);
});
});
});

describe("static methods", () => {
Expand Down

0 comments on commit 26888cf

Please sign in to comment.