This repository has been archived by the owner on Sep 7, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 27
/
Copy pathapp-pouchdb-conflict-resolution.html
101 lines (87 loc) · 3.41 KB
/
app-pouchdb-conflict-resolution.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
<!--
@license
Copyright (c) 2016 The Polymer Project Authors. All rights reserved.
This code may only be used under the BSD style license found at http://polymer.github.io/LICENSE.txt
The complete set of authors may be found at http://polymer.github.io/AUTHORS.txt
The complete set of contributors may be found at http://polymer.github.io/CONTRIBUTORS.txt
Code distributed by Google as part of the polymer project is also
subject to an additional IP rights grant found at http://polymer.github.io/PATENTS.txt
-->
<link rel="import" href="../polymer/polymer.html">
<link rel="import" href="pouchdb.html">
<!--
`app-pouchdb-conflict-resolution` enables declarative configuration of conflict
resolution strategies ordered by logical relationships in the DOM. Currently
two basic strategies are supported: `firstWriteWins` and `lastWriteWins`.
To use `app-pouchdb-conflict-resolution`, simply include the element somewhere
in a document subtree at or above the ShadowRoot of an `app-pouchdb-document`
or `app-pouchdb-query`:
```html
<app-pouchdb-conflict-resolution
strategy="lastWriteWins">
</app-pouchdb-conflict-resolution>
<app-pouchdb-document
db-name="cats"
doc-id="parsnip">
</app-pouchdb-document>
```
When a conflict occurs, the `app-pouchdb-document` will request fire an event
to notify of the conflict and request a resolution strategy. The
`app-pouchdb-conflict-resolution` element listens at its nearest ShadowRoot
boundary for conflict notifications, and responds to them as needed with a
configured strategy.
-->
<script>
(function() {
'use strict';
Polymer({
is: 'app-pouchdb-conflict-resolution',
properties: {
/**
* The name of the strategy to use to reslve the conflict. Supported
* strategies are `firstWriteWins` (the default) and `lastWriteWins`.
*/
strategy: {type: String, value: 'firstWriteWins'},
/**
* By default, this element stops propagation of any conflict events
* that it is able to handle. If set to `true`, the element will allow
* such events to continue propagating, opening the possibility that
* another conflict resolution strategy higher up the document tree will
* superseed this one.
*/
allowAncestralResolution: {type: Boolean, value: false}
},
created: function() {
this._eventTarget = null;
},
attached: function() {
this._eventTarget = Polymer.dom(this).host || document;
this.listen(this._eventTarget, 'app-pouchdb-conflict', 'resolveConflict');
},
detached: function() {
this.unlisten(this._eventTarget, 'app-pouchdb-conflict', 'resolveConflict');
},
resolveConflict: function(event) {
if (!this.strategy || !this[this.strategy]) {
return Promise.reject('No resolution strategy available.');
}
event.detail.resolveConflict(this[this.strategy].bind(this));
if (!this.allowAncestralResolution) {
event.stopImmediatePropagation();
}
},
firstWriteWins: function(db, method, doc, error) {
return db.get(doc._id).then(function(canonicalDoc) {
return {id: canonicalDoc._id, rev: canonicalDoc._rev};
});
return Promise.resolve();
},
lastWriteWins: function(db, method, doc, error) {
return db.get(doc._id, {conflicts: true}).then(function(canonicalDoc) {
doc._rev = canonicalDoc._rev;
return db.put(doc);
}.bind(this));
}
});
})();
</script>