mirror of https://gitlab.com/ecentrics/breeze
Ezerous
4 years ago
8 changed files with 1336 additions and 1360 deletions
@ -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, timestamp: +new Date }); |
|
||||
}; |
|
||||
const onReplicate = () => { |
|
||||
emit({ type: ORBIT_DATABASE_REPLICATING, database, timestamp: +new Date }); |
|
||||
}; |
|
||||
const onReplicated = () => { |
|
||||
emit({ type: ORBIT_DATABASE_REPLICATED, database, timestamp: +new Date }); |
|
||||
}; |
|
||||
|
|
||||
const eventListener = database.events |
|
||||
.once('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