store.js (1702B)
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 "use strict"; 6 7 const createStore = require("resource://devtools/client/shared/redux/create-store.js"); 8 const { 9 combineReducers, 10 } = require("resource://devtools/client/shared/vendor/redux.js"); 11 // Reducers which need to be available immediately when the Inspector loads. 12 const reducers = { 13 // Provide a dummy default reducer. 14 // Redux throws an error when calling combineReducers() with an empty object. 15 default: (state = {}) => state, 16 }; 17 18 function createReducer(laterReducers = {}) { 19 return combineReducers({ 20 ...reducers, 21 ...laterReducers, 22 }); 23 } 24 25 module.exports = inspector => { 26 const store = createStore(createReducer(), { 27 // Enable log middleware in tests 28 shouldLog: true, 29 // Pass the client inspector instance so thunks (dispatched functions) 30 // can access it from their arguments 31 thunkOptions: { inspector }, 32 }); 33 34 // Map of registered reducers loaded on-demand. 35 store.laterReducers = {}; 36 37 /** 38 * Augment the current Redux store with a slice reducer. 39 * Call this method to add reducers on-demand after the initial store creation. 40 * 41 * @param {string} key 42 * Slice name. 43 * @param {Function} reducer 44 * Slice reducer function. 45 */ 46 store.injectReducer = (key, reducer) => { 47 if (store.laterReducers[key]) { 48 console.log(`Already loaded reducer: ${key}`); 49 return; 50 } 51 store.laterReducers[key] = reducer; 52 store.replaceReducer(createReducer(store.laterReducers)); 53 }; 54 55 return store; 56 };