Skip to content

Commit

Permalink
Refactor channels to adjust to API spec.
Browse files Browse the repository at this point in the history
Using EventEmitter to emit state changes and messages.

Fixes #134, #183, #121, #189.
  • Loading branch information
tcard committed Feb 15, 2016
1 parent 3aa1c06 commit 97f0b16
Show file tree
Hide file tree
Showing 37 changed files with 621 additions and 706 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ ARTRealtimeChannel * channel = [client.channels get:@"test"];

### Publishing to a channel
```
[channel publish:@"Hello, Channel!" cb:^(ARTStatus *status) {
[channel publish:nil data:@"Hello, Channel!" cb:^(ARTErrorInfo *errorInfo) {
if(status.status != ARTStatusOk) {
//something went wrong.
}
Expand Down Expand Up @@ -100,7 +100,7 @@ ARTRealtimeChannel * channel = [client.channels get:@"test"];

## Publishing a message to a channel
```
[channel publish:@"Hello, channel!" cb:^(ARTStatus *status){
[channel publish:nil data:@"Hello, channel!" cb:^(ARTErrorInfo *errorInfo){
if(status.status != ARTStatusOk) {
//something went wrong
}
Expand Down
3 changes: 2 additions & 1 deletion ably-ios/ARTChannel+Private.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,12 @@ ART_ASSUME_NONNULL_BEGIN

@interface ARTChannel()

@property (readonly, getter=getLogger) ARTLog *logger;
@property (nonatomic, strong, art_null_resettable) ARTChannelOptions *options;
@property (nonatomic, strong, readonly) ARTDataEncoder *dataEncoder;

- (ARTMessage *__art_nonnull)encodeMessageIfNeeded:(ARTMessage *__art_nonnull)message;
- (void)internalPostMessages:(id)data callback:(art_nullable ARTErrorCallback)callback;
- (void)internalPostMessages:(id)data callback:(art_nullable void (^)(ARTErrorInfo *__art_nullable error))callback;

@end

Expand Down
8 changes: 2 additions & 6 deletions ably-ios/ARTChannel.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,11 @@ ART_ASSUME_NONNULL_BEGIN
@interface ARTChannel : NSObject

@property (nonatomic, strong, readonly) NSString *name;
@property (readonly, getter=getLogger) ARTLog *logger;

- (instancetype)initWithName:(NSString *)name andOptions:(ARTChannelOptions *)options andLogger:(ARTLog *)logger;

- (void)publish:(art_nullable id)data callback:(art_nullable ARTErrorCallback)callback;
- (void)publish:(art_nullable id)data name:(art_nullable NSString *)name callback:(art_nullable ARTErrorCallback)callback;

- (void)publishMessage:(ARTMessage *)message callback:(art_nullable ARTErrorCallback)callback;
- (void)publishMessages:(__GENERIC(NSArray, ARTMessage *) *)messages callback:(art_nullable ARTErrorCallback)callback;
- (void)publish:(art_nullable NSString *)name data:(art_nullable id)data cb:(art_nullable void (^)(ARTErrorInfo *__art_nullable error))callback;
- (void)publish:(__GENERIC(NSArray, ARTMessage *) *)messages cb:(art_nullable void (^)(ARTErrorInfo *__art_nullable error))callback;

- (BOOL)history:(art_nullable ARTDataQuery *)query callback:(void(^)(__GENERIC(ARTPaginatedResult, ARTMessage *) *__art_nullable result, NSError *__art_nullable error))callback error:(NSError *__art_nullable *__art_nullable)errorPtr;

Expand Down
17 changes: 5 additions & 12 deletions ably-ios/ARTChannel.m
Original file line number Diff line number Diff line change
Expand Up @@ -34,15 +34,12 @@ - (void)setOptions:(ARTChannelOptions *)options {
}
}

- (void)publish:(id)data callback:(ARTErrorCallback)callback {
[self publish:data name:nil callback:callback];
- (void)publish:(art_nullable NSString *)name data:(art_nullable id)data cb:(art_nullable void (^)(ARTErrorInfo *__art_nullable error))callback {
[self internalPostMessages:[self encodeMessageIfNeeded:[[ARTMessage alloc] initWithData:data name:name]]
callback:callback];
}

- (void)publish:(id)data name:(NSString *)name callback:(ARTErrorCallback)callback {
[self publishMessage:[[ARTMessage alloc] initWithData:data name:name] callback:callback];
}

- (void)publishMessages:(NSArray *)messages callback:(ARTErrorCallback)callback {
- (void)publish:(__GENERIC(NSArray, ARTMessage *) *)messages cb:(art_nullable void (^)(ARTErrorInfo *__art_nullable error))callback {
[self internalPostMessages:[messages artMap:^id(ARTMessage *message) {
return [self encodeMessageIfNeeded:message];
}] callback:callback];
Expand All @@ -60,16 +57,12 @@ - (ARTMessage *)encodeMessageIfNeeded:(ARTMessage *)message {
return message;
}

- (void)publishMessage:(ARTMessage *)message callback:(ARTErrorCallback)callback {
[self internalPostMessages:[self encodeMessageIfNeeded:message] callback:callback];
}

- (BOOL)history:(ARTDataQuery *)query callback:(void (^)(__GENERIC(ARTPaginatedResult, ARTMessage *) *, NSError *))callback error:(NSError **)errorPtr {
NSAssert(false, @"-[%@ %@] should always be overriden.", self.class, NSStringFromSelector(_cmd));
return NO;
}

- (void)internalPostMessages:(id)data callback:(ARTErrorCallback)callback {
- (void)internalPostMessages:(id)data callback:(void (^)(ARTErrorInfo *__art_nullable error))callback {
NSAssert(false, @"-[%@ %@] should always be overriden.", self.class, NSStringFromSelector(_cmd));
}

Expand Down
45 changes: 45 additions & 0 deletions ably-ios/ARTRealtimeChannel+Private.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,56 @@
//

#import "ARTRealtimeChannel.h"
#import "ARTEventEmitter.h"

@class ARTPresenceMap;
@class ARTProtocolMessage;

@interface ARTRealtimeChannel ()

@property (readonly, weak, nonatomic) ARTRealtime *realtime;
@property (readwrite, strong, nonatomic) NSMutableArray *queuedMessages;
@property (readwrite, strong, nonatomic) NSString *attachSerial;
@property (readonly, strong, nonatomic) NSMutableDictionary *subscriptions;
@property (readonly, strong, nonatomic) NSMutableArray *presenceSubscriptions;
@property (readonly, strong, nonatomic) NSMutableDictionary *presenceDict;
@property (readonly, getter=getClientId) NSString *clientId;
@property (readonly, strong, nonatomic) __GENERIC(ARTEventEmitter, NSNumber *, ARTErrorInfo *) *statesEventEmitter;
@property (readonly, strong, nonatomic) __GENERIC(ARTEventEmitter, NSString *, ARTMessage *) *messagesEventEmitter;
@property (readwrite, strong, nonatomic) ARTPresenceMap *presenceMap;
@property (readwrite, assign, nonatomic) ARTPresenceAction lastPresenceAction;

- (instancetype)initWithRealtime:(ARTRealtime *)realtime andName:(NSString *)name withOptions:(ARTChannelOptions *)options;
+ (instancetype)channelWithRealtime:(ARTRealtime *)realtime andName:(NSString *)name withOptions:(ARTChannelOptions *)options;

@end

@interface ARTRealtimeChannel (Private)

- (void)transition:(ARTRealtimeChannelState)state status:(ARTStatus *)status;

- (void)onChannelMessage:(ARTProtocolMessage *)message;
- (void)publishPresence:(ARTPresenceMessage *)pm cb:(ARTStatusCallback)cb;
- (void)publishProtocolMessage:(ARTProtocolMessage *)pm cb:(ARTStatusCallback)cb;

- (void)setAttached:(ARTProtocolMessage *)message;
- (void)setDetached:(ARTProtocolMessage *)message;
- (void)onMessage:(ARTProtocolMessage *)message;
- (void)onPresence:(ARTProtocolMessage *)message;
- (void)onError:(ARTProtocolMessage *)error;

- (void)sendQueuedMessages;
- (void)failQueuedMessages:(ARTStatus *)status;

- (void)setSuspended:(ARTStatus *)error;
- (void)setFailed:(ARTStatus *)error;
- (void)throwOnDisconnectedOrFailed;

- (void)broadcastPresence:(ARTPresenceMessage *)pm;
- (void)detachChannel:(ARTStatus *) error;

- (void)requestContinueSync;

- (void)releaseChannel;

@end
81 changes: 23 additions & 58 deletions ably-ios/ARTRealtimeChannel.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,75 +11,40 @@
#import "ARTLog.h"
#import "ARTRestChannel.h"
#import "ARTPresenceMessage.h"
#import "ARTEventEmitter.h"
#import "ARTRealtimePresence.h"
#import "ARTDataQuery.h"

@protocol ARTSubscription;
@interface ARTRealtimeHistoryQuery : ARTDataQuery

@class ARTRealtime;
@class ARTDataQuery;
@class ARTRealtimePresence;
@class ARTPresenceMap;
@class ARTMessage;
@class ARTProtocolMessage;
@class ARTRealtimeChannelSubscription;
@class ARTRealtimeChannelStateSubscription;
@property (nonatomic, assign) BOOL untilAttach;

@interface ARTRealtimeChannel : ARTRestChannel

@property (readonly, weak, nonatomic) ARTRealtime *realtime;
@property (readwrite, assign, nonatomic) ARTRealtimeChannelState state;
@property (readwrite, strong, nonatomic) NSMutableArray *queuedMessages;
@property (readwrite, strong, nonatomic) NSString *attachSerial;
@property (readonly, strong, nonatomic) NSMutableDictionary *subscriptions;
@property (readonly, strong, nonatomic) NSMutableArray *presenceSubscriptions;
@property (readonly, strong, nonatomic) NSMutableDictionary *presenceDict;
@property (readonly, getter=getClientId) NSString *clientId;
@property (readonly, strong, nonatomic) NSMutableArray *stateSubscriptions;
@property (readwrite, strong, nonatomic) ARTPresenceMap *presenceMap;
@property (readwrite, assign, nonatomic) ARTPresenceAction lastPresenceAction;

- (instancetype)initWithRealtime:(ARTRealtime *)realtime andName:(NSString *)name withOptions:(ARTChannelOptions *)options;
+ (instancetype)channelWithRealtime:(ARTRealtime *)realtime andName:(NSString *)name withOptions:(ARTChannelOptions *)options;

- (void)transition:(ARTRealtimeChannelState)state status:(ARTStatus *)status;

- (void)onChannelMessage:(ARTProtocolMessage *)message;
- (void)publishMessages:(__GENERIC(NSArray, ARTMessage *) *)messages cb:(ARTStatusCallback)cb;
- (void)publishPresence:(ARTPresenceMessage *)pm cb:(ARTStatusCallback)cb;
- (void)publishProtocolMessage:(ARTProtocolMessage *)pm cb:(ARTStatusCallback)cb;

- (void)setAttached:(ARTProtocolMessage *)message;
- (void)setDetached:(ARTProtocolMessage *)message;
- (void)onMessage:(ARTProtocolMessage *)message;
- (void)onPresence:(ARTProtocolMessage *)message;
- (void)onError:(ARTProtocolMessage *)error;
- (void)setSuspended:(ARTStatus *)error;
@end

- (void)sendQueuedMessages;
- (void)failQueuedMessages:(ARTStatus *)status;
ART_ASSUME_NONNULL_BEGIN

- (void)unsubscribe:(ARTRealtimeChannelSubscription *)subscription;
- (void)unsubscribeState:(ARTRealtimeChannelStateSubscription *)subscription;
@interface ARTRealtimeChannel : ARTRestChannel

- (void)broadcastPresence:(ARTPresenceMessage *)pm;
@property (readwrite, assign, nonatomic) ARTRealtimeChannelState state;
@property (readonly, strong, nonatomic, art_nullable) ARTErrorInfo *errorReason;
@property (readonly, getter=getPresence) ARTRealtimePresence *presence;

- (void)publish:(id)data withName:(NSString *)name cb:(ARTStatusCallback)cb;
- (void)publish:(id)data cb:(ARTStatusCallback)cb;
- (void)attach;
- (void)attach:(art_nullable void (^)(ARTErrorInfo *__art_nullable))cb;

- (id<ARTSubscription>)subscribe:(ARTRealtimeChannelMessageCb)cb;
- (id<ARTSubscription>)subscribeToName:(NSString *)name cb:(ARTRealtimeChannelMessageCb)cb;
- (id<ARTSubscription>)subscribeToNames:(NSArray *)names cb:(ARTRealtimeChannelMessageCb)cb;
- (id<ARTSubscription>)subscribeToStateChanges:(ARTRealtimeChannelStateCb)cb;
- (void)detach;
- (void)detach:(art_nullable void (^)(ARTErrorInfo *__art_nullable))cb;

- (ARTErrorInfo *)attach;
- (ARTErrorInfo *)detach;
- (void)detachChannel:(ARTStatus *) error;
- (__GENERIC(ARTEventListener, ARTMessage *) *)subscribe:(void (^)(ARTMessage *message))cb;
- (__GENERIC(ARTEventListener, ARTMessage *) *)subscribe:(NSString *)name cb:(void (^)(ARTMessage *message))cb;

- (void)requestContinueSync;
- (void)unsubscribe:(__GENERIC(ARTEventListener, ARTMessage *) *)listener;
- (void)unsubscribe:(NSString *)name listener:(__GENERIC(ARTEventListener, ARTMessage *) *)listener;

- (void)releaseChannel;
- (ARTRealtimeChannelState)state;
- (BOOL)history:(art_nullable ARTRealtimeHistoryQuery *)query callback:(void(^)(__GENERIC(ARTPaginatedResult, ARTMessage *) *__art_nullable result, NSError *__art_nullable error))callback error:(NSError *__art_nullable *__art_nullable)errorPtr;

- (ARTRealtimePresence *)presence;
- (ARTPresenceMap *)presenceMap;
ART_EMBED_INTERFACE_EVENT_EMITTER(ARTRealtimeChannelState, ARTErrorInfo *)

@end

ART_ASSUME_NONNULL_END
Loading

0 comments on commit 97f0b16

Please sign in to comment.