create-store.js (2274B)
1 /* This Source Code Form is subject to the terms of the Mozilla Public 2 * License, v. 2.0. If a copy of the MPL was not distributed with this 3 * file, You can obtain one at <http://mozilla.org/MPL/2.0/>. */ 4 5 /* global window */ 6 7 /** 8 * Redux store utils 9 */ 10 11 import { 12 createStore, 13 applyMiddleware, 14 } from "devtools/client/shared/vendor/redux"; 15 import { log } from "./middleware/log"; 16 import { timing } from "./middleware/timing"; 17 import { context } from "./middleware/context"; 18 19 const { 20 ignore, 21 } = require("resource://devtools/client/shared/redux/middleware/ignore.js"); 22 const { 23 promise, 24 } = require("resource://devtools/client/shared/redux/middleware/promise.js"); 25 const { 26 thunk, 27 } = require("resource://devtools/client/shared/redux/middleware/thunk.js"); 28 const { 29 waitUntilService, 30 } = require("resource://devtools/client/shared/redux/middleware/wait-service.js"); 31 32 /** 33 * This creates a dispatcher with all the standard middleware in place 34 * that all code requires. It can also be optionally configured in 35 * various ways, such as logging and recording. 36 * 37 * @param {object} opts: 38 * - log: log all dispatched actions to console 39 * - history: an array to store every action in. Should only be 40 * used in tests. 41 * - middleware: array of middleware to be included in the redux store 42 * @memberof utils/create-store 43 * @static 44 */ 45 const configureStore = (opts = {}) => { 46 const middleware = [ 47 thunk(opts.thunkArgs), 48 context, 49 promise, 50 ignore, 51 52 // Order is important: services must go last as they always 53 // operate on "already transformed" actions. Actions going through 54 // them shouldn't have any special fields like promises, they 55 // should just be normal JSON objects. 56 waitUntilService, 57 ]; 58 59 if (opts.middleware) { 60 opts.middleware.forEach(fn => middleware.push(fn)); 61 } 62 63 if (opts.log) { 64 middleware.push(log); 65 } 66 67 if (opts.timing) { 68 middleware.push(timing); 69 } 70 71 // Hook in the redux devtools browser extension if it exists 72 const devtoolsExt = 73 typeof window === "object" && window.devToolsExtension 74 ? window.devToolsExtension() 75 : f => f; 76 77 return applyMiddleware(...middleware)(devtoolsExt(createStore)); 78 }; 79 80 export default configureStore;