Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add concurrency helpers for EffectHandler and EventSource #206

Open
wants to merge 4 commits into
base: master
Choose a base branch
from

Conversation

kmcbride
Copy link
Collaborator

@kmcbride kmcbride commented Feb 16, 2024

Effect Router

These changes introduce support for asynchronous effect route handlers by wrapping the existing EffectHandler route to create and callback from a new task. It currently supports routing to async functions that return a single event, a sequence of events, or nothing at all.

Additionally, a .receiveOn(queue:) synchronization helper has been added so that callback events can be moved to the loop queue — a necessity if routing to event-producing async functions and not using MobiusController, but also generally useful to all effect handlers that would otherwise need to dispatch their callbacks on a specific queue.

let effectRouter = EffectRouter<EffectType, EventType>()
    
    // Routing to async func references
    .routeCase(EffectType.caseName1).to(asyncFunc)
    .routeCase(EffectType.caseName2).to(asyncThrowsFunc)
    // Routing to async func that produces events that need to be received on a specific queue
    .routeCase(EffectType.caseName5).receiveOn(queue: loopQueue).to(asyncEventProducingFunc)
    // Routing to async closures
    .routeCase(EffectType.caseName6).to { @MainActor (effectAssociatedValue: EffectAssociatedValueType) async in
        await someAsyncCode(input: effectAssociatedValue)
    }

Event Source

These changes also introduce support for adding an AsyncSequence to the CompositeEventSourceBuilder.

let eventSourceBuilder = CompositeEventSourceBuilder<EventType>()
    
    // The `receiveOn consumerQueue:` parameter is intended to be used when building a `MobiusLoop`
    // so that events can be sent back to the loop queue from the task
    .addEventSource(eventProducingSequence, receiveOn: loopQueue)
    // It can safely be omitted when building a `MobiusController`
    // because queue switching is handled automatically
    .addEventSource(eventProducingSequence)

@kmcbride kmcbride force-pushed the async-effect-routes branch from b3e2a1d to 57427e9 Compare February 16, 2024 19:11
@kmcbride kmcbride mentioned this pull request Feb 16, 2024
@kmcbride kmcbride force-pushed the async-effect-routes branch 5 times, most recently from 8629f2d to 123a981 Compare February 23, 2024 21:26
@kmcbride kmcbride changed the title Add asynchronous effect handling routes Add concurrency helpers for EffectHandler and EventSource Feb 24, 2024
@kmcbride kmcbride force-pushed the async-effect-routes branch 4 times, most recently from 0ca6e3e to 6984d35 Compare February 25, 2024 18:33
@kmcbride kmcbride force-pushed the async-effect-routes branch from 6984d35 to 4ab4b74 Compare February 27, 2024 17:47
@kmcbride kmcbride marked this pull request as ready for review March 4, 2024 15:07
@kmcbride kmcbride force-pushed the async-effect-routes branch from 3c8e774 to ae85b57 Compare March 11, 2024 15:10
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant