Bug 1874684 - Part 4: Prefer const references instead of copying Instant values....
[gecko.git] / devtools / client / inspector / store.js
blobd9d7c0f99523080b19843a0bbbd345bb82b041e8
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/. */
5 "use strict";
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,
18 function createReducer(laterReducers = {}) {
19   return combineReducers({
20     ...reducers,
21     ...laterReducers,
22   });
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   });
34   // Map of registered reducers loaded on-demand.
35   store.laterReducers = {};
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   };
55   return store;