Skip to content
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

Added Browser support #6

Merged
merged 1 commit into from
Jan 17, 2024
Merged
Show file tree
Hide file tree
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
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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
Expand Down
6 changes: 2 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 💡

Expand All @@ -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
```


Expand Down
32 changes: 29 additions & 3 deletions lib/client.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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).
Expand Down Expand Up @@ -131,7 +133,10 @@ class EventFlux extends EventFluxBase {
Function(EventFluxException)? onError,
Map<String, dynamic>? 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<EventFluxData>();
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -295,8 +303,8 @@ class EventFlux extends EventFluxBase {
Future<EventFluxStatus> _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;
Expand Down Expand Up @@ -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();
}
}
}
3 changes: 2 additions & 1 deletion pubspec.yaml
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -17,6 +17,7 @@ dependencies:
flutter:
sdk: flutter
http: ^1.1.2
fetch_client: ^1.0.2

dev_dependencies:
flutter_test:
Expand Down