Skip to content

Commit

Permalink
Introduce takingOver Signal initializer.
Browse files Browse the repository at this point in the history
  • Loading branch information
srdanrasic committed May 2, 2019
1 parent faffcfc commit eb6e7d1
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -53,4 +53,13 @@ Signal<Int, NSError> { observer in
}
//.observe { print($0) }

var didTapReload: () -> Void = {}
let reloadTaps = Signal(takingOver: &didTapReload)

reloadTaps
//.observeNext { print("reload") }

didTapReload()
didTapReload()

//: [Next](@next)
38 changes: 37 additions & 1 deletion Sources/Signal.swift
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,12 @@ extension Signal {
return NonDisposable.instance
}
}

/// Create a signal and an observer that can be used to send events on the signal.
public static func withObserver() -> (Signal<Element, Error>, AnyObserver<Element, Error>) {
let subject = PublishSubject<Element, Error>()
return (subject.toSignal(), AnyObserver(observer: subject.on))
}
}

extension Signal {
Expand Down Expand Up @@ -209,11 +215,41 @@ extension Signal {

extension Signal where Error == Swift.Error {

/// Creates a new signal by evaluating a throwing closure, capturing the
/// Create a new signal by evaluating a throwing closure, capturing the
/// returned value as a next event followed by a completion event, or any thrown error as a failure event.
///
/// - Parameter body: A throwing closure to evaluate.
public init(catching body: @escaping () throws -> Element) {
self = Signal(result: Result(catching: body))
}
}

extension Signal where Error == Never {

/// Create a new signal and assign its next element observer to the given variable.
/// Calling the closure assigned to the varaible will send the next element on the signal.
///
/// - Parameter nextObserver: A variable that will be assigned the observer of next elements.
///
/// - Note: Calling this initializer will replace the value of the given variable.
public init(takingOver nextObserver: inout (Element) -> Void) {
let (signal, observer) = Signal.withObserver()
nextObserver = { observer.next($0) }
self = signal
}
}

extension Signal where Element == Void, Error == Never {

/// Create a new signal and assign its next element observer to the given variable.
/// Calling the closure assigned to the varaible will send the next element on the signal.
///
/// - Parameter nextObserver: A variable that will be assigned the observer of next elements.
///
/// - Note: Calling this initializer will replace the value of the given variable.
public init(takingOver nextObserver: inout () -> Void) {
let (signal, observer) = Signal.withObserver()
nextObserver = { observer.next() }
self = signal
}
}
2 changes: 1 addition & 1 deletion Supporting Files/Info.plist
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
<key>CFBundlePackageType</key>
<string>FMWK</string>
<key>CFBundleShortVersionString</key>
<string>3.11.0</string>
<string>3.12.0</string>
<key>CFBundleSignature</key>
<string>????</string>
<key>CFBundleVersion</key>
Expand Down

0 comments on commit eb6e7d1

Please sign in to comment.