2016-07-16 02:06:10 +00:00
|
|
|
import {bindActionCreators} from 'redux';
|
|
|
|
import * as entitiesActions from './modules/entities';
|
2016-11-07 20:24:38 +00:00
|
|
|
import * as globalActions from './modules/global';
|
2016-11-10 05:56:23 +00:00
|
|
|
import * as db from '../../common/database';
|
|
|
|
import * as models from '../../models';
|
2016-11-10 02:40:53 +00:00
|
|
|
import * as fetch from '../../common/fetch';
|
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-11-07 20:24:38 +00:00
|
|
|
const global = bindActionCreators(globalActions, dispatch);
|
2016-09-22 03:32:12 +00:00
|
|
|
const handleNewChanges = entities.addChanges;
|
2016-11-07 20:24:38 +00:00
|
|
|
const handleCommand = global.newCommand;
|
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-11-07 20:24:38 +00:00
|
|
|
fetch.offCommand(handleCommand);
|
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 = [
|
2016-11-10 01:15:27 +00:00
|
|
|
await models.settings.getOrCreate(),
|
|
|
|
await models.workspace.all(),
|
|
|
|
await models.environment.all(),
|
|
|
|
await models.cookieJar.all(),
|
|
|
|
await models.requestGroup.all(),
|
|
|
|
await models.request.all()
|
2016-10-02 20:57:00 +00:00
|
|
|
];
|
|
|
|
|
|
|
|
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-11-07 20:24:38 +00:00
|
|
|
fetch.onCommand(handleCommand);
|
2016-04-26 07:29:24 +00:00
|
|
|
}
|