Skip to content

Commit

Permalink
Add PoucDbCluster and PouchDbClusterSyncElement
Browse files Browse the repository at this point in the history
  • Loading branch information
rjcorwin committed Sep 24, 2019
1 parent 1417f12 commit 0b364de
Show file tree
Hide file tree
Showing 10 changed files with 299 additions and 0 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
/node_modules/
package-lock.json
33 changes: 33 additions & 0 deletions demo/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, minimum-scale=1, initial-scale=1, user-scalable=yes">

<title>pouchdb-cluster-sync demo</title>

<script src="../node_modules/@webcomponents/webcomponentsjs/webcomponents-loader.js"></script>

<script type="module">
import '@polymer/iron-demo-helpers/demo-pages-shared-styles';
import '@polymer/iron-demo-helpers/demo-snippet';
</script>

<script type="module" src="../pouchdb-cluster-sync.js"></script>

<custom-style>
<style is="custom-style" include="demo-pages-shared-styles">
</style>
</custom-style>
</head>
<body>
<div class="vertical-section-container centered">
<h3>Basic pouchdb-cluster-sync demo</h3>
<demo-snippet>
<template>
<pouchdb-cluster-sync></pouchdb-cluster-sync>
</template>
</demo-snippet>
</div>
</body>
</html>
15 changes: 15 additions & 0 deletions index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<meta http-equiv="refresh" content="0;url=demo/" />
<title>pouchdb-cluster-sync</title>
</head>
<body>
<!--
Visit demo/index.html to see live examples of your element running.
This page will automatically redirect you there when run in the browser
with `polymer serve`.
-->
</body>
</html>
17 changes: 17 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
{
"name": "pouchdb-cluster",
"main": "pouchdb-cluster.js",
"dependencies": {
"@polymer/polymer": "^3.0.0",
"event-emitter-mixin": "^0.3.8",
"uuid": "^3.3.3"
},
"devDependencies": {
"@polymer/iron-demo-helpers": "^3.1.0",
"@polymer/test-fixture": "^4.0.2",
"@webcomponents/webcomponentsjs": "^2.0.0",
"chai": "^4.2.0",
"mocha": "^6.1.4",
"wct-mocha": "^1.0.1"
}
}
8 changes: 8 additions & 0 deletions polymer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"npm": true,
"lint": {
"rules": [
"polymer-3"
]
}
}
32 changes: 32 additions & 0 deletions pouchdb-cluster-sync-element.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import {html, PolymerElement} from '@polymer/polymer/polymer-element.js';

/**
* `pouchdb-cluster-sync`
*
*
* @customElement
* @polymer
* @demo demo/index.html
*/
class PouchdbClusterSyncElement extends PolymerElement {
static get template() {
return html`
<style>
:host {
display: block;
}
</style>
<h2>Hello [[prop1]]!</h2>
`;
}
static get properties() {
return {
prop1: {
type: String,
value: 'pouchdb-cluster-sync',
},
};
}
}

window.customElements.define('pouchdb-cluster-sync', PouchdbClusterSyncElement);
20 changes: 20 additions & 0 deletions pouchdb-cluster.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import { uuidv4 } from 'uuid'

/**
* `pouchdb-cluster`
*
* Multi-master replication for PouchDB
*
* @demo demo/index.html
*/
class PouchDbCluster {

constuctor(PouchDB, network, nickname, id) {
super()
this.PouchDB = PouchDB
this.network = network
this.nickname = nickname ? nickname : uuidv4()
this.id = id ? id : uuidv4()
}

}
19 changes: 19 additions & 0 deletions test/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, minimum-scale=1.0, initial-scale=1.0, user-scalable=yes">

<script src="../node_modules/mocha/mocha.js"></script>
<script src="../node_modules/wct-mocha/wct-mocha.js"></script>
</head>
<body>
<script>
// Load and run all tests (.html, .js):
WCT.loadSuites([
'pouchdb-cluster-sync_test.html',
'pouchdb-cluster-sync-element_test.html'
]);
</script>
</body>
</html>
55 changes: 55 additions & 0 deletions test/pouchdb-cluster-sync-element_test.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, minimum-scale=1, initial-scale=1, user-scalable=yes">

<title>pouchdb-cluster-sync-element test</title>

<script src="../node_modules/@webcomponents/webcomponentsjs/webcomponents-loader.js"></script>
<script src="../node_modules/@polymer/test-fixture/test-fixture.js"></script>
<script src="../node_modules/mocha/mocha.js"></script>
<script src="../node_modules/chai/chai.js"></script>
<script src="../node_modules/wct-mocha/wct-mocha.js"></script>

<script type="module" src="../pouchdb-cluster-sync-element.js"></script>
</head>
<body>

<test-fixture id="BasicTestFixture">
<template>
<pouchdb-cluster-sync-element></pouchdb-cluster-sync-element>
</template>
</test-fixture>

<test-fixture id="ChangedPropertyTestFixture">
<template>
<pouchdb-cluster-sync-element prop1="new-prop1"></pouchdb-cluster-sync-element>
</template>
</test-fixture>

<script type="module">
suite('pouchdb-cluster-sync-element', () => {

test('instantiating the element with default properties works', () => {
const element = fixture('BasicTestFixture');
assert.equal(element.prop1, 'pouchdb-cluster-sync-element');
const elementShadowRoot = element.shadowRoot;
const elementHeader = elementShadowRoot.querySelector('h2');
assert.equal(elementHeader.innerHTML, 'Hello pouchdb-cluster-sync-element!');
});

test('setting a property on the element works', () => {
// Create a test fixture
const element = fixture('ChangedPropertyTestFixture');
assert.equal(element.prop1, 'new-prop1');
const elementShadowRoot = element.shadowRoot;
const elementHeader = elementShadowRoot.querySelector('h2');
assert.equal(elementHeader.innerHTML, 'Hello new-prop1!');
});

});
</script>

</body>
</html>
98 changes: 98 additions & 0 deletions test/pouchdb-cluster-sync_test.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, minimum-scale=1, initial-scale=1, user-scalable=yes">

<title>pouchdb-cluster-sync test</title>

<script src="../node_modules/@webcomponents/webcomponentsjs/webcomponents-loader.js"></script>
<script src="../node_modules/@polymer/test-fixture/test-fixture.js"></script>
<script src="../node_modules/mocha/mocha.js"></script>
<script src="../node_modules/chai/chai.js"></script>
<script src="../node_modules/wct-mocha/wct-mocha.js"></script>

</head>
<body>

<script type="module">

import { uuidv4 } from 'uuid'
import EventEmitterMixin from 'event-emitter-mixin'

class SimulationNetworkInterface extends EventEmitterMixin() {

constructor(id) {
super()
if (!window.simulationNetwork) {
window.simulationNetwork = {
nodes: []
}
}
this.id = id ? id : uuidv4()
}

async join() {
window.simulationNetwork.nodes.push({ id: this.id })
}

async getNodes() {
return window.simulationNetwork.nodes
}

}

suite('pouchdb-cluster-sync', () => {

test('instantiating the class works', () => {
const network = new SimulationNetworkInterface()
const cpSync = new PouchDbCluster(PouchDB, network)
});

test('joins a cluster', async () => {
const TEST_CONTENT_ID = 'TEST_CONTENT_ID'
const NODE_1_DB_NAME = 'NODE_1_DB_NAME'
const NODE_1_ID = 'NODE_1_ID'
const NODE_1_NICK = 'NODE_1_NICK'
const NODE_2_DB_NAME = 'NODE_2_DB_NAME'
const NODE_2_ID = 'NODE_2_ID'
const NODE_2_NICK = 'NODE_2_NICK'
const NODE_3_DB_NAME = 'NODE_3_DB_NAME'
const NODE_3_ID = 'NODE_3_ID'
const NODE_3_NICK = 'NODE_3_NICK'
const node1NetworkInterface = new SimulationNetworkInterface()
const node2NetworkInterface = new SimulationNetworkInterface()
const node3NetworkInterface = new SimulationNetworkInterface()
const node1CpSync = new PouchDbCluster(PouchDB, node1NetworkInterface, NODE_1_ID, NODE_1_NICK)
const node2CpSync = new PouchDbCluster(PouchDB, node2NetworkInterface, NODE_2_ID, NODE_2_NICK)
const node3CpSync = new PouchDbCluster(PouchDB, node3NetworkInterface, NODE_3_ID, NODE_3_NICK)
await node1NetworkInterface.join()
await node2NetworkInterface.join()
await node2NetworkInterface.join()
cpSync1.on('new-node', (node) => {
console.log(node)
})
cpSync2.on('new-node', (node) => {
console.log(node)
})
cpSync3.on('new-node', (node) => {
console.log(node)
})
cpSync1.advertise(TEST_CONTENT_ID, NODE_1_DB_NAME)
cpSync2.advertise(TEST_CONTENT_ID, NODE_2_DB_NAME)
cpSync3.advertise(TEST_CONTENT_ID, NODE_3_DB_NAME)
await cpSync1.sync()
// ...
})
test('is notified of another node in the cluster')
test('divergent clusters merge')
test('notifies nodes that I am consensus maker')
test('is notified of consensus maker')



});
</script>

</body>
</html>

0 comments on commit 0b364de

Please sign in to comment.