mirror of https://gitlab.com/ecentrics/breeze
Ezerous
4 years ago
16 changed files with 513 additions and 585 deletions
@ -1,17 +1,17 @@ |
|||
{ |
|||
"name": "@ezerous/breeze", |
|||
"version": "0.1.1", |
|||
"version": "0.2.0", |
|||
"description": "A reactive data-store for OrbitDB.", |
|||
"license": "MIT", |
|||
"author": "Ezerous <ezerous@gmail.com>", |
|||
"main": "src/index.js", |
|||
"repository": "github:Ezerous/breeze", |
|||
"dependencies": { |
|||
"deepmerge": "4.2.2", |
|||
"ipfs": "0.50.1", |
|||
"is-plain-object": "4.1.1", |
|||
"orbit-db": "0.25.1", |
|||
"orbit-db-identity-provider": "0.3.1", |
|||
"redux-saga": "1.1.3" |
|||
"deepmerge": "~4.2.2", |
|||
"ipfs": "~0.50.2", |
|||
"is-plain-object": "~5.0.0", |
|||
"orbit-db": "~0.26.0", |
|||
"orbit-db-identity-provider": "~0.3.1", |
|||
"redux-saga": "~1.1.3" |
|||
} |
|||
} |
|||
|
@ -1,20 +1,33 @@ |
|||
import * as BreezeActions from './breezeActions' |
|||
import { STATUS_INITIALIZING, STATUS_INITIALIZED, STATUS_FAILED } from "../constants"; |
|||
import { BREEZE_INITIALIZING,BREEZE_INITIALIZED, BREEZE_FAILED } from "./breezeActions"; |
|||
|
|||
const initialState = { |
|||
initialized: false |
|||
status: STATUS_INITIALIZING |
|||
} |
|||
|
|||
const breezeStatusReducer = (state = initialState, action) => { |
|||
/* |
|||
* Breeze Status |
|||
*/ |
|||
if (action.type === BreezeActions.BREEZE_INITIALIZED) { |
|||
switch (action.type) { |
|||
case BREEZE_INITIALIZING: |
|||
return { |
|||
...state, |
|||
initialized: true |
|||
} |
|||
status: STATUS_INITIALIZING |
|||
}; |
|||
case BREEZE_INITIALIZED: |
|||
return { |
|||
...state, |
|||
status: STATUS_INITIALIZED |
|||
}; |
|||
case BREEZE_FAILED: |
|||
return { |
|||
...state, |
|||
status: STATUS_FAILED |
|||
}; |
|||
default: |
|||
return state; |
|||
} |
|||
return state |
|||
} |
|||
|
|||
export default breezeStatusReducer |
|||
|
@ -0,0 +1,4 @@ |
|||
// Status
|
|||
export const STATUS_INITIALIZING = 'initializing'; |
|||
export const STATUS_INITIALIZED = 'initialized'; |
|||
export const STATUS_FAILED = 'failed'; |
@ -1,18 +0,0 @@ |
|||
import {ORBIT_DATABASE_CREATED} from "./orbitActions"; |
|||
import {BREEZE_INITIALIZED} from "../breezeStatus/breezeActions"; |
|||
|
|||
export const orbitMiddleware = breezeInstance => () => next => action => { |
|||
const { type } = action |
|||
|
|||
if (type === BREEZE_INITIALIZED) |
|||
breezeInstance = action.breeze |
|||
|
|||
if (type === ORBIT_DATABASE_CREATED) { |
|||
const { database } = action; |
|||
breezeInstance.orbitDatabases[database.id] = database; |
|||
} |
|||
return next(action); |
|||
} |
|||
|
|||
const initializedMiddleware = orbitMiddleware(undefined) |
|||
export default initializedMiddleware |
@ -1,61 +0,0 @@ |
|||
import { call, put, take, takeEvery } from 'redux-saga/effects' |
|||
import {eventChannel} from "@redux-saga/core"; |
|||
|
|||
import { |
|||
ORBIT_DATABASE_CREATED, |
|||
ORBIT_DATABASE_LISTEN, |
|||
ORBIT_DATABASE_READY, |
|||
ORBIT_DATABASE_REPLICATED, |
|||
ORBIT_DATABASE_REPLICATING |
|||
} from './orbitActions'; |
|||
|
|||
/* |
|||
* Database Events |
|||
* See also https://redux-saga.js.org/docs/advanced/Channels.html
|
|||
*/ |
|||
function createOrbitDatabaseChannel (database){ |
|||
return eventChannel(emit => { |
|||
const onReady = () => { |
|||
emit({ type: ORBIT_DATABASE_READY, database }); |
|||
}; |
|||
const onReplicate = () => { |
|||
emit({ type: ORBIT_DATABASE_REPLICATING, database }); |
|||
}; |
|||
const onReplicated = () => { |
|||
emit({ type: ORBIT_DATABASE_REPLICATED, database }); |
|||
}; |
|||
|
|||
const eventListener = database.events |
|||
.on('ready', onReady) |
|||
.on('replicate', onReplicate) |
|||
.on('replicated', onReplicated) |
|||
|
|||
return () => { |
|||
eventListener.removeListener('ready',onReady) |
|||
eventListener.removeListener('replicate',onReplicate) |
|||
eventListener.removeListener('replicated',onReplicated) |
|||
}; |
|||
|
|||
}) |
|||
} |
|||
|
|||
export function * callListenForOrbitDatabaseEvent ({database}) { |
|||
const orbitDatabaseChannel = yield call(createOrbitDatabaseChannel, database) |
|||
yield put({type: ORBIT_DATABASE_LISTEN, id: database.id}); |
|||
|
|||
try { |
|||
while (true) { |
|||
let event = yield take(orbitDatabaseChannel); |
|||
yield put(event); |
|||
} |
|||
} finally { |
|||
orbitDatabaseChannel.close(); |
|||
} |
|||
} |
|||
|
|||
function * orbitStatusSaga () { |
|||
yield takeEvery(ORBIT_DATABASE_CREATED, callListenForOrbitDatabaseEvent); |
|||
} |
|||
|
|||
export default orbitStatusSaga |
|||
|
File diff suppressed because it is too large
Loading…
Reference in new issue