Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Presence#history wasn't working (infinite recursion) #51

Merged
merged 1 commit into from
Nov 16, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 1 addition & 4 deletions ably-ios/ARTChannel+Private.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,7 @@

ART_ASSUME_NONNULL_BEGIN

@interface ARTChannel() {
@public
id<ARTPayloadEncoder> _payloadEncoder;
}
@interface ARTChannel()

@property (nonatomic, strong, art_null_resettable) ARTChannelOptions *options;

Expand Down
7 changes: 4 additions & 3 deletions ably-ios/ARTChannel.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
#import <Foundation/Foundation.h>
#import "ARTTypes.h"

@protocol ARTPayloadEncoder;

@class ARTChannelOptions;
@class ARTMessage;
@class ARTPaginatedResult;
Expand All @@ -20,6 +22,8 @@ ART_ASSUME_NONNULL_BEGIN

@property (nonatomic, strong, readonly) NSString *name;

@property (readonly, strong, nonatomic) id<ARTPayloadEncoder> payloadEncoder;

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

- (void)publish:(art_nullable id)payload callback:(art_nullable ARTErrorCallback)callback;
Expand All @@ -30,9 +34,6 @@ ART_ASSUME_NONNULL_BEGIN

- (void)history:(art_nullable ARTDataQuery *)query callback:(void(^)(ARTPaginatedResult /* <ARTMessage *> */ *__art_nullable result, NSError *__art_nullable error))callback;

// TODO:
//- (void)presence

@end

ART_ASSUME_NONNULL_END
5 changes: 3 additions & 2 deletions ably-ios/ARTChannel.m
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ - (instancetype)initWithName:(NSString *)name andOptions:(ARTChannelOptions *)op
if (self = [super init]) {
_name = name;
_options = options;
_payloadEncoder = [ARTPayload defaultPayloadEncoder:options.cipherParams];
}
return self;
}
Expand All @@ -42,14 +43,14 @@ - (void)publish:(id)data name:(NSString *)name callback:(ARTErrorCallback)callba

- (void)publishMessages:(NSArray *)messages callback:(ARTErrorCallback)callback {
messages = [messages artMap:^(ARTMessage *message) {
return [message encode:_payloadEncoder];
return [message encode:self.payloadEncoder];
}];

[self internalPostMessages:messages callback:callback];
}

- (void)publishMessage:(ARTMessage *)message callback:(ARTErrorCallback)callback {
[self internalPostMessages:[message encode:_payloadEncoder] callback:callback];
[self internalPostMessages:[message encode:self.payloadEncoder] callback:callback];
}

- (void)history:(ARTDataQuery *)query callback:(void (^)(ARTPaginatedResult *, NSError *))callback {
Expand Down
19 changes: 3 additions & 16 deletions ably-ios/ARTPresence.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
@protocol ARTSubscription;

@class ARTChannel;
@class ARTRealtimeChannel;
@class ARTPaginatedResult;
@class ARTDataQuery;

Expand All @@ -24,7 +23,9 @@ ART_ASSUME_NONNULL_BEGIN
*/
@interface ARTPresence : NSObject

- (instancetype)initWithChannel:(ARTRealtimeChannel *)channel;
@property (readonly, getter=getChannel) ARTChannel *channel;

- (instancetype)initWithChannel:(ARTChannel *)channel;

/**
Get the presence state for one channel
Expand All @@ -36,20 +37,6 @@ ART_ASSUME_NONNULL_BEGIN
*/
- (void)history:(art_nullable ARTDataQuery *)query callback:(void (^)(ARTPaginatedResult /* <ARTPresenceMessage *> */ *__art_nullable result, NSError *__art_nullable error))callback;

- (void)enter:(id)data cb:(ARTStatusCallback)cb;
- (void)update:(id)data cb:(ARTStatusCallback)cb;
- (void)leave:(id) data cb:(ARTStatusCallback)cb;

- (void)enterClient:(NSString *) clientId data:(id) data cb:(ARTStatusCallback) cb;
- (void)updateClient:(NSString *) clientId data:(id) data cb:(ARTStatusCallback) cb;
- (void)leaveClient:(NSString *) clientId data:(id) data cb:(ARTStatusCallback) cb;
- (BOOL)isSyncComplete;

- (id<ARTSubscription>)subscribe:(ARTRealtimeChannelPresenceCb)cb;
- (id<ARTSubscription>)subscribe:(ARTPresenceAction)action cb:(ARTRealtimeChannelPresenceCb)cb;
- (void)unsubscribe:(id<ARTSubscription>)subscription;
- (void)unsubscribe:(id<ARTSubscription>)subscription action:(ARTPresenceAction)action;

@end

ART_ASSUME_NONNULL_END
124 changes: 11 additions & 113 deletions ably-ios/ARTPresence.m
Original file line number Diff line number Diff line change
Expand Up @@ -8,135 +8,33 @@

#import "ARTPresence.h"

#import "ARTLog.h"
#import "ARTRealtime.h"
#import "ARTRealtimeChannel+Private.h"
#import "ARTPresenceMap.h"
#import "ARTStatus.h"
#import "ARTRealtimeChannelSubscription.h"
#import "ARTDataQuery+Private.h"
#import "ARTRest.h"
#import "ARTAuth.h"
#import "ARTChannel.h"

@interface ARTPresence ()

@property (readonly, weak, nonatomic) ARTRealtimeChannel *channel;
@interface ARTPresence () {
ARTChannel *_channel;
}

@end

@implementation ARTPresence

- (instancetype) initWithChannel:(ARTRealtimeChannel *) channel {
- (instancetype) initWithChannel:(ARTChannel *) channel {
if (self = [super init]) {
_channel = channel;
}
return self;
}

- (void)get:(void (^)(ARTPaginatedResult /* <ARTPresenceMessage *> */ *result, NSError *error))callback {
[self.channel throwOnDisconnectedOrFailed];
[self.channel.presence get:callback];
- (ARTChannel *)getChannel {
return _channel;
}

- (void)history:(ARTDataQuery *)query callback:(void (^)(ARTPaginatedResult /* <ARTPresenceMessage *> */ *result, NSError *error))callback {
[self.channel throwOnDisconnectedOrFailed];
[self.channel.presence history:query callback:callback];
}

- (void)enter:(id)data cb:(ARTStatusCallback)cb {
[self enterClient:self.channel.clientId data:data cb:cb];
}

- (void)enterClient:(NSString *)clientId data:(id)data cb:(ARTStatusCallback)cb {
if (!clientId) {
[NSException raise:@"Cannot publish presence without a clientId" format:@""];
}
ARTPresenceMessage *msg = [[ARTPresenceMessage alloc] init];
msg.action = ARTPresenceEnter;
msg.clientId = clientId;
if(data) {
msg.payload = [ARTPayload payloadWithPayload:data encoding:@""];
}

msg.connectionId = self.channel.realtime.connectionId;
[self.channel publishPresence:msg cb:cb];
}

- (void)update:(id)data cb:(ARTStatusCallback)cb {
[self updateClient:self.channel.clientId data:data cb:cb];
}

- (void)updateClient:(NSString *) clientId data:(id) data cb:(ARTStatusCallback) cb {
ARTPresenceMessage *msg = [[ARTPresenceMessage alloc] init];
msg.action = ARTPresenceUpdate;
msg.clientId = clientId;
if(!msg.clientId) {
cb([ARTStatus state:ARTStateNoClientId]);
return;
}
if(data) {
msg.payload = [ARTPayload payloadWithPayload:data encoding:@""];
}
msg.connectionId = self.channel.realtime.connectionId;

[self.channel publishPresence:msg cb:cb];

}

- (void)leave:(id) data cb:(ARTStatusCallback)cb {
[self leaveClient:self.channel.clientId data:data cb:cb];
}

- (void) leaveClient:(NSString *) clientId data:(id) data cb:(ARTStatusCallback) cb {

if([clientId isEqualToString:self.channel.clientId]) {
if(self.channel.lastPresenceAction != ARTPresenceEnter && self.channel.lastPresenceAction != ARTPresenceUpdate) {
[NSException raise:@"Cannot leave a channel before you've entered it" format:@""];
}
}
ARTPresenceMessage *msg = [[ARTPresenceMessage alloc] init];
msg.action = ARTPresenceLeave;

if(data) {
msg.payload= [ARTPayload payloadWithPayload:data encoding:@""];
}
msg.clientId = clientId;
msg.connectionId = self.channel.realtime.connectionId;
if(!msg.clientId) {
cb([ARTStatus state:ARTStateNoClientId]);
return;
}
[self.channel publishPresence:msg cb:cb];

}

- (BOOL)isSyncComplete {
return [self.channel.presenceMap isSyncComplete];
}

- (id<ARTSubscription>)subscribe:(ARTRealtimeChannelPresenceCb)cb {
ARTRealtimeChannelPresenceSubscription *subscription = [[ARTRealtimeChannelPresenceSubscription alloc] initWithChannel:self.channel cb:cb];
[self.channel.presenceSubscriptions addObject:subscription];
[self.channel attach];
return subscription;
}

- (id<ARTSubscription>)subscribe:(ARTPresenceAction) action cb:(ARTRealtimeChannelPresenceCb)cb {
ARTRealtimeChannelPresenceSubscription *subscription = (ARTRealtimeChannelPresenceSubscription *) [self subscribe:cb];
[subscription excludedActions];
//[subscription excludeAllActionsExcept:action];
return subscription;
}

- (void)unsubscribe:(id<ARTSubscription>)subscription action:(ARTPresenceAction) action {
ARTRealtimeChannelPresenceSubscription * s = (ARTRealtimeChannelPresenceSubscription *) subscription;
[s excludedActions];
//[s excludeAction:action];
- (void)get:(void (^)(ARTPaginatedResult /* <ARTPresenceMessage *> */ *result, NSError *error))callback {
NSAssert(false, @"-[%@ %@] should always be overriden.", self.class, NSStringFromSelector(_cmd));
}

- (void)unsubscribe:(ARTRealtimeChannelPresenceSubscription *)subscription {
ARTRealtimeChannelPresenceSubscription *s = (ARTRealtimeChannelPresenceSubscription *) subscription;
[self.channel.presenceSubscriptions removeObject:s];
- (void)history:(ARTDataQuery *)query callback:(void (^)(ARTPaginatedResult /* <ARTPresenceMessage *> */ *result, NSError *error))callback {
NSAssert(false, @"-[%@ %@] should always be overriden.", self.class, NSStringFromSelector(_cmd));
}

@end
4 changes: 2 additions & 2 deletions ably-ios/ARTPresenceMap.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,10 @@
- (void)startSync;
- (void)endSync;
- (BOOL)isSyncComplete;
- (BOOL) stillSyncing;
- (BOOL)stillSyncing;

typedef void(^VoidCb)();
- (void) syncMessageProcessed;
- (void)syncMessageProcessed;
- (void)onSync:(VoidCb) cb;

@end
8 changes: 3 additions & 5 deletions ably-ios/ARTRealtimeChannel.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,10 @@
#import "ARTPresenceMessage.h"

@protocol ARTSubscription;
@protocol ARTPayloadEncoder;

@class ARTRealtime;
@class ARTDataQuery;
@class ARTPresence;
@class ARTRealtimePresence;
@class ARTPresenceMap;
@class ARTMessage;
@class ARTPaginatedResult;
Expand All @@ -28,7 +27,7 @@
@interface ARTRealtimeChannel : ARTRestChannel

@property (readonly, strong, nonatomic) ARTRealtime *realtime;
@property (readonly, strong, nonatomic) ARTPresence *presence;
@property (readonly, strong, nonatomic) ARTRealtimePresence *presence;
@property (readwrite, assign, nonatomic) ARTRealtimeChannelState state;
@property (readwrite, strong, nonatomic) NSMutableArray *queuedMessages;
@property (readwrite, strong, nonatomic) NSString *attachSerial;
Expand All @@ -37,8 +36,7 @@
@property (readonly, strong, nonatomic) NSMutableDictionary *presenceDict;
@property (readonly, getter=getClientId) NSString *clientId;
@property (readonly, strong, nonatomic) NSMutableArray *stateSubscriptions;
@property (readonly, strong, nonatomic) id<ARTPayloadEncoder> payloadEncoder;
@property (readwrite, strong, nonatomic) ARTPresenceMap * presenceMap;
@property (readwrite, strong, nonatomic) ARTPresenceMap *presenceMap;
@property (readwrite, assign, nonatomic) ARTPresenceAction lastPresenceAction;

- (instancetype)initWithRealtime:(ARTRealtime *)realtime andName:(NSString *)name withOptions:(ARTChannelOptions *)options;
Expand Down
5 changes: 2 additions & 3 deletions ably-ios/ARTRealtimeChannel.m
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
#import "ARTRealtime+Private.h"
#import "ARTMessage.h"
#import "ARTAuth.h"
#import "ARTPresence.h"
#import "ARTRealtimePresence.h"
#import "ARTChannel.h"
#import "ARTChannelOptions.h"
#import "ARTProtocolMessage.h"
Expand All @@ -30,14 +30,13 @@ - (instancetype)initWithRealtime:(ARTRealtime *)realtime andName:(NSString *)nam
self = [super initWithName:name withOptions:options andRest:realtime.rest];
if (self) {
_realtime = realtime;
_presence = [[ARTPresence alloc] initWithChannel:self];
_presence = [[ARTRealtimePresence alloc] initWithChannel:self];
_state = ARTRealtimeChannelInitialised;
_queuedMessages = [NSMutableArray array];
_attachSerial = nil;
_subscriptions = [NSMutableDictionary dictionary];
_presenceSubscriptions = [NSMutableArray array];
_stateSubscriptions = [NSMutableArray array];
_payloadEncoder = [ARTPayload defaultPayloadEncoder:options.cipherParams];
_presenceMap =[[ARTPresenceMap alloc] init];
_lastPresenceAction = ARTPresenceAbsent;
}
Expand Down
3 changes: 3 additions & 0 deletions ably-ios/ARTRealtimeChannelSubscription.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,9 @@

- (instancetype)initWithChannel:(ARTRealtimeChannel *)channel cb:(ARTRealtimeChannelPresenceCb)cb;

- (void)excludeAction:(ARTPresenceAction)action;
- (void)excludeAllActionsExcept:(ARTPresenceAction)action;

- (void)unsubscribe;

@end
Expand Down
7 changes: 4 additions & 3 deletions ably-ios/ARTRealtimeChannelSubscription.m
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

#import "ARTRealtime.h"
#import "ARTRealtimeChannel.h"
#import "ARTPresence.h"
#import "ARTRealtimePresence.h"

#pragma mark - ARTRealtimeChannelSubscription

Expand Down Expand Up @@ -47,10 +47,11 @@ - (instancetype)initWithChannel:(ARTRealtimeChannel *)channel cb:(ARTRealtimeCha
return self;
}

- (void)excludeAction:(ARTPresenceAction) action {
- (void)excludeAction:(ARTPresenceAction)action {
[_excludedActions addObject:[NSNumber numberWithInt:(int) action]];
}
- (void)excludeAllActionsExcept:(ARTPresenceAction) action {

- (void)excludeAllActionsExcept:(ARTPresenceAction)action {
for(int i=0; i<(int) ARTPresenceLast; i++) {
if(i != (int) action) {
[_excludedActions addObject:[NSNumber numberWithInt:(int) i]];
Expand Down
32 changes: 32 additions & 0 deletions ably-ios/ARTRealtimePresence.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
//
// ARTRealtimePresence.h
// ably
//
// Created by Ricardo Pereira on 12/11/15.
// Copyright © 2015 Ably. All rights reserved.
//

#import <Foundation/Foundation.h>
#import "ARTRestPresence.h"

@class ARTRealtimeChannel;

@interface ARTRealtimePresence : ARTRestPresence

- (instancetype)initWithChannel:(ARTRealtimeChannel *)channel;

- (void)enter:(id)data cb:(ARTStatusCallback)cb;
- (void)update:(id)data cb:(ARTStatusCallback)cb;
- (void)leave:(id)data cb:(ARTStatusCallback)cb;

- (void)enterClient:(NSString *)clientId data:(id)data cb:(ARTStatusCallback)cb;
- (void)updateClient:(NSString *)clientId data:(id)data cb:(ARTStatusCallback)cb;
- (void)leaveClient:(NSString *)clientId data:(id)data cb:(ARTStatusCallback)cb;
- (BOOL)isSyncComplete;

- (id<ARTSubscription>)subscribe:(ARTRealtimeChannelPresenceCb)cb;
- (id<ARTSubscription>)subscribe:(ARTPresenceAction)action cb:(ARTRealtimeChannelPresenceCb)cb;
- (void)unsubscribe:(id<ARTSubscription>)subscription;
- (void)unsubscribe:(id<ARTSubscription>)subscription action:(ARTPresenceAction)action;

@end
Loading