Skip to content

Commit

Permalink
Change/subscription (#48)
Browse files Browse the repository at this point in the history
* Remove unused _subscriptions variable

* Resubscribe on reconnection

* Update package version.
  • Loading branch information
tanutapi authored Dec 28, 2021
1 parent a3783a9 commit 949f958
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 20 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# 2.1.3
- Resend subscription packets on reconnection.
# 2.1.2
- Return MeteorClientLoginResult on logoutOtherClients.
# 2.1.1
Expand Down
5 changes: 5 additions & 0 deletions lib/src/ddp_client.dart
Original file line number Diff line number Diff line change
Expand Up @@ -305,6 +305,11 @@ class DdpClient {
var msg = json.encode(data);
printDebug('Send: $msg');
_socket!.add(msg);

// Resend all subscriptions
_subscriptionHandlers.forEach((id, handler) {
_sendMsgSub(id, handler.subName, handler.args);
});
}
}

Expand Down
3 changes: 0 additions & 3 deletions lib/src/meteor_client.dart
Original file line number Diff line number Diff line change
Expand Up @@ -83,8 +83,6 @@ class MeteorClient {
DateTime? _tokenExpires;
UserLogInStatus _logInStatus = UserLogInStatus.loggedOut;

final Map<String, SubscriptionHandler> _subscriptions = {};

/// Meteor.collections
final Map<String, Map<String, dynamic>> _collections = {};
final Map<String, BehaviorSubject<Map<String, dynamic>>> _collectionsSubject =
Expand Down Expand Up @@ -287,7 +285,6 @@ class MeteorClient {
Function? onReady}) {
var handler =
connection.subscribe(name, args, onStop: onStop, onReady: onReady);
_subscriptions[handler.subId] = handler;
return handler;
}

Expand Down
2 changes: 1 addition & 1 deletion pubspec.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name: dart_meteor
description: This library make connection between meteor backend and flutter app easily. Design to work seamlessly with StreamBuilder and FutureBuilder.
version: 2.1.2
version: 2.1.3
homepage: https://github.com/tanutapi/dart_meteor

environment:
Expand Down
57 changes: 41 additions & 16 deletions test/dart_meteor_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -248,7 +248,8 @@ void main() {
// expiration than the curren token. Otherwise, a bad guy with a
// stolen token could use this method to stop his stolen token from
// ever expiring.
expect(result2.tokenExpires.millisecondsSinceEpoch, lessThanOrEqualTo(result1.tokenExpires.millisecondsSinceEpoch));
expect(result2.tokenExpires.millisecondsSinceEpoch,
lessThanOrEqualTo(result1.tokenExpires.millisecondsSinceEpoch));
});
});

Expand Down Expand Up @@ -291,13 +292,13 @@ void main() {
});

group('subscription', () {
var meteor = MeteorClient.connect(
url: url,
debug: true,
);
late MeteorClient meteor;

setUp(() async {
meteor.reconnect();
meteor = MeteorClient.connect(
url: url,
debug: true,
);
await Future.delayed(Duration(seconds: 2));
});

Expand Down Expand Up @@ -347,17 +348,13 @@ void main() {
}
});

test('clearAllMessages', () async {
await meteor.loginWithPassword('user1', 'password1');
await meteor.call('clearAllMessages');
});

test(
'collection(messages) stream should have values and "createdAt" should be instance of DateTime',
() async {
var completer = Completer();
expect(completer.future, completion(true));
await meteor.loginWithPassword('user1', 'password1');
await meteor.call('clearAllMessages');
meteor.subscribe(
'messages',
);
Expand All @@ -383,16 +380,44 @@ void main() {
completer.complete(false);
}
});

test('meteor should resume subscription after reconnected', () async {
var completer = Completer();
expect(completer.future, completion(true));
await meteor.loginWithPassword('user1', 'password1');
await meteor.call('clearAllMessages');
meteor.subscribe(
'messages',
);
meteor.collection('messages').listen((value) {
var msgCnt = value.values.toList().length;
print('resume subscription, message count: $msgCnt');
if (msgCnt == 2) {
completer.complete(true);
}
});
await meteor.call('sendMessage', args: ['message 1']);
await Future.delayed(Duration(seconds: 2));
meteor.disconnect();
await Future.delayed(Duration(seconds: 2));
meteor.reconnect();
await Future.delayed(Duration(seconds: 2));
await meteor.call('sendMessage', args: ['message 2']);
await Future.delayed(Duration(seconds: 2));
if (!completer.isCompleted) {
completer.complete(false);
}
});
});

group('Reactive with rxdart', () {
var meteor = MeteorClient.connect(
url: url,
debug: true,
);
late MeteorClient meteor;

setUp(() async {
meteor.reconnect();
meteor = MeteorClient.connect(
url: url,
debug: true,
);
await Future.delayed(Duration(seconds: 2));
});

Expand Down

0 comments on commit 949f958

Please sign in to comment.