Skip to content

Instantly share code, notes, and snippets.

export const enum WizardActionTypes {
Hydrate = "WIZARD/HYDRATE",
LearnSpell = "WIZARD/LEARN_SPELL",
KillParents = "WIZARD/KILL_PARENTS"
}
export interface HydrateWizardsAction extends AnyAction {
type: WizardActionTypes.Hydrate;
payload: WizardNamespaceShape;
}
switch (action.type) {
case WizardActionTypes.Hydrate: {
return {
...action.payload
};
}
case WizardActionTypes.KillParents: {
// data/wizards/actions.ts
import { WizardActionTypes, WizardNamespaceShape } from "./types";
export function hydrateWizardNamespace(initialData: WizardNamespaceShape) {
return {
type: WizardActionTypes.Hydrate,
payload: initialData
};
}
import { getStore, registerReducer } from "../../redux-utils/store";
import { WIZARD_NAMESPACE_KEY } from "../../redux-utils/types";
import { once } from "lodash";
import wizardReducer from "./reducer";
import { WizardNamespaceShape } from "./types";
import { hydrateWizardNamespace } from "./actions";
export const getStoreForWizardApp = once(
(initialData?: WizardNamespaceShape) => {
registerReducer({ [WIZARD_NAMESPACE_KEY]: wizardReducer });
const store = getStore();
//redux-utils/types.ts
import { MuggleNamespaceShape } from "../data/muggles/types";
import { WizardNamespaceShape } from "../data/wizards/types";
import { Reducer } from "redux";
export const MUGGLE_NAMESPACE_KEY = "MUGGLE_NAMESPACE";
export const WIZARD_NAMESPACE_KEY = "WIZARD_NAMESPACE";
export type FullStoreShape = {
[MUGGLE_NAMESPACE_KEY]: MuggleNamespaceShape;
[WIZARD_NAMESPACE_KEY]: WizardNamespaceShape;
// redux-utils/types.ts
import { MuggleNamespaceShape } from "../data/muggles/types";
import { WizardNamespaceShape } from "../data/wizards/types";
import { Reducer } from "redux";
export const MUGGLE_NAMESPACE_KEY = "MUGGLE_NAMESPACE";
export const WIZARD_NAMESPACE_KEY = "WIZARD_NAMESPACE";
export type FullStoreShape = {
[MUGGLE_NAMESPACE_KEY]: MuggleNamespaceShape;
* @returns {Store} A Redux store that lets you read the state, dispatch actions
* and subscribe to changes.
*/
export default function createStore(reducer, preloadedState, enhancer) {
if (
(typeof preloadedState === 'function' && typeof enhancer === 'function') ||
(typeof enhancer === 'function' && typeof arguments[3] === 'function')
) {
throw new Error(
'It looks like you are passing several store enhancers to ' +
'createStore(). This is not supported. Instead, compose them ' +
'together to a single function'
)
}
let currentReducer = reducer
let currentState = preloadedState
let currentListeners = []
let nextListeners = currentListeners
let isDispatching = false
function ensureCanMutateNextListeners() {
if (nextListeners === currentListeners) {
nextListeners = currentListeners.slice()
}
}