diff --git a/src/Breeze.js b/src/Breeze.js index c62861f..585080e 100644 --- a/src/Breeze.js +++ b/src/Breeze.js @@ -2,6 +2,7 @@ import { BREEZE_INITIALIZING } from "./breezeStatus/breezeActions" import defaultOptions from "./misc/defaultOptions"; import merge from "./misc/mergeUtils" import {addOrbitDB, removeOrbitDB, orbitInit} from "./orbit/orbitActions"; +import {STATUS_INITIALIZED} from "./constants"; // Load as promise so that async Breeze initialization can still resolve const isEnvReadyPromise = new Promise((resolve) => { @@ -37,8 +38,20 @@ class Breeze { }) } - initOrbit(id) { - this.store.dispatch(orbitInit(this, id)); + async initOrbit(id) { + const { store, orbit } = this; + store.dispatch(orbitInit(this, id)); + + return new Promise(resolve => { + const unsubscribe = store.subscribe(() => { + const status = store.getState().orbit.status; + + if (status === STATUS_INITIALIZED) { + unsubscribe(); + return resolve(orbit); + } + }) + }); } // dbInfo = {address, type} (where address can also be a name) diff --git a/src/breezeStatus/breezeStatusReducer.js b/src/breezeStatus/breezeStatusReducer.js index 811504e..8f2cc99 100644 --- a/src/breezeStatus/breezeStatusReducer.js +++ b/src/breezeStatus/breezeStatusReducer.js @@ -1,8 +1,8 @@ -import { STATUS_INITIALIZING, STATUS_INITIALIZED, STATUS_FAILED } from "../constants"; +import { STATUS_UNINITIALIZED, STATUS_INITIALIZING, STATUS_INITIALIZED, STATUS_FAILED } from "../constants"; import { BREEZE_INITIALIZING,BREEZE_INITIALIZED, BREEZE_FAILED } from "./breezeActions"; const initialState = { - status: STATUS_INITIALIZING + status: STATUS_UNINITIALIZED } const breezeStatusReducer = (state = initialState, action) => { diff --git a/src/constants.js b/src/constants.js index 922ed3a..db2a52f 100644 --- a/src/constants.js +++ b/src/constants.js @@ -1,4 +1,5 @@ // Status +export const STATUS_UNINITIALIZED = 'uninitialized'; export const STATUS_INITIALIZING = 'initializing'; export const STATUS_INITIALIZED = 'initialized'; export const STATUS_FAILED = 'failed'; diff --git a/src/index.js b/src/index.js index d4e7167..4a2ebc2 100644 --- a/src/index.js +++ b/src/index.js @@ -7,6 +7,7 @@ import breezeStatusSaga from './breezeStatus/breezeStatusSaga'; import orbitSaga from "./orbit/orbitSaga"; import * as BreezeActions from './breezeStatus/breezeActions' +import * as IpfsActions from './ipfs/ipfsActions' import * as OrbitActions from './orbit/orbitActions' import * as breezeConstants from './constants' @@ -27,7 +28,8 @@ const breezeSagas = [ const breezeActions = { breeze: BreezeActions, - orbit: OrbitActions + orbit: OrbitActions, + ipfs: IpfsActions } export { diff --git a/src/ipfs/ipfsReducer.js b/src/ipfs/ipfsReducer.js index 6357f02..d34f69f 100644 --- a/src/ipfs/ipfsReducer.js +++ b/src/ipfs/ipfsReducer.js @@ -1,8 +1,8 @@ import { IPFS_INITIALIZING , IPFS_INITIALIZED, IPFS_FAILED } from "./ipfsActions"; -import { STATUS_INITIALIZING, STATUS_INITIALIZED, STATUS_FAILED } from "../constants"; +import { STATUS_UNINITIALIZED, STATUS_INITIALIZING, STATUS_INITIALIZED, STATUS_FAILED } from "../constants"; const initialState = { - status: STATUS_INITIALIZING + status: STATUS_UNINITIALIZED }; const ipfsReducer = (state = initialState, action) => { diff --git a/src/ipfs/ipfsSaga.js b/src/ipfs/ipfsSaga.js index c2251d8..9338edc 100644 --- a/src/ipfs/ipfsSaga.js +++ b/src/ipfs/ipfsSaga.js @@ -15,7 +15,7 @@ export function * initializeIPFS (ipfsOptions) { // Initialize IPFS const ipfs = yield call(IPFS.create, ipfsOptions); - yield put({ type: IpfsActions.IPFS_INITIALIZED }); + yield put({ type: IpfsActions.IPFS_INITIALIZED, ipfs }); return ipfs; } catch (error) { diff --git a/src/orbit/orbitReducer.js b/src/orbit/orbitReducer.js index 8797364..7d2a125 100644 --- a/src/orbit/orbitReducer.js +++ b/src/orbit/orbitReducer.js @@ -19,10 +19,10 @@ import { DB_STATUS_REMOVED } from "./orbitConstants"; -import {STATUS_INITIALIZING, STATUS_INITIALIZED, STATUS_FAILED } from "../constants"; +import { STATUS_UNINITIALIZED, STATUS_INITIALIZING, STATUS_INITIALIZED, STATUS_FAILED } from "../constants"; const initialState = { - status: STATUS_INITIALIZING, + status: STATUS_UNINITIALIZED, databases: {} };