Skip to content

Commit

Permalink
Clean up internal representation of messages with a WVJBMessage typedef
Browse files Browse the repository at this point in the history
  • Loading branch information
marcuswestin committed Oct 26, 2013
1 parent b68bc26 commit 5069f08
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 6 deletions.
1 change: 1 addition & 0 deletions Changelog
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
v4.1.0
+ Allow for sending null/nil data packets
+ Drop support for JSONKit
+ Clean up internal represenation of messages

v4.0.2
+ Fix NSInvalidArgumentException: "attempt to insert nil object" when using shorthand -callHandler:
Expand Down
13 changes: 7 additions & 6 deletions WebViewJavascriptBridge/WebViewJavascriptBridge.m
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
#define WVJB_WEAK __unsafe_unretained
#endif

typedef NSDictionary WVJBMessage;

@implementation WebViewJavascriptBridge {
WVJB_WEAK WVJB_WEBVIEW_TYPE* _webView;
Expand Down Expand Up @@ -110,15 +111,15 @@ - (void)_sendData:(id)data responseCallback:(WVJBResponseCallback)responseCallba
[self _queueMessage:message];
}

- (void)_queueMessage:(NSDictionary *)message {
- (void)_queueMessage:(WVJBMessage*)message {
if (_startupMessageQueue) {
[_startupMessageQueue addObject:message];
} else {
[self _dispatchMessage:message];
}
}

- (void)_dispatchMessage:(NSDictionary *)message {
- (void)_dispatchMessage:(WVJBMessage*)message {
NSString *messageJSON = [self _serializeMessage:message];
[self _log:@"SEND" json:messageJSON];
messageJSON = [messageJSON stringByReplacingOccurrencesOfString:@"\\" withString:@"\\\\"];
Expand Down Expand Up @@ -147,8 +148,8 @@ - (void)_flushMessageQueue {
NSLog(@"WebViewJavascriptBridge: WARNING: Invalid %@ received: %@", [messages class], messages);
return;
}
for (NSDictionary *message in messages) {
if (![message isKindOfClass:[NSDictionary class]]) {
for (WVJBMessage* message in messages) {
if (![message isKindOfClass:[WVJBMessage class]]) {
NSLog(@"WebViewJavascriptBridge: WARNING: Invalid %@ received: %@", [message class], message);
continue;
}
Expand All @@ -164,7 +165,7 @@ - (void)_flushMessageQueue {
NSString* callbackId = message[@"callbackId"];
if (callbackId) {
responseCallback = ^(id responseData) {
NSDictionary* msg = @{ @"responseId":callbackId, @"responseData":responseData };
WVJBMessage* msg = @{ @"responseId":callbackId, @"responseData":responseData };
[self _queueMessage:msg];
};
} else {
Expand Down Expand Up @@ -199,7 +200,7 @@ - (NSString *)_serializeMessage:(id)message {
return [[NSString alloc] initWithData:[NSJSONSerialization dataWithJSONObject:message options:0 error:nil] encoding:NSUTF8StringEncoding];
}

- (NSDictionary *)_deserializeMessageJSON:(NSString *)messageJSON {
- (NSArray*)_deserializeMessageJSON:(NSString *)messageJSON {
return [NSJSONSerialization JSONObjectWithData:[messageJSON dataUsingEncoding:NSUTF8StringEncoding] options:NSJSONReadingAllowFragments error:nil];
}

Expand Down

0 comments on commit 5069f08

Please sign in to comment.