Browse Source

Listen to Orbit write events

develop
Ezerous 4 years ago
parent
commit
e90db19f2e
  1. 4
      package.json
  2. 1
      src/orbit/orbitActions.js
  3. 1
      src/orbit/orbitConstants.js
  4. 20
      src/orbit/orbitReducer.js
  5. 9
      src/orbit/orbitSaga.js
  6. 2386
      yarn.lock

4
package.json

@ -1,6 +1,6 @@
{
"name": "@ezerous/breeze",
"version": "0.2.0",
"version": "0.3.0",
"description": "A reactive data-store for OrbitDB.",
"license": "MIT",
"author": "Ezerous <ezerous@gmail.com>",
@ -8,7 +8,7 @@
"repository": "github:Ezerous/breeze",
"dependencies": {
"deepmerge": "~4.2.2",
"ipfs": "~0.50.2",
"ipfs": "~0.51.0",
"is-plain-object": "~5.0.0",
"orbit-db": "~0.26.0",
"orbit-db-identity-provider": "~0.3.1",

1
src/orbit/orbitActions.js

@ -18,6 +18,7 @@ export const ORBIT_DATABASE_LISTEN = 'ORBIT_DATABASE_LISTEN';
export const ORBIT_DATABASE_READY = 'ORBIT_DATABASE_READY';
export const ORBIT_DATABASE_REPLICATING = 'ORBIT_DATABASE_REPLICATING';
export const ORBIT_DATABASE_REPLICATED = 'ORBIT_DATABASE_REPLICATED';
export const ORBIT_DATABASE_WRITE = 'ORBIT_DATABASE_WRITE';
export function orbitInit (breeze, id) {
return {

1
src/orbit/orbitConstants.js

@ -10,3 +10,4 @@ export const DB_STATUS_INIT = 'init';
export const DB_STATUS_READY = 'ready';
export const DB_STATUS_REPLICATING = 'replicating';
export const DB_STATUS_REPLICATED = 'replicated';
export const DB_STATUS_WRITTEN = 'written';

20
src/orbit/orbitReducer.js

@ -5,7 +5,8 @@ import {
ORBIT_DATABASE_REPLICATING,
ORBIT_INITIALIZING,
ORBIT_INITIALIZED,
ORBIT_INIT_FAILED
ORBIT_INIT_FAILED,
ORBIT_DATABASE_WRITE
} from "./orbitActions";
import {
@ -13,6 +14,7 @@ import {
DB_STATUS_READY,
DB_STATUS_REPLICATED,
DB_STATUS_REPLICATING,
DB_STATUS_WRITTEN
} from "./orbitConstants";
import {STATUS_INITIALIZING, STATUS_INITIALIZED, STATUS_FAILED } from "../constants";
@ -47,24 +49,32 @@ const orbitReducer = (state = initialState, action) => {
return newDatabasesStatus(state, action, DB_STATUS_REPLICATING);
case ORBIT_DATABASE_REPLICATED:
return newDatabasesStatus(state, action, DB_STATUS_REPLICATED);
case ORBIT_DATABASE_WRITE:
return newDatabasesStatus(state, action, DB_STATUS_WRITTEN);
default:
return state;
}
};
function newDatabasesStatus (state, action, status) {
const { timestamp, database: {id} } = action;
// Previous values, if exist
const lastReplication = state.databases[id] ? state.databases[id].lastReplication : null;
const lastWrite = state.databases[id] ? state.databases[id].lastWrite : null;
return {
...state,
databases:{
...state.databases,
[action.database.id]: {
...state[action.database.id],
[id]: {
...state.databases[id],
status,
timestamp: action.timestamp
timestamp,
lastReplication: status === DB_STATUS_REPLICATED ? timestamp : lastReplication,
lastWrite: status === DB_STATUS_WRITTEN ? timestamp : lastWrite,
}
}
}
}
export default orbitReducer;

9
src/orbit/orbitSaga.js

@ -11,6 +11,7 @@ import {
ORBIT_DATABASE_READY,
ORBIT_DATABASE_REPLICATED,
ORBIT_DATABASE_REPLICATING,
ORBIT_DATABASE_WRITE,
ORBIT_IDENTITY_PROVIDER_ADD,
ORBIT_IDENTITY_PROVIDER_ADDED,
ORBIT_IDENTITY_PROVIDER_FAILED,
@ -70,7 +71,8 @@ function * initOrbit(action) {
}
/*
* Creates an orbit database given a name and a type as its parameters
* Creates and loads an orbit database given a name and a type as its parameters
* Note: db.name can also be an OrbitDB address
*/
function * createDatabase({ orbit, db }) {
try {
@ -112,16 +114,21 @@ function createOrbitDatabaseChannel (database){
const onReplicated = () => {
emit({ type: ORBIT_DATABASE_REPLICATED, database, timestamp: +new Date });
};
const onWrite = (address, entry) => {
emit({ type: ORBIT_DATABASE_WRITE, database, entry, timestamp: +new Date });
};
const eventListener = database.events
.once('ready', onReady)
.on('replicate', onReplicate)
.on('replicated', onReplicated)
.on('write', onWrite)
return () => {
eventListener.removeListener('ready',onReady)
eventListener.removeListener('replicate',onReplicate)
eventListener.removeListener('replicated',onReplicated)
eventListener.removeListener('write',onWrite)
};
})

2386
yarn.lock

File diff suppressed because it is too large
Loading…
Cancel
Save