Local search (#220)
* Version Bump * local search skeleton * Added WaitService middleware * Return matching queries * wait action middleware rename/refactor * bigger debounce time * Fix: Initialize state using Immutable * Local Search without integrations * Local Search refactor: Keep state in closure, recurse * “string” should be treated as the default widget by the inference. Closes #199
This commit is contained in:
21
src/redux/configureStore.js
Normal file
21
src/redux/configureStore.js
Normal file
@ -0,0 +1,21 @@
|
||||
import { createStore, applyMiddleware, compose } from 'redux';
|
||||
import thunkMiddleware from 'redux-thunk';
|
||||
import waitUntilAction from './middleware/waitUntilAction';
|
||||
import reducer from '../reducers/combinedReducer';
|
||||
|
||||
export default function configureStore(initialState) {
|
||||
const store = createStore(reducer, initialState, compose(
|
||||
applyMiddleware(thunkMiddleware, waitUntilAction),
|
||||
window.devToolsExtension ? window.devToolsExtension() : f => f
|
||||
));
|
||||
|
||||
if (process.env.NODE_ENV !== 'production' && module.hot) {
|
||||
// Enable Webpack hot module replacement for reducers
|
||||
module.hot.accept('../reducers/combinedReducer', () => {
|
||||
const nextReducer = require('../reducers/combinedReducer') // eslint-disable-line
|
||||
store.replaceReducer(nextReducer);
|
||||
});
|
||||
}
|
||||
|
||||
return store;
|
||||
}
|
47
src/redux/middleware/waitUntilAction.js
Normal file
47
src/redux/middleware/waitUntilAction.js
Normal file
@ -0,0 +1,47 @@
|
||||
// Based on wait-service by Mozilla:
|
||||
// https://github.com/mozilla/gecko-dev/blob/master/devtools/client/shared/redux/middleware/wait-service.js
|
||||
|
||||
/**
|
||||
* A middleware that provides the ability for actions to install a
|
||||
* function to be run once when a specific condition is met by an
|
||||
* action coming through the system. Think of it as a thunk that
|
||||
* blocks until the condition is met.
|
||||
*/
|
||||
export const WAIT_UNTIL_ACTION = 'WAIT_UNTIL_ACTION';
|
||||
|
||||
export default function waitUntilAction({ dispatch, getState }) {
|
||||
let pending = [];
|
||||
|
||||
function checkPending(action) {
|
||||
const readyRequests = [];
|
||||
const stillPending = [];
|
||||
|
||||
// Find the pending requests whose predicates are satisfied with
|
||||
// this action. Wait to run the requests until after we update the
|
||||
// pending queue because the request handler may synchronously
|
||||
// dispatch again and run this service (that use case is
|
||||
// completely valid).
|
||||
for (const request of pending) {
|
||||
if (request.predicate(action)) {
|
||||
readyRequests.push(request);
|
||||
} else {
|
||||
stillPending.push(request);
|
||||
}
|
||||
}
|
||||
|
||||
pending = stillPending;
|
||||
for (const request of readyRequests) {
|
||||
request.run(dispatch, getState, action);
|
||||
}
|
||||
}
|
||||
|
||||
return next => (action) => {
|
||||
if (action.type === WAIT_UNTIL_ACTION) {
|
||||
pending.push(action);
|
||||
return null;
|
||||
}
|
||||
const result = next(action);
|
||||
checkPending(action);
|
||||
return result;
|
||||
};
|
||||
}
|
Reference in New Issue
Block a user