insomnia/app/redux/initstore.js
Gregory Schier e9d64ebb23 Workspaces (#7)
* Got a hacky workspace implementation running

* Removed some hax with reducer composition

* Moved some more around

* Moved files back out

* Started on entities reducer

* Split up some components

* Moved nested modules back out of workspaces

* Started on new Sidebar tree stuff

* Better store stuff

* Some more tweaks

* Removed workspace update action

* Re-implemented filtering in the Sidbare

* Switch to get the newest response
2016-04-26 00:29:24 -07:00

49 lines
1.1 KiB
JavaScript

import {bindActionCreators} from 'redux'
import * as entitiesActions from './modules/entities'
import * as db from '../database'
const CHANGE_ID = 'store.listener';
export function initStore (dispatch) {
db.offChange(CHANGE_ID);
// New stuff...
const entities = bindActionCreators(entitiesActions, dispatch);
const docChanged = doc => {
if (!doc.hasOwnProperty('type')) {
return;
}
// New stuff...
entities[doc._deleted ? 'remove' : 'update'](doc);
};
console.log('-- Restoring Store --');
const start = Date.now();
return db.workspaceAll().then(res => {
const restoreChildren = (doc) => {
docChanged(doc);
return db.getChildren(doc).then(res => {
// Done condition
if (!res.docs.length) {
return;
}
return Promise.all(
res.docs.map(doc => restoreChildren(doc))
);
})
};
return res.docs.map(restoreChildren)
}).then(() => {
console.log(`Restore took ${(Date.now() - start) / 1000} s`);
}).then(() => {
db.onChange(CHANGE_ID, res => docChanged(res.doc));
});
}