destroy method

Future<void> destroy()

Invalidates this session

You cannot use any objects that were created in this session after you destroy it.

If you want to use TalkJS after having called destroy() you must instantiate a new Session instance.

Implementation

Future<void> destroy() async {
  if (_headlessWebView == null) {
    // no-op
    if (kDebugMode) {
      print('📗 session destroy: Session already destroyed');
    }
    return;
  }

  // We await for the completer only if the `me` property has been set
  if ((!_completer.isCompleted) && (_me != null)) {
    if (kDebugMode) {
      print(
          '📗 session destroy: !_completer.isCompleted, awaiting for _completer.future');
    }
    await _completer.future;
  }

  if (kDebugMode) {
    print('📗 session destroy: Destroying session');
  }

  // If the `me` property has not been set, it means that nothing has been done
  // in the WebView. As a matter of fact we don't even know if the WebView has finished initializing.
  if (_me != null) {
    await _execute('session.destroy()');
  }

  _headlessWebView!.dispose();
  _headlessWebView = null;
  _webViewController = null;

  // _completer.isCompleted could be false if we're calling `session.destroy()` beofre setting the `me` property
  if (!_completer.isCompleted) {
    _completer.completeError(StateError("The session has been destroyed"));
  }
}