From 113d4520c3f4045d10b1b26872f966c594725c25 Mon Sep 17 00:00:00 2001 From: Lawrence Forooghian Date: Tue, 14 Jan 2025 16:11:08 -0300 Subject: [PATCH] add proxy for channel this will allow us to insert the agent parameter (no functional changes in this commit) TODO this doesn't cover the channel's other properties TODO reuse existing instance --- Source/ARTRealtimeWrapperSDKProxy.m | 241 +++++++++++++++++- .../include/Ably/ARTRealtimeWrapperSDKProxy.h | 25 +- 2 files changed, 258 insertions(+), 8 deletions(-) diff --git a/Source/ARTRealtimeWrapperSDKProxy.m b/Source/ARTRealtimeWrapperSDKProxy.m index 5d4d4f01a..f447c3b2d 100644 --- a/Source/ARTRealtimeWrapperSDKProxy.m +++ b/Source/ARTRealtimeWrapperSDKProxy.m @@ -12,6 +12,33 @@ @interface ARTRealtimeWrapperSDKProxy () @end +@interface ARTRealtimeWrapperSDKChannelsProxy () + +- (instancetype)init NS_UNAVAILABLE; +- (instancetype)initWithChannels:(ARTRealtimeChannels *)channels + options:(ARTWrapperProxyOptions *)options NS_DESIGNATED_INITIALIZER; + +// TODO split into files; this is private + +@property (nonatomic, readonly) ARTRealtimeChannels *channels; +@property (nonatomic, readonly) ARTWrapperProxyOptions *options; + +@end + +@interface ARTRealtimeWrapperSDKChannelProxy () + +- (instancetype)init NS_UNAVAILABLE; +- (instancetype)initWithChannel:(ARTRealtimeChannel *)channel + options:(ARTWrapperProxyOptions *)options NS_DESIGNATED_INITIALIZER; + +// TODO split into files; this is private + +@property (nonatomic, readonly) ARTRealtimeChannel *channel; +// TODO consider making this naming consistent (in this class it's to avoid clash with property called `options`) +@property (nonatomic, readonly) ARTWrapperProxyOptions *proxyOptions; + +@end + NS_ASSUME_NONNULL_END @implementation ARTRealtimeWrapperSDKProxy @@ -60,12 +87,6 @@ - (void)connect { [self.realtime connect]; } -- (nonnull id)createWrapperSDKProxyWithOptions:(nonnull ARTWrapperProxyOptions *)options { - @throw [NSException exceptionWithName:NSInvalidArgumentException - reason:@"You can not call -createWrapperProxyWithOptions: on a proxy object." - userInfo: nil]; -} - - (void)ping:(nonnull ARTCallback)cb { [self.realtime ping:cb]; } @@ -102,3 +123,211 @@ - (void)time:(nonnull ARTDateTimeCallback)callback { } @end + +@implementation ARTRealtimeWrapperSDKChannelsProxy + +- (instancetype)initWithChannels:(ARTRealtimeChannels *)channels options:(ARTWrapperProxyOptions *)options { + if (self = [super init]) { + _channels = channels; + _options = options; + } + + return self; +} + +- (BOOL)exists:(nonnull NSString *)name { + return [self.channels exists:name]; +} + +- (void)release:(nonnull NSString *)name { + [self.channels release:name]; +} + +- (void)release:(nonnull NSString *)name callback:(nullable ARTCallback)errorInfo { + [self.channels release:name callback:errorInfo]; +} + +- (ARTRealtimeWrapperSDKChannelProxy *)get:(NSString *)name { + ARTRealtimeChannel *channel = [self.channels get:name]; + // TODO reuse existing instance + return [[ARTRealtimeWrapperSDKChannelProxy alloc] initWithChannel:channel + options:self.options]; +} + +- (ARTRealtimeWrapperSDKChannelProxy *)get:(NSString *)name options:(ARTRealtimeChannelOptions *)options { + ARTRealtimeChannel *channel = [self.channels get:name + options:options]; + // TODO reuse existing instance + return [[ARTRealtimeWrapperSDKChannelProxy alloc] initWithChannel:channel + options:self.options]; +} + +@end + +@implementation ARTRealtimeWrapperSDKChannelProxy + +- (instancetype)initWithChannel:(ARTRealtimeChannel *)channel options:(ARTWrapperProxyOptions *)options { + if (self = [super init]) { + _channel = channel; + _proxyOptions = options; + } + + return self; +} + +- (ARTErrorInfo *)errorReason { + return self.channel.errorReason; +} + +- (NSString *)name { + return self.channel.name; +} + +- (ARTRealtimeChannelOptions *)getOptions { + return self.channel.options; +} + +- (id)presence { + return self.channel.presence; +} + +- (ARTChannelProperties *)properties { + return self.channel.properties; +} + +- (ARTRealtimeChannelState)state { + return self.channel.state; + +} + +#if TARGET_OS_IOS +- (ARTPushChannel *)push { + return self.channel.push; +} +#endif + +- (void)history:(nonnull ARTPaginatedMessagesCallback)callback { + [self.channel history:callback]; +} + +- (void)publish:(nonnull NSArray *)messages { + [self.channel publish:messages]; +} + +- (void)publish:(nonnull NSArray *)messages callback:(nullable ARTCallback)callback { + [self.channel publish:messages callback:callback]; +} + +- (void)publish:(nullable NSString *)name data:(nullable id)data { + [self.channel publish:name data:data]; +} + +- (void)publish:(nullable NSString *)name data:(nullable id)data callback:(nullable ARTCallback)callback { + [self.channel publish:name data:data callback:callback]; +} + +- (void)publish:(nullable NSString *)name data:(nullable id)data clientId:(nonnull NSString *)clientId { + [self.channel publish:name data:data clientId:clientId]; +} + +- (void)publish:(nullable NSString *)name data:(nullable id)data clientId:(nonnull NSString *)clientId callback:(nullable ARTCallback)callback { + [self.channel publish:name data:data clientId:clientId]; +} + +- (void)publish:(nullable NSString *)name data:(nullable id)data clientId:(nonnull NSString *)clientId extras:(nullable id)extras { + [self.channel publish:name data:data clientId:clientId extras:extras]; +} + +- (void)publish:(nullable NSString *)name data:(nullable id)data clientId:(nonnull NSString *)clientId extras:(nullable id)extras callback:(nullable ARTCallback)callback { + [self.channel publish:name data:data clientId:clientId extras:extras callback:callback]; +} + +- (void)publish:(nullable NSString *)name data:(nullable id)data extras:(nullable id)extras { + [self.channel publish:name data:data extras:extras]; +} + +- (void)publish:(nullable NSString *)name data:(nullable id)data extras:(nullable id)extras callback:(nullable ARTCallback)callback { + [self.channel publish:name data:data extras:extras callback:callback]; +} + +- (void)attach { + [self.channel attach]; +} + +- (void)attach:(nullable ARTCallback)callback { + [self.channel attach:callback]; +} + +- (void)detach { + [self.channel detach]; +} + +- (void)detach:(nullable ARTCallback)callback { + [self.channel detach:callback]; +} + +- (BOOL)history:(ARTRealtimeHistoryQuery * _Nullable)query callback:(nonnull ARTPaginatedMessagesCallback)callback error:(NSError * _Nullable __autoreleasing * _Nullable)errorPtr { + return [self.channel history:query callback:callback error:errorPtr]; +} + +- (void)off { + [self.channel off]; +} + +- (void)off:(nonnull ARTEventListener *)listener { + [self.channel off:listener]; +} + +- (void)off:(ARTChannelEvent)event listener:(nonnull ARTEventListener *)listener { + [self.channel off:event listener:listener]; +} + +- (nonnull ARTEventListener *)on:(nonnull void (^)(ARTChannelStateChange * _Nonnull))cb { + return [self.channel on:cb]; +} + +- (nonnull ARTEventListener *)on:(ARTChannelEvent)event callback:(nonnull void (^)(ARTChannelStateChange * _Nonnull))cb { + return [self.channel on:event callback:cb]; +} + +- (nonnull ARTEventListener *)once:(nonnull void (^)(ARTChannelStateChange * _Nonnull))cb { + return [self.channel once:cb]; +} + +- (nonnull ARTEventListener *)once:(ARTChannelEvent)event callback:(nonnull void (^)(ARTChannelStateChange * _Nonnull))cb { + return [self.channel once:event callback:cb]; +} + +- (void)setOptions:(ARTRealtimeChannelOptions * _Nullable)options callback:(nullable ARTCallback)callback { + [self.channel setOptions:options callback:callback]; +} + +- (ARTEventListener * _Nullable)subscribe:(nonnull ARTMessageCallback)callback { + return [self.channel subscribe:callback]; +} + +- (ARTEventListener * _Nullable)subscribe:(nonnull NSString *)name callback:(nonnull ARTMessageCallback)callback { + return [self.channel subscribe:name callback:callback]; +} + +- (ARTEventListener * _Nullable)subscribe:(nonnull NSString *)name onAttach:(nullable ARTCallback)onAttach callback:(nonnull ARTMessageCallback)callback { + return [self.channel subscribe:name onAttach:onAttach callback:callback]; +} + +- (ARTEventListener * _Nullable)subscribeWithAttachCallback:(nullable ARTCallback)onAttach callback:(nonnull ARTMessageCallback)callback { + return [self.channel subscribeWithAttachCallback:onAttach callback:callback]; +} + +- (void)unsubscribe { + [self.channel unsubscribe]; +} + +- (void)unsubscribe:(ARTEventListener * _Nullable)listener { + [self.channel unsubscribe:listener]; +} + +- (void)unsubscribe:(nonnull NSString *)name listener:(ARTEventListener * _Nullable)listener { + [self.channel unsubscribe:name listener:listener]; +} + +@end diff --git a/Source/include/Ably/ARTRealtimeWrapperSDKProxy.h b/Source/include/Ably/ARTRealtimeWrapperSDKProxy.h index 6691a1a81..605aaff1e 100644 --- a/Source/include/Ably/ARTRealtimeWrapperSDKProxy.h +++ b/Source/include/Ably/ARTRealtimeWrapperSDKProxy.h @@ -3,10 +3,12 @@ @class ARTWrapperProxyOptions; @class ARTConnection; -@class ARTRealtimeChannels; @class ARTPush; @class ARTAuth; +@class ARTRealtimeWrapperSDKChannelsProxy; +@class ARTRealtimeWrapperSDKChannelProxy; + // TODO exclude from docs NS_ASSUME_NONNULL_BEGIN @@ -15,10 +17,29 @@ NS_SWIFT_SENDABLE @interface ARTRealtimeWrapperSDKProxy : NSObject @property (readonly) ARTConnection *connection; -@property (readonly) ARTRealtimeChannels *channels; +@property (readonly) ARTRealtimeWrapperSDKChannelsProxy *channels; @property (readonly) ARTPush *push; @property (readonly) ARTAuth *auth; @end +NS_SWIFT_SENDABLE +@interface ARTRealtimeWrapperSDKChannelsProxy : NSObject + +- (ARTRealtimeWrapperSDKChannelProxy *)get:(NSString *)name; +- (ARTRealtimeWrapperSDKChannelProxy *)get:(NSString *)name options:(ARTRealtimeChannelOptions *)options; + +@end + +NS_SWIFT_SENDABLE +@interface ARTRealtimeWrapperSDKChannelProxy : NSObject + +@property (readonly) ARTRealtimePresence *presence; + +#if TARGET_OS_IPHONE +@property (readonly) ARTPushChannel *push; +#endif + +@end + NS_ASSUME_NONNULL_END