Skip to content

Commit

Permalink
Resolve #33 Null safety
Browse files Browse the repository at this point in the history
  • Loading branch information
jumperchen committed Apr 8, 2021
1 parent 898e6be commit 0af9d5f
Show file tree
Hide file tree
Showing 17 changed files with 219 additions and 233 deletions.
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
## 1.0.0

**New Feature:**

* [#33](https://github.com/rikulo/socket.io-dart/issues/33) Null safety


## 0.9.4

**Bug Fix:**
Expand Down
57 changes: 26 additions & 31 deletions lib/src/adapter/adapter.dart
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,14 @@ import 'package:socket_io/src/util/event_emitter.dart';

abstract class Adapter {
Map nsps = {};
Map<String, _Room> rooms;
Map<String, Map> sids;
Map<String, _Room> rooms = {};
Map<String, Map> sids = {};

void add(String id, String room, [dynamic Function([dynamic]) fn]);
void del(String id, String room, [dynamic Function([dynamic]) fn]);
void delAll(String id, [dynamic Function([dynamic]) fn]);
void broadcast(Map packet, [Map opts]);
void clients(List rooms, [dynamic Function([dynamic]) fn]);
void clients([List<String> rooms, dynamic Function([dynamic]) fn]);
void clientRooms(String id, [dynamic Function(dynamic, [dynamic]) fn]);

static Adapter newInstance(String key, Namespace nsp) {
Expand All @@ -37,15 +37,15 @@ class _MemoryStoreAdapter extends EventEmitter implements Adapter {
@override
Map nsps = {};
@override
Map<String, _Room> rooms;
Map<String, _Room> rooms = {};

@override
Map<String, Map> sids;
Encoder encoder;
Namespace nsp;
_MemoryStoreAdapter(nsp) {
Map<String, Map> sids = {};
late Encoder encoder;
late Namespace nsp;

_MemoryStoreAdapter(Namespace nsp) {
this.nsp = nsp;
rooms = {};
sids = {};
encoder = nsp.server.encoder;
}

Expand All @@ -57,11 +57,11 @@ class _MemoryStoreAdapter extends EventEmitter implements Adapter {
/// @api public
@override
void add(String id, String room, [dynamic Function([dynamic]) fn]) {
void add(String id, String room, [dynamic Function([dynamic])? fn]) {
sids[id] = sids[id] ?? {};
sids[id][room] = true;
sids[id]![room] = true;
rooms[room] = rooms[room] ?? _Room();
rooms[room].add(id);
rooms[room]!.add(id);
if (fn != null) scheduleMicrotask(() => fn(null));
}

Expand All @@ -72,12 +72,12 @@ class _MemoryStoreAdapter extends EventEmitter implements Adapter {
/// @param {Function} callback
/// @api public
@override
void del(String id, String room, [dynamic Function([dynamic]) fn]) {
void del(String id, String room, [dynamic Function([dynamic])? fn]) {
sids[id] = sids[id] ?? {};
sids[id].remove(room);
sids[id]!.remove(room);
if (rooms.containsKey(room)) {
rooms[room].del(id);
if (rooms[room].length == 0) rooms.remove(room);
rooms[room]!.del(id);
if (rooms[room]!.length == 0) rooms.remove(room);
}

if (fn != null) scheduleMicrotask(() => fn(null));
Expand All @@ -89,13 +89,13 @@ class _MemoryStoreAdapter extends EventEmitter implements Adapter {
/// @param {Function} callback
/// @api public
@override
void delAll(String id, [dynamic Function([dynamic]) fn]) {
void delAll(String id, [dynamic Function([dynamic])? fn]) {
var rooms = sids[id];
if (rooms != null) {
for (var room in rooms.keys) {
if (this.rooms.containsKey(room)) {
this.rooms[room].del(id);
if (this.rooms[room].length == 0) this.rooms.remove(room);
this.rooms[room]!.del(id);
if (this.rooms[room]!.length == 0) this.rooms.remove(room);
}
}
}
Expand All @@ -114,7 +114,7 @@ class _MemoryStoreAdapter extends EventEmitter implements Adapter {
/// @param {Object} packet object
/// @api public
@override
void broadcast(Map packet, [Map opts]) {
void broadcast(Map packet, [Map? opts]) {
opts = opts ?? {};
List rooms = opts['rooms'] ?? [];
List except = opts['except'] ?? [];
Expand Down Expand Up @@ -161,9 +161,8 @@ class _MemoryStoreAdapter extends EventEmitter implements Adapter {
/// @param {Function} callback
/// @api public
@override
void clients(List rooms, [dynamic Function([dynamic]) fn]) {
rooms = rooms ?? [];

void clients(
[List<String> rooms = const [], dynamic Function([dynamic])? fn]) {
var ids = {};
var sids = [];
var socket;
Expand Down Expand Up @@ -200,7 +199,7 @@ class _MemoryStoreAdapter extends EventEmitter implements Adapter {
/// @param {Function} callback
/// @api public
@override
void clientRooms(String id, [dynamic Function(dynamic, [dynamic]) fn]) {
void clientRooms(String id, [dynamic Function(dynamic, [dynamic])? fn]) {
var rooms = sids[id];
if (fn != null) scheduleMicrotask(() => fn(null, rooms?.keys));
}
Expand All @@ -210,12 +209,8 @@ class _MemoryStoreAdapter extends EventEmitter implements Adapter {
///
/// @api private
class _Room {
Map<String, bool> sockets;
int length;
_Room() {
sockets = {};
length = 0;
}
Map<String, bool> sockets = {};
int length = 0;

/// Adds a socket to a room.
///
Expand Down
19 changes: 9 additions & 10 deletions lib/src/client.dart
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ class Client {
Socket conn;
dynamic id;
dynamic request;
Encoder encoder;
Decoder decoder;
Encoder encoder = Encoder();
Decoder decoder = Decoder();
List sockets = [];
Map nsps = {};
List<String> connectBuffer = [];
Expand All @@ -31,11 +31,9 @@ class Client {
/// @param {Server} server instance
/// @param {Socket} connection
/// @api private
Client(this.server, this.conn) {
encoder = Encoder();
decoder = Decoder();
id = conn.id;
request = conn.connect.request;
Client(this.server, this.conn)
: id = conn.id,
request = conn.connect.request {
setup();
}

Expand Down Expand Up @@ -125,12 +123,13 @@ class Client {
/// @param {Object} packet object
/// @param {Object} options
/// @api private
void packet(packet, [Map opts]) {
void packet(packet, [Map? opts]) {
var self = this;
opts = opts ?? {};
opts ??= {};
// this writes to the actual connection
void writeToEngine(encodedPackets) {
if (opts['volatile'] != null && !self.conn.transport.writable) return;
if (opts!['volatile'] != null && self.conn.transport.writable != true)
return;
for (var i = 0; i < encodedPackets.length; i++) {
self.conn.write(encodedPackets[i], {'compress': opts['compress']});
}
Expand Down
16 changes: 8 additions & 8 deletions lib/src/engine/connect.dart
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@ import 'dart:async';
import 'dart:io';

class SocketConnect extends HttpConnectWrapper {
WebSocket _socket;
Completer _done;
bool _completed;
WebSocket? _socket;
Completer? _done;
bool? _completed;
SocketConnect(HttpConnect origin) : super(origin);

SocketConnect.fromWebSocket(HttpConnect origin, WebSocket socket)
Expand All @@ -25,26 +25,26 @@ class SocketConnect extends HttpConnectWrapper {

bool isUpgradeRequest() => _socket != null;

WebSocket get websocket => _socket;
WebSocket? get websocket => _socket;

Future get done {
if (_completed == true) {
return Future.value('done');
}
if (_socket != null) {
return _socket.done;
return _socket!.done;
} else {
_done = Completer();
return _done.future;
return _done!.future;
}
}

/// Closes the current connection.
void close() {
if (_done != null) {
_done.complete('done');
_done!.complete('done');
} else if (_socket != null) {
_socket.close();
_socket!.close();
} else {
_completed = true;
}
Expand Down
2 changes: 1 addition & 1 deletion lib/src/engine/engine.dart
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import 'package:socket_io/src/engine/server.dart';
import 'package:socket_io/src/util/event_emitter.dart';

class Engine extends EventEmitter {
static Engine attach(server, [Map options]) {
static Engine attach(server, [Map? options]) {
var engine = Server(options);
engine.attachTo(server, options);
return engine;
Expand Down
Loading

0 comments on commit 0af9d5f

Please sign in to comment.