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-sync.html
107 lines (92 loc) · 3.13 KB
/
app-pouchdb-sync.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
102
103
104
105
106
107
<!--
@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-sync` arranges for one-directional or bi-directional
synchronization between two PouchDB databases. For one-directional
synchronization, it forwards to `PouchDB.replicate`, and for bi-directional
synchronization, it forwards to `PouchDB.sync`.
Here is an example of bi-directional synchronization between a local database
and a remote one:
```html
<app-pouchdb-sync
src="cats"
target="https://example.com:5678/cats"
bidirectional>
</app-pouchdb-sync>
```
For more information on PouchDB synchronization topics, please refer to the
documentation [here](https://pouchdb.com/guides/replication.html).
-->
<script>
(function() {
'use strict';
Polymer({
is: 'app-pouchdb-sync',
properties: {
/**
* The source to sync from. If this sync is `bidirectional`, then the
* `src` and `target` values are interchangeable.
*/
src: {type: String},
/**
* The `target` to sync to. If this sync is `bidirectional`, then the
* `src` and `target` values are interchangeable.
*/
target: {type: String},
/**
* While `false`, synchronization will only happen from the `src` to the
* `target`. One-directional synchronization follows the semantics of
* `PouchDB.replicate`. Set to `true` to make the sync bidirectional,
* which uses `PouchDB.sync` instead.
*/
bidirectional: {type: Boolean, value: false},
/**
* An event emitter that notifies of events in the synchronization
* process.
*/
sync: {
type: Object,
computed: '__computeSync(src, target, bidirectional)',
observer: '__syncChanged'
},
/**
* Set to true to log change events that are emitted by the `sync`
* instance.
*/
log: {type: Boolean, value: false}
},
__computeSync: function(src, target, bidirectional) {
var options = {live: true, retry: true};
return bidirectional ? PouchDB.sync(src, target, options) :
PouchDB.replicate(src, target, options);
},
__syncChanged: function(sync, oldSync) {
if (oldSync) {
oldSync.removeAllListeners();
oldSync.cancel();
}
if (sync) {
sync.on('change', this.__onSyncChange.bind(this));
sync.on('error', this.__onSyncError.bind(this));
}
},
__onSyncChange: function(change) {
if (this.log) {
console.log(change);
}
},
__onSyncError: function(error) {
console.error(error);
}
});
})();
</script>