Skip to content

Commit

Permalink
add proxy for channel
Browse files Browse the repository at this point in the history
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
  • Loading branch information
lawrence-forooghian committed Jan 16, 2025
1 parent 381e21d commit 113d452
Show file tree
Hide file tree
Showing 2 changed files with 258 additions and 8 deletions.
241 changes: 235 additions & 6 deletions Source/ARTRealtimeWrapperSDKProxy.m
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -60,12 +87,6 @@ - (void)connect {
[self.realtime connect];
}

- (nonnull id<ARTRealtimeProtocol>)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];
}
Expand Down Expand Up @@ -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<ARTRealtimePresenceProtocol>)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<ARTMessage *> *)messages {
[self.channel publish:messages];
}

- (void)publish:(nonnull NSArray<ARTMessage *> *)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<ARTJsonCompatible>)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<ARTJsonCompatible>)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<ARTJsonCompatible>)extras {
[self.channel publish:name data:data extras:extras];
}

- (void)publish:(nullable NSString *)name data:(nullable id)data extras:(nullable id<ARTJsonCompatible>)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
25 changes: 23 additions & 2 deletions Source/include/Ably/ARTRealtimeWrapperSDKProxy.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -15,10 +17,29 @@ NS_SWIFT_SENDABLE
@interface ARTRealtimeWrapperSDKProxy : NSObject <ARTRealtimeProtocol>

@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 <ARTRealtimeChannelsProtocol>

- (ARTRealtimeWrapperSDKChannelProxy *)get:(NSString *)name;
- (ARTRealtimeWrapperSDKChannelProxy *)get:(NSString *)name options:(ARTRealtimeChannelOptions *)options;

@end

NS_SWIFT_SENDABLE
@interface ARTRealtimeWrapperSDKChannelProxy : NSObject <ARTRealtimeChannelProtocol>

@property (readonly) ARTRealtimePresence *presence;

#if TARGET_OS_IPHONE
@property (readonly) ARTPushChannel *push;
#endif

@end

NS_ASSUME_NONNULL_END

0 comments on commit 113d452

Please sign in to comment.