Skip to content

Live query connection stream #313

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 3 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 20 additions & 1 deletion lib/src/network/parse_live_query.dart
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import 'dart:async';
import 'dart:convert';
import 'dart:io';

import 'package:connectivity/connectivity.dart';
import 'package:flutter/widgets.dart';
import 'package:web_socket_channel/io.dart';
import 'package:web_socket_channel/web_socket_channel.dart';
import 'package:connectivity/connectivity.dart';

import '../../parse_server_sdk.dart';

Expand All @@ -31,10 +32,16 @@ class Subscription {
}
}

enum LiveQueryClientEvent { CONNECTED, DISCONNECTED }

class Client with WidgetsBindingObserver {
factory Client() => _getInstance();
Client._internal(
{bool debug, ParseHTTPClient client, bool autoSendSessionId}) {
_clientEventStreamController = StreamController<LiveQueryClientEvent>();
_clientEventStream =
_clientEventStreamController.stream.asBroadcastStream();

_client = client ??
ParseHTTPClient(
sendSessionId:
Expand Down Expand Up @@ -68,6 +75,10 @@ class Client with WidgetsBindingObserver {
return _instance;
}

Stream<LiveQueryClientEvent> get getClientEventStream {
return _clientEventStream;
}

WebSocket _webSocket;
ParseHTTPClient _client;
bool _debug;
Expand All @@ -76,6 +87,8 @@ class Client with WidgetsBindingObserver {
String _liveQueryURL;
bool _userDisconnected = false;
bool _connecting = false;
StreamController<LiveQueryClientEvent> _clientEventStreamController;
Stream<LiveQueryClientEvent> _clientEventStream;

final Map<int, Subscription> _requestSubScription = <int, Subscription>{};

Expand Down Expand Up @@ -185,13 +198,17 @@ class Client with WidgetsBindingObserver {
_channel.stream.listen((dynamic message) {
_handleMessage(message);
}, onDone: () {
_clientEventStreamController.sink
.add(LiveQueryClientEvent.DISCONNECTED);
if (!_userDisconnected) {
reconnect();
}
if (_debug) {
print('$_printConstLiveQuery: Done');
}
}, onError: (Object error) {
_clientEventStreamController.sink
.add(LiveQueryClientEvent.DISCONNECTED);
if (!_userDisconnected) {
reconnect();
}
Expand All @@ -203,6 +220,7 @@ class Client with WidgetsBindingObserver {
ParseApiRQ.liveQuery, _debug, 'IOWebSocketChannel'));
});
} on Exception catch (e) {
_clientEventStreamController.sink.add(LiveQueryClientEvent.DISCONNECTED);
if (_debug) {
print('$_printConstLiveQuery: Error: ${e.toString()}');
}
Expand Down Expand Up @@ -286,6 +304,7 @@ class Client with WidgetsBindingObserver {
_requestSubScription.values.toList().forEach((Subscription subcription) {
_subscribeLiveQuery(subcription);
});
_clientEventStreamController.sink.add(LiveQueryClientEvent.CONNECTED);
return;
}
if (actionData.containsKey('requestId')) {
Expand Down