insomnia/app/ui/redux/initstore.js

40 lines
1.2 KiB
JavaScript
Raw Normal View History

import {bindActionCreators} from 'redux';
import * as entitiesActions from './modules/entities';
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';
export async function initStore (dispatch) {
const entities = bindActionCreators(entitiesActions, dispatch);
const global = bindActionCreators(globalActions, dispatch);
const handleNewChanges = entities.addChanges;
const handleCommand = global.newCommand;
console.log('-- Restoring Store --');
const start = Date.now();
db.offChange(handleNewChanges);
fetch.offCommand(handleCommand);
// Restore docs in parent->child->grandchild order
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()
];
for (let docs of results) {
docs = Array.isArray(docs) ? docs : [docs];
handleNewChanges(docs.map(doc => [db.CHANGE_UPDATE, doc]));
}
console.log(`-- Restored DB in ${(Date.now() - start) / 1000} s --`);
db.onChange(handleNewChanges);
fetch.onCommand(handleCommand);
}