mirror of
https://github.com/Kong/insomnia.git
synced 2026-10-25 16:19:20 -04:00
Better store stuff
This commit is contained in:
1 parent
410a218573
commit
5b12b3d698
6 files changed
+42
-61
No files matched your search
@@ -38,8 +38,6 @@ class RequestGroupActionsDropdown extends Component {
|
||||
|
||||
RequestGroupActionsDropdown.propTypes = {
|
||||
actions: PropTypes.shape({
|
||||
update: PropTypes.func.isRequired,
|
||||
remove: PropTypes.func.isRequired,
|
||||
showUpdateNamePrompt: PropTypes.func.isRequired,
|
||||
showEnvironmentEditModal: PropTypes.func.isRequired
|
||||
}),
|
||||
|
||||
+18
-5
@@ -27,6 +27,10 @@ export function offChange (id) {
|
||||
delete changeListeners[id];
|
||||
}
|
||||
|
||||
export function allDocs () {
|
||||
return db.allDocs({include_docs: true});
|
||||
}
|
||||
|
||||
db.changes({
|
||||
since: 'now',
|
||||
live: true,
|
||||
@@ -48,8 +52,7 @@ export function initDB () {
|
||||
console.log('-- Initializing Database --');
|
||||
return Promise.all([
|
||||
db.createIndex({index: {fields: ['parentId']}}),
|
||||
db.createIndex({index: {fields: ['type']}}),
|
||||
db.createIndex({index: {fields: ['activated', 'type']}})
|
||||
db.createIndex({index: {fields: ['type']}})
|
||||
]).catch(err => {
|
||||
console.error('Failed to PouchDB Indexes', err);
|
||||
});
|
||||
@@ -103,7 +106,7 @@ function modelCreate (type, idPrefix, defaults, patch = {}) {
|
||||
parentId: null
|
||||
};
|
||||
|
||||
const model = Object.assign(
|
||||
const doc = Object.assign(
|
||||
baseDefaults,
|
||||
defaults,
|
||||
patch,
|
||||
@@ -118,8 +121,7 @@ function modelCreate (type, idPrefix, defaults, patch = {}) {
|
||||
}
|
||||
);
|
||||
|
||||
update(model);
|
||||
return model;
|
||||
return update(doc).then(() => doc);
|
||||
}
|
||||
|
||||
// ~~~~~~~ //
|
||||
@@ -195,6 +197,17 @@ export function workspaceActivate (workspace) {
|
||||
export function workspaceAll () {
|
||||
return db.find({
|
||||
selector: {type: 'Workspace'}
|
||||
}).then(res => {
|
||||
if (res.docs.length) {
|
||||
return res;
|
||||
} else {
|
||||
// No workspaces? Create first one and try again
|
||||
// TODO: Replace this with UI flow maybe?
|
||||
console.log('-- Creating First Workspace --');
|
||||
return workspaceCreate({name: 'First Workspace'}).then(() => {
|
||||
return workspaceAll();
|
||||
})
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
|
||||
+10
-10
@@ -9,21 +9,21 @@ const FORMAT_MAP = {
|
||||
|
||||
function importRequestGroup (iRequestGroup, parentId, exportFormat) {
|
||||
if (exportFormat === 1) {
|
||||
const requestGroup = db.requestGroupCreate({
|
||||
db.requestGroupCreate({
|
||||
parentId,
|
||||
collapsed: true,
|
||||
name: iRequestGroup.name,
|
||||
environment: (iRequestGroup.environments || {}).base || {}
|
||||
}).then(requestGroup => {
|
||||
// Sometimes (maybe all the time, I can't remember) requests will be nested
|
||||
if (iRequestGroup.hasOwnProperty('requests')) {
|
||||
// Let's process them oldest to newest
|
||||
iRequestGroup.requests.reverse();
|
||||
iRequestGroup.requests.map(
|
||||
r => importRequest(r, requestGroup._id, exportFormat)
|
||||
);
|
||||
}
|
||||
});
|
||||
|
||||
// Sometimes (maybe all the time, I can't remember) requests will be nested
|
||||
if (iRequestGroup.hasOwnProperty('requests')) {
|
||||
// Let's process them oldest to newest
|
||||
iRequestGroup.requests.reverse();
|
||||
iRequestGroup.requests.map(
|
||||
r => importRequest(r, requestGroup._id, exportFormat)
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
+11
-12
@@ -6,7 +6,7 @@ const CHANGE_ID = 'store.listener';
|
||||
|
||||
export function initStore (dispatch) {
|
||||
db.offChange(CHANGE_ID);
|
||||
|
||||
|
||||
// New stuff...
|
||||
const entities = bindActionCreators(entitiesActions, dispatch);
|
||||
|
||||
@@ -14,7 +14,7 @@ export function initStore (dispatch) {
|
||||
if (!doc.hasOwnProperty('type')) {
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
// New stuff...
|
||||
entities[doc._deleted ? 'remove' : 'update'](doc);
|
||||
};
|
||||
@@ -22,28 +22,27 @@ export function initStore (dispatch) {
|
||||
console.log('-- Restoring Store --');
|
||||
|
||||
const start = Date.now();
|
||||
return db.workspaceAll().then(res => {
|
||||
res.docs.map(docChanged);
|
||||
}).then(() => {
|
||||
return db.workspaceGetActive()
|
||||
}).then(res => {
|
||||
const workspace = res.docs.length ? res.docs[0] : db.workspaceCreate();
|
||||
|
||||
const restoreChildren = (doc, maxDepth, depth = 0) => {
|
||||
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, maxDepth, depth++))
|
||||
res.docs.map(doc => restoreChildren(doc))
|
||||
);
|
||||
})
|
||||
};
|
||||
|
||||
return restoreChildren(workspace, 5);
|
||||
return res.docs.map(restoreChildren)
|
||||
}).then(() => {
|
||||
console.log(`Restore took ${(Date.now() - start) / 1000} s`);
|
||||
}).then(() => {
|
||||
db.onChange(CHANGE_ID, res => docChanged(res.doc));
|
||||
});
|
||||
}
|
||||
|
||||
@@ -2,7 +2,6 @@ import {combineReducers} from 'redux'
|
||||
|
||||
import {TYPE_WORKSPACE, TYPE_REQUEST_GROUP, TYPE_REQUEST, TYPE_RESPONSE} from '../../database/index'
|
||||
import * as workspaceFns from './workspaces'
|
||||
import * as requestGroupFns from './requestGroups'
|
||||
|
||||
const ENTITY_UPDATE = 'entities/update';
|
||||
const ENTITY_REMOVE = 'entities/remove';
|
||||
@@ -60,15 +59,9 @@ const workspaces = generateEntityReducer(
|
||||
workspaceFns.WORKSPACE_DELETE
|
||||
);
|
||||
|
||||
const requestGroups = generateEntityReducer(
|
||||
'requestGroup',
|
||||
requestGroupFns.REQUEST_GROUP_UPDATE,
|
||||
requestGroupFns.REQUEST_GROUP_DELETE
|
||||
);
|
||||
|
||||
export default combineReducers({
|
||||
workspaces,
|
||||
requestGroups,
|
||||
requestGroups: genericEntityReducer('requestGroup'),
|
||||
requests: genericEntityReducer('request'),
|
||||
responses: genericEntityReducer('response')
|
||||
})
|
||||
@@ -80,14 +73,14 @@ export default combineReducers({
|
||||
|
||||
const updateFns = {
|
||||
[TYPE_WORKSPACE]: workspaceFns.update,
|
||||
[TYPE_REQUEST_GROUP]: requestGroupFns.update,
|
||||
[TYPE_REQUEST_GROUP]: requestGroup => ({type: ENTITY_UPDATE, requestGroup}),
|
||||
[TYPE_RESPONSE]: response => ({type: ENTITY_UPDATE, response}),
|
||||
[TYPE_REQUEST]: request => ({type: ENTITY_UPDATE, request})
|
||||
};
|
||||
|
||||
const removeFns = {
|
||||
[TYPE_WORKSPACE]: workspaceFns.remove,
|
||||
[TYPE_REQUEST_GROUP]: requestGroupFns.remove,
|
||||
[TYPE_REQUEST_GROUP]: requestGroup => ({type: ENTITY_UPDATE, requestGroup}),
|
||||
[TYPE_RESPONSE]: response => ({type: ENTITY_UPDATE, response}),
|
||||
[TYPE_REQUEST]: request => ({type: ENTITY_REMOVE, request})
|
||||
};
|
||||
|
||||
@@ -3,8 +3,6 @@ import {combineReducers} from 'redux'
|
||||
import {show} from './modals'
|
||||
import {MODAL_ENVIRONMENT_EDITOR, MODAL_REQUEST_GROUP_RENAME} from '../../lib/constants'
|
||||
|
||||
export const REQUEST_GROUP_UPDATE = 'request-groups/update';
|
||||
export const REQUEST_GROUP_DELETE = 'request-groups/delete';
|
||||
export const REQUEST_GROUP_TOGGLE = 'request-groups/toggle';
|
||||
|
||||
// ~~~~~~~~ //
|
||||
@@ -13,24 +11,12 @@ export const REQUEST_GROUP_TOGGLE = 'request-groups/toggle';
|
||||
|
||||
function allReducer (state = [], action) {
|
||||
switch (action.type) {
|
||||
|
||||
case REQUEST_GROUP_UPDATE:
|
||||
const i = state.findIndex(rg => rg._id === action.requestGroup._id);
|
||||
|
||||
if (i === -1) {
|
||||
return [action.requestGroup, ...state];
|
||||
} else {
|
||||
return [...state.slice(0, i), action.requestGroup, ...state.slice(i + 1)]
|
||||
}
|
||||
|
||||
case REQUEST_GROUP_TOGGLE:
|
||||
return state.map(
|
||||
rg => rg._id === action._id ? Object.assign({}, rg, {collapsed: !rg.collapsed}) : rg
|
||||
);
|
||||
|
||||
case REQUEST_GROUP_DELETE:
|
||||
return state.filter(rg => rg._id !== action.requestGroup._id);
|
||||
|
||||
default:
|
||||
return state;
|
||||
}
|
||||
@@ -45,14 +31,6 @@ export default combineReducers({
|
||||
// ACTIONS //
|
||||
// ~~~~~~~ //
|
||||
|
||||
export function update (requestGroup) {
|
||||
return {type: REQUEST_GROUP_UPDATE, requestGroup};
|
||||
}
|
||||
|
||||
export function remove (requestGroup) {
|
||||
return {type: REQUEST_GROUP_DELETE, requestGroup};
|
||||
}
|
||||
|
||||
export function toggle (requestGroup) {
|
||||
return {type: REQUEST_GROUP_TOGGLE, requestGroup}
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user