diff --git a/VinceRP.xcodeproj/project.pbxproj b/VinceRP.xcodeproj/project.pbxproj index 3f3dafb..c06cdc9 100644 --- a/VinceRP.xcodeproj/project.pbxproj +++ b/VinceRP.xcodeproj/project.pbxproj @@ -65,7 +65,6 @@ 791F30F51C0392B1004AA9E2 /* Timer.swift in Sources */ = {isa = PBXBuildFile; fileRef = 791F30AA1C03924C004AA9E2 /* Timer.swift */; }; 791F31051C0392C0004AA9E2 /* Box.swift in Sources */ = {isa = PBXBuildFile; fileRef = 791F30F71C0392C0004AA9E2 /* Box.swift */; }; 791F31061C0392C0004AA9E2 /* CommonConstants.swift in Sources */ = {isa = PBXBuildFile; fileRef = 791F30F81C0392C0004AA9E2 /* CommonConstants.swift */; }; - 791F31071C0392C0004AA9E2 /* EquatableArray.swift in Sources */ = {isa = PBXBuildFile; fileRef = 791F30F91C0392C0004AA9E2 /* EquatableArray.swift */; }; 791F31081C0392C0004AA9E2 /* ExceptionHandling.swift in Sources */ = {isa = PBXBuildFile; fileRef = 791F30FA1C0392C0004AA9E2 /* ExceptionHandling.swift */; }; 791F31091C0392C0004AA9E2 /* Flattenable.swift in Sources */ = {isa = PBXBuildFile; fileRef = 791F30FB1C0392C0004AA9E2 /* Flattenable.swift */; }; 791F310A1C0392C0004AA9E2 /* FunctionalArray.swift in Sources */ = {isa = PBXBuildFile; fileRef = 791F30FC1C0392C0004AA9E2 /* FunctionalArray.swift */; }; @@ -79,7 +78,6 @@ 791F31121C0392C0004AA9E2 /* WeakSet.swift in Sources */ = {isa = PBXBuildFile; fileRef = 791F31041C0392C0004AA9E2 /* WeakSet.swift */; }; 791F31131C0392CB004AA9E2 /* Box.swift in Sources */ = {isa = PBXBuildFile; fileRef = 791F30F71C0392C0004AA9E2 /* Box.swift */; }; 791F31141C0392CB004AA9E2 /* CommonConstants.swift in Sources */ = {isa = PBXBuildFile; fileRef = 791F30F81C0392C0004AA9E2 /* CommonConstants.swift */; }; - 791F31151C0392CB004AA9E2 /* EquatableArray.swift in Sources */ = {isa = PBXBuildFile; fileRef = 791F30F91C0392C0004AA9E2 /* EquatableArray.swift */; }; 791F31161C0392CB004AA9E2 /* ExceptionHandling.swift in Sources */ = {isa = PBXBuildFile; fileRef = 791F30FA1C0392C0004AA9E2 /* ExceptionHandling.swift */; }; 791F31171C0392CB004AA9E2 /* Flattenable.swift in Sources */ = {isa = PBXBuildFile; fileRef = 791F30FB1C0392C0004AA9E2 /* Flattenable.swift */; }; 791F31181C0392CB004AA9E2 /* FunctionalArray.swift in Sources */ = {isa = PBXBuildFile; fileRef = 791F30FC1C0392C0004AA9E2 /* FunctionalArray.swift */; }; @@ -224,7 +222,6 @@ 791F30EA1C0392A7004AA9E2 /* ThreadLocal.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; name = ThreadLocal.swift; path = ../vincerp/Common/Threading/ThreadLocal.swift; sourceTree = ""; }; 791F30F71C0392C0004AA9E2 /* Box.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; name = Box.swift; path = ../vincerp/Common/Util/Box.swift; sourceTree = ""; }; 791F30F81C0392C0004AA9E2 /* CommonConstants.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; name = CommonConstants.swift; path = ../vincerp/Common/Util/CommonConstants.swift; sourceTree = ""; }; - 791F30F91C0392C0004AA9E2 /* EquatableArray.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; name = EquatableArray.swift; path = ../vincerp/Common/Util/EquatableArray.swift; sourceTree = ""; }; 791F30FA1C0392C0004AA9E2 /* ExceptionHandling.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; name = ExceptionHandling.swift; path = ../vincerp/Common/Util/ExceptionHandling.swift; sourceTree = ""; }; 791F30FB1C0392C0004AA9E2 /* Flattenable.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; name = Flattenable.swift; path = ../vincerp/Common/Util/Flattenable.swift; sourceTree = ""; }; 791F30FC1C0392C0004AA9E2 /* FunctionalArray.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; name = FunctionalArray.swift; path = ../vincerp/Common/Util/FunctionalArray.swift; sourceTree = ""; }; @@ -395,7 +392,6 @@ children = ( 791F30F71C0392C0004AA9E2 /* Box.swift */, 791F30F81C0392C0004AA9E2 /* CommonConstants.swift */, - 791F30F91C0392C0004AA9E2 /* EquatableArray.swift */, 791F30FA1C0392C0004AA9E2 /* ExceptionHandling.swift */, 791F30FB1C0392C0004AA9E2 /* Flattenable.swift */, 791F30FC1C0392C0004AA9E2 /* FunctionalArray.swift */, @@ -1130,7 +1126,6 @@ 791F312D1C039612004AA9E2 /* NSTextField+VinceRP.swift in Sources */, 791F311F1C0392CB004AA9E2 /* WeakReference.swift in Sources */, 791F30DE1C039286004AA9E2 /* Propagator.swift in Sources */, - 791F31151C0392CB004AA9E2 /* EquatableArray.swift in Sources */, 791F30D71C039286004AA9E2 /* ChangeObserver.swift in Sources */, 791F30F41C0392AC004AA9E2 /* ThreadLocal.swift in Sources */, ); @@ -1183,7 +1178,6 @@ 791F30B41C039260004AA9E2 /* Addable.swift in Sources */, 791F30D21C039273004AA9E2 /* Throttle.swift in Sources */, 02C489DE1BE3CBE6004FD281 /* UIResponder+VinceRP.swift in Sources */, - 791F31071C0392C0004AA9E2 /* EquatableArray.swift in Sources */, 795FF5521BB814D400E19285 /* UIActivityIndicatorView+VinceRP.swift in Sources */, 791F31091C0392C0004AA9E2 /* Flattenable.swift in Sources */, 791F30CE1C039273004AA9E2 /* Reducer.swift in Sources */, diff --git a/VinceRP/Common/Api.swift b/VinceRP/Common/Api.swift index a73882b..380a5c1 100644 --- a/VinceRP/Common/Api.swift +++ b/VinceRP/Common/Api.swift @@ -33,7 +33,7 @@ public func reactive(initValue: T) -> Source { return Source(initValue: initValue) } -public func reactive() -> Source { +public func reactive() -> Source { return Source() } diff --git a/VinceRP/Common/Core/BatchUpdate.swift b/VinceRP/Common/Core/BatchUpdate.swift index fac101e..b3a5a14 100644 --- a/VinceRP/Common/Core/BatchUpdate.swift +++ b/VinceRP/Common/Core/BatchUpdate.swift @@ -7,7 +7,7 @@ protocol UpdateProtocol { func ancestors() -> [NodeTuple] } -public struct BatchUpdate { +public struct BatchUpdate { var updates: [UpdateProtocol] init(_ v: Source, withValue t: T) { @@ -30,7 +30,7 @@ public struct BatchUpdate { } -struct Update: UpdateProtocol { +struct Update: UpdateProtocol { let v: Source let t: T diff --git a/VinceRP/Common/Core/Node.swift b/VinceRP/Common/Core/Node.swift index 6c95ae8..eba2ecd 100644 --- a/VinceRP/Common/Core/Node.swift +++ b/VinceRP/Common/Core/Node.swift @@ -3,7 +3,7 @@ // Copyright (c) 2015 Viktor Belenyesi. All rights reserved. // -public class Node: Hashable, Equatable { +public class Node: Hashable { private static var hashCounter = AtomicLong(0) private let childrenHolder = SpinSet>(WeakSet()) diff --git a/VinceRP/Common/Core/NodeTuple.swift b/VinceRP/Common/Core/NodeTuple.swift index efacdcf..b84d243 100644 --- a/VinceRP/Common/Core/NodeTuple.swift +++ b/VinceRP/Common/Core/NodeTuple.swift @@ -3,7 +3,7 @@ // Copyright (c) 2015 Viktor Belenyesi. All rights reserved. // -struct NodeTuple: Hashable, Equatable { +struct NodeTuple: Hashable { let source: Node let reactor: Node diff --git a/VinceRP/Common/Core/Source.swift b/VinceRP/Common/Core/Source.swift index a5a484d..f5e266c 100644 --- a/VinceRP/Common/Core/Source.swift +++ b/VinceRP/Common/Core/Source.swift @@ -5,7 +5,7 @@ private let noValueError = NSError(domain: "no value", code: -1, userInfo: nil) -public class Source: Hub { +public class Source: Hub { let state: AtomicReference>> diff --git a/VinceRP/Common/Threading/AtomicLong.swift b/VinceRP/Common/Threading/AtomicLong.swift index baf571b..9145ebe 100644 --- a/VinceRP/Common/Threading/AtomicLong.swift +++ b/VinceRP/Common/Threading/AtomicLong.swift @@ -5,7 +5,7 @@ public typealias long = Int32 -public class AtomicLong: Hashable, Equatable { +public class AtomicLong: Hashable { private let pointer: UnsafeMutablePointer = UnsafeMutablePointer < long>.alloc(1) diff --git a/VinceRP/Common/Util/EquatableArray.swift b/VinceRP/Common/Util/EquatableArray.swift deleted file mode 100644 index 8bba3a5..0000000 --- a/VinceRP/Common/Util/EquatableArray.swift +++ /dev/null @@ -1,54 +0,0 @@ -// -// EquatableArray.swift -// VinceRP -// -// Created by Viktor Belényesi on 11/3/15. -// Copyright © 2015 Viktor Belenyesi. All rights reserved. -// - -extension Array: Equatable {} - -func == - (lhs: C?, rhs: C?) -> Bool { - switch (lhs,rhs) { - case (.Some(let lhs), .Some(let rhs)): - return lhs == rhs - case (.None, .None): - return true - default: - return false - } -} - -public func ==(lhs: [T], rhs: [T]) -> Bool { - if lhs.count != rhs.count { - return false - } - - for index in 0..(lhs: [T], rhs: [T]) -> Bool { - return false -} - -public func ==(lhs: [T?], rhs: [T?]) -> Bool { - if lhs.count != rhs.count { - return false - } - - for index in 0..(targetObject: AnyObject, propertyName: String) -> Source? { + static func getEmitter(targetObject: AnyObject, propertyName: String) -> Source? { let key = getKey(targetObject, propertyName: propertyName) return propertyMap[key] as! Source? } - static func synthesizeEmitter(initValue: T) -> Source { + static func synthesizeEmitter(initValue: T) -> Source { return Source(initValue: initValue) } - static func synthesizeObserver(targetObject: AnyObject, propertyName: String, initValue: T) -> PropertyObserver { + static func synthesizeObserver(targetObject: AnyObject, propertyName: String, initValue: T) -> PropertyObserver { return PropertyObserver(targetObject: targetObject as! NSObject, propertyName: propertyName) { (currentTargetObject: NSObject, currentPropertyName:String, currentValue:AnyObject) in if let existingEmitter:Source = getEmitter(currentTargetObject, propertyName: propertyName) { existingEmitter.update(currentValue as! T) @@ -32,21 +32,21 @@ public class ReactivePropertyGenerator { } } - static func createEmitter(targetObject: AnyObject, propertyName: String, initValue: T) -> Source { + static func createEmitter(targetObject: AnyObject, propertyName: String, initValue: T) -> Source { let result = synthesizeEmitter(initValue) let key = getKey(targetObject, propertyName: propertyName) propertyMap[key] = result return result } - static func createObserver(targetObject: AnyObject, propertyName: String, initValue: T) -> PropertyObserver { + static func createObserver(targetObject: AnyObject, propertyName: String, initValue: T) -> PropertyObserver { let result = synthesizeObserver(targetObject, propertyName: propertyName, initValue: initValue) let key = getKey(targetObject, propertyName: propertyName) observerMap[key] = result return result } - public static func source(targetObject: AnyObject, propertyName: String, initValue: T, initializer: ((Source) -> ())? = nil) -> Source { + public static func source(targetObject: AnyObject, propertyName: String, initValue: T, initializer: ((Source) -> ())? = nil) -> Source { if let emitter:Source = getEmitter(targetObject, propertyName: propertyName) { return emitter } @@ -58,7 +58,7 @@ public class ReactivePropertyGenerator { return emitter } - public static func property(targetObject: AnyObject, propertyName: String, initValue: T, initializer: ((Source) -> ())? = nil) -> Hub { + public static func property(targetObject: AnyObject, propertyName: String, initValue: T, initializer: ((Source) -> ())? = nil) -> Hub { let result = source(targetObject, propertyName: propertyName, initValue: initValue, initializer: initializer) createObserver(targetObject, propertyName: propertyName, initValue: initValue) return result as Hub diff --git a/VinceRP/Common/Util/WeakReference.swift b/VinceRP/Common/Util/WeakReference.swift index 1a72107..9932e4b 100644 --- a/VinceRP/Common/Util/WeakReference.swift +++ b/VinceRP/Common/Util/WeakReference.swift @@ -4,7 +4,7 @@ // // https://github.com/scala/scala/blob/2.11.x/src/library/scala/ref/WeakReference.scala -public class WeakReference: Hashable, Equatable { +public class WeakReference: Hashable { public weak var value: T? public init(_ value: T) { diff --git a/VinceRP/Extension/AppKit/NSResponder+VinceRP.swift b/VinceRP/Extension/AppKit/NSResponder+VinceRP.swift index fde920a..0e9446e 100644 --- a/VinceRP/Extension/AppKit/NSResponder+VinceRP.swift +++ b/VinceRP/Extension/AppKit/NSResponder+VinceRP.swift @@ -7,11 +7,11 @@ import AppKit public extension NSResponder { - public func reactiveProperty(forProperty propertyName: String, initValue: T, initializer: ((Source) -> ())? = nil) -> Hub { + public func reactiveProperty(forProperty propertyName: String, initValue: T, initializer: ((Source) -> ())? = nil) -> Hub { return ReactivePropertyGenerator.property(self, propertyName: propertyName, initValue: initValue, initializer: initializer) } - public func reactiveEmitter(name propertyName: String, initValue: T) -> Source { + public func reactiveEmitter(name propertyName: String, initValue: T) -> Source { return ReactivePropertyGenerator.source(self, propertyName: propertyName, initValue: initValue) } diff --git a/VinceRP/Extension/UIKit/UIResponder+VinceRP.swift b/VinceRP/Extension/UIKit/UIResponder+VinceRP.swift index 4f1fe7d..6f91008 100644 --- a/VinceRP/Extension/UIKit/UIResponder+VinceRP.swift +++ b/VinceRP/Extension/UIKit/UIResponder+VinceRP.swift @@ -7,11 +7,11 @@ import UIKit public extension UIResponder { - public func reactiveProperty(forProperty propertyName: String, initValue: T, initializer: ((Source) -> ())? = nil) -> Hub { + public func reactiveProperty(forProperty propertyName: String, initValue: T, initializer: ((Source) -> ())? = nil) -> Hub { return ReactivePropertyGenerator.property(self, propertyName: propertyName, initValue: initValue, initializer: initializer) } - public func reactiveSource(name propertyName: String, initValue: T) -> Source { + public func reactiveSource(name propertyName: String, initValue: T) -> Source { return ReactivePropertyGenerator.source(self, propertyName: propertyName, initValue: initValue) } diff --git a/VinceRPTests/Common/SmokeSpec.swift b/VinceRPTests/Common/SmokeSpec.swift index 53874da..3a823ec 100644 --- a/VinceRPTests/Common/SmokeSpec.swift +++ b/VinceRPTests/Common/SmokeSpec.swift @@ -19,27 +19,6 @@ class SmokeSpec: QuickSpec { describe("basic") { context("reactive variable") { - - it("is happy without default value") { - // given - let a: Source = reactive() - - // then - expect(a.hasValue()) == false - - // when - a <- 1 - - // then - expect(a*) == 1 - expect(a.hasValue()) == true - - // when - a <- fakeError - - // then - expect(a.hasValue()) == true - } it("holds the initial value") { // given @@ -213,6 +192,48 @@ class SmokeSpec: QuickSpec { expect(f*) == "budapest=8+6" expect(counter) == 3 } + + it("is happy without default value") { + // given + let a: Source = reactive() + + // then + expect(a.hasValue()) == false + + // when + a <- 1 + + // then + expect(a*) == 1 + expect(a.hasValue()) == true + + // when + a <- fakeError + + // then + expect(a.hasValue()) == true + } + + + it("is works with optionals") { + // given + let a: Source = reactive(1) + + // then + expect(a*) == 1 + + // when + a <- nil + + // then + expect(a*).to(beNil()) + + // when + a <- fakeError + + // then + expect(a.toTry().isFailure()) == true + } } diff --git a/VinceRPTests/Common/Threading/AtomicReferenceSpec.swift b/VinceRPTests/Common/Threading/AtomicReferenceSpec.swift index 13682c3..a0dc1df 100644 --- a/VinceRPTests/Common/Threading/AtomicReferenceSpec.swift +++ b/VinceRPTests/Common/Threading/AtomicReferenceSpec.swift @@ -8,7 +8,7 @@ import Quick import Nimble -class Foo : Equatable { +class Foo: Equatable { let i: Int init(_ i: Int) { self.i = i