Skip to content

Commit

Permalink
Remove module enhancer and accept enhancers in createStore (microsoft#29
Browse files Browse the repository at this point in the history
)
  • Loading branch information
navneet-g authored Nov 12, 2018
1 parent 407c81f commit 1ee66cc
Show file tree
Hide file tree
Showing 21 changed files with 2,406 additions and 2,455 deletions.
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,9 @@ import {getUsersModule} from "./usersModule";
const store: IModuleStore<IState> = createStore(
/* initial state */
{},

/** enhancers **/
[],

/* extensions to include */
[],
Expand Down
3 changes: 3 additions & 0 deletions docs/GettingStarted.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,9 @@ const store: IModuleStore<IState> = createStore(
/* initial state */
{},

/** enhancers **/
[],

/* Extensions to load */
[],

Expand Down
2 changes: 1 addition & 1 deletion docs/reference/Extensions.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ export interface IExtension {
## Adding extensions to the store
To add an extension to the `ModuleStore`, pass it as the second argument to `createStore`
```typescript
const store: IModuleStore<IState> = createStore({}, [getMyExtension()])
const store: IModuleStore<IState> = createStore({}, [], [getMyExtension()])
```


Expand Down
3 changes: 3 additions & 0 deletions docs/reference/ModuleStore.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@ const store: IModuleStore<IState> = createStore(
/* initial state */
{},

/** enhancers **/
[],

/* Extensions to load */
[],

Expand Down
3 changes: 3 additions & 0 deletions docs/reference/ReduxObservable.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@ const store: IModuleStore<IState> = createStore(
/* initial state */
{},

/** enhancers **/
[],

/* Extensions to load */
[getObservableExtension()],

Expand Down
3 changes: 3 additions & 0 deletions docs/reference/ReduxSaga.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@ const store: IModuleStore<IState> = createStore(
/* initial state */
{},

/** enhancers **/
[],

/* Extensions to load */
[getSagaExtension({} /* saga context */)],

Expand Down
3 changes: 3 additions & 0 deletions docs/reference/ReduxThunk.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@ const store: IModuleStore<IState> = createStore(
/* initial state */
{},

/** enhancers **/
[],

/* Extensions to load */
[getThunkExtension()],

Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion packages/redux-dynamic-modules-saga/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ describe("Saga extension tests", () => {
it("Saga extension registers module and starts saga", () => {
const testContext = {};
called = false;
createStore({}, [getSagaExtension(testContext)], getTestModule());
createStore({}, [], [getSagaExtension(testContext)], getTestModule());
expect(called);

expect(testContext["moduleManager"]).toBeTruthy();
Expand Down
2 changes: 1 addition & 1 deletion packages/redux-dynamic-modules-thunk/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion packages/redux-dynamic-modules/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

129 changes: 0 additions & 129 deletions packages/redux-dynamic-modules/src/ModuleEnhancer.ts

This file was deleted.

115 changes: 97 additions & 18 deletions packages/redux-dynamic-modules/src/ModuleStore.ts
Original file line number Diff line number Diff line change
@@ -1,23 +1,102 @@
import { createStore as reduxCreateStore, DeepPartial } from "redux";
import { IModule, IModuleStore, IExtension } from "./Contracts";
import { moduleEnhancer } from './ModuleEnhancer';
import { applyMiddleware, compose, createStore as createReduxStore, DeepPartial, StoreEnhancer } from "redux";
import { getMiddlewareManager } from ".";
import { IExtension, IModule, IModuleStore } from "./Contracts";
import { getModuleManager } from "./Managers/ModuleManager";
import { getRefCountedManager } from './Managers/RefCountedManager';

/**
* Configure the module store
*/
export function createStore<S1>(initialState: DeepPartial<S1>, extensions: IExtension[], reduxModule: IModule<S1>): IModuleStore<S1>;
export function createStore<S1, S2>(initialState: DeepPartial<S1 & S2>, extensions: IExtension[], m1: IModule<S1>, m2: IModule<S2>): IModuleStore<S1 & S2>;
export function createStore<S1, S2, S3>(initialState: DeepPartial<S1 & S2 & S3>, extensions: IExtension[], m1: IModule<S1>, m2: IModule<S2>, m3: IModule<S3>): IModuleStore<S1 & S2 & S3>;
export function createStore<S1, S2, S3, S4>(initialState: DeepPartial<S1 & S2 & S3 & S4>, extensions: IExtension[], m1: IModule<S1>, m2: IModule<S2>, m3: IModule<S3>, m4: IModule<S4>): IModuleStore<S1 & S2 & S3 & S4>;
export function createStore<S1, S2, S3, S4, S5>(initialState: DeepPartial<S1 & S2 & S3 & S4 & S5>, extensions: IExtension[], m1: IModule<S1>, m2: IModule<S2>, m3: IModule<S3>, m4: IModule<S4>, m5: IModule<S5>): IModuleStore<S1 & S2 & S3 & S4 & S5>;
export function createStore<S1, S2, S3, S4, S5, S6>(initialState: DeepPartial<S1 & S2 & S3 & S4 & S5 & S6>, extensions: IExtension[], m1: IModule<S1>, m2: IModule<S2>, m3: IModule<S3>, m4: IModule<S4>, m5: IModule<S5>, m6: IModule<S6>): IModuleStore<S1 & S2 & S3 & S4 & S5 & S6>;
export function createStore<S1, S2, S3, S4, S5, S6, S7>(initialState: DeepPartial<S1 & S2 & S3 & S4 & S5 & S6 & S7>, extensions: IExtension[], m1: IModule<S1>, m2: IModule<S2>, m3: IModule<S3>, m4: IModule<S4>, m5: IModule<S5>, m6: IModule<S6>, m7: IModule<S7>): IModuleStore<S1 & S2 & S3 & S4 & S5 & S6 & S7>;
export function createStore<S1, S2, S3, S4, S5, S6, S7, S8>(initialState: DeepPartial<S1 & S2 & S3 & S4 & S5 & S6 & S7 & S8>, extensions: IExtension[], m1: IModule<S1>, m2: IModule<S2>, m3: IModule<S3>, m4: IModule<S4>, m5: IModule<S5>, m6: IModule<S6>, m7: IModule<S7>, m8: IModule<S8>): IModuleStore<S1 & S2 & S3 & S4 & S5 & S6 & S7 & S8>;
export function createStore<State>(initialState: DeepPartial<State>, extensions: IExtension[], ...initialModules: IModule<any>[]): IModuleStore<State>;
export function createStore<State>(initialState: DeepPartial<State>, extensions: IExtension[], ...initialModules: IModule<any>[]): IModuleStore<State> {
return reduxCreateStore(
/* reducer */undefined,
initialState,
moduleEnhancer(extensions, initialModules)
export function createStore<S1>(initialState: DeepPartial<S1>, enhancers: StoreEnhancer[], extensions: IExtension[], reduxModule: IModule<S1>): IModuleStore<S1>;
export function createStore<S1, S2>(initialState: DeepPartial<S1 & S2>, enhancers: StoreEnhancer[], extensions: IExtension[], m1: IModule<S1>, m2: IModule<S2>): IModuleStore<S1 & S2>;
export function createStore<S1, S2, S3>(initialState: DeepPartial<S1 & S2 & S3>, enhancers: StoreEnhancer[], extensions: IExtension[], m1: IModule<S1>, m2: IModule<S2>, m3: IModule<S3>): IModuleStore<S1 & S2 & S3>;
export function createStore<S1, S2, S3, S4>(initialState: DeepPartial<S1 & S2 & S3 & S4>, enhancers: StoreEnhancer[], extensions: IExtension[], m1: IModule<S1>, m2: IModule<S2>, m3: IModule<S3>, m4: IModule<S4>): IModuleStore<S1 & S2 & S3 & S4>;
export function createStore<S1, S2, S3, S4, S5>(initialState: DeepPartial<S1 & S2 & S3 & S4 & S5>, enhancers: StoreEnhancer[], extensions: IExtension[], m1: IModule<S1>, m2: IModule<S2>, m3: IModule<S3>, m4: IModule<S4>, m5: IModule<S5>): IModuleStore<S1 & S2 & S3 & S4 & S5>;
export function createStore<S1, S2, S3, S4, S5, S6>(initialState: DeepPartial<S1 & S2 & S3 & S4 & S5 & S6>, enhancers: StoreEnhancer[], extensions: IExtension[], m1: IModule<S1>, m2: IModule<S2>, m3: IModule<S3>, m4: IModule<S4>, m5: IModule<S5>, m6: IModule<S6>): IModuleStore<S1 & S2 & S3 & S4 & S5 & S6>;
export function createStore<S1, S2, S3, S4, S5, S6, S7>(initialState: DeepPartial<S1 & S2 & S3 & S4 & S5 & S6 & S7>, enhancers: StoreEnhancer[], extensions: IExtension[], m1: IModule<S1>, m2: IModule<S2>, m3: IModule<S3>, m4: IModule<S4>, m5: IModule<S5>, m6: IModule<S6>, m7: IModule<S7>): IModuleStore<S1 & S2 & S3 & S4 & S5 & S6 & S7>;
export function createStore<S1, S2, S3, S4, S5, S6, S7, S8>(initialState: DeepPartial<S1 & S2 & S3 & S4 & S5 & S6 & S7 & S8>, enhancers: StoreEnhancer[], extensions: IExtension[], m1: IModule<S1>, m2: IModule<S2>, m3: IModule<S3>, m4: IModule<S4>, m5: IModule<S5>, m6: IModule<S6>, m7: IModule<S7>, m8: IModule<S8>): IModuleStore<S1 & S2 & S3 & S4 & S5 & S6 & S7 & S8>;
export function createStore<State>(initialState: DeepPartial<State>, enhancers: StoreEnhancer[], extensions: IExtension[], ...initialModules: IModule<any>[]): IModuleStore<State>;
export function createStore<State>(initialState: DeepPartial<State>, enhancers: StoreEnhancer[], extensions: IExtension[], ...initialModules: IModule<any>[]): IModuleStore<State> {
if (!extensions) {
extensions = [];
}

const extensionMiddleware = extensions.reduce(
(mw, p) => {
if (p.middleware) {
mw.push(...p.middleware)
}

return mw;
},
[]
);
}

let composeEnhancers = compose;

//@ts-ignore
if (process.env.NODE_ENV === "development") {
composeEnhancers = window["__REDUX_DEVTOOLS_EXTENSION_COMPOSE__"] || compose;
}

const middlewareManager = getRefCountedManager(getMiddlewareManager(), (a, b) => a === b);
const enhancer = composeEnhancers(
...enhancers,
applyMiddleware(
...extensionMiddleware,
middlewareManager.enhancer));

const modules = getRefCountedManager(
getModuleManager<State>(middlewareManager, extensions),
(a: IModule<any>, b: IModule<any>) => a.id === b.id);

// Create store
const store: IModuleStore<State> = createReduxStore<State, any, {}, {}>(
modules.getReducer,
initialState,
enhancer as any
) as IModuleStore<State>;

modules.setDispatch(store.dispatch);

const addModules = (modulesToBeAdded: IModule<any>[]) => {
modules.add(modulesToBeAdded);
return {
remove: () => {
modules.remove(modulesToBeAdded);
}
};
}

const addModule = (moduleToBeAdded: IModule<any>) => {
return addModules([moduleToBeAdded]);
};

extensions.forEach(p => {
if (p.onModuleManagerCreated) {
p.onModuleManagerCreated({
addModule,
addModules
});
}
});

store.addModule = addModule;
store.addModules = addModules;

store.dispose = () => {
// get all added modules and remove them
modules.dispose();
middlewareManager.dispose();
extensions.forEach(p => {
if (p.dispose) {
p.dispose();
}
});
};


store.addModules(initialModules);

return store as IModuleStore<State>;
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,25 +22,25 @@ describe("Store with extensions", () => {
middlewareFunction();
};
testExtension.middleware = [middleware];
testStore = createStore({}, [testExtension]);
testStore = createStore({}, [], [testExtension]);

testStore.dispatch({ type: "ANY" });
expect(middlewareFunction).toHaveBeenCalled();
});

it("Manager created called", () => {
testStore = createStore({}, [testExtension]);
testStore = createStore({}, [], [testExtension]);
expect(testExtension.onModuleManagerCreated).toHaveBeenCalled();
});

it("OnModule Added called", () => {
testStore = createStore({}, [testExtension]);
testStore = createStore({}, [], [testExtension]);
testStore.addModule({ id: "new_module" });
expect(testExtension.onModuleAdded).toHaveBeenCalled();
});

it("OnModule Removed called", () => {
testStore = createStore({}, [testExtension]);
testStore = createStore({}, [], [testExtension]);
const module = testStore.addModule({ id: "new_module" });
module.remove();

Expand Down
Loading

0 comments on commit 1ee66cc

Please sign in to comment.