forked from pubkey/rxdb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrenderer.js
88 lines (77 loc) · 2.62 KB
/
renderer.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
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
const database = require('./database');
const renderTest = require('./test/render.test.js');
const { addRxPlugin } = require('rxdb');
const { RxDBReplicationCouchDBPlugin } = require('rxdb/plugins/replication-couchdb');
addRxPlugin(RxDBReplicationCouchDBPlugin);
const heroesList = document.querySelector('#heroes-list');
const syncURL = 'http://0.0.0.0:10102/db/heroes';
async function run() {
/**
* to check if rxdb works correctly, we run some integration-tests here
* if you want to use this electron-example as boilerplate, remove this line
*/
await renderTest();
const dbSuffix = await window.getDBSuffix();
const db = await database.createDatabase(
'heroesdb' + dbSuffix, // we add a random timestamp in dev-mode to reset the database on each start
'memory'
);
console.log('starting sync with ' + syncURL);
const syncState = await db.heroes.syncCouchDB({
remote: syncURL,
waitForLeadership: false,
direction: {
pull: true,
push: true
},
options: {
live: true
},
});
syncState.error$.subscribe(err => {
console.error('# Got replication error:');
console.dir(err);
console.trace(err);
/**
* We have to throw here, otherwise
* something might not work but the CI will not fail
* and instead time out after a very long time.
*/
throw err;
});
/**
* map the result of the find-query to the heroes-list in the dom
*/
db.heroes.find()
.sort({
name: 'asc'
})
.$.subscribe(function (heroes) {
if (!heroes) {
heroesList.innerHTML = 'Loading..';
return;
}
console.log('observable fired');
console.dir(heroes);
heroesList.innerHTML = heroes
.map(hero => {
return '<li>' +
'<div class="color-box" style="background:' + hero.color + '"></div>' +
'<div class="name" name="' + hero.name + '">' + hero.name + '</div>' +
'</li>';
})
.reduce((pre, cur) => pre += cur, '');
});
window.addHero = async function () {
const name = document.querySelector('input[name="name"]').value;
const color = document.querySelector('input[name="color"]').value;
const obj = {
name: name,
color: color
};
console.log('inserting hero:');
console.dir(obj);
db.heroes.insert(obj);
};
}
run();