Giter VIP home page Giter VIP logo

classyflux's Introduction

ClassyFlux

License Language Build Status Coverage Status

Simple yet fully-featured Flux pattern implemented in Swift

Brief Documentation

You can read more about Flux pattern here

FluxAction

FluxAction usually a plain object that carries information describing the context of particular user action or system event. Actions dispatched via FluxDispatcher implementation and broadcasted to all the workers registered in the dispatcher. FluxStore could modify its state in response to actions. FluxMiddleware could start asynchronous work and report back by broadcasting another actions.

Example declaration:

enum Action {
    struct UpdateName: FluxAction {
        let name: String
    }
}

Example of sending the action with the default dispatcher on the main thread:

Action.UpdateName(name: "Great Name").dispatch()

FluxDispatcher

FluxDispatcher dispatches an actions to the mix of workers, which could be stores, middlewares or even other dispatchers. Actions dispatched from one worker to another and can be modified and/or replaced by the workers. Be careful of the order of the workers, it is important and can cause behavioural artefacts if not configured correctly.

Usage example:

let dispatcher = FluxDispatcher() 

dispatcher.register(workers: [SomeStore(), SomeMiddleware(dispatcher: dispatcher), AnalyticsLogger()])

SomeAction(value: Value()).dispatch(with: dispatcher)

FluxStore

FluxStore is a container that manages the underlaying state, broadcasts notifications about state changes. FluxStore is ObservableObject and can be used directly in SwiftUI view to render a state. State can be modified by sending relevant action to the dispatcher, where FluxStore instance is registered. FluxStore syncs state changes on main queue.

Example declaration:

class SomeStore: FluxStore<SomeState> {

    init() {
        super.init(initialState: SomeState())

        registerReducer(for: SomeAction.self) { (state, action) in
            state.value = action.value
            return [\SomeState.value] // Reports which part of the state did change
        }
    }
}

FluxMiddleware

FluxMiddleware is intended to be used to run asynchronous work and update other components by sending appropriate actions to the dispatcher.

Example declaration:

class SomeMiddleware: FluxMiddleware {

    private weak var dispatcher: FluxDispatcher?
    
    init(dispatcher: FluxDispatcher) {
        self.dispatcher = dispatcher
        
        super.init()

        registerHandler(for: SomeAction.self) { [unowned self] (action) in
            self.performWork(value: action.value) // Start async work
            // Handler passes the same action to subsequent workers by default
        }
        
        registerComposer(for: OtherAction.self) { (action) in
            return .next(ThirdAction()) // Pass another action to subsequent workers
        }
        
        registerComposer(for: IgnoredAction.self) { (action) in
            return .stop() // Do not pass the action to subsequent workers
        }
    }
    
    private func performWork(value: Value) {
        RemoteAPI.sendRequest(value: value) { [weak self] in
            SomeOtherAction(outcome: $0).dispatch(with: self?.dispatcher)
        }
    }
}

Author

Natan Zalkin [email protected]

License

ClassyFlux is available under the MIT license. See the LICENSE file for more info.

classyflux's People

Contributors

cozmonate avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.