Skip to content

Commit

Permalink
Extract duplicate code from ARTChannel.publish methods. (#928)
Browse files Browse the repository at this point in the history
  • Loading branch information
tcard authored Dec 9, 2019
1 parent a89a864 commit f4fe9b7
Showing 1 changed file with 5 additions and 21 deletions.
26 changes: 5 additions & 21 deletions Source/ARTChannel.m
Original file line number Diff line number Diff line change
Expand Up @@ -64,26 +64,7 @@ - (void)publish:(NSString *)name data:(id)data extras:(id<ARTJsonCompatible>)ext
}

- (void)publish:(art_nullable NSString *)name data:(art_nullable id)data extras:(id<ARTJsonCompatible>)extras callback:(art_nullable void (^)(ARTErrorInfo *__art_nullable error))callback {
NSError *error = nil;
ARTMessage *message = [[ARTMessage alloc] initWithName:name data:data];
message.extras = extras;
ARTMessage *messagesWithDataEncoded = [self encodeMessageIfNeeded:message error:&error];
if (error) {
if (callback) callback([ARTErrorInfo createFromNSError:error]);
return;
}

// Checked after encoding, so that the client can receive callback with encoding errors
if ([self exceedMaxSize:@[message]]) {
ARTErrorInfo *sizeError = [ARTErrorInfo createWithCode:40009
message:@"maximum message length exceeded"];
if (callback) {
callback(sizeError);
}
return;
}

[self internalPostMessages:messagesWithDataEncoded callback:callback];
[self publish:name message:[[ARTMessage alloc] initWithName:name data:data] extras:extras callback:callback];
}

- (void)publish:(NSString *)name data:(id)data clientId:(NSString *)clientId {
Expand All @@ -99,8 +80,11 @@ - (void)publish:(NSString *)name data:(id)data clientId:(NSString *)clientId cal
}

- (void)publish:(NSString *)name data:(id)data clientId:(NSString *)clientId extras:(id<ARTJsonCompatible>)extras callback:(void (^)(ARTErrorInfo * _Nullable))callback {
[self publish:name message:[[ARTMessage alloc] initWithName:name data:data clientId:clientId] extras:extras callback:callback];
}

- (void)publish:(NSString *)name message:(ARTMessage *)message extras:(id<ARTJsonCompatible>)extras callback:(void (^)(ARTErrorInfo * _Nullable))callback {
NSError *error = nil;
ARTMessage *message = [[ARTMessage alloc] initWithName:name data:data clientId:clientId];
message.extras = extras;
ARTMessage *messagesWithDataEncoded = [self encodeMessageIfNeeded:message error:&error];
if (error) {
Expand Down

0 comments on commit f4fe9b7

Please sign in to comment.