Skip to content

Commit

Permalink
Added attachOnSubscribe to ARTRealtimeChannelOptions.
Browse files Browse the repository at this point in the history
  • Loading branch information
maratal committed Sep 20, 2024
1 parent 80f05fd commit d8e6e95
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 4 deletions.
6 changes: 4 additions & 2 deletions Source/ARTRealtimeChannel.m
Original file line number Diff line number Diff line change
Expand Up @@ -436,11 +436,13 @@ - (ARTEventListener *)_subscribe:(nullable NSString *)name onAttach:(nullable AR
__block ARTEventListener *listener = nil;
dispatch_sync(_queue, ^{
if (self.state_nosync == ARTRealtimeChannelFailed) {
if (onAttach) onAttach([ARTErrorInfo createWithCode:ARTErrorChannelOperationFailedInvalidState message:@"attempted to subscribe while channel is in FAILED state."]);
if (onAttach && self.options.attachOnSubscribe) { // RTL7h
onAttach([ARTErrorInfo createWithCode:ARTErrorChannelOperationFailedInvalidState message:@"attempted to subscribe while channel is in FAILED state."]);
}
ARTLogWarn(self.logger, @"R:%p C:%p (%@) subscribe of '%@' has been ignored (attempted to subscribe while channel is in FAILED state)", self->_realtime, self, self.name, name == nil ? @"all" : name);
return;
}
if (self.shouldAttach) { // RTL7c
if (self.shouldAttach && self.getOptions_nosync.attachOnSubscribe) { // RTL7g
[self _attach:onAttach];
}
listener = name == nil ? [self.messagesEventEmitter on:cb] : [self.messagesEventEmitter on:name callback:cb];
Expand Down
28 changes: 28 additions & 0 deletions Source/ARTRealtimeChannelOptions.m
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,21 @@
@implementation ARTRealtimeChannelOptions {
NSStringDictionary *_params;
ARTChannelMode _modes;
BOOL _attachOnSubscribe;
}

- (instancetype)init {
if (self = [super init]) {
_attachOnSubscribe = true;
}
return self;
}

- (instancetype)initWithCipher:(id<ARTCipherParamsCompatible>)cipherParams {
if (self = [super initWithCipher:cipherParams]) {
_attachOnSubscribe = true;
}
return self;
}

- (NSStringDictionary *)params {
Expand Down Expand Up @@ -32,4 +47,17 @@ - (void)setModes:(ARTChannelMode)modes {
_modes = modes;
}

- (BOOL)attachOnSubscribe {
return _attachOnSubscribe;
}

- (void)setAttachOnSubscribe:(BOOL)value {
if (self.isFrozen) {
@throw [NSException exceptionWithName:NSObjectInaccessibleException
reason:[NSString stringWithFormat:@"%@: You can't change options after you've passed it to receiver.", self.class]
userInfo:nil];
}
_attachOnSubscribe = value;
}

@end
7 changes: 5 additions & 2 deletions Source/ARTRealtimePresence.m
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
#import "ARTProtocolMessage+Private.h"
#import "ARTEventEmitter+Private.h"
#import "ARTClientOptions.h"
#import "ARTRealtimeChannelOptions.h"

#pragma mark - ARTRealtimePresenceQuery

Expand Down Expand Up @@ -498,11 +499,13 @@ - (ARTEventListener *)_subscribe:(ARTPresenceAction)action onAttach:(nullable AR
__block ARTEventListener *listener = nil;
dispatch_sync(_queue, ^{
if (self->_channel.state_nosync == ARTRealtimeChannelFailed) {
if (onAttach) onAttach([ARTErrorInfo createWithCode:ARTErrorChannelOperationFailedInvalidState message:@"attempted to subscribe while channel is in Failed state."]);
if (onAttach && self->_channel.getOptions_nosync.attachOnSubscribe) { // RTL7h
onAttach([ARTErrorInfo createWithCode:ARTErrorChannelOperationFailedInvalidState message:@"attempted to subscribe while channel is in Failed state."]);
}
ARTLogWarn(self.logger, @"R:%p C:%p (%@) presence subscribe to '%@' action(s) has been ignored (attempted to subscribe while channel is in FAILED state)", self->_realtime, self->_channel, self->_channel.name, ARTPresenceActionToStr(action));
return;
}
if (self->_channel.shouldAttach) { // RTP6c
if (self->_channel.shouldAttach && self->_channel.options.attachOnSubscribe) { // RTP6c
[self->_channel _attach:onAttach];
}
listener = action == ARTPresenceActionAll ? [_eventEmitter on:cb] : [_eventEmitter on:[ARTEvent newWithPresenceAction:action] callback:cb];
Expand Down
5 changes: 5 additions & 0 deletions Source/include/Ably/ARTRealtimeChannelOptions.h
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,11 @@ NS_ASSUME_NONNULL_BEGIN
*/
@property (nonatomic) ARTChannelMode modes;

/**
* Determines whether calling `subscribe` on a channel or presence object should trigger an implicit attach. Defaults to `true`.
*/
@property (nonatomic) BOOL attachOnSubscribe;

@end

NS_ASSUME_NONNULL_END

0 comments on commit d8e6e95

Please sign in to comment.