Skip to content

Tavernari/ReSwiftMiddleware

Repository files navigation

ReSwiftMiddleware

Swift 5.0

I have used the ReSwift Framework, so when I needed to implement middlewares, a see it a little bit confused to use, so I made this tool to help others to create Middlewares.

About

This library has two important structs MiddlewaresCollection and MiddlewareExecutor.

The MiddlewaresCollection is an object where you need to add yours MiddlewareExecutor. The MiddlewareExecutor is a protocol which you use to implement to execute your middleware implementation.

How to implement

First we need to create our Middlewares.

All this code is in the Unit Tests inside the project.

import Foundation
import ReSwift

struct MiddlewareLogger : MiddlewareExecutor {

    let logger: LogConsole

    init(logger: LogConsole) {
        self.logger = logger;
    }

    func execute(
            action: Action, 
            getState: @escaping () -> StateType?, 
            dispatch: @escaping DispatchFunction) -> Action? {

        if let counterAction = action as? CounterActions {
            self.logger.register(value: counterAction.toString())
        }

        return action
    }
}

If you need to interrupt the flow, you return nil on execute method.

The example of implementing MiddlewaresCollection.

let middlewaresCollection = MiddlewaresCollection<CounterState>()
let middlewareLogger = MiddlewareLogger(logger: logger)

middlewaresCollection.add(middlewareLogger);

Now an example of how to connect this collection to [ReSwfit]

let store = Store<CounterState>(
    reducer: reducerCounterState, 
    state: CounterState(), 
    middleware: middlewaresCollection.middlewares)

Installation

CocoaPods

You can install ReSwift via CocoaPods by adding it to your Podfile:

use_frameworks!

source 'https://github.com/CocoaPods/Specs.git'
platform :ios, '8.0'

pod 'ReSwiftMiddleware'

And run pod install.

About

This lib was created to help developers to handle the middlewares on ReSwift library

Resources

License

Stars

Watchers

Forks

Packages

No packages published