2016-07-16 02:06:10 +00:00
|
|
|
import {bindActionCreators} from 'redux';
|
|
|
|
import * as entitiesActions from './modules/entities';
|
2016-10-02 20:57:00 +00:00
|
|
|
import * as db from '../../backend/database';
|
2016-04-26 07:29:24 +00:00
|
|
|
|
2016-10-02 20:57:00 +00:00
|
|
|
export async function initStore (dispatch) {
|
2016-04-26 07:29:24 +00:00
|
|
|
const entities = bindActionCreators(entitiesActions, dispatch);
|
2016-09-22 03:32:12 +00:00
|
|
|
const handleNewChanges = entities.addChanges;
|
2016-04-26 07:29:24 +00:00
|
|
|
|
|
|
|
console.log('-- Restoring Store --');
|
|
|
|
|
|
|
|
const start = Date.now();
|
2016-10-02 20:57:00 +00:00
|
|
|
|
2016-09-22 03:32:12 +00:00
|
|
|
db.offChange(handleNewChanges);
|
2016-04-26 07:29:24 +00:00
|
|
|
|
2016-04-27 03:17:05 +00:00
|
|
|
// Restore docs in parent->child->grandchild order
|
2016-10-02 20:57:00 +00:00
|
|
|
const results = [
|
|
|
|
await db.settings.getOrCreate(),
|
|
|
|
await db.workspace.all(),
|
|
|
|
await db.environment.all(),
|
|
|
|
await db.cookieJar.all(),
|
|
|
|
await db.requestGroup.all(),
|
|
|
|
await db.request.all()
|
|
|
|
];
|
|
|
|
|
|
|
|
for (let docs of results) {
|
|
|
|
docs = Array.isArray(docs) ? docs : [docs];
|
|
|
|
handleNewChanges(docs.map(doc => [db.CHANGE_UPDATE, doc]));
|
|
|
|
}
|
2016-08-15 17:04:36 +00:00
|
|
|
|
2016-10-02 20:57:00 +00:00
|
|
|
console.log(`-- Restored DB in ${(Date.now() - start) / 1000} s --`);
|
|
|
|
db.onChange(handleNewChanges);
|
2016-04-26 07:29:24 +00:00
|
|
|
}
|