From 4fd35eb41d90e5cc19a6effd19bc4c86642426af Mon Sep 17 00:00:00 2001 From: Hiroshi Horie <548776+hiroshihorie@users.noreply.github.com> Date: Fri, 4 Mar 2022 21:39:28 +0900 Subject: [PATCH] organize the cleanUp chain --- lib/src/core/engine.dart | 19 +++++++------------ lib/src/core/room.dart | 24 +++++++++++++++--------- lib/src/core/signal_client.dart | 11 ++++------- 3 files changed, 26 insertions(+), 28 deletions(-) diff --git a/lib/src/core/engine.dart b/lib/src/core/engine.dart index 003e8ad..8d2eaa4 100644 --- a/lib/src/core/engine.dart +++ b/lib/src/core/engine.dart @@ -88,9 +88,9 @@ class Engine extends Disposable with EventsEmittable { _setUpSignalListeners(); onDispose(() async { + await cleanUp(); await events.dispose(); await delays.dispose(); - await close(); await _signalListener.dispose(); }); } @@ -137,27 +137,22 @@ class Engine extends Disposable with EventsEmittable { } } - /// Close connection between the server. - Future close() async { - logger.fine('${runtimeType}.close()'); - if (_connectionState == ConnectionState.disconnected) { - logger.warning('${runtimeType}.close() already disconnected'); - } - // _statsTimer.cancel(); + // resets internal state to a re-usable state + Future cleanUp() async { + logger.fine('[${objectId}] cleanUp()'); + // cancel all ongoing delays await delays.cancelAll(); - // PCTransport is responsible for disposing RTCPeerConnection await publisher?.dispose(); publisher = null; await subscriber?.dispose(); subscriber = null; - await signalClient.disconnect(); + await signalClient.cleanUp(); _updateConnectionState(ConnectionState.disconnected); - // notifyListeners(); } @internal @@ -647,7 +642,7 @@ class Engine extends Disposable with EventsEmittable { '[Signal] Received Leave while engine is reconnecting, ignoring...'); return; } - await close(); + await cleanUp(); }) ..on( (event) => events.emit(EngineRemoteMuteChangedEvent( diff --git a/lib/src/core/room.dart b/lib/src/core/room.dart index 373e2a4..2d610d4 100644 --- a/lib/src/core/room.dart +++ b/lib/src/core/room.dart @@ -91,6 +91,8 @@ class Room extends DisposableChangeNotifier with EventsEmittable { }); onDispose(() async { + // clean up routine + await _cleanUp(); // dispose events await events.dispose(); // dispose local participant @@ -489,24 +491,28 @@ class Room extends DisposableChangeNotifier with EventsEmittable { extension RoomPrivateMethods on Room { // resets internal state to a re-usable state Future _cleanUp() async { - logger.fine('[$objectId] _handleClose()'); - if (connectionState == ConnectionState.disconnected) { - logger.warning('[$objectId]: close() already disconnected'); - } + logger.fine('[${objectId}] cleanUp()'); // clean up RemoteParticipants - for (final _ in _participants.values.toList()) { + for (final participant in _participants.values) { // RemoteParticipant is responsible for disposing resources - await _.dispose(); + await participant.dispose(); } _participants.clear(); // clean up LocalParticipant await localParticipant?.unpublishAllTracks(); - // clean up engine - await engine.close(); - _activeSpeakers.clear(); + + // clean up engine + await engine.cleanUp(); + + // reset params + _name = null; + _sid = null; + _metadata = null; + _serverVersion = null; + _serverRegion = null; } } diff --git a/lib/src/core/signal_client.dart b/lib/src/core/signal_client.dart index 58836d5..cdfa5d8 100644 --- a/lib/src/core/signal_client.dart +++ b/lib/src/core/signal_client.dart @@ -36,8 +36,8 @@ class SignalClient extends Disposable with EventsEmittable { }); onDispose(() async { - await events.dispose(); await cleanUp(); + await events.dispose(); }); } @@ -106,19 +106,16 @@ class SignalClient extends Disposable with EventsEmittable { } } + // resets internal state to a re-usable state @internal Future cleanUp() async { + logger.fine('[${objectId}] cleanUp()'); + await _ws?.dispose(); _ws = null; _queue.clear(); } - @internal - Future disconnect() async { - logger.fine('SignalClient disconnect'); - await cleanUp(); - } - void _sendRequest( lk_rtc.SignalRequest req, { bool enqueueIfReconnecting = true,