Created
November 12, 2019 12:53
-
-
Save fonkadelic/4dfbec5d632493823c22ab0bc1c88a42 to your computer and use it in GitHub Desktop.
This file contains 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
public final class EagerEffect<A> { | |
private var callbacks: [(A) -> Void] = [] | |
private var value: A? { | |
didSet { value.map(notify) } | |
} | |
public init(run: @escaping (@escaping (A) -> Void) -> Void) { | |
run { self.value = $0 } | |
} | |
public func run(_ observer: @escaping (A) -> Void) { | |
value.map(observer) | |
callbacks.append(observer) | |
} | |
private func notify(_ a: A) { | |
callbacks.forEach { callback in | |
callback(a) | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment