insomnia/app/index.js

70 lines
1.6 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-03-16 05:49:42 +00:00
import configureStore from './stores/configureStore'
2016-04-16 23:24:57 +00:00
import App from './containers/App'
2016-03-20 04:00:40 +00:00
2016-04-16 23:24:57 +00:00
import * as RequestGroupActions from './actions/requestGroups'
import * as RequestActions from './actions/requests'
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'
const store = configureStore();
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),
Request: bindActionCreators(RequestActions, store.dispatch)
};
function refreshDoc (doc) {
const fns = actionFns[doc.type];
if (fns) {
fns[doc._deleted ? 'remove' : 'update'](doc);
} else {
console.warn('Unknown change', doc.type, doc);
}
}
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
);