Skip to content

Commit 924d114

Browse files
committed
Performance improvements
1 parent be9d892 commit 924d114

19 files changed

+279
-305
lines changed

ReactiveKit.podspec

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
Pod::Spec.new do |s|
22
s.name = "ReactiveKit"
3-
s.version = "1.0.5"
3+
s.version = "1.0.6"
44
s.summary = "A Swift Reactive Programming Framework"
55
s.description = "ReactiveKit is a collection of Swift frameworks for reactive and functional reactive programming."
66
s.homepage = "https://github.com/ReactiveKit/ReactiveKit"
77
s.license = 'MIT'
88
s.author = { "Srdan Rasic" => "[email protected]" }
9-
s.source = { :git => "https://github.com/ReactiveKit/ReactiveKit.git", :tag => "v1.0.5" }
9+
s.source = { :git => "https://github.com/ReactiveKit/ReactiveKit.git", :tag => "v1.0.6" }
1010

1111
s.ios.deployment_target = '8.0'
1212
s.osx.deployment_target = '10.9'

ReactiveKit.xcodeproj/project.pbxproj

Lines changed: 29 additions & 26 deletions
Large diffs are not rendered by default.

ReactiveKit/Info.plist

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
<key>CFBundlePackageType</key>
1616
<string>FMWK</string>
1717
<key>CFBundleShortVersionString</key>
18-
<string>1.0.5</string>
18+
<string>1.0.6</string>
1919
<key>CFBundleSignature</key>
2020
<string>????</string>
2121
<key>CFBundleVersion</key>

ReactiveKit/Observable/MutableObservable.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ public struct MutableObservable<Value>: ObservableType {
3939
observable = Observable(value)
4040
}
4141

42-
public func observe(on context: ExecutionContext, observer: Value -> ()) -> DisposableType {
42+
public func observe(on context: ExecutionContext? = ImmediateOnMainExecutionContext, observer: Value -> ()) -> DisposableType {
4343
return observable.observe(on: context, observer: observer)
4444
}
4545
}

ReactiveKit/Observable/Observable.swift

Lines changed: 11 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -27,37 +27,22 @@ public protocol ObservableType: StreamType {
2727
var value: Value { get set }
2828
}
2929

30-
public class Observable<Value>: ActiveStream<Value>, ObservableType {
30+
public final class Observable<Value>: ActiveStream<Value>, ObservableType {
3131

3232
public var value: Value {
33-
get {
34-
return try! lastEvent()
35-
}
36-
set {
37-
capturedObserver?(newValue)
33+
didSet {
34+
next(value)
3835
}
3936
}
40-
41-
private var capturedObserver: (Value -> ())? = nil
42-
37+
4338
public init(_ value: Value) {
44-
var capturedObserver: (Value -> ())!
45-
super.init(limit: 1, producer: { observer in
46-
capturedObserver = observer
47-
observer(value)
48-
return nil
49-
})
50-
self.capturedObserver = capturedObserver
39+
self.value = value
40+
super.init()
5141
}
52-
53-
public init(@noescape producer: (Value -> ()) -> DisposableType?) {
54-
super.init(limit: 1, producer: { observer in
55-
return producer(observer)
56-
})
57-
}
58-
}
5942

60-
@warn_unused_result
61-
public func create<Value>(producer: (Value -> ()) -> DisposableType?) -> Observable<Value> {
62-
return Observable(producer: producer)
43+
public override func observe(on context: ExecutionContext? = ImmediateOnMainExecutionContext, observer: Observer) -> DisposableType {
44+
let disposable = super.observe(on: context, observer: observer)
45+
observer(value)
46+
return disposable
47+
}
6348
}

ReactiveKit/Internals/StreamBuffer.swift renamed to ReactiveKit/ObservableBuffer/ObservableBuffer.swift

Lines changed: 21 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -22,37 +22,34 @@
2222
// THE SOFTWARE.
2323
//
2424

25-
public class StreamBuffer<Event> {
26-
27-
public var array: [Event] = []
25+
public final class ObservableBuffer<Event>: ActiveStream<Event> {
26+
27+
public var buffer: [Event] = []
2828
public let limit: Int
29-
29+
3030
public init(limit: Int = Int.max) {
3131
self.limit = limit
32+
super.init()
3233
}
33-
34-
public func next(event: Event) {
35-
array.append(event)
36-
if array.count > limit {
37-
array = Array(array.suffixFrom(1))
38-
}
34+
35+
public init(limit: Int = Int.max, @noescape producer: Observer -> DisposableType?) {
36+
self.limit = limit
37+
super.init(producer: producer)
3938
}
40-
41-
public func replay(observer: Event -> ()) {
42-
for value in array {
43-
observer(value)
39+
40+
public override func next(event: Event) {
41+
buffer.append(event)
42+
if buffer.count > limit {
43+
buffer = Array(buffer.suffixFrom(1))
4444
}
45+
super.next(event)
4546
}
46-
47-
public func last() throws -> Event {
48-
if array.count > 0 {
49-
return array.last!
50-
} else {
51-
throw StreamBufferError.NoEvent
47+
48+
public override func observe(on context: ExecutionContext? = ImmediateOnMainExecutionContext, observer: Observer) -> DisposableType {
49+
let disposable = super.observe(on: context, observer: observer)
50+
for event in buffer {
51+
observer(event)
5252
}
53+
return disposable
5354
}
5455
}
55-
56-
public enum StreamBufferError: ErrorType {
57-
case NoEvent
58-
}

ReactiveKit/ObservableCollection/MutableObservableCollection.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,11 +36,11 @@ public struct MutableObservableCollection<Collection: CollectionType>: Observabl
3636
observableCollection = ObservableCollection(collection)
3737
}
3838

39-
public mutating func dispatch(event: ObservableCollectionEvent<Collection>) {
40-
observableCollection.dispatch(event)
39+
public mutating func next(event: ObservableCollectionEvent<Collection>) {
40+
observableCollection.next(event)
4141
}
4242

43-
public func observe(on context: ExecutionContext, observer: ObservableCollectionEvent<Collection> -> ()) -> DisposableType {
43+
public func observe(on context: ExecutionContext? = ImmediateOnMainExecutionContext, observer: ObservableCollectionEvent<Collection> -> ()) -> DisposableType {
4444
return observableCollection.observe(on: context, observer: observer)
4545
}
4646

ReactiveKit/ObservableCollection/ObservableCollection+Array.swift

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -27,38 +27,38 @@ extension ObservableCollectionType where Collection == Array<Element> {
2727
public mutating func append(x: Collection.Generator.Element) {
2828
var new = collection
2929
new.append(x)
30-
dispatch(ObservableCollectionEvent(collection: new, inserts: [collection.count], deletes: [], updates: []))
30+
next(ObservableCollectionEvent(collection: new, inserts: [collection.count], deletes: [], updates: []))
3131
}
3232

3333
public mutating func insert(newElement: Collection.Generator.Element, atIndex: Int) {
3434
var new = collection
3535
new.insert(newElement, atIndex: atIndex)
36-
dispatch(ObservableCollectionEvent(collection: new, inserts: [atIndex], deletes: [], updates: []))
36+
next(ObservableCollectionEvent(collection: new, inserts: [atIndex], deletes: [], updates: []))
3737
}
3838

3939
public mutating func insertContentsOf(newElements: [Collection.Generator.Element], at index: Collection.Index) {
4040
var new = collection
4141
new.insertContentsOf(newElements, at: index)
42-
dispatch(ObservableCollectionEvent(collection: new, inserts: Array(index..<index+newElements.count), deletes: [], updates: []))
42+
next(ObservableCollectionEvent(collection: new, inserts: Array(index..<index+newElements.count), deletes: [], updates: []))
4343
}
4444

4545
public mutating func removeAtIndex(index: Int) -> Collection.Generator.Element {
4646
var new = collection
4747
let element = new.removeAtIndex(index)
48-
dispatch(ObservableCollectionEvent(collection: new, inserts: [], deletes: [index], updates: []))
48+
next(ObservableCollectionEvent(collection: new, inserts: [], deletes: [index], updates: []))
4949
return element
5050
}
5151

5252
public mutating func removeLast() -> Collection.Generator.Element {
5353
var new = collection
5454
let element = new.removeLast()
55-
dispatch(ObservableCollectionEvent(collection: new, inserts: [], deletes: [new.count], updates: []))
55+
next(ObservableCollectionEvent(collection: new, inserts: [], deletes: [new.count], updates: []))
5656
return element
5757
}
5858

5959
public mutating func removeAll() {
6060
let deletes = Array(0..<collection.count)
61-
dispatch(ObservableCollectionEvent(collection: [], inserts: [], deletes: deletes, updates: []))
61+
next(ObservableCollectionEvent(collection: [], inserts: [], deletes: deletes, updates: []))
6262
}
6363

6464
public subscript(index: Collection.Index) -> Collection.Generator.Element {
@@ -68,7 +68,7 @@ extension ObservableCollectionType where Collection == Array<Element> {
6868
set {
6969
var new = collection
7070
new[index] = newValue
71-
dispatch(ObservableCollectionEvent(collection: new, inserts: [], deletes: [], updates: [index]))
71+
next(ObservableCollectionEvent(collection: new, inserts: [], deletes: [], updates: [index]))
7272
}
7373
}
7474
}

ReactiveKit/ObservableCollection/ObservableCollection+Dictionary.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -59,12 +59,12 @@ extension ObservableCollectionType where Index: DictionaryIndexType, Collection
5959
var new = collection
6060
if let index = new.indexForKey(key) {
6161
let oldValue = new.updateValue(value, forKey: key)
62-
dispatch(ObservableCollectionEvent(collection: new, inserts: [], deletes: [], updates: [index]))
62+
next(ObservableCollectionEvent(collection: new, inserts: [], deletes: [], updates: [index]))
6363
return oldValue
6464
} else {
6565
new.updateValue(value, forKey: key)
6666
let index = new.indexForKey(key)!
67-
dispatch(ObservableCollectionEvent(collection: new, inserts: [index], deletes: [], updates: []))
67+
next(ObservableCollectionEvent(collection: new, inserts: [index], deletes: [], updates: []))
6868
return nil
6969
}
7070
}
@@ -73,7 +73,7 @@ extension ObservableCollectionType where Index: DictionaryIndexType, Collection
7373
if let index = collection.indexForKey(key) {
7474
var new = collection
7575
let oldValue = new.removeValueForKey(key)
76-
dispatch(ObservableCollectionEvent(collection: new, inserts: [], deletes: [index], updates: []))
76+
next(ObservableCollectionEvent(collection: new, inserts: [], deletes: [index], updates: []))
7777
return oldValue
7878
} else {
7979
return nil

ReactiveKit/ObservableCollection/ObservableCollection+Set.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -43,9 +43,9 @@ extension ObservableCollectionType where Element: Hashable, Collection == Set<El
4343
new.insert(member)
4444

4545
if let index = collection.indexOf(member) {
46-
dispatch(ObservableCollectionEvent(collection: new, inserts: [], deletes: [], updates: [index]))
46+
next(ObservableCollectionEvent(collection: new, inserts: [], deletes: [], updates: [index]))
4747
} else {
48-
dispatch(ObservableCollectionEvent(collection: new, inserts: [new.indexOf(member)!], deletes: [], updates: []))
48+
next(ObservableCollectionEvent(collection: new, inserts: [new.indexOf(member)!], deletes: [], updates: []))
4949
}
5050
}
5151

@@ -54,7 +54,7 @@ extension ObservableCollectionType where Element: Hashable, Collection == Set<El
5454

5555
if let index = collection.indexOf(member) {
5656
let old = new.removeAtIndex(index)
57-
dispatch(ObservableCollectionEvent(collection: new, inserts: [], deletes: [index], updates: []))
57+
next(ObservableCollectionEvent(collection: new, inserts: [], deletes: [index], updates: []))
5858
return old
5959
} else {
6060
return nil

ReactiveKit/ObservableCollection/ObservableCollection.swift

Lines changed: 30 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -28,44 +28,37 @@ public protocol ObservableCollectionType: CollectionType, StreamType {
2828
typealias Element = Collection.Generator.Element
2929

3030
var collection: Collection { get }
31-
mutating func dispatch(event: ObservableCollectionEvent<Collection>)
31+
mutating func next(event: ObservableCollectionEvent<Collection>)
3232

33-
func observe(on context: ExecutionContext, observer: ObservableCollectionEvent<Collection> -> ()) -> DisposableType
33+
func observe(on context: ExecutionContext?, observer: ObservableCollectionEvent<Collection> -> ()) -> DisposableType
3434
}
3535

36-
public class ObservableCollection<Collection: CollectionType>: ActiveStream<ObservableCollectionEvent<Collection>>, ObservableCollectionType {
37-
38-
public private(set) var collection: Collection {
39-
get {
40-
return try! lastEvent().collection
41-
}
42-
set {
43-
dispatch(ObservableCollectionEvent(collection: newValue, inserts: [], deletes: [], updates: []))
44-
}
36+
public final class ObservableCollection<Collection: CollectionType>: ActiveStream<ObservableCollectionEvent<Collection>>, ObservableCollectionType {
37+
38+
private var collectionEvent: ObservableCollectionEvent<Collection>! = nil
39+
40+
public var collection: Collection {
41+
return collectionEvent.collection
4542
}
46-
47-
private var capturedObserver: (ObservableCollectionEvent<Collection> -> ())? = nil
48-
49-
public convenience init(_ collection: Collection) {
50-
var capturedObserver: (ObservableCollectionEvent<Collection> -> ())!
51-
52-
self.init() { observer in
53-
capturedObserver = observer
54-
observer(ObservableCollectionEvent.initial(collection))
55-
return nil
56-
}
57-
58-
self.capturedObserver = capturedObserver
43+
44+
public init(_ collection: Collection) {
45+
collectionEvent = ObservableCollectionEvent.initial(collection)
46+
super.init()
5947
}
6048

61-
public init(@noescape producer: (ObservableCollectionEvent<Collection> -> ()) -> DisposableType?) {
62-
super.init(limit: 1, producer: { observer in
63-
return producer(observer)
64-
})
49+
public override init(@noescape producer: (ObservableCollectionEvent<Collection> -> ()) -> DisposableType?) {
50+
super.init(producer: producer)
6551
}
66-
67-
public func dispatch(event: ObservableCollectionEvent<Collection>) {
68-
capturedObserver?(event)
52+
53+
public override func next(event: ObservableCollectionEvent<Collection>) {
54+
collectionEvent = event
55+
super.next(event)
56+
}
57+
58+
public override func observe(on context: ExecutionContext? = ImmediateOnMainExecutionContext, observer: Observer) -> DisposableType {
59+
let disposable = super.observe(on: context, observer: observer)
60+
observer(collectionEvent)
61+
return disposable
6962
}
7063

7164
// MARK: CollectionType conformance
@@ -111,18 +104,7 @@ public extension ObservableCollectionType {
111104
public mutating func replace(newCollection: Collection) {
112105
let deletes = Array(collection.indices)
113106
let inserts = Array(newCollection.indices)
114-
dispatch(ObservableCollectionEvent(collection: newCollection, inserts: inserts, deletes: deletes, updates: []))
115-
}
116-
117-
@warn_unused_result
118-
public func zipPrevious() -> Observable<(ObservableCollectionEvent<Collection>?, ObservableCollectionEvent<Collection>)> {
119-
return create { observer in
120-
var previous: ObservableCollectionEvent<Collection>? = nil
121-
return self.observe(on: ImmediateExecutionContext) { event in
122-
observer(previous, event)
123-
previous = event
124-
}
125-
}
107+
next(ObservableCollectionEvent(collection: newCollection, inserts: inserts, deletes: deletes, updates: []))
126108
}
127109
}
128110

@@ -132,7 +114,7 @@ public extension ObservableCollectionType where Collection.Index == Int {
132114
@warn_unused_result
133115
public func map<U>(transform: Collection.Generator.Element -> U) -> ObservableCollection<Array<U>> {
134116
return create { observer in
135-
return self.observe(on: ImmediateExecutionContext) { event in
117+
return self.observe(on: nil) { event in
136118
observer(event.map(transform))
137119
}
138120
}
@@ -142,7 +124,7 @@ public extension ObservableCollectionType where Collection.Index == Int {
142124
@warn_unused_result
143125
public func lazyMap<U>(transform: Collection.Generator.Element -> U) -> ObservableCollection<LazyMapCollection<Collection, U>> {
144126
return create { observer in
145-
return self.observe(on: ImmediateExecutionContext) { event in
127+
return self.observe(on: nil) { event in
146128
observer(event.lazyMap(transform))
147129
}
148130
}
@@ -155,7 +137,7 @@ public extension ObservableCollectionType where Collection.Index == Int {
155137
@warn_unused_result
156138
public func filter(include: Collection.Generator.Element -> Bool) -> ObservableCollection<Array<Collection.Generator.Element>> {
157139
return create { observer in
158-
return self.observe(on: ImmediateExecutionContext) { event in
140+
return self.observe(on: nil) { event in
159141
observer(event.filter(include))
160142
}
161143
}
@@ -168,7 +150,7 @@ public extension ObservableCollectionType where Collection.Index: Hashable {
168150
@warn_unused_result
169151
public func sort(isOrderedBefore: (Collection.Generator.Element, Collection.Generator.Element) -> Bool) -> ObservableCollection<Array<Collection.Generator.Element>> {
170152
return create { observer in
171-
return self.observe(on: ImmediateExecutionContext) { event in
153+
return self.observe(on: nil) { event in
172154
observer(event.sort(isOrderedBefore))
173155
}
174156
}
@@ -181,7 +163,7 @@ public extension ObservableCollectionType where Collection.Index: Equatable {
181163
@warn_unused_result
182164
public func sort(isOrderedBefore: (Collection.Generator.Element, Collection.Generator.Element) -> Bool) -> ObservableCollection<Array<Collection.Generator.Element>> {
183165
return create { observer in
184-
return self.observe(on: ImmediateExecutionContext) { event in
166+
return self.observe(on: nil) { event in
185167
observer(event.sort(isOrderedBefore))
186168
}
187169
}

0 commit comments

Comments
 (0)