Skip to content

Added old value to .RemoveElement operation #22

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

Open
wants to merge 3 commits 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
18 changes: 9 additions & 9 deletions ReactiveArray/Operation.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ public enum Operation<T>: CustomDebugStringConvertible {
case Append(value: T)
case Insert(value: T, atIndex: Int)
case Update(value: T, atIndex: Int)
case RemoveElement(atIndex: Int)
case RemoveElement(atIndex: Int, oldValue : T)

public func map<U>(mapper: T -> U) -> Operation<U> {
let result: Operation<U>
Expand All @@ -24,8 +24,8 @@ public enum Operation<T>: CustomDebugStringConvertible {
result = Operation<U>.Insert(value: mapper(value), atIndex: index)
case .Update(let value, let index):
result = Operation<U>.Update(value: mapper(value), atIndex: index)
case .RemoveElement(let index):
result = Operation<U>.RemoveElement(atIndex: index)
case .RemoveElement(let index, let oldValue):
result = Operation<U>.RemoveElement(atIndex: index, oldValue: mapper(oldValue))
}
return result
}
Expand All @@ -39,8 +39,8 @@ public enum Operation<T>: CustomDebugStringConvertible {
description = ".Insert(value: \(value), atIndex:\(index))"
case .Update(let value, let index):
description = ".Update(value: \(value), atIndex:\(index))"
case .RemoveElement(let index):
description = ".RemoveElement(atIndex:\(index))"
case .RemoveElement(let index, let oldValue):
description = ".RemoveElement(atIndex:\(index), oldValue:\(oldValue))"
}
return description
}
Expand All @@ -53,8 +53,8 @@ public enum Operation<T>: CustomDebugStringConvertible {
return value
case .Update(let value, _):
return value
default:
return Optional.None
case .RemoveElement(_, let oldValue):
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The value property no longer needs to be Optional

return oldValue
}
}

Expand All @@ -68,8 +68,8 @@ public func ==<T: Equatable>(lhs: Operation<T>, rhs: Operation<T>) -> Bool {
return leftIndex == rightIndex && leftValue == rightValue
case (.Update(let leftValue, let leftIndex), .Update(let rightValue, let rightIndex)):
return leftIndex == rightIndex && leftValue == rightValue
case (.RemoveElement(let leftIndex), .RemoveElement(let rightIndex)):
return leftIndex == rightIndex
case (.RemoveElement(let leftIndex, let leftValue), .RemoveElement(let rightIndex, let rightValue)):
return leftIndex == rightIndex && leftValue == rightValue
default:
return false
}
Expand Down
6 changes: 4 additions & 2 deletions ReactiveArray/ReactiveArray.swift
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,8 @@ public final class ReactiveArray<T>: CollectionType, MutableCollectionType, Cust
}

public func removeAtIndex(index:Int) {
let operation: Operation<T> = .RemoveElement(atIndex: index)
let value = _elements[index]
let operation: Operation<T> = .RemoveElement(atIndex: index, oldValue: value)
_sink.sendNext(operation)
}

Expand All @@ -145,10 +146,11 @@ public final class ReactiveArray<T>: CollectionType, MutableCollectionType, Cust
_mutableCount.value = _elements.count
case .Update(let value, let index):
_elements[index] = value
case .RemoveElement(let index):
case .RemoveElement(let index, _):
_elements.removeAtIndex(index)
_mutableCount.value = _elements.count
}
}

}

10 changes: 5 additions & 5 deletions ReactiveArrayTests/OperationSpec.swift
Original file line number Diff line number Diff line change
Expand Up @@ -67,13 +67,13 @@ class OperationSpec: QuickSpec {
context("when the operation is a Delete operation") {

beforeEach {
operation = Operation.RemoveElement(atIndex: 5)
operation = Operation.RemoveElement(atIndex: 5, oldValue:10)
}

it("does nothing") {
it("maps the old value") {
let mappedOperation = operation.map { $0 * 2 }

let areEqual = mappedOperation == operation
let areEqual = mappedOperation == Operation.RemoveElement(atIndex: 5, oldValue: 20)
expect(areEqual).to(beTrue())
}

Expand Down Expand Up @@ -122,11 +122,11 @@ class OperationSpec: QuickSpec {
context("when the operation is an Remove operation") {

beforeEach {
operation = Operation.RemoveElement(atIndex: 5)
operation = Operation.RemoveElement(atIndex: 5, oldValue:10)
}

it("returns .None") {
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should change to returns the removed value

expect(operation.value).to(beNil())
expect(operation.value).to(equal(10))
}

}
Expand Down
22 changes: 13 additions & 9 deletions ReactiveArrayTests/ReactiveArraySpec.swift
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ private func waitForOperation<T>(fromProducer producer: SignalProducer<Operation
onAppend: T -> () = { fail("Invalid operation type: .Append(\($0))") },
onInsert: (T, Int) -> () = { fail("Invalid operation type: .Insert(\($0), \($1))") },
onUpdate: (T, Int) -> () = { fail("Invalid operation type: .Update(\($0), \($1))") },
onDelete: Int -> () = { fail("Invalid operation type: .Delete(\($0))") }) {
onDelete: (Int, T) -> () = { fail("Invalid operation type: .Delete(\($0))") }) {

waitUntil(timeout: 10.0) { done in
producer.startWithNext { operation in
Expand All @@ -27,8 +27,8 @@ private func waitForOperation<T>(fromProducer producer: SignalProducer<Operation
onInsert(value, index)
case .Update(let value, let index):
onUpdate(value, index)
case .RemoveElement(let index):
onDelete(index)
case .RemoveElement(let index, let value):
onDelete(index, value)
}
done()
}
Expand All @@ -42,7 +42,7 @@ private func waitForOperation<T>(fromSignal signal: Signal<Operation<T>, NoError
onAppend: T -> () = { fail("Invalid operation type: .Append(\($0))") },
onInsert: (T, Int) -> () = { fail("Invalid operation type: .Insert(\($0), \($1))") },
onUpdate: (T, Int) -> () = { fail("Invalid operation type: .Update(\($0), \($1))") },
onDelete: Int -> () = { fail("Invalid operation type: .Delete(\($0))") }) {
onDelete: (Int, T) -> () = { fail("Invalid operation type: .Delete(\($0))") }) {

let producer = SignalProducer<Operation<T>, NoError> { (observer, disposable) in signal.observe(observer) }
waitForOperation(fromProducer: producer, when: when, onAppend: onAppend, onInsert: onInsert, onUpdate: onUpdate, onDelete: onDelete)
Expand All @@ -53,7 +53,7 @@ private func waitForOperation<T>(fromArray array: ReactiveArray<T>,
onAppend: T -> () = { fail("Invalid operation type: .Append(\($0))") },
onInsert: (T, Int) -> () = { fail("Invalid operation type: .Insert(\($0), \($1))") },
onUpdate: (T, Int) -> () = { fail("Invalid operation type: .Update(\($0), \($1))") },
onDelete: Int -> () = { fail("Invalid operation type: .Delete(\($0))") }) {
onDelete: (Int, T) -> () = { fail("Invalid operation type: .Delete(\($0))") }) {

waitForOperation(fromSignal: array.signal, when: when, onAppend: onAppend, onInsert: onInsert, onUpdate: onUpdate, onDelete: onDelete)
}
Expand Down Expand Up @@ -152,8 +152,9 @@ class ReactiveArraySpec: QuickSpec {
when: {
array.removeAtIndex(1)
},
onDelete: { index in
onDelete: { index,value in
expect(index).to(equal(1))
expect(value).to(equal(2))
}
)
}
Expand Down Expand Up @@ -250,8 +251,9 @@ class ReactiveArraySpec: QuickSpec {
when: {
array.removeAtIndex(1)
},
onDelete: { index in
onDelete: { index,value in
expect(index).to(equal(1))
expect(value).to(equal(12))
}
)
}
Expand Down Expand Up @@ -327,8 +329,9 @@ class ReactiveArraySpec: QuickSpec {
when: {
a.removeAtIndex(0)
},
onDelete: { index in
onDelete: { index,value in
expect(index).to(equal(0))
expect(value).to(equal(1))
}
)
}
Expand Down Expand Up @@ -397,8 +400,9 @@ class ReactiveArraySpec: QuickSpec {
when: {
array.removeAtIndex(1)
},
onDelete: { index in
onDelete: { index,value in
expect(index).to(equal(1))
expect(value).to(equal(2))
}
)
}
Expand Down