Swiftpack.co - shareup/simple-publisher as Swift Package

Swiftpack.co is a collection of thousands of indexed Swift packages. Search packages.
See all packages published by shareup.
shareup/simple-publisher v1.2.0
Easily make your class a Publisher for Combine in swift
⭐️ 2
🕓 3 years ago
iOS macOS watchOS tvOS
.package(url: "https://github.com/shareup/simple-publisher.git", from: "v1.2.0")

SimplePublisher

⚠️⚠️⚠️ ARCHIVED ⚠️⚠️⚠️

This repository has been archived because its purpose is best served by using the default PassthroughSubject.


Very quickly give your struct or class the ability to publish out to subscribers by conforming to SimplePublisher.

Installation

// swift-tools-version:5.1
// platforms: [.macOS(.v10_15), .iOS(.v13), .tvOS(.v13), .watchOS(.v5),],

.package(url: "https://github.com/shareup/simple-publisher.git", .upToNextMajor(from: "1.1.0")),

A full usage example

Assuming one has a very simple periodic emitter:

class Emitter {
    var timer: Timer?
    
    func start() {
        guard timer == nil else { return }
        self.timer = Timer.scheduledTimer(withTimeInterval: 2.0, repeats: true, block: trigger(_:))
    }
    
    private func trigger(_ timer: Timer) {
        print("💡 \(UUID().uuidString)")
    }
    
    func stop() {
        timer?.invalidate()
        self.timer = nil
    }
    
    deinit {
        stop()
    }
}

one can turn it into a SimplePublisher by subclassing:

struct Emitter: SimplePublisher {
    typealias Output = String
    typealias Failure = Never
    var subject = SimpleSubject<Output, Failure>()
    
    // ...
    
    private func trigger(_ timer: Timer) {
        publish(UUID().uuidString)
    }
    
    // ...
}

The main concepts to know are:

  • Publishers must indicate what the Output type is – what are you going to publish out for subscribers to receive?
  • Publishers must indicate the type for Failure – what type of Error are you going to emit to subscribers when something goes wrong?
  • Setup a SimpleSubject which takes care of all the Subscriber and Subscription bookeeping ⭐️

Subscribing

One can use sink or any other subscribers like forever:

let emitter = Emitter()
emitter.start()
emitter.sink { print("Sinked \($0)") } // will print once
emitter.forever { print("Tick → \($0)") } // will print over and over assuming forever is setup as a dependency

Things to remember

Combine doesn't clean up Subscribers for you, you need to cancel them or cancel the Subscription (especially in tests):

let subscriber = emitter.sink { print("Yo \($0)") }
defer { subscriber.cancel() }

See the tests in this repo for more examples.

Full example all together

class Emitter: SimplePublisher {
    typealias Output = String
    typealias Failure = Never
    var subject = SimpleSubject<Output, Failure>()
    
    var timer: Timer?
    
    func start() {
        guard timer == nil else { return }
        self.timer = Timer.scheduledTimer(withTimeInterval: 2.0, repeats: true, block: trigger(_:))
    }
    
    private func trigger(_ timer: Timer) {
        publish(UUID().uuidString)
    }
    
    func stop() {
        timer?.invalidate()
        self.timer = nil
    }
    
    deinit {
        stop()
    }
}

let emitter = Emitter()
emitter.start()

emitter.forever { print("Tick → \($0)") }

GitHub

link
Stars: 2
Last commit: 3 years ago
Advertisement: IndiePitcher.com - Cold Email Software for Startups

Swiftpack is being maintained by Petr Pavlik | @ptrpavlik | @swiftpackco | API | Analytics