Skip to content

[Idea] Add FlattenStrategy.throttle #549

@inamiy

Description

@inamiy

Related to FlattenStrategy.race implemented in #233, I thought it will be useful to have FlattenStrategy.raceContinuous (or case race(continuous: Bool)) FlattenStrategy.throttleas well.

In theory, this will work similar to .flatMap(.merge) { someAPIAction.apply($0) } but omitting Action which may sometimes become too verbose to declare (if we aren't interested in enabledIf, isExecuting, etc).

Use case

Example: Loading next API after scrollView reached bottom

let reachedBottom = Signal<(), NoError>.pipe()  // let's say, scrollView triggers this
let nextPageProperty = MutableProperty<Int>(0) // state

let responses = reachedBottom.output
    .withLatest(from: nextPageProperty.producer)
    .flatMap(.throttle) { nextPage -> SignalProducer<Response, NoError>in
        return loadNext(nextPage) // some API request
            .flatMapError { _ in .empty } // ignore network error
    }

nextPageProperty <~ responses.map { $0.nextPage }

responses
    .observeCompleted { print("done!") }

// main

reachedBottom.input.send(value: ())
// 1st loadNext progressing...
reachedBottom.input.send(value: ())
// 1st loadNext still progressing, no 2nd loadNext

wait(1) // wait until API finished
// 1st loadNext completed

reachedBottom.input.send(value: ())
// 2nd loadNext progressing...
reachedBottom.input.send(value: ())
// 2nd loadNext still progressing, no 3rd loadNext

reachedBottom.input.sendCompleted()
// 2nd loadNext still progressing, no print "done" yet

wait(1) // wait until API finished
// 2nd loadNext completed, prints "done"

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions