mirror of
https://github.com/wassname/talk.git
synced 2026-07-23 13:10:20 +08:00
Some of them were only related to the embed stream. Auth code is too convoluted with the embed stream and can't be reused atm.
67 lines
1.5 KiB
JavaScript
67 lines
1.5 KiB
JavaScript
import {createStore, combineReducers, applyMiddleware, compose} from 'redux';
|
|
import thunk from 'redux-thunk';
|
|
import {getClient} from './client';
|
|
|
|
let listeners = [];
|
|
|
|
export function injectReducers(reducers) {
|
|
const store = getStore();
|
|
store.coralReducers = {...store.coralReducers, ...reducers};
|
|
store.replaceReducer(combineReducers(store.coralReducers));
|
|
}
|
|
|
|
/**
|
|
* Add a action listener to the redux store.
|
|
* The action is passed as the first argument to the callback.
|
|
*/
|
|
export function addListener(cb) {
|
|
listeners.push(cb);
|
|
}
|
|
|
|
export function getStore() {
|
|
if (window.coralStore) {
|
|
return window.coralStore;
|
|
}
|
|
|
|
const apolloErrorReporter = () => (next) => (action) => {
|
|
if (action.type === 'APOLLO_QUERY_ERROR') {
|
|
console.error(action.error);
|
|
}
|
|
return next(action);
|
|
};
|
|
|
|
const customListener = () => (next) => (action) => {
|
|
listeners.forEach((cb) => {cb(action);});
|
|
return next(action);
|
|
};
|
|
|
|
const middlewares = [
|
|
applyMiddleware(
|
|
getClient().middleware(),
|
|
thunk,
|
|
apolloErrorReporter,
|
|
customListener,
|
|
),
|
|
];
|
|
|
|
if (window.devToolsExtension) {
|
|
|
|
// we can't have the last argument of compose() be undefined
|
|
middlewares.push(window.devToolsExtension());
|
|
}
|
|
|
|
const coralReducers = {
|
|
apollo: getClient().reducer()
|
|
};
|
|
|
|
window.coralStore = createStore(
|
|
combineReducers(coralReducers),
|
|
{},
|
|
compose(...middlewares)
|
|
);
|
|
|
|
window.coralStore.coralReducers = coralReducers;
|
|
|
|
return window.coralStore;
|
|
}
|