Skip to content

Commit

Permalink
Exponential reconnection logic, PWA icons
Browse files Browse the repository at this point in the history
  • Loading branch information
FeodorFitsner committed May 3, 2022
1 parent fb06a2d commit 1e31b5e
Show file tree
Hide file tree
Showing 9 changed files with 20 additions and 11 deletions.
5 changes: 5 additions & 0 deletions client/lib/models/app_state.dart
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ class AppState extends Equatable {
final Uri? pageUri;
final String sessionId;
final bool isLoading;
final int reconnectingTimeout;
final String error;
final Size size;
final String sizeBreakpoint;
Expand All @@ -23,6 +24,7 @@ class AppState extends Equatable {
{required this.pageUri,
required this.sessionId,
required this.isLoading,
required this.reconnectingTimeout,
required this.error,
required this.size,
required this.sizeBreakpoint,
Expand All @@ -33,6 +35,7 @@ class AppState extends Equatable {
pageUri: null,
sessionId: "",
isLoading: true,
reconnectingTimeout: 0,
error: "",
size: Size(0, 0),
sizeBreakpoint: "",
Expand All @@ -50,6 +53,7 @@ class AppState extends Equatable {
{Uri? pageUri,
String? sessionId,
bool? isLoading,
int? reconnectingTimeout,
String? error,
Size? size,
String? sizeBreakpoint,
Expand All @@ -59,6 +63,7 @@ class AppState extends Equatable {
pageUri: pageUri ?? this.pageUri,
sessionId: sessionId ?? this.sessionId,
isLoading: isLoading ?? this.isLoading,
reconnectingTimeout: reconnectingTimeout ?? this.reconnectingTimeout,
error: error ?? this.error,
size: size ?? this.size,
sizeBreakpoint: sizeBreakpoint ?? this.sizeBreakpoint,
Expand Down
11 changes: 9 additions & 2 deletions client/lib/reducers.dart
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,10 @@ AppState appReducer(AppState state, dynamic action) {
//
if (action.payload.error != null && action.payload.error!.isNotEmpty) {
// error
return state.copyWith(isLoading: false, error: action.payload.error);
return state.copyWith(
isLoading: false,
reconnectingTimeout: 0,
error: action.payload.error);
} else {
final sessionId = action.payload.session!.id;

Expand All @@ -74,14 +77,18 @@ AppState appReducer(AppState state, dynamic action) {
// connected to the session
return state.copyWith(
isLoading: false,
reconnectingTimeout: 0,
sessionId: sessionId,
controls: action.payload.session!.controls);
}
} else if (action is PageReconnectingAction) {
//
// reconnecting WebSocket
//
return state.copyWith(isLoading: true);
return state.copyWith(
isLoading: true,
reconnectingTimeout:
state.reconnectingTimeout == 0 ? 1 : state.reconnectingTimeout * 2);
} else if (action is AppBecomeInactiveAction) {
//
// app become inactive
Expand Down
9 changes: 5 additions & 4 deletions client/lib/web_socket_client.dart
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import 'dart:convert';

import 'package:flet_view/models/app_state.dart';
import 'package:flet_view/protocol/add_page_controls_payload.dart';
import 'package:flet_view/protocol/app_become_inactive_payload.dart';
import 'package:flet_view/protocol/append_control_props_request.dart';
Expand All @@ -22,20 +23,19 @@ import 'protocol/register_webclient_request.dart';
import 'protocol/register_webclient_response.dart';

WebSocketClient ws = WebSocketClient();
int reconnectionTimeoutSeconds = 5;

class WebSocketClient {
WebSocketChannel? _channel;
String _serverUrl = "";
Store? _store;
Store<AppState>? _store;
bool _connected = false;
String _pageName = "";
String _pageHash = "";
String _winWidth = "";
String _winHeight = "";
String? _sessionId;

set store(Store store) {
set store(Store<AppState> store) {
_store = store;
}

Expand All @@ -49,7 +49,8 @@ class WebSocketClient {
_channel!.stream.listen(_onMessage, onDone: () async {
debugPrint("WS stream closed");
_store!.dispatch(PageReconnectingAction());
Future.delayed(Duration(seconds: reconnectionTimeoutSeconds))
debugPrint("Reconnect in ${_store!.state.reconnectingTimeout} seconds");
Future.delayed(Duration(seconds: _store!.state.reconnectingTimeout))
.then((value) {
connect(serverUrl: _serverUrl);
_registerWebClient();
Expand Down
Binary file modified client/web/icons/Icon-192.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified client/web/icons/Icon-512.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified client/web/icons/Icon-maskable-192.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified client/web/icons/Icon-maskable-512.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 1 addition & 1 deletion sdk/python/playground/icons-browser.py
Original file line number Diff line number Diff line change
Expand Up @@ -125,4 +125,4 @@ def search_click(e):
)


flet.app(name="test1", port=8550, target=main, view=flet.WEB_BROWSER)
flet.app(name="test1", port=8550, target=main, view=flet.FLET_APP)
4 changes: 0 additions & 4 deletions sdk/python/playground/textfield-test.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
import logging
from cmath import exp
from datetime import datetime
from time import sleep
from tkinter import W

import flet
from flet import (
Expand All @@ -14,13 +12,11 @@
Row,
Text,
Theme,
border,
border_radius,
dropdown,
icons,
padding,
)
from flet.border_radius import BorderRadius
from flet.checkbox import Checkbox
from flet.container import Container
from flet.icon import Icon
Expand Down

0 comments on commit 1e31b5e

Please sign in to comment.