Skip to content
This repository was archived by the owner on Sep 30, 2023. It is now read-only.

Commit 5686a3d

Browse files
authored
Merge pull request #87 from vaultec81/master
Add oplog events support
2 parents 6f0214b + f2e5bd9 commit 5686a3d

File tree

3 files changed

+102
-8
lines changed

3 files changed

+102
-8
lines changed

README.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,12 @@ console.log(db.replicationStatus)
128128
```javascript
129129
db.events.on('write', (id, hash, entry) => ... )
130130
```
131+
- `log.op.${operation}` - (entry)
132+
133+
Emitted after an entry was added to the database regardless of whether the entry is added remotely, or locally. `${operation}` is replaced with a specified oplog operation. `none` is specified to listen for a oplog entry without an operation specified. The supported operations are diagrammed in the entry payload.
134+
```javascript
135+
db.events.on('log.op.ADD', (id, hash, payload) => ... )
136+
```
131137

132138
#### Private methods
133139

src/Store.js

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,12 @@ class Store {
133133
} catch (e) {
134134
console.error('Store Error:', e)
135135
}
136+
this.events.on("replicated.progress", (address, hash, entry, progress, have) => {
137+
this._procEntry(entry);
138+
});
139+
this.events.on("write", (address, entry, heads) => {
140+
this._procEntry(entry);
141+
});
136142
}
137143

138144
get all () {
@@ -188,14 +194,9 @@ class Store {
188194
}
189195

190196
// Remove all event listeners
191-
this.events.removeAllListeners('load')
192-
this.events.removeAllListeners('load.progress')
193-
this.events.removeAllListeners('replicate')
194-
this.events.removeAllListeners('replicate.progress')
195-
this.events.removeAllListeners('replicated')
196-
this.events.removeAllListeners('ready')
197-
this.events.removeAllListeners('write')
198-
this.events.removeAllListeners('peer')
197+
for(var event in this.events._events) {
198+
this.events.removeAllListeners(event);
199+
}
199200

200201
// Database is now closed
201202
// TODO: afaik we don't use 'closed' event anymore,
@@ -528,6 +529,17 @@ class Store {
528529
_addOperationBatch (data, batchOperation, lastOperation, onProgressCallback) {
529530
throw new Error('Not implemented!')
530531
}
532+
533+
_procEntry(entry) {
534+
var { payload, hash } = entry;
535+
var { op } = payload;
536+
if(op) {
537+
this.events.emit(`log.op.${op}`, this.address.toString(), hash, payload);
538+
} else {
539+
this.events.emit(`log.op.none`, this.address.toString(), hash, payload);
540+
}
541+
this.events.emit('log.op', op, this.address.toString(), hash, payload)
542+
}
531543

532544
_onLoadProgress (hash, entry, progress, total) {
533545
this._recalculateReplicationStatus(progress, total)

test/events.spec.js

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
'use strict'
2+
3+
var assert = require('assert')
4+
const Store = require('../src/Store')
5+
6+
const Cache = require('orbit-db-cache')
7+
const Keystore = require('orbit-db-keystore')
8+
const IdentityProvider = require('orbit-db-identity-provider')
9+
const DefaultOptions = Store.DefaultOptions
10+
11+
// Test utils
12+
const {
13+
config,
14+
testAPIs,
15+
startIpfs,
16+
stopIpfs,
17+
implementations
18+
} = require('orbit-db-test-utils')
19+
20+
const properLevelModule = implementations.filter(i => i.key.indexOf('memdown') > -1).map(i => i.module)[0]
21+
const storage = require('orbit-db-storage-adapter')(properLevelModule)
22+
23+
Object.keys(testAPIs).forEach((IPFS) => {
24+
describe(`Events ${IPFS}`, function () {
25+
let ipfs, testIdentity, identityStore, store, cacheStore
26+
27+
this.timeout(config.timeout)
28+
29+
const ipfsConfig = Object.assign({}, config.defaultIpfsConfig, {
30+
repo: config.defaultIpfsConfig.repo + '-entry' + new Date().getTime()
31+
})
32+
after(async () => {
33+
await store.close()
34+
await stopIpfs(ipfs)
35+
await identityStore.close()
36+
await cacheStore.close()
37+
})
38+
39+
afterEach(async () => {
40+
await store.drop()
41+
await cacheStore.open()
42+
await identityStore.open()
43+
})
44+
before(async () => {
45+
identityStore = await storage.createStore('identity')
46+
const keystore = new Keystore(identityStore)
47+
48+
cacheStore = await storage.createStore('cache')
49+
const cache = new Cache(cacheStore)
50+
51+
testIdentity = await IdentityProvider.createIdentity({ id: 'userA', keystore })
52+
ipfs = await startIpfs(IPFS, ipfsConfig)
53+
54+
const address = 'test-address'
55+
const options = Object.assign({}, DefaultOptions, { cache })
56+
store = new Store(ipfs, testIdentity, address, options)
57+
})
58+
it('Specific log.op event', (done) => {
59+
var data = {
60+
op: "SET",
61+
key: "transaction",
62+
value: "data"
63+
}
64+
store.events.on("log.op.SET", (id, address, payload) => {
65+
var {op, key, value} = payload;
66+
assert.strictEqual(op, data.op);
67+
assert.strictEqual(key, data.key);
68+
assert.strictEqual(value, data.value);
69+
assert.strictEqual(id, 'test-address');
70+
done();
71+
})
72+
store._addOperation(data)
73+
74+
})
75+
})
76+
})

0 commit comments

Comments
 (0)