Created
August 31, 2019 22:56
-
-
Save PaulWoodIII/5290ea14bc92ead1b548bdb120f3096e to your computer and use it in GitHub Desktop.
helpful object that will emit regularly
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// 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