Skip to content

Commit

Permalink
Require Dart 3.2, use pkg:web
Browse files Browse the repository at this point in the history
  • Loading branch information
kevmoo committed Oct 26, 2023
1 parent 37df57d commit 7f44aba
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 29 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/test-package.yml
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ jobs:
matrix:
# Add macos-latest and/or windows-latest if relevant for this package.
os: [ubuntu-latest]
sdk: [3.1.0, dev]
sdk: [3.2.0, dev]
steps:
- uses: actions/checkout@8ade135a41bc03ea155e62e844d188df1ea18608
- uses: dart-lang/setup-dart@8a4b97ea2017cc079571daec46542f76189836b1
Expand Down
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
## 4.1.3-dev

- Update the minimum Dart SDK version to `3.1.0`.
- Update the minimum Dart SDK version to `3.2.0`.

## 4.1.2

Expand Down
38 changes: 13 additions & 25 deletions lib/client/sse_client.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@

import 'dart:async';
import 'dart:convert';
import 'dart:html';
import 'dart:js_interop';

import 'package:js/js.dart';
import 'package:logging/logging.dart';
import 'package:pool/pool.dart';
import 'package:stream_channel/stream_channel.dart';
import 'package:web/helpers.dart';

import '../src/util/uuid.dart';

Expand Down Expand Up @@ -52,14 +52,15 @@ class SseClient extends StreamChannelMixin<String?> {
? generateUuidV4()
: '$debugKey-${generateUuidV4()}' {
_serverUrl = '$serverUrl?sseClientId=$_clientId';
_eventSource = EventSource(_serverUrl, withCredentials: true);
_eventSource =
EventSource(_serverUrl, EventSourceInit(withCredentials: true));
_eventSource.onOpen.first.whenComplete(() {
_onConnected.complete();
_outgoingController.stream
.listen(_onOutgoingMessage, onDone: _onOutgoingDone);
});
_eventSource.addEventListener('message', _onIncomingMessage);
_eventSource.addEventListener('control', _onIncomingControlMessage);
_eventSource.addEventListener('message', _onIncomingMessage.toJS);
_eventSource.addEventListener('control', _onIncomingControlMessage.toJS);

_eventSource.onOpen.listen((_) {
_errorTimer?.cancel();
Expand Down Expand Up @@ -114,7 +115,7 @@ class SseClient extends StreamChannelMixin<String?> {

void _onIncomingControlMessage(Event message) {
var data = (message as MessageEvent).data;
if (data == 'close') {
if (data.dartify() == 'close') {
close();
} else {
throw UnsupportedError('[$_clientId] Illegal Control Message "$data"');
Expand Down Expand Up @@ -147,8 +148,10 @@ class SseClient extends StreamChannelMixin<String?> {
final url = '$_serverUrl&messageId=${++_lastMessageId}';
await _fetch(
url,
_FetchOptions(
method: 'POST', body: encodedMessage, credentials: 'include'));
RequestInit(
method: 'POST',
body: encodedMessage.jsify(),
credentials: 'include'));
} catch (error) {
final augmentedError =
'[$_clientId] SSE client failed to send $message:\n $error';
Expand All @@ -159,20 +162,5 @@ class SseClient extends StreamChannelMixin<String?> {
}
}

// Custom implementation of Fetch API until Dart supports GET vs. POST,
// credentials, etc. See https://github.com/dart-lang/http/issues/595.
@JS('fetch')
external Object _nativeJsFetch(String resourceUrl, _FetchOptions options);

Future<dynamic> _fetch(String resourceUrl, _FetchOptions options) =>
promiseToFuture(_nativeJsFetch(resourceUrl, options));

@JS()
@anonymous
class _FetchOptions {
external factory _FetchOptions({
required String method, // e.g., 'GET', 'POST'
required String credentials, // e.g., 'omit', 'same-origin', 'include'
required String? body,
});
}
Future<dynamic> _fetch(String resourceUrl, RequestInit options) =>
window.fetch(resourceUrl.toJS, options).toDart;
2 changes: 1 addition & 1 deletion lib/src/server/sse_handler.dart
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ class SseConnection extends StreamChannelMixin<String?> {
// period.
// If the connection comes back, this will be cancelled and all messages
// left in the queue tried again.
_keepAliveTimer = Timer(_keepAlive!, _close);
_keepAliveTimer = Timer(_keepAlive, _close);
}
}

Expand Down
3 changes: 2 additions & 1 deletion pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ description: >-
repository: https://github.com/dart-lang/sse

environment:
sdk: ^3.1.0
sdk: ^3.2.0-beta

dependencies:
async: ^2.0.8
Expand All @@ -17,6 +17,7 @@ dependencies:
pool: ^1.5.0
shelf: ^1.1.0
stream_channel: ^2.0.0
web: ^0.3.0

dev_dependencies:
dart_flutter_team_lints: ^2.0.0
Expand Down

0 comments on commit 7f44aba

Please sign in to comment.