Skip to content

Instantly share code, notes, and snippets.

View andersio's full-sized avatar

Anders Ha andersio

View GitHub Profile
//
// BenchmarksTests.swift
// BenchmarksTests
//
// Created by Anders on 23/7/2016.
// Copyright © 2016 Anders. All rights reserved.
//
import XCTest
import Foundation
// View model for each tweets.
class TweetViewModel {
let username: AnyProperty<String>
let content: AnyProperty<String>
let date: AnyProperty<String>
init(tweet: tweet, controller: controller) {
let dateFormatter = { … }
username = AnyProperty(tweet.property(forKeyPath: #keyPath(Tweet.name)))
content = AnyProperty(tweet.property(forKeyPath: #keyPath(Tweet.content)))
import Result
import ReactiveCocoa
import Foundation
/*:
## Sandbox
A place where you can build your sand castles 🏖.
*/
import XCPlayground

Rationale

startWith* was a compromise made in ReactiveCocoa 4.0 as a means to provide disambiguated, trailing closure compatible shorthands to start. While they have been serving the community well since then, the community of Swift has established a new set of API guidelines for Swift. It specifically asks for API designs to look as grammatical as possible at the call site.

startWith* is probably the APIs with the largest impact in ReativeSwift. Unfortunately, it is grammatically incorrect in its use of preposition IMO. For example, let’s say we have a line producer.startWithNext { value in action(value) }. At the call site, it is generally read as:

Start [a producer] with Next, and pipe the value into the action.  

The problem here is that we do not start a producer with next events, values or errors, but for the next events, the values and the errors. We start it for receiving the values, for any po

@andersio
andersio / gist:9a3e1da44f986ab8fd64b8b2bbbe1859
Last active July 4, 2016 07:08
Revisiting `startWith`.
Changes to the Protocol Hierarchy
  ----------------------------         --------------------------        
  | SignalProducerObservable |    ←    | SignalProducerProtocol |  
  ----------------------------         --------------------------  
               ↑                                   ↑  
  ---------------------------              ------------------  
  | FinalizedSignalProducer |              | SignalProducer |  
  ---------------------------              ------------------  
  
struct LargeStruct {
var a: Int = 0
var b: Int = 1
var c: Int = 2
var d: Int = 3
var e: Int = 4
var f: Int = 5
var g: Int = 6
var h: Int = 7
class Atomic<Value> {
/// ...
/// Atomically modifies the variable.
///
/// Returns the old value.
public func modify(@noescape action: (inout Value) throws -> Void) rethrows -> Value {
return try withValue { value in
try action(&_value)
return value
diff --git a/ReactiveCocoa/Swift/Signal.swift b/ReactiveCocoa/Swift/Signal.swift
index 5e628eb..17b0d96 100644
--- a/ReactiveCocoa/Swift/Signal.swift
+++ b/ReactiveCocoa/Swift/Signal.swift
@@ -84,11 +84,7 @@ public final class Signal<Value, Error: ErrorType> {
}
deinit {
- atomicObservers.withValue { observers in
- if let observers = observers where observers.isEmpty {
@andersio
andersio / prototype.swift
Last active May 27, 2016 14:56
prototype
public protocol PropertyDescribing {
/// The type or metatype the property belongs to.
associatedtype Container
/// The expected static type.
associatedtype Content
/// The name of the property.
var name: String { get }
/// A relay of `AnyProperty`, enforcing the consistent order of `value` and signal
/// events.
final private class AnyPropertyRelay<Value> {
let atomicBox: Atomic<Value?>
let disposable: CompositeDisposable
let (relaySignal, relayObserver) = Signal<Value, NoError>.pipe()
var value: Value {
return atomicBox.value!
}