Skip to content
This repository has been archived by the owner on Dec 12, 2021. It is now read-only.

Add additional | operator for table-style mapping with World injection #27

Merged
merged 1 commit into from
Oct 22, 2020

Conversation

inamiy
Copy link
Owner

@inamiy inamiy commented Oct 22, 2020

By #26 , table-style mapping becomes hard to inject World, so this PR adds extra func | to resolve the issue.

Example

Previously:

        /// Sends `.loginOK` after delay, simulating async work during `.loggingIn`.
        let loginOKEffect = Effect<S>(queue: .request) { world in
            Just(StateDiagram.Input.loginOK)
                .delay(for: world.simulatedAsyncWorkDelay, scheduler: world.scheduler)
        }

        /// Sends `.logoutOK` after delay, simulating async work during `.loggingOut`.
        let logoutOKEffect = Effect<S>(queue: .request) { world in
            Just(StateDiagram.Input.logoutOK)
                .delay(for: world.simulatedAsyncWorkDelay, scheduler: world.scheduler)
        }

        let canForceLogout: (State) -> Bool = [.loggingIn, .loggedIn].contains

        let mappings: [StateDiagram.EffectMapping<S>] = [
            .login    | .loggedOut  => .loggingIn  | loginOKEffect,
            .loginOK  | .loggingIn  => .loggedIn   | .empty,
            .logout   | .loggedIn   => .loggingOut | logoutOKEffect,
            .logoutOK | .loggingOut => .loggedOut  | .empty,

            .forceLogout | canForceLogout => .loggingOut | logoutOKEffect
        ]

This PR will allow to define closure-style Effect as follows:

        /// Sends `.loginOK` after delay, simulating async work during `.loggingIn`.
        let loginOKEffect = { (world: World<S>) -> Effect in
            Just(StateDiagram.Input.loginOK)
                .delay(for: world.simulatedAsyncWorkDelay, scheduler: world.scheduler)
                .toEffect(queue: .request)
        }

        /// Sends `.logoutOK` after delay, simulating async work during `.loggingOut`.
        let logoutOKEffect = { (world: World<S>) -> Effect in
            Just(StateDiagram.Input.logoutOK)
                .delay(for: world.simulatedAsyncWorkDelay, scheduler: world.scheduler)
                .toEffect(queue: .request)
        }

@inamiy inamiy merged commit 1d6e977 into master Oct 22, 2020
@inamiy inamiy deleted the table-style-world-injection branch October 22, 2020 17:41
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

1 participant