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
9 changes: 8 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -155,4 +155,11 @@ xcuserdata
####
# UNKNOWN: recommended by others, but I can't discover what these files are
#
# ...none. Everything is now explained.
# ...none. Everything is now explained.


####
# Xcode VCS metadata
#

*.xccheckout
20 changes: 20 additions & 0 deletions LICENSE.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
The MIT License (MIT)

Copyright (c) 2013 Jason Jarrett

Permission is hereby granted, free of charge, to any person obtaining a copy of
this software and associated documentation files (the "Software"), to deal in
the Software without restriction, including without limitation the rights to
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
the Software, and to permit persons to whom the Software is furnished to do so,
subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
40 changes: 37 additions & 3 deletions PubSub.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,45 @@
@end


#define PubSub(name, type) \


// Variable expansion with variable arguments thanks to
// http://stackoverflow.com/questions/11761703/overloading-macro-on-number-of-arguments
// TODO look at a generic PubSub(name, ...) macro?


#define PubSub0(name) \
@interface Pub (PubSub_ ## name ## _Category) \
+ (void) name; \
@end\
@interface Sub (PubSub_ ## name ## _Category)\
+ (void) while:(id)obj name:(void(^)(void))callback;\
@end

#define PubSub1(name, arg1Type, arg1Name) \
@interface Pub (PubSub_ ## name ## _Category) \
+ (void) name:(arg1Type)arg1Name; \
@end\
@interface Sub (PubSub_ ## name ## _Category)\
+ (void) while:(id)obj name:(void(^)(arg1Type arg1Name))callback;\
@end

#define PubSub2(name, arg1Type, arg1Name, arg2Type, arg2Name) \
@interface Pub (PubSub_ ## name ## _Category) \
+ (void) name:(type*)arg; \
+ (void) name:(arg1Type)arg1Name arg2Name:(arg2Type)arg2Name; \
@end\
@interface Sub (PubSub_ ## name ## _Category)\
+ (void) while:(id)obj name:(void(^)(type*))callback;\
+ (void) while:(id)obj name:(void(^)(arg1Type arg1Name, arg2Type arg2Name))callback;\
@end


#define PubSub3(name, arg1Type, arg1Name, arg2Type, arg2Name, arg3Type, arg3Name) \
@interface Pub (PubSub_ ## name ## _Category) \
+ (void) name:(arg1Type)arg1Name arg2Name:(arg2Type)arg2Name arg3Name:(arg3Type)arg3Name; \
@end\
@interface Sub (PubSub_ ## name ## _Category)\
+ (void) while:(id)obj name:(void(^)(arg1Type arg1Name, arg2Type arg2Name, arg3Type arg3Name))callback;\
@end



89 changes: 75 additions & 14 deletions PubSub.m
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,6 @@
//

#import "PubSub.h"
#import <objc/runtime.h>
#import <objc/Protocol.h>
#import <Foundation/Foundation.h>


Expand All @@ -27,8 +25,6 @@ + (NSMethodSignature *) methodSignatureForSelector:(SEL)selector
signature = [self methodSignatureForSelector:@selector(mockSubscribeWhile:callback:)];
}
return signature;

return nil;
}

+ (void) forwardInvocation:(NSInvocation *)anInvocation
Expand All @@ -38,8 +34,14 @@ + (void) forwardInvocation:(NSInvocation *)anInvocation
__unsafe_unretained id keepAlive = nil;
[anInvocation getArgument:&keepAlive atIndex:2];

void(^callback)(id arg) = nil;
[anInvocation getArgument:&callback atIndex:3];
void(^callbackZero)(void) = nil;
void(^callbackOne)(id arg1) = nil;
void(^callbackTwo)(id arg1, id arg2) = nil;
void(^callbackThree)(id arg1, id arg2, id arg3) = nil;
[anInvocation getArgument:&callbackZero atIndex:3];
[anInvocation getArgument:&callbackOne atIndex:3];
[anInvocation getArgument:&callbackTwo atIndex:3];
[anInvocation getArgument:&callbackThree atIndex:3];

NSString* notificationName = [NSStringFromSelector(aSelector) componentsSeparatedByString:@":"][1];

Expand All @@ -52,7 +54,35 @@ + (void) forwardInvocation:(NSInvocation *)anInvocation
return;
}

callback(notification.object);
if(notification.userInfo.count == 0){
callbackZero();
return;
}

__block id (^getArgAtIndex)(NSString *) = ^id(NSString *index) {
id result = notification.userInfo[index];
if(result == [NSNull null]){
return nil;
}
return result;
};


if(notification.userInfo.count == 1){
callbackOne(getArgAtIndex(@"0"));
return;
}

if(notification.userInfo.count == 2){
callbackTwo(getArgAtIndex(@"0"), getArgAtIndex(@"1"));
return;
}

if(notification.userInfo.count == 3){
callbackThree(getArgAtIndex(@"0"), getArgAtIndex(@"1"), getArgAtIndex(@"2"));
return;
}

}];
}

Expand All @@ -62,25 +92,56 @@ + (void) forwardInvocation:(NSInvocation *)anInvocation

@implementation Pub

+ (void) mockPublish:(id)arg { }
+ (void) mockPublishZero { }
+ (void) mockPublishOne:(id)arg1 { }
+ (void) mockPublishTwo:(id)arg1 :(id)arg2 { }
+ (void) mockPublishThree:(id)arg1 :(id)arg2 :(id)arg3 { }

+ (NSMethodSignature *) methodSignatureForSelector:(SEL)selector
{

NSMethodSignature* signature = [super methodSignatureForSelector:selector];
if (!signature)
{
signature = [self methodSignatureForSelector:@selector(mockPublish:)];
NSString *selectorName = NSStringFromSelector(selector);
NSArray *selectorArgs = [selectorName componentsSeparatedByString:@":"];
int argCount = selectorArgs.count - 1;
if(argCount == 0) {
signature = [self methodSignatureForSelector:@selector(mockPublishZero)];
} else if(argCount == 1) {
signature = [self methodSignatureForSelector:@selector(mockPublishOne:)];
} else if(argCount == 2) {
signature = [self methodSignatureForSelector:@selector(mockPublishTwo::)];
} else if(argCount == 3) {
signature = [self methodSignatureForSelector:@selector(mockPublishThree:::)];
}
}
return signature;
}

+ (void)forwardInvocation:(NSInvocation *)i
+ (void)forwardInvocation:(NSInvocation *)anInvocation
{
id arg = nil;
[i getArgument:&arg atIndex:2];

NSString* notificationName = [NSStringFromSelector(i.selector) componentsSeparatedByString:@":"][0];
[[NSNotificationCenter defaultCenter] postNotificationName:notificationName object:arg];
NSString *selectorName = NSStringFromSelector(anInvocation.selector);
NSArray *selectorArgs = [selectorName componentsSeparatedByString:@":"];

NSString* notificationName = selectorArgs[0];

NSMutableDictionary *argsDict = [NSMutableDictionary new];

for (NSUInteger i = 0; i < (selectorArgs.count-1); i++) {
id arg = nil;
NSInteger paramIndex = i+2;
[anInvocation getArgument:&arg atIndex:paramIndex];

if(arg == nil){
arg = [NSNull null];
}

[argsDict setObject:arg forKey:[[NSNumber numberWithInt:i] stringValue]];
}

[[NSNotificationCenter defaultCenter] postNotificationName:notificationName object:nil userInfo:argsDict];
}

@end
16 changes: 16 additions & 0 deletions PubSub.podspec
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
Pod::Spec.new do |s|

s.name = "PubSub"
s.version = "0.0.1"
s.summary = "A minimal syntax block based wrapper around NSNotificaitonCenter."
s.homepage = "https://github.com/staxmanade/PubSub-iOS"
s.license = { :type => 'MIT', :file => 'LICENSE.md' }
s.author = { "Jason Jarrett" => "jason@elegantcode.com" }
s.authors = { "Josh Wright" => "josh@joshwright.com", "Jason Jarrett" => "jason@elegantcode.com"}
s.platform = :ios
s.ios.deployment_target = "5.0"
s.source = { :git => "https://github.com/staxmanade/PubSub-iOS.git", :tag => "0.0.1" }
s.source_files = 'PubSub.h', 'PubSub.m'
s.requires_arc = true

end
5 changes: 5 additions & 0 deletions TestProject/TestPubSub/TestPubSub.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
objects = {

/* Begin PBXBuildFile section */
58ABB18418BBC79000A2F782 /* PubSub.podspec in Resources */ = {isa = PBXBuildFile; fileRef = 58ABB18318BBC79000A2F782 /* PubSub.podspec */; };
BC23B537175E6CB800577890 /* UIKit.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = BC23B536175E6CB800577890 /* UIKit.framework */; };
BC23B539175E6CB800577890 /* Foundation.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = BC23B538175E6CB800577890 /* Foundation.framework */; };
BC23B53B175E6CB800577890 /* CoreGraphics.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = BC23B53A175E6CB800577890 /* CoreGraphics.framework */; };
Expand All @@ -18,6 +19,7 @@
/* End PBXBuildFile section */

/* Begin PBXFileReference section */
58ABB18318BBC79000A2F782 /* PubSub.podspec */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text; name = PubSub.podspec; path = ../../PubSub.podspec; sourceTree = "<group>"; };
BC23B532175E6CB800577890 /* TestPubSub.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = TestPubSub.app; sourceTree = BUILT_PRODUCTS_DIR; };
BC23B536175E6CB800577890 /* UIKit.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = UIKit.framework; path = System/Library/Frameworks/UIKit.framework; sourceTree = SDKROOT; };
BC23B538175E6CB800577890 /* Foundation.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = Foundation.framework; path = System/Library/Frameworks/Foundation.framework; sourceTree = SDKROOT; };
Expand Down Expand Up @@ -51,6 +53,7 @@
BC23B527175E6CB800577890 = {
isa = PBXGroup;
children = (
58ABB18318BBC79000A2F782 /* PubSub.podspec */,
BC23B562175EB67200577890 /* readme.md */,
BC23B53C175E6CB800577890 /* TestPubSub */,
BC23B535175E6CB800577890 /* Frameworks */,
Expand Down Expand Up @@ -159,6 +162,7 @@
isa = PBXResourcesBuildPhase;
buildActionMask = 2147483647;
files = (
58ABB18418BBC79000A2F782 /* PubSub.podspec in Resources */,
BC23B541175E6CB800577890 /* InfoPlist.strings in Resources */,
BC23B563175EB67200577890 /* readme.md in Resources */,
);
Expand Down Expand Up @@ -281,6 +285,7 @@
BC23B552175E6CB800577890 /* Release */,
);
defaultConfigurationIsVisible = 0;
defaultConfigurationName = Release;
};
/* End XCConfigurationList section */
};
Expand Down
87 changes: 68 additions & 19 deletions TestProject/TestPubSub/TestPubSub/AppDelegate.m
Original file line number Diff line number Diff line change
Expand Up @@ -24,35 +24,84 @@ - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(

keeper = [[NSObject alloc] init];

[Sub while:keeper nicknameChanged:^(NSString* newNickname) {
NSLog(@"nickname changed to: %@", newNickname);
//
// Zero arg subscription
//
[Sub while:keeper testWithZeroArgs:^{
NSLog(@"Zero args...");
}];

[Pub nicknameChanged:@"A"];
[Pub testWithZeroArgs];
[Pub testWithZeroArgs];
[Pub testWithZeroArgs];
[Pub testWithZeroArgs];

//
// One arg subscription
//
[Sub while:keeper testWithOneArg:^(NSString *a) {
NSLog(@"One arg: %@", a);
}];

[Pub testWithOneArg:@"A"];

[self performSelector:@selector(b) withObject:nil afterDelay:.1];
[self performSelector:@selector(c) withObject:nil afterDelay:.15];
[self performSelector:@selector(d) withObject:nil afterDelay:.2];

//
// Two arg subscription
//
[Sub while:keeper testWithTwoArgs:^(NSString *a, NSString *b) {
NSLog(@"Two args: a: %@, b: %@", a, b);
}];
[Pub testWithTwoArgs:@"a" b:@"B"];
[Pub testWithTwoArgs:@"A" b:@"b"];
[Pub testWithTwoArgs:@"aaa" b:@"bbb"];

//
// Three arg subscription
//
[Sub while:keeper testWithThreeArgs:^(NSString *a, NSString *b, NSString *c) {
NSLog(@"Three args: a: %@, b: %@, c: %@", a, b, c);
}];
[Pub testWithThreeArgs:@"a" b:@"b" c:@"c"];
[Pub testWithThreeArgs:@"a" b:@"b" c:@"c"];
[Pub testWithThreeArgs:@"a" b:@"b" c:@"c"];
[Pub testWithThreeArgs:@"a" b:@"b" c:@"c"];
[Pub testWithThreeArgs:@"a" b:@"b" c:@"c"];


[self testEventWithNilArguments];
[self testWithAnInt];

return YES;
}

- (void) b
{
NSLog(@"publishing b");
[Pub nicknameChanged:@"B"];
}

- (void) c
{
NSLog(@"keeper = nil");
keeper = nil;
- (void) testEventWithNilArguments {
[Sub while:self testWith1NilArguments:^(NSString *a) {
NSParameterAssert(a == nil);
}];
[Pub testWith1NilArguments:nil];

[Sub while:self testWith2NilArguments:^(NSString *a, NSString *b) {
NSParameterAssert(a == nil);
NSParameterAssert(b == nil);
}];
[Pub testWith2NilArguments:nil b:nil];

[Sub while:self testWith3NilArguments:^(NSString *a, NSString *b, NSString *c) {
NSParameterAssert(a == nil);
NSParameterAssert(b == nil);
NSParameterAssert(c == nil);
}];
[Pub testWith3NilArguments:nil b:nil c:nil];
}

- (void) d
{
NSLog(@"publishing d");
[Pub nicknameChanged:@"D"];
- (void) testWithAnInt {
// TODO: find a way to support this scenario
// [Sub while:self testWithAnInt:^(int a) {
// NSParameterAssert(a == 7);
// }];
// [Pub testWithAnInt:7];
}

@end
Loading