From 3651863ccfd433590c19da9a2d21514c2b099aa2 Mon Sep 17 00:00:00 2001 From: Sai Gokula Krishnan Date: Thu, 18 Jan 2024 04:20:02 +0530 Subject: [PATCH] Fixes #5 --- CHANGELOG.md | 6 ++++++ README.md | 6 ++---- lib/client.dart | 32 +++++++++++++++++++++++++++++--- pubspec.yaml | 3 ++- 4 files changed, 39 insertions(+), 8 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index f83ba01..99d0724 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,6 +1,12 @@ # Changelog 📝 +### v1.6.0 🚀 + +#### Added +- Web support + - Your existing code now flawlessly extends its magic to the web – no extra setup required, just pure, uninterrupted functionality across platforms! + ### v1.5.1 📝 #### Updated diff --git a/README.md b/README.md index 98c3314..b146032 100644 --- a/README.md +++ b/README.md @@ -10,14 +10,12 @@ EventFlux is a Dart package designed for efficient handling of server-sent event |----------|---------|---------------------------------| | Android | ✅ | Fully tested and functioning smoothly. | | iOS | ✅ | As Steve Jobs would say, "It just works." | -| Web | 🏗️ | Web's getting there - building as we speak!| +| Web | ✅ | Full functionality unlocked! | | MacOS | ✅ | Smooth sailing on MacOS. | | Windows | ❓ | Windows users, where you at? Need testers! | | Linux | ❓ | Calling all penguins - help me to test on Linux! | -*Pssst... see those question marks? That's your cue, tech adventurers! Dive in, test, and tell me all about it.* 🚀🛠️ - ## Inspiration 💡 @@ -42,7 +40,7 @@ Add EventFlux to your Dart project's dependencies, and you're golden: ```yaml dependencies: - eventflux: ^1.5.1 + eventflux: ^1.6.0 ``` diff --git a/lib/client.dart b/lib/client.dart index cb2880a..d4884c0 100644 --- a/lib/client.dart +++ b/lib/client.dart @@ -10,6 +10,8 @@ import 'package:eventflux/models/data.dart'; import 'package:eventflux/models/exception.dart'; import 'package:eventflux/models/response.dart'; import 'package:eventflux/utils.dart'; +import 'package:fetch_client/fetch_client.dart'; +import 'package:flutter/foundation.dart'; import 'package:http/http.dart'; /// A class for managing event-driven data streams using Server-Sent Events (SSE). @@ -131,7 +133,10 @@ class EventFlux extends EventFluxBase { Function(EventFluxException)? onError, Map? body}) { /// Initalise variables - _client = Client(); + /// Create a new HTTP client based on the platform + _client = _getClientBasedOnPlatform(); + + /// Set `_isExplicitDisconnect` to `false` before connecting. _isExplicitDisconnect = false; _streamController = StreamController(); @@ -265,6 +270,9 @@ class EventFlux extends EventFluxBase { status: EventFluxStatus.connected, stream: _streamController!.stream)); }).catchError((e) async { + if (onError != null) { + onError(EventFluxException(message: e.toString())); + } _stop(); _reconnectWithDelay(_isExplicitDisconnect, autoReconnect, type, url, header, onSuccessCallback, @@ -295,8 +303,8 @@ class EventFlux extends EventFluxBase { Future _stop() async { eventFluxLog('Disconnecting', LogEvent.info); try { - _streamController!.close(); - _client!.close(); + _streamController?.close(); + _client?.close(); Future.delayed(const Duration(seconds: 1), () {}); eventFluxLog('Disconnected', LogEvent.info); return EventFluxStatus.disconnected; @@ -337,4 +345,22 @@ class EventFlux extends EventFluxBase { }); } } + + /// Internal method to get the HTTP client based on the platform. + /// + /// This method checks if the code is running on a web platform using `kIsWeb`. + /// - If on the web, it returns a `FetchClient` with CORS mode enabled to handle + /// web-specific network requests. + /// - For non-web platforms (like Android, iOS, MacOS, etc.), it returns a standard + /// `Client` instance. + /// + /// Usage of `FetchClient` with CORS mode is necessary for web due to browser + /// security restrictions on cross-origin requests. + Client _getClientBasedOnPlatform() { + if (kIsWeb) { + return FetchClient(mode: RequestMode.cors); + } else { + return Client(); + } + } } diff --git a/pubspec.yaml b/pubspec.yaml index 334080e..1106fae 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -1,6 +1,6 @@ name: eventflux description: "Efficient handling of server-sent event streams with easy connectivity and data management." -version: 1.5.1 +version: 1.6.0 homepage: https://gokula.dev repository: https://github.com/Imgkl/EventFlux issue_tracker: https://github.com/Imgkl/EventFlux/issues @@ -17,6 +17,7 @@ dependencies: flutter: sdk: flutter http: ^1.1.2 + fetch_client: ^1.0.2 dev_dependencies: flutter_test: