-
Notifications
You must be signed in to change notification settings - Fork 6
/
pouch-resolve-conflicts.js
52 lines (45 loc) · 1.63 KB
/
pouch-resolve-conflicts.js
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
(function e(t,n,r){function s(o,u){if(!n[o]){if(!t[o]){var a=typeof require=="function"&&require;if(!u&&a)return a(o,!0);if(i)return i(o,!0);var f=new Error("Cannot find module '"+o+"'");throw f.code="MODULE_NOT_FOUND",f}var l=n[o]={exports:{}};t[o][0].call(l.exports,function(e){var n=t[o][1][e];return s(n?n:e)},l,l.exports,e,t,n,r)}return n[o].exports}var i=typeof require=="function"&&require;for(var o=0;o<r.length;o++)s(r[o]);return s})({1:[function(require,module,exports){
exports.resolveConflicts = function(doc, resolveFun) {
var db = this
return db
.get(doc._id, {
open_revs: doc._conflicts
})
.then(function(responses) {
return responses
.filter(function(response) {
return 'ok' in response
})
.map(function(response) {
return response.ok
})
})
.then(function(conflicts) {
return conflicts.concat(doc)
})
.then(function(docs) {
var wDocs = docs.slice()
var winning = wDocs.reduce(function(winning, doc) {
return winning && resolveFun(doc, winning)
}, wDocs.pop())
if (!winning) throw({
error: 'conflict_resolution_failed',
reason: 'The conflict could not be resolve, resolveFun did not return a doc'
})
return docs.map(function(doc) {
if (doc._rev === winning._rev) return winning
return {
_id: doc._id,
_rev: doc._rev,
_deleted: true
}
})
})
.then(function(docs) {
return db.bulkDocs(docs)
})
}
if (typeof window !== 'undefined' && window.PouchDB) {
window.PouchDB.plugin(module.exports)
}
},{}]},{},[1]);