insomnia/app/index.js

76 lines
1.8 KiB
JavaScript
Raw Normal View History

2016-03-16 05:49:42 +00:00
import React from 'react'
2016-03-20 04:00:40 +00:00
import {render} from 'react-dom'
import {Provider} from 'react-redux'
2016-04-16 23:24:57 +00:00
import {bindActionCreators} from 'redux'
2016-04-23 06:16:23 +00:00
import createStore from './redux/create'
2016-04-16 23:24:57 +00:00
import App from './containers/App'
2016-03-20 04:00:40 +00:00
2016-04-23 06:16:23 +00:00
import * as RequestGroupActions from './redux/modules/requestGroups'
import * as RequestActions from './redux/modules/requests'
import * as ResponseActions from './redux/modules/responses'
2016-04-16 23:24:57 +00:00
import * as db from './database'
2016-03-18 06:30:48 +00:00
// Global CSS
2016-03-16 05:49:42 +00:00
import './css/index.scss'
2016-03-16 23:34:25 +00:00
import './css/lib/chrome/platform_app.css'
2016-03-16 05:49:42 +00:00
import './css/lib/fontawesome/css/font-awesome.css'
2016-04-23 06:16:23 +00:00
const store = createStore();
2016-03-16 05:49:42 +00:00
2016-03-21 05:47:49 +00:00
// Dispatch the initial load of data
2016-04-16 23:24:57 +00:00
console.log('-- Init Insomnia --');
const actionFns = {
RequestGroup: bindActionCreators(RequestGroupActions, store.dispatch),
2016-04-18 04:39:15 +00:00
Request: bindActionCreators(RequestActions, store.dispatch),
Response: bindActionCreators(ResponseActions, store.dispatch)
2016-04-16 23:24:57 +00:00
};
function refreshDoc (doc) {
const fns = actionFns[doc.type];
if (fns) {
fns[doc._deleted ? 'remove' : 'update'](doc);
2016-04-18 04:39:15 +00:00
} else if (doc.hasOwnProperty('type')) {
2016-04-16 23:24:57 +00:00
console.warn('Unknown change', doc.type, doc);
2016-04-18 04:39:15 +00:00
} else {
// Probably a design doc update or something...
2016-04-16 23:24:57 +00:00
}
}
function watchDB () {
console.log('-- Watching PouchDB --');
let buffer = [];
let timeout = null;
// Debounce and buffer changes if they happen in quick succession
db.changes.on('change', (response) => {
const doc = response.doc;
buffer.push(doc);
clearTimeout(timeout);
timeout = setTimeout(() => {
buffer.map(refreshDoc);
buffer = [];
}, 50);
});
}
function restoreDB() {
db.allDocs().then(response => {
response.rows.map(row => refreshDoc(row.doc));
})
}
watchDB();
restoreDB();
2016-03-21 05:47:49 +00:00
2016-03-16 05:49:42 +00:00
render(
2016-04-16 23:24:57 +00:00
<Provider store={store}><App /></Provider>,
2016-03-20 04:00:40 +00:00
document.getElementById('root')
2016-03-16 05:49:42 +00:00
);
2016-04-20 06:09:46 +00:00