Skip to content

Commit

Permalink
Merge pull request #189 from ably/move-classes-into-files
Browse files Browse the repository at this point in the history
Refactoring: Move classes into separate files
  • Loading branch information
QuintinWillison committed Nov 2, 2021
2 parents 58930ef + cc55182 commit bd67ff9
Show file tree
Hide file tree
Showing 13 changed files with 66 additions and 55 deletions.
4 changes: 2 additions & 2 deletions lib/src/message/src/message.dart
Expand Up @@ -90,7 +90,7 @@ class Message {
/// RSL6 and RLS6b as mentioned in TM3
Message.fromEncoded(
Map<String, dynamic> jsonObject, [
ChannelOptions? channelOptions,
RestChannelOptions? channelOptions,
]) : id = jsonObject['id'] as String?,
name = jsonObject['name'] as String?,
clientId = jsonObject['clientId'] as String?,
Expand All @@ -111,7 +111,7 @@ class Message {
/// https://docs.ably.com/client-lib-development-guide/features/#TM3
static List<Message> fromEncodedArray(
List<Map<String, dynamic>> jsonArray, [
ChannelOptions? channelOptions,
RestChannelOptions? channelOptions,
]) =>
jsonArray.map((e) => Message.fromEncoded(e, channelOptions)).toList();

Expand Down
4 changes: 2 additions & 2 deletions lib/src/message/src/presence_message.dart
Expand Up @@ -92,7 +92,7 @@ class PresenceMessage {
/// RSL6 and RLS6b as mentioned in TP4
PresenceMessage.fromEncoded(
Map<String, dynamic> jsonObject, [
ChannelOptions? channelOptions,
RestChannelOptions? channelOptions,
]) : id = jsonObject['id'] as String?,
action = PresenceAction.values.firstWhere((e) =>
e.toString().split('.')[1] == jsonObject['action'] as String?),
Expand All @@ -114,7 +114,7 @@ class PresenceMessage {
/// https://docs.ably.com/client-lib-development-guide/features/#TP4
static List<PresenceMessage> fromEncodedArray(
List<Map<String, dynamic>> jsonArray, [
ChannelOptions? channelOptions,
RestChannelOptions? channelOptions,
]) =>
jsonArray
.map((jsonObject) => PresenceMessage.fromEncoded(
Expand Down
7 changes: 4 additions & 3 deletions lib/src/platform/src/codec.dart
Expand Up @@ -8,6 +8,7 @@ import '../../message/message.dart';
import '../../push_notifications/push_notifications.dart';
import '../../push_notifications/src/local_device.dart';
import '../../realtime/realtime.dart';
import '../../realtime/src/realtime_channel_options.dart';
import '../../rest/rest.dart';
import '../platform.dart';

Expand Down Expand Up @@ -72,7 +73,7 @@ class Codec extends StandardMessageCodec {
CodecTypes.tokenRequest:
_CodecPair<TokenRequest>(_encodeTokenRequest, null),
CodecTypes.restChannelOptions:
_CodecPair<ChannelOptions>(_encodeRestChannelOptions, null),
_CodecPair<RestChannelOptions>(_encodeRestChannelOptions, null),
CodecTypes.realtimeChannelOptions: _CodecPair<RealtimeChannelOptions>(
_encodeRealtimeChannelOptions,
null,
Expand Down Expand Up @@ -309,9 +310,9 @@ class Codec extends StandardMessageCodec {
return jsonMap;
}

/// Encodes [ChannelOptions] to a Map
/// Encodes [RestChannelOptions] to a Map
/// returns null if [v] is null
Map<String, dynamic> _encodeRestChannelOptions(final ChannelOptions v) {
Map<String, dynamic> _encodeRestChannelOptions(final RestChannelOptions v) {
final jsonMap = <String, dynamic>{};
_writeToJson(jsonMap, TxRestChannelOptions.cipher, v.cipher);
return jsonMap;
Expand Down
2 changes: 2 additions & 0 deletions lib/src/platform/src/realtime/realtime_channel.dart
Expand Up @@ -10,6 +10,8 @@ import '../../../generated/platform_constants.dart';
import '../../../message/message.dart';
import '../../../push_notifications/push_notifications.dart';
import '../../../realtime/realtime.dart';
import '../../../realtime/src/realtime_channel_options.dart';
import '../../../realtime/src/realtime_channels_interface.dart';
import '../../platform.dart';
import '../../platform_internal.dart';

Expand Down
12 changes: 12 additions & 0 deletions lib/src/platform/src/rest/publish_queue_item.dart
@@ -0,0 +1,12 @@
import 'dart:async';

import '../../../message/message.dart';

/// An item for used to enqueue a message to be published after an ongoing
/// authCallback is completed
class PublishQueueItem {
final List<Message> messages;
final Completer<void> completer;

PublishQueueItem(this.completer, this.messages);
}
20 changes: 6 additions & 14 deletions lib/src/platform/src/rest/rest_channel.dart
Expand Up @@ -9,6 +9,7 @@ import '../../../generated/platform_constants.dart';
import '../../../message/message.dart';
import '../../../rest/rest.dart';
import '../../platform.dart';
import 'publish_queue_item.dart';

/// Plugin based implementation of Rest channel
class RestChannel extends PlatformObject implements RestChannelInterface {
Expand All @@ -23,7 +24,7 @@ class RestChannel extends PlatformObject implements RestChannelInterface {

late RestPresence _presence;

/// instantiates with [Rest], [name] and [ChannelOptions]
/// instantiates with [Rest], [name] and [RestChannelOptions]
RestChannel(this.rest, this.push, this.name) {
_presence = RestPresence(this);
}
Expand Down Expand Up @@ -53,7 +54,7 @@ class RestChannel extends PlatformObject implements RestChannelInterface {
);
}

final _publishQueue = Queue<_PublishQueueItem>();
final _publishQueue = Queue<PublishQueueItem>();
Completer<void>? _authCallbackCompleter;

@override
Expand All @@ -66,7 +67,7 @@ class RestChannel extends PlatformObject implements RestChannelInterface {
messages ??= [
if (message == null) Message(name: name, data: data) else message
];
final queueItem = _PublishQueueItem(Completer<void>(), messages);
final queueItem = PublishQueueItem(Completer<void>(), messages);
_publishQueue.add(queueItem);
unawaited(_publishInternal());
return queueItem.completer.future;
Expand Down Expand Up @@ -155,18 +156,9 @@ class RestChannel extends PlatformObject implements RestChannelInterface {
}

@override
Future<void> setOptions(ChannelOptions options) =>
invoke(PlatformMethod.setRealtimeChannelOptions, {
Future<void> setOptions(RestChannelOptions options) =>
invoke(PlatformMethod.setRestChannelOptions, {
TxTransportKeys.channelName: name,
TxTransportKeys.options: options,
});
}

/// An item for used to enqueue a message to be published after an ongoing
/// authCallback is completed
class _PublishQueueItem {
final List<Message> messages;
final Completer<void> completer;

_PublishQueueItem(this.completer, this.messages);
}
30 changes: 1 addition & 29 deletions lib/src/realtime/src/channels.dart
Expand Up @@ -2,33 +2,17 @@ import 'dart:async';

import '../../authentication/authentication.dart';
import '../../common/common.dart';
import '../../common/src/channels.dart';
import '../../common/src/event_emitter.dart';
import '../../error/src/error_info.dart';
import '../../message/src/message.dart';
import '../../push_notifications/src/push_channel.dart';
import '../../rest/src/channel_options.dart';
import '../realtime.dart';
import 'channel_event.dart';
import 'channel_state.dart';
import 'channel_state_event.dart';
import 'presence.dart';
import 'realtime.dart';

/// options provided when instantiating a realtime channel
///
/// https://docs.ably.com/client-lib-development-guide/features/#TB1
class RealtimeChannelOptions extends ChannelOptions {
/// https://docs.ably.com/client-lib-development-guide/features/#TB2c
final Map<String, String>? params;

/// https://docs.ably.com/client-lib-development-guide/features/#TB2d
final List<ChannelMode>? modes;

/// create channel options with a cipher, params and modes
RealtimeChannelOptions(Object cipher, {this.params, this.modes})
: super(cipher);
}
import 'realtime_channel_options.dart';

/// A named channel through with realtime client can interact with ably service.
///
Expand Down Expand Up @@ -120,15 +104,3 @@ abstract class RealtimeChannelInterface
/// https://docs.ably.com/client-lib-development-guide/features/#RTL16
Future<void> setOptions(RealtimeChannelOptions options);
}

/// A collection of realtime channel objects
///
/// https://docs.ably.com/client-lib-development-guide/features/#RTS1
abstract class RealtimeChannelsInterface<T extends RealtimeChannelInterface>
extends Channels<T> {
/// instance of ably realtime client
RealtimeInterface realtime;

/// instantiates with the ably [RealtimeInterface] instance
RealtimeChannelsInterface(this.realtime);
}
1 change: 1 addition & 0 deletions lib/src/realtime/src/realtime.dart
Expand Up @@ -2,6 +2,7 @@ import '../../authentication/authentication.dart';
import '../../common/common.dart';
import '../../push_notifications/push_notifications.dart';
import '../realtime.dart';
import 'realtime_channels_interface.dart';

/// an abstract class for Ably's Realtime client
///
Expand Down
17 changes: 17 additions & 0 deletions lib/src/realtime/src/realtime_channel_options.dart
@@ -0,0 +1,17 @@
import '../../rest/rest.dart';
import '../realtime.dart';

/// options provided when instantiating a realtime channel
///
/// https://docs.ably.com/client-lib-development-guide/features/#TB1
class RealtimeChannelOptions extends RestChannelOptions {
/// https://docs.ably.com/client-lib-development-guide/features/#TB2c
final Map<String, String>? params;

/// https://docs.ably.com/client-lib-development-guide/features/#TB2d
final List<ChannelMode>? modes;

/// create channel options with a cipher, params and modes
RealtimeChannelOptions(Object cipher, {this.params, this.modes})
: super(cipher);
}
14 changes: 14 additions & 0 deletions lib/src/realtime/src/realtime_channels_interface.dart
@@ -0,0 +1,14 @@
import 'package:ably_flutter/src/common/common.dart';
import 'package:ably_flutter/src/realtime/realtime.dart';

/// A collection of realtime channel objects
///
/// https://docs.ably.com/client-lib-development-guide/features/#RTS1
abstract class RealtimeChannelsInterface<T extends RealtimeChannelInterface>
extends Channels<T> {
/// instance of ably realtime client
RealtimeInterface realtime;

/// instantiates with the ably [RealtimeInterface] instance
RealtimeChannelsInterface(this.realtime);
}
2 changes: 1 addition & 1 deletion lib/src/rest/rest.dart
@@ -1,6 +1,6 @@
export 'src/channel_options.dart';
export 'src/channels.dart';
export 'src/presence.dart';
export 'src/rest.dart';
export 'src/rest_channel_options.dart';
export 'src/rest_history_params.dart';
export 'src/rest_presence_params.dart';
4 changes: 2 additions & 2 deletions lib/src/rest/src/channels.dart
Expand Up @@ -3,8 +3,8 @@ import '../../common/src/channels.dart';
import '../../message/src/message.dart';
import '../../push_notifications/push_notifications.dart';
import '../rest.dart';
import 'channel_options.dart';
import 'rest.dart';
import 'rest_channel_options.dart';

/// A named channel through with rest client can interact with ably service.
///
Expand Down Expand Up @@ -51,7 +51,7 @@ abstract class RestChannelInterface {
/// stored channel options, then indicates success
///
/// https://docs.ably.com/client-lib-development-guide/features/#RSL7
Future<void> setOptions(ChannelOptions options);
Future<void> setOptions(RestChannelOptions options);
}

/// A collection of rest channel objects
Expand Down
@@ -1,10 +1,10 @@
/// options provided when instantiating a channel
///
/// https://docs.ably.com/client-lib-development-guide/features/#TB1
class ChannelOptions {
class RestChannelOptions {
/// https://docs.ably.com/client-lib-development-guide/features/#TB2b
final Object cipher;

/// create channel options with a cipher
ChannelOptions(this.cipher);
RestChannelOptions(this.cipher);
}

0 comments on commit bd67ff9

Please sign in to comment.