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 {
  // We check the completer first because _headlessWebView may be null because we haven't loaded the version asset yet.
  // 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 (_headlessWebView == null) {
    // no-op
    if (kDebugMode) {
      print('📗 session destroy: Session already destroyed');
    }
    return;
  }

  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()` before setting the `me` property
  if (!_completer.isCompleted) {
    _completer.completeError(StateError("The session has been destroyed"));
  }
}