Skip to content
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
4 changes: 4 additions & 0 deletions Observable-Swift/Event.swift
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,10 @@ public struct Event<T>: UnownableEvent {
return add(SubscriptionType(owner: owner, handler: handler))
}

public mutating func remove(owner owner : AnyObject) {
_subscriptions = _subscriptions.filter { $0.owner !== owner }
}

public mutating func unshare() {
// _subscriptions.unshare()
}
Expand Down
6 changes: 5 additions & 1 deletion Observable-Swift/EventReference.swift
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,10 @@ public class EventReference<T>: OwnableEvent {
return event.add(owner: owner, handler)
}

public func remove(owner owner : AnyObject) {
return event.remove(owner: owner)
}

public convenience init() {
self.init(event: Event<T>())
}
Expand All @@ -46,4 +50,4 @@ public class EventReference<T>: OwnableEvent {
self.event = event
}

}
}
5 changes: 4 additions & 1 deletion Observable-Swift/EventSubscription.swift
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ public class EventSubscription<T> {
public typealias HandlerType = T -> ()

internal var _valid : () -> Bool

internal weak var owner : AnyObject?

/// Handler to be caled when value changes.
internal var handler : HandlerType
Expand Down Expand Up @@ -54,6 +56,7 @@ public class EventSubscription<T> {
} else {
_valid = { [weak o] in o != nil }
}
owner = o
handler = h
}

Expand All @@ -66,4 +69,4 @@ public class EventSubscription<T> {
public func removeOwnedObject(o: AnyObject) {
_owned = _owned.filter{ $0 !== o }
}
}
}
14 changes: 14 additions & 0 deletions Observable-SwiftTests/ObservableTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,20 @@ class ObservableTests: XCTestCase {
XCTAssertEqual(y, 5, "Should not update after removed")
}

func testRemoveByOwner() {
var x = Observable(0)
var y = 0

x.afterChange.add(owner:self) { _ in y += 1 }

for i in 0..<5 { x <- i }

x.afterChange.remove(owner:self)

for i in 0..<5 { x <- i }
XCTAssertEqual(y, 5, "Should not update after removed")
}

func testValueAfterCopy() {
let original = Observable(0)
var copy = makeCopy(original)
Expand Down