Skip to content

Instantly share code, notes, and snippets.

@PaulWoodIII
Created August 31, 2019 22:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save PaulWoodIII/5290ea14bc92ead1b548bdb120f3096e to your computer and use it in GitHub Desktop.
Save PaulWoodIII/5290ea14bc92ead1b548bdb120f3096e to your computer and use it in GitHub Desktop.
helpful object that will emit regularly
//
// Pulse.swift
//
// Created by Paul Wood on 8/31/19.
// Copyright © 2019 Paul Wood. All rights reserved.
//
import Foundation
import Combine
class Pulse: ObservableObject {
var shouldEmmit: CurrentValueSubject<Bool, Never>
public var publisher: AnyPublisher<Void, Never>
init() {
let shouldEmmit = CurrentValueSubject<Bool, Never>(false)
let publisher = Timer.publish(every: 0.5, on: RunLoop.main, in: .default)
.autoconnect()
.filter({ _ in shouldEmmit.value })
.map { _ -> Void in }
.eraseToAnyPublisher()
self.shouldEmmit = shouldEmmit
self.publisher = publisher
}
public func toggle() {
self.shouldEmmit.send(!self.shouldEmmit.value)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment