Skip to content

Instantly share code, notes, and snippets.

@fonkadelic
Created November 12, 2019 12:53
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 fonkadelic/4dfbec5d632493823c22ab0bc1c88a42 to your computer and use it in GitHub Desktop.
Save fonkadelic/4dfbec5d632493823c22ab0bc1c88a42 to your computer and use it in GitHub Desktop.
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