Skip to content

Consider something like LifecycleWorker #71

Description

@bencochran

Kotlin has LifecycleWorker that makes it easy to perform side-effects when the worker is started or stopped.

At the moment, this is essentially equivalent to doing the following in Swift:

func render(state: State, context: RenderContext<SomeWorkflow>) -> Rendering {
    let onStart: () -> Void = …
    let onEnd: () -> Void = …

    context.runSideEffect(key: "lifecycle") { lifetime in
        onStart()
        lifetime.onEnded(onEnd)
    }

    …
}

We might consider making some convenience for this in Swift land. I’d hesitate tying it to Worker specifically since that lives in WorkflowReactiveSwift and this could be built more generally.

Open to suggestions on API shape

Activity

  1. AquaGeek commented on Oct 20, 2020

    @AquaGeek
    Collaborator

    I think it'd be easier if we tied this to Worker, but we could build it more generally:

    public struct LifecycleWorker {
        let key: AnyHashable
        let onStarted: () -> Void
        let onStopped: () -> Void
    
        init(key: AnyHashable, onStarted: @escaping () -> Void, onStopped: @escaping () -> Void) {
            self.key = key
    
            self.onStarted = onStarted
            self.onStopped = onStopped
        }
    }
    
    extension LifecycleWorker: AnyWorkflowConvertible {
        public typealias Rendering = Void
        public typealias Output = Never
    
        public func asAnyWorkflow() -> AnyWorkflow<Rendering, Output> {
            LifecycleWorkerWorkflow(worker: self).asAnyWorkflow()
        }
    }
    
    // Internal plumbing
    
    struct LifecycleWorkerWorkflow: Workflow {
        typealias State = Void
        typealias Rendering = Void
    
        private var worker: LifecycleWorker
    
        init(worker: LifecycleWorker) {
            self.worker = worker
        }
    
        func render(state: Void, context: RenderContext<LifecycleWorkerWorkflow>) -> Void {
            context.runSideEffect(key: worker.key) { lifetime in
                worker.onStarted()
                lifetime.onEnded(worker.onStopped)
            }
        }
    }

    Does it create confusion that this is a "worker" but the worker definition lives in the other module?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions