Blockchain ク gainst to the list

This article uses the current latest version of Xcode 13 + macOS Monterey + iOS15

Applying Sequence Operations

drop(untilOutputFrom:)

Events issued by Input are discarded until another Publisher is issued by the incoming parameter.

The flow chart is as follows:

Here’s an example:

var store = Set<AnyCancellable> ()let input = PassthroughSubject<Int.Never> ()let publisher = PassthroughSubject<String.Never> ()let output = PassthroughSubject<Int.Never>()

input.drop(untilOutputFrom: publisher).subscribe(output).store(in: &store)

output.sink(receiveCompletion: { print("Completed ".$0)}, receiveValue: { print("Received Value ".$0)})
    .store(in: &store)


input.send(1)
input.send(2)
publisher.send("any")
input.send(3)
input.send(4)
Copy the code

Output:

Received Value  3
Received Value  4
Copy the code

dropFirst(_:)

Pass an Int n to discard the first n events published for Input until the NTH event.

The flow chart is as follows:

drop(while:)/tryDrop(while:)

Pass in a closure that discards all Input events until the closure returns false and continues to publish. When false is returned, the closure is no longer called. TryDrop can throw an exception in a closure.

The flow chart is as follows:

append(_:)

There are three reloaded versions of append:

  • 1. Pass a dynamic argument list such as append(100, 200,300)

  • 2. Pass an array such as append([100, 200,300])

The two versions differ almost exclusively in the form of arguments. After the Input is published, the append content is inserted before finish.

The flow chart is as follows:

  • 3. Pass in a Publisher

    When the Input publishing event completes, Publisher publishing will continue until Publisher publishes Finish. Events published before Input published finish are discarded:

    The flow chart is as follows:

Here’s an example:

var store = Set<AnyCancellable> ()let input = PassthroughSubject<Int.Never> ()let output = PassthroughSubject<Int.Never> ()let publisher = PassthroughSubject<Int.Never>()

input.append(publisher).subscribe(output).store(in: &store)

output.sink(receiveCompletion: { print("Completed ".$0)}, receiveValue: { print("Received Value ".$0)})
    .store(in: &store)


input.send(1)
input.send(2)
input.send(3)
input.send(4)
publisher.send(100)
input.send(completion: .finished)
publisher.send(200)
publisher.send(300)
publisher.send(completion: .finished)
Copy the code

Output:

Received Value  1
Received Value  2
Received Value  3
Received Value  4
Received Value  200
Received Value  300
Completed  finished
Copy the code

prepend(_:)

In contrast to append(_:), an event is inserted before Input.

Prepend has three overloaded versions:

  • 1. Pass in a dynamic argument list such as prepend(100, 200,300)

  • 2. Pass an array such as prepend([100, 200,300])

The two versions differ almost exclusively in the form of arguments, inserting the prepend content immediately before the first event is published, regardless of whether the Input publishes the event or not.

Flow chart:

  • 3. Pass in a Publisher, publish the event of Publisher first, and publish the event of Input only after Publisher releases Finish, before which the event of Input will be discarded.

    The flow chart is as follows:

prefix(_:)

This is the opposite of dropFirst: passing an Int n will only republish the first n events of the Input. After all abandon. Publish Finish when the NTH element is published

The flow chart is as follows:

prefix(while:)/tryPrefix(while:)

Instead of drop(while:)/tryDrop(while:), pass a closure that republishes all Input events before the closure returns false. The closure returns false and publishes finish. TryPrefix can throw an exception in a closure.

Flow chart:

prefix(untilOutputFrom:)

Instead of drop(untilOutputFrom:), pass in a parameter Publisher and republish the Input published event until Publisher publishes the first event, then publish finish.

The process is as follows: