Skip to content

Commit

Permalink
Merge pull request ReactiveCocoa#2039 from ReactiveCocoa/nsnotif-prod…
Browse files Browse the repository at this point in the history
…ucer

NSNotificationCenter.rac_notifications returns a producer
  • Loading branch information
jspahrsummers committed May 23, 2015
2 parents 6710e0f + b8b6aa2 commit 41d52d4
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 6 deletions.
8 changes: 7 additions & 1 deletion ReactiveCocoa.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -449,6 +449,8 @@
D43F27A11A9F7E8A00C1AD76 /* RACDynamicPropertySuperclass.h in Headers */ = {isa = PBXBuildFile; fileRef = D43F279E1A9F7E8A00C1AD76 /* RACDynamicPropertySuperclass.h */; settings = {ATTRIBUTES = (Public, ); }; };
D43F27A21A9F7E8A00C1AD76 /* RACDynamicPropertySuperclass.m in Sources */ = {isa = PBXBuildFile; fileRef = D43F279F1A9F7E8A00C1AD76 /* RACDynamicPropertySuperclass.m */; };
D43F27A31A9F7E8A00C1AD76 /* RACDynamicPropertySuperclass.m in Sources */ = {isa = PBXBuildFile; fileRef = D43F279F1A9F7E8A00C1AD76 /* RACDynamicPropertySuperclass.m */; };
D8170FC11B100EBC004192AD /* FoundationExtensionsSpec.swift in Sources */ = {isa = PBXBuildFile; fileRef = D8170FC01B100EBC004192AD /* FoundationExtensionsSpec.swift */; };
D8170FC21B100EBC004192AD /* FoundationExtensionsSpec.swift in Sources */ = {isa = PBXBuildFile; fileRef = D8170FC01B100EBC004192AD /* FoundationExtensionsSpec.swift */; };
/* End PBXBuildFile section */

/* Begin PBXContainerItemProxy section */
Expand Down Expand Up @@ -800,6 +802,7 @@
D0C3131D19EF2D9700984962 /* RACTestUIButton.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = RACTestUIButton.m; sourceTree = "<group>"; };
D43F279E1A9F7E8A00C1AD76 /* RACDynamicPropertySuperclass.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = RACDynamicPropertySuperclass.h; sourceTree = "<group>"; };
D43F279F1A9F7E8A00C1AD76 /* RACDynamicPropertySuperclass.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = RACDynamicPropertySuperclass.m; sourceTree = "<group>"; };
D8170FC01B100EBC004192AD /* FoundationExtensionsSpec.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = FoundationExtensionsSpec.swift; sourceTree = "<group>"; };
/* End PBXFileReference section */

/* Begin PBXFrameworksBuildPhase section */
Expand Down Expand Up @@ -1306,15 +1309,16 @@
isa = PBXGroup;
children = (
D021671C1A6CD50500987861 /* ActionSpec.swift */,
B696FB801A7640C00075236D /* TestError.swift */,
D0C312EE19EF2A7700984962 /* AtomicSpec.swift */,
D0C312EF19EF2A7700984962 /* BagSpec.swift */,
D0C312F019EF2A7700984962 /* DisposableSpec.swift */,
D8170FC01B100EBC004192AD /* FoundationExtensionsSpec.swift */,
D0A226101A72F30B00D33B74 /* ObjectiveCBridgingSpec.swift */,
D0A2260D1A72F16D00D33B74 /* PropertySpec.swift */,
D0C312F219EF2A7700984962 /* SchedulerSpec.swift */,
D0A2260A1A72E6C500D33B74 /* SignalProducerSpec.swift */,
D0A226071A72E0E900D33B74 /* SignalSpec.swift */,
B696FB801A7640C00075236D /* TestError.swift */,
);
path = Swift;
sourceTree = "<group>";
Expand Down Expand Up @@ -1725,6 +1729,7 @@
D03766EB19EDA60000A782A9 /* RACKVOWrapperSpec.m in Sources */,
D03766E719EDA60000A782A9 /* RACEventSpec.m in Sources */,
D03766F719EDA60000A782A9 /* RACSequenceSpec.m in Sources */,
D8170FC11B100EBC004192AD /* FoundationExtensionsSpec.swift in Sources */,
D03766C919EDA60000A782A9 /* NSObjectRACPropertySubscribingSpec.m in Sources */,
D03766C319EDA60000A782A9 /* NSObjectRACDeallocatingSpec.m in Sources */,
D03766BD19EDA60000A782A9 /* NSEnumeratorRACSequenceAdditionsSpec.m in Sources */,
Expand Down Expand Up @@ -1911,6 +1916,7 @@
D03766C619EDA60000A782A9 /* NSObjectRACLiftingSpec.m in Sources */,
B696FB821A7640C00075236D /* TestError.swift in Sources */,
D0C3131F19EF2D9700984962 /* RACTestExampleScheduler.m in Sources */,
D8170FC21B100EBC004192AD /* FoundationExtensionsSpec.swift in Sources */,
D03766D219EDA60000A782A9 /* NSURLConnectionRACSupportSpec.m in Sources */,
D03766F419EDA60000A782A9 /* RACSequenceAdditionsSpec.m in Sources */,
D0C3131419EF2B2000984962 /* SchedulerSpec.swift in Sources */,
Expand Down
10 changes: 5 additions & 5 deletions ReactiveCocoa/Swift/FoundationExtensions.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,16 @@ import Foundation
import Result

extension NSNotificationCenter {
/// Returns a signal of notifications posted that match the given criteria.
/// This signal will not terminate naturally, so it must be explicitly
/// Returns a producer of notifications posted that match the given criteria.
/// This producer will not terminate naturally, so it must be explicitly
/// disposed to avoid leaks.
public func rac_notifications(name: String? = nil, object: AnyObject? = nil) -> Signal<NSNotification, NoError> {
return Signal { observer in
public func rac_notifications(name: String? = nil, object: AnyObject? = nil) -> SignalProducer<NSNotification, NoError> {
return SignalProducer { observer, disposable in
let notificationObserver = self.addObserverForName(name, object: object, queue: nil) { notification in
sendNext(observer, notification)
}

return ActionDisposable {
disposable.addDisposable {
self.removeObserver(notificationObserver)
}
}
Expand Down
39 changes: 39 additions & 0 deletions ReactiveCocoaTests/Swift/FoundationExtensionsSpec.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
//
// FoundationExtensionsSpec.swift
// ReactiveCocoa
//
// Created by Neil Pankey on 5/22/15.
// Copyright (c) 2015 GitHub. All rights reserved.
//

import Result
import Nimble
import Quick
import ReactiveCocoa

class FoundationExtensionsSpec: QuickSpec {
override func spec() {
describe("NSNotificationCenter.rac_notifications") {

it("should send notifications on the producer") {
let center = NSNotificationCenter.defaultCenter()
let producer = center.rac_notifications(name: "rac_notifications_test")

var notif: NSNotification? = nil
let disposable = producer.start(next: { notif = $0 })

center.postNotificationName("some_other_notification", object: nil)
expect(notif).to(beNil())

center.postNotificationName("rac_notifications_test", object: nil)
expect(notif?.name).to(equal("rac_notifications_test"))

notif = nil
disposable.dispose()

center.postNotificationName("rac_notifications_test", object: nil)
expect(notif).to(beNil())
}
}
}
}

0 comments on commit 41d52d4

Please sign in to comment.