Skip to content
This repository was archived by the owner on Feb 24, 2025. It is now read-only.

Commit 9e80b8d

Browse files
authoredNov 22, 2023
migrate to pkg web (#294)
1 parent 5241175 commit 9e80b8d

File tree

5 files changed

+54
-15
lines changed

5 files changed

+54
-15
lines changed
 

‎CHANGELOG.md

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
## 2.4.1-wip
22

3-
- Bump minimum Dart version to 3.2.0
43
- Update the examples to use `WebSocketChannel.ready` and clarify that
54
`WebSocketChannel.ready` should be awaited before sending data over the
65
`WebSocketChannel`.
76
- Mention `ready` in the docs for `connect`.
7+
- Bump minimum Dart version to 3.2.0
8+
- Move to `pkg:web` to support WebAssembly compilation.
89

910
## 2.4.0
1011

‎lib/html.dart

+29-11
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,16 @@
33
// BSD-style license that can be found in the LICENSE file.
44

55
import 'dart:async';
6-
import 'dart:html';
6+
import 'dart:js_interop';
77
import 'dart:typed_data';
88

99
import 'package:async/async.dart';
1010
import 'package:stream_channel/stream_channel.dart';
11+
import 'package:web/helpers.dart';
1112

1213
import 'src/channel.dart';
1314
import 'src/exception.dart';
15+
import 'src/web_helpers.dart';
1416

1517
/// A [WebSocketChannel] that communicates using a `dart:html` [WebSocket].
1618
class HtmlWebSocketChannel extends StreamChannelMixin
@@ -73,8 +75,15 @@ class HtmlWebSocketChannel extends StreamChannelMixin
7375
/// [BinaryType.blob], they're delivered as [Blob]s instead.
7476
HtmlWebSocketChannel.connect(Object url,
7577
{Iterable<String>? protocols, BinaryType? binaryType})
76-
: this(WebSocket(url.toString(), protocols)
77-
..binaryType = (binaryType ?? BinaryType.list).value);
78+
: this(
79+
WebSocket(
80+
url.toString(),
81+
(protocols?.toList() ?? const <String>[])
82+
.map((e) => e.toJS)
83+
.toList()
84+
.toJS,
85+
)..binaryType = (binaryType ?? BinaryType.list).value,
86+
);
7887

7988
/// Creates a channel wrapping [innerWebSocket].
8089
HtmlWebSocketChannel(this.innerWebSocket) {
@@ -109,11 +118,7 @@ class HtmlWebSocketChannel extends StreamChannelMixin
109118
_controller.local.sink.close();
110119
});
111120

112-
innerWebSocket.onMessage.listen((event) {
113-
var data = event.data;
114-
if (data is ByteBuffer) data = data.asUint8List();
115-
_controller.local.sink.add(data);
116-
});
121+
innerWebSocket.onMessage.listen(_innerListen);
117122

118123
// The socket API guarantees that only a single error event will be emitted,
119124
// and that once it is no other events will be emitted.
@@ -124,16 +129,29 @@ class HtmlWebSocketChannel extends StreamChannelMixin
124129
});
125130
}
126131

132+
void _innerListen(MessageEvent event) {
133+
final eventData = event.data;
134+
Object? data;
135+
if (eventData.typeofEquals('object') &&
136+
(eventData as JSObject).instanceOfString('ArrayBuffer')) {
137+
data = (eventData as JSArrayBuffer).toDart.asUint8List();
138+
} else {
139+
data = event.data;
140+
}
141+
_controller.local.sink.add(data);
142+
}
143+
127144
/// Pipes user events to [innerWebSocket].
128145
void _listen() {
129-
_controller.local.stream.listen(innerWebSocket.send, onDone: () {
146+
_controller.local.stream.listen((obj) => innerWebSocket.send(obj!.jsify()!),
147+
onDone: () {
130148
// On Chrome and possibly other browsers, `null` can't be passed as the
131149
// default here. The actual arity of the function call must be correct or
132150
// it will fail.
133151
if (_localCloseCode != null && _localCloseReason != null) {
134-
innerWebSocket.close(_localCloseCode, _localCloseReason);
152+
innerWebSocket.close(_localCloseCode!, _localCloseReason!);
135153
} else if (_localCloseCode != null) {
136-
innerWebSocket.close(_localCloseCode);
154+
innerWebSocket.close(_localCloseCode!);
137155
} else {
138156
innerWebSocket.close();
139157
}

‎lib/src/web_helpers.dart

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
// Copyright (c) 2023, the Dart project authors. Please see the AUTHORS file
2+
// for details. All rights reserved. Use of this source code is governed by a
3+
// BSD-style license that can be found in the LICENSE file.
4+
5+
import 'package:web/helpers.dart';
6+
7+
// TODO(kevmoo): remove when https://github.com/dart-lang/web/commit/4cb5811ed06
8+
// is in a published release and the min constraint on pkg:web is updated
9+
extension WebSocketEvents on WebSocket {
10+
Stream<Event> get onOpen => EventStreamProviders.openEvent.forTarget(this);
11+
Stream<MessageEvent> get onMessage =>
12+
EventStreamProviders.messageEvent.forTarget(this);
13+
Stream<CloseEvent> get onClose =>
14+
EventStreamProviders.closeEvent.forTarget(this);
15+
Stream<Event> get onError =>
16+
EventStreamProviders.errorEventSourceEvent.forTarget(this);
17+
}

‎pubspec.yaml

+1
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ dependencies:
1414
async: ^2.5.0
1515
crypto: ^3.0.0
1616
stream_channel: ^2.1.0
17+
web: ^0.4.0
1718

1819
dev_dependencies:
1920
dart_flutter_team_lints: ^2.0.0

‎test/html_test.dart

+5-3
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,14 @@
66
library;
77

88
import 'dart:async';
9-
import 'dart:html';
9+
import 'dart:js_interop';
1010
import 'dart:typed_data';
1111

1212
import 'package:async/async.dart';
1313
import 'package:test/test.dart';
14+
import 'package:web/helpers.dart' hide BinaryType;
1415
import 'package:web_socket_channel/html.dart';
16+
import 'package:web_socket_channel/src/web_helpers.dart';
1517
import 'package:web_socket_channel/web_socket_channel.dart';
1618

1719
void main() {
@@ -176,6 +178,6 @@ void main() {
176178
Future<List<int>> _decodeBlob(Blob blob) async {
177179
final reader = FileReader();
178180
reader.readAsArrayBuffer(blob);
179-
await reader.onLoad.first;
180-
return reader.result as Uint8List;
181+
await reader.onLoadEnd.first;
182+
return (reader.result as JSArrayBuffer).toDart.asUint8List();
181183
}

0 commit comments

Comments
 (0)