Skip to content

Commit 7127b07

Browse files
committed
improve setUser method
1 parent 16a3caa commit 7127b07

File tree

4 files changed

+26
-36
lines changed

4 files changed

+26
-36
lines changed

lib/src/core/extension.dart

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,11 @@ extension FutureX<T> on Future<T> {
3737
);
3838
}
3939

40-
Future<T> tap(void Function(T) tapFn) {
40+
Future<T> tap(void Function(T) tapFn) async {
41+
try {
42+
tapFn(await this);
43+
// ignore: empty_catches
44+
} catch (_e) {}
4145
return this;
4246
}
4347

lib/src/features/realtime/mqtt_service_impl.dart

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ class MqttServiceImpl implements IRealtimeService {
7575
@override
7676
Future<void> connect() async {
7777
try {
78-
log('connecting to mqtt');
78+
log('connecting to mqtt (${_mqtt.server})');
7979
var status = await _mqtt.connect();
8080
log('connected to mqtt: $status');
8181
} on NoConnectionException catch (error) {
@@ -129,7 +129,7 @@ class MqttServiceImpl implements IRealtimeService {
129129
.asyncMap((_) =>
130130
_mqtt.connectionStatus.state == MqttConnectionState.disconnected)
131131
.distinct()
132-
.where((it) => it == true)
132+
.where((it) => it == true && _s.isRealtimeEnabled)
133133
.asBroadcastStream();
134134
}
135135

lib/src/qiscus_core.dart

Lines changed: 17 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -175,7 +175,7 @@ class QiscusSDK {
175175
Future.sync(() async {
176176
await _authenticated;
177177
await clearSubscription();
178-
await __<Storage>().clear();
178+
__<Storage>().clear();
179179
await __<IRealtimeService>().end();
180180
}).toCallback1(callback);
181181
}
@@ -691,30 +691,7 @@ class QiscusSDK {
691691
..syncInterval = syncInterval.milliseconds
692692
..syncIntervalWhenConnected = syncIntervalWhenConnected.milliseconds;
693693

694-
await __<AppConfigUseCase>()(noParams).toCallback1(callback);
695-
}
696-
697-
Future<void> __subscribes(String token) async {
698-
final params = TokenParams(token);
699-
final onMessageReceived = __<OnMessageReceived>();
700-
final realtimeService = __<IRealtimeService>();
701-
final onMessageUpdated = __<OnMessageUpdated>();
702-
703-
var stream = StreamGroup.merge<void>([
704-
onMessageReceived.subscribe(params).map((_) => null),
705-
onMessageUpdated.subscribe(params).map((_) => null),
706-
realtimeService.subscribe(TopicBuilder.notification(token)).asStream(),
707-
]);
708-
709-
return stream.first;
710-
}
711-
712-
Future<Tuple2<String, Account>> _subscribes(
713-
Tuple2<String, Account> data,
714-
) async {
715-
await __subscribes(data.first);
716-
717-
return data;
694+
__<AppConfigUseCase>()(noParams).toCallback1(callback);
718695
}
719696

720697
void setUser({
@@ -745,8 +722,7 @@ class QiscusSDK {
745722

746723
authenticate
747724
.call(params)
748-
.then((either) => _connectMqtt(either))
749-
.then(_subscribes)
725+
.tap((_) => _connectMqtt())
750726
.then((it) => it.second.toModel())
751727
.toCallback2(callback);
752728
}
@@ -760,18 +736,27 @@ class QiscusSDK {
760736

761737
authenticate(params)
762738
.then((account) => Tuple2(token, account))
763-
.then((either) => _connectMqtt(either))
764-
.then(_subscribes)
739+
.tap((_) => _connectMqtt())
765740
.then((it) => it.second.toModel())
766741
.toCallback2(callback);
767742
}
768743

769-
Future<T> _connectMqtt<T>(T it) async {
744+
void _connectMqtt<T>() async {
770745
if (__<Storage>().isRealtimeEnabled) {
771746
await __<IRealtimeService>().connect();
772-
return it;
773747
}
774-
return it;
748+
final params = TokenParams(token);
749+
final onMessageReceived = __<OnMessageReceived>();
750+
final realtimeService = __<IRealtimeService>();
751+
final onMessageUpdated = __<OnMessageUpdated>();
752+
753+
var stream = StreamGroup.merge<void>([
754+
onMessageReceived.subscribe(params).map((_) => null),
755+
onMessageUpdated.subscribe(params).map((_) => null),
756+
realtimeService.subscribe(TopicBuilder.notification(token)).asStream(),
757+
]);
758+
759+
await stream.first;
775760
}
776761

777762
void unsubscribeChatRoom(QChatRoom room) {

lib/src/realtime/realtime.dart

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,8 @@ extension IMqttReceiveX on MqttClient {
9090
return connectionStatus.state == MqttConnectionState.connected;
9191
}
9292

93-
void subscribe$(String topic) {
93+
void subscribe$(String topic) async {
94+
await isConnected$;
9495
subscribe(topic, MqttQos.atLeastOnce);
9596
}
9697
}

0 commit comments

Comments
 (0)