4.1.0 • Published 5 years ago

redux-di-middleware v4.1.0

Weekly downloads
21
License
GPL-2.0
Repository
github
Last release
5 years ago

npm version Build Status codecov Codacy Badge Greenkeeper badge

redux-di-middleware

A redux middleware for dependency injection and async operations, written in Typescript

Install

npm install redux-di-middleware

Usage

Setting up with the Store

You can set up the middleware during the store creation.

const di = new ReduxDiMiddleware();
const injectableService = new ExampleInjectable();

// This service will be registered as a singleton in the default DI Container.
// You can register services from another type as well into the same container.
di.setInjectable(injectableService);

const store = createStore(mockReducer, applyMiddleware(di.getMiddleware()));

An example action

Injectable actions have to be a field called inject that must be a function (and can be async as well)

const exampleAction: InjectableAction<{state: { value: string}}, Action> = ({
    type: "DO_SOMETHING",
    inject: async (options) => {
        // gets the preconfigured injected instance
        const service = options.getInjectable(ExampleInjectable);
        
        // gets the current state
        const currentState = options.getState();
        try {
            const value = await service.getValueAsync(currentState.state.value);
            
            // dispatch an another action on the store with a result
            options.dispatch({
                type: "DO_SOMETHING_FINISHED",
                value,
            });
        } catch (error) {
            options.dispatch({
                type: "DO_SOMETHING_FAILED",
                error,
            });
        }
    },
});

Using named containers

If you want to inject more than a simple instance you can use named containers.

const injectableService = new ExampleInjectable();
// sets to the default container
di.setInjectable(injectableService);

// sets to "myCustomContainer"
const otherInjectableService = new ExampleInjectable();
di.setInjectable(otherInjectableService, "myCustomContainer")

// ... later in the Action
// gets from the default container
const service = options.getInjectable(ExampleInjectable);

// gets from "myCustomContainer"
const serviceFromCustomContainer = options.getInjectable(ExampleInjectable, "myCustomContainer");
4.1.0

5 years ago

4.0.2

5 years ago

4.0.1

5 years ago

4.0.0

5 years ago

3.0.0

5 years ago

2.0.3

5 years ago

2.0.2

5 years ago

2.0.1

6 years ago

2.0.0

6 years ago

1.0.2

6 years ago

1.0.1

6 years ago