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

Require Dart 3.2, use pkg:web #92

Merged
merged 2 commits into from
Nov 15, 2023
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
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@b4ffde65f46336ab88eb53be808477a3936bae11
- uses: dart-lang/setup-dart@b64355ae6ca0b5d484f0106a033dd1388965d06d
Expand Down
4 changes: 2 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
## 4.1.3-dev
## 4.1.3

- 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?.toJS,
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<void> _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
5 changes: 3 additions & 2 deletions pubspec.yaml
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
name: sse
version: 4.1.3-dev
version: 4.1.3
description: >-
Provides client and server functionality for setting up bi-directional
communication through Server Sent Events (SSE) and corresponding POST
requests.
repository: https://github.com/dart-lang/sse

environment:
sdk: ^3.1.0
sdk: ^3.2.0

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 <0.5.0'

dev_dependencies:
dart_flutter_team_lints: ^2.0.0
Expand Down
3 changes: 1 addition & 2 deletions test/web/index.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,8 @@
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.

import 'dart:html';

import 'package:sse/client/sse_client.dart';
import 'package:web/helpers.dart';

void main() {
var channel = SseClient('/test');
Expand Down