Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

swift formatted code. v5 #252

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,33 +1,33 @@
//: [Previous](@previous)

import Foundation
import ReactiveKit
import PlaygroundSupport
import ReactiveKit

PlaygroundPage.current.needsIndefiniteExecution = true

//: # Creating Signals
//: Uncomment the `observe { ... }` line to explore the behaviour!

SafeSignal(just: "Jim")
//.observe { print($0) }
// .observe { print($0) }

SafeSignal(just: "Jim after 1 second", after: 1)
//.observe { print($0) }
// .observe { print($0) }

SafeSignal(sequence: [1, 2, 3])
//.observe { print($0) }
// .observe { print($0) }

SafeSignal(sequence: [1, 2, 3], interval: 1)
//.observe { print($0) }
// .observe { print($0) }

SafeSignal(sequence: 1..., interval: 1)
//.observe { print($0) }
// .observe { print($0) }

SafeSignal(performing: {
(0...1000).reduce(0, +)
(0 ... 1000).reduce(0, +)
})
//.observe { print($0) }
// .observe { print($0) }

Signal<String, NSError>(evaluating: {
if let file = try? String(contentsOf: URL(fileURLWithPath: "list.txt")) {
Expand All @@ -36,12 +36,12 @@ Signal<String, NSError>(evaluating: {
return .failure(NSError(domain: "No such file", code: 0, userInfo: nil))
}
})
//.observe { print($0) }
// .observe { print($0) }

Signal(catching: {
try String(contentsOf: URL(string: "https://pokeapi.co/api/v2/pokemon/ditto/")!, encoding: .utf8)
})
//.observe { print($0) }
// .observe { print($0) }

Signal<Int, NSError> { observer in
observer.receive(1)
Expand All @@ -51,13 +51,14 @@ Signal<Int, NSError> { observer in
print("disposed")
}
}
//.observe { print($0) }

// .observe { print($0) }

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

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

didTapReload()
didTapReload()
Expand Down
Original file line number Diff line number Diff line change
@@ -1,16 +1,15 @@
//: Playground - noun: a place where people can play

import ReactiveKit
import PlaygroundSupport
import ReactiveKit

//: Explore ReactiveKit here

enum MyError: Error {
case unknown
}

let a = Signal<Int, Error>(sequence: 0...4, interval: 0.5)
let b = SafeSignal(sequence: 0...2, interval: 2)
let a = Signal<Int, Error>(sequence: 0 ... 4, interval: 0.5)
let b = SafeSignal(sequence: 0 ... 2, interval: 2)

b.concat(with: b)

Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
//: [Previous](@previous)

import Foundation
import ReactiveKit
import PlaygroundSupport
import ReactiveKit

PlaygroundPage.current.needsIndefiniteExecution = true

Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
//: [Previous](@previous)

import Foundation
import ReactiveKit
import PlaygroundSupport
import ReactiveKit

PlaygroundPage.current.needsIndefiniteExecution = true

Expand All @@ -20,7 +20,6 @@ pokemons
.map { $0.uppercased() }
// .observe { print($0) }


// If we are interested only in some elements of the signal, for
// example in Pokemons whose name starts with "S", we can use the filter operator:

Expand Down Expand Up @@ -109,7 +108,6 @@ pokemonDetails

// Try commenting out `.shareReplay()` line and see what happens in that case!


// There are many more operators on signals. Let's go through few of them.

// When we are interested only in the first few elements, we can apply `take(first:)` operator:
Expand All @@ -136,7 +134,7 @@ pokemons
// in our case into the name, ignoring the number.

let aPokemonEverySecond = pokemons
.zip(with: SafeSignal(sequence: 0..., interval: 1)) { name, index in name }
.zip(with: SafeSignal(sequence: 0..., interval: 1)) { name, _ in name }

aPokemonEverySecond
// .observe { print($0) }
Expand All @@ -152,7 +150,7 @@ aPokemonEverySecond
// from the two signals.

aPokemonEverySecond
.combineLatest(with: SafeSignal(sequence: 0...6, interval: 0.5))
.combineLatest(with: SafeSignal(sequence: 0 ... 6, interval: 0.5))
.observe { print($0) }

//: [Next](@next)
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
//: [Previous](@previous)

import Foundation
import ReactiveKit
import PlaygroundSupport
import ReactiveKit
import UIKit

PlaygroundPage.current.needsIndefiniteExecution = true
Expand Down Expand Up @@ -33,7 +33,8 @@ class PokeProfile: UIView {
addSubview(stackView)
backgroundColor = .white
}
required init?(coder aDecoder: NSCoder) { fatalError() }

required init?(coder _: NSCoder) { fatalError() }
}

// Open Assistent Editor to see the view!
Expand Down
3 changes: 1 addition & 2 deletions Sources/Atomic.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,11 @@
import Foundation

final class Atomic<T> {

private var _value: T
private let lock: NSLocking

init(_ value: T, lock: NSLocking = NSRecursiveLock()) {
self._value = value
_value = value
self.lock = lock
}

Expand Down
34 changes: 12 additions & 22 deletions Sources/Bindable.swift
Original file line number Diff line number Diff line change
Expand Up @@ -26,25 +26,23 @@ import Foundation

/// Bindable is like an observer, but knows to manage the subscription by itself.
public protocol BindableProtocol {

/// Type of the received elements.
associatedtype Element

/// Establish a one-way binding between the signal and the receiver.
/// - Warning: You are recommended to use `bind(to:)` on the signal when binding.
func bind(signal: Signal<Element, Never>) -> Disposable
}

extension SignalProtocol where Error == Never {

/// Establish a one-way binding between the source and the bindable.
/// - Parameter bindable: A binding target that will receive signal events.
/// - Returns: A disposable that can cancel the binding.
@discardableResult
public func bind<B: BindableProtocol>(to bindable: B) -> Disposable where B.Element == Element {
return bindable.bind(signal: toSignal())
}

/// Establish a one-way binding between the source and the bindable.
/// - Parameter bindable: A binding target that will receive signal events.
/// - Returns: A disposable that can cancel the binding.
Expand All @@ -55,7 +53,6 @@ extension SignalProtocol where Error == Never {
}

extension BindableProtocol where Self: SignalProtocol, Self.Error == Never {

/// Establish a two-way binding between the source and the bindable.
/// - Parameter target: A binding target that will receive events from
/// the receiver and a source that will send events to the receiver.
Expand All @@ -70,7 +67,6 @@ extension BindableProtocol where Self: SignalProtocol, Self.Error == Never {
}

extension SignalProtocol where Error == Never {

/// Bind the receiver to the target using the given setter closure. Closure is
/// called whenever the signal emits `next` event.
///
Expand All @@ -85,11 +81,10 @@ extension SignalProtocol where Error == Never {
/// - Returns: A disposable that can cancel the binding.
@discardableResult
public func bind<Target: Deallocatable>(to target: Target, setter: @escaping (Target, Element) -> Void) -> Disposable
where Target: BindingExecutionContextProvider
{
where Target: BindingExecutionContextProvider {
return bind(to: target, context: target.bindingExecutionContext, setter: setter)
}

/// Bind the receiver to the target using the given setter closure. Closure is
/// called whenever the signal emits `next` event.
///
Expand All @@ -112,7 +107,7 @@ extension SignalProtocol where Error == Never {
}
}
}

/// Bind the receiver to target's property specified by the key path. The property is
/// updated whenever the signal emits `next` event.
///
Expand All @@ -125,13 +120,12 @@ extension SignalProtocol where Error == Never {
/// - keyPath: A key path to the property that will be updated with each sent element.
/// - Returns: A disposable that can cancel the binding.
@discardableResult
public func bind<Target: Deallocatable>(to target: Target, keyPath: ReferenceWritableKeyPath<Target, Element>) -> Disposable where Target: BindingExecutionContextProvider
{
return bind(to: target) { (target, element) in
public func bind<Target: Deallocatable>(to target: Target, keyPath: ReferenceWritableKeyPath<Target, Element>) -> Disposable where Target: BindingExecutionContextProvider {
return bind(to: target) { target, element in
target[keyPath: keyPath] = element
}
}

/// Bind the receiver to target's property specified by the key path. The property is
/// updated whenever the signal emits `next` event.
///
Expand All @@ -145,16 +139,14 @@ extension SignalProtocol where Error == Never {
/// - context: An execution context on which to execute the setter.
/// - Returns: A disposable that can cancel the binding.
@discardableResult
public func bind<Target: Deallocatable>(to target: Target, keyPath: ReferenceWritableKeyPath<Target, Element>, context: ExecutionContext) -> Disposable
{
return bind(to: target, context: context) { (target, element) in
public func bind<Target: Deallocatable>(to target: Target, keyPath: ReferenceWritableKeyPath<Target, Element>, context: ExecutionContext) -> Disposable {
return bind(to: target, context: context) { target, element in
target[keyPath: keyPath] = element
}
}
}

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

/// Bind the receiver to the target using the given setter closure. Closure is
/// called whenever the signal emits `next` event.
///
Expand All @@ -169,11 +161,10 @@ extension SignalProtocol where Error == Never, Element == Void {
/// - Returns: A disposable that can cancel the binding.
@discardableResult
public func bind<Target: Deallocatable>(to target: Target, setter: @escaping (Target) -> Void) -> Disposable
where Target: BindingExecutionContextProvider
{
where Target: BindingExecutionContextProvider {
return bind(to: target, context: target.bindingExecutionContext, setter: setter)
}

/// Bind the receiver to the target using the given setter closure. Closure is
/// called whenever the signal emits `next` event.
///
Expand All @@ -200,7 +191,6 @@ extension SignalProtocol where Error == Never, Element == Void {

/// Provides an execution context used to deliver binding events.
public protocol BindingExecutionContextProvider {

/// An execution context used to deliver binding events.
var bindingExecutionContext: ExecutionContext { get }
}
Loading