-
Notifications
You must be signed in to change notification settings - Fork 45
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
thinkdigitalsoftware-namespace-fix #15
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -19,89 +19,78 @@ import 'package:socket_io/src/server.dart'; | |
import 'package:socket_io/src/socket.dart'; | ||
import 'package:socket_io/src/util/event_emitter.dart'; | ||
|
||
/** | ||
* Blacklisted events. | ||
*/ | ||
/// Blacklisted events. | ||
|
||
List<String> events = [ | ||
'connect', // for symmetry with client | ||
'connection', 'newListener' | ||
]; | ||
|
||
/** | ||
* Flags. | ||
*/ | ||
/// Flags. | ||
List<String> flags = ['json', 'volatile']; | ||
|
||
class Namespace extends EventEmitter { | ||
String name; | ||
Server server; | ||
List<Socket> sockets = []; | ||
Map<String, Socket> connected = {}; | ||
List fns = []; | ||
List<Function> fns = []; | ||
int ids = 0; | ||
List rooms = []; | ||
Map flags = {}; | ||
Adapter adapter; | ||
Logger _logger = new Logger('socket_io:Namespace'); | ||
|
||
/** | ||
* Namespace constructor. | ||
* | ||
* @param {Server} server instance | ||
* @param {Socket} name | ||
* @api private | ||
*/ | ||
Namespace(Server this.server, String this.name) { | ||
this.initAdapter(); | ||
final Logger _logger = Logger('socket_io:Namespace'); | ||
|
||
/// Namespace constructor. | ||
/// | ||
/// @param {Server} server instance | ||
/// @param {Socket} name | ||
/// @api private | ||
Namespace(this.server, this.name) { | ||
initAdapter(); | ||
} | ||
|
||
/** | ||
* Initializes the `Adapter` for this nsp. | ||
* Run upon changing adapter by `Server#adapter` | ||
* in addition to the constructor. | ||
* | ||
* @api private | ||
*/ | ||
initAdapter() { | ||
this.adapter = Adapter.newInstance(this.server.adapter, this); | ||
/// Initializes the `Adapter` for this nsp. | ||
/// Run upon changing adapter by `Server#adapter` | ||
/// in addition to the constructor. | ||
/// | ||
/// @api private | ||
void initAdapter() { | ||
adapter = Adapter.newInstance(server.adapter, this); | ||
} | ||
|
||
/** | ||
* Sets up namespace middleware. | ||
* | ||
* @return {Namespace} self | ||
* @api public | ||
*/ | ||
use(fn) { | ||
this.fns.add(fn); | ||
/// Sets up namespace middleware. | ||
/// | ||
/// @return {Namespace} self | ||
/// @api public | ||
Namespace use(fn) { | ||
fns.add(fn); | ||
return this; | ||
} | ||
|
||
/** | ||
* Executes the middleware for an incoming client. | ||
* | ||
* @param {Socket} socket that will get added | ||
* @param {Function} last fn call in the middleware | ||
* @api private | ||
*/ | ||
run(socket, fn) { | ||
/// Executes the middleware for an incoming client. | ||
/// | ||
/// @param {Socket} socket that will get added | ||
/// @param {Function} last fn call in the middleware | ||
/// @api private | ||
void run(socket, Function fn) { | ||
var fns = this.fns.sublist(0); | ||
if (fns.isEmpty) return fn(null); | ||
|
||
run0(0, fns, socket, fn); | ||
} | ||
|
||
static run0(idx, fns, socket, fn) { | ||
return fns[idx](socket, (err) { | ||
//TODO: Figure out return type for this method | ||
static run0(int index, List<Function> fns, Socket socket, Function fn) { | ||
return fns[index](socket, (err) { | ||
// upon error, short-circuit | ||
if (err) return fn(err); | ||
|
||
// if no middleware left, summon callback | ||
if (!fns[idx + 1]) return fn(null); | ||
if (fns.length < index + 1) return fn(null); | ||
|
||
// go on to next | ||
run0(idx + 1, fns, socket, fn); | ||
run0(index + 1, fns, socket, fn); | ||
}); | ||
} | ||
|
||
|
@@ -116,30 +105,26 @@ class Namespace extends EventEmitter { | |
// to(name); | ||
// } | ||
|
||
/** | ||
* Targets a room when emitting. | ||
* | ||
* @param {String} name | ||
* @return {Namespace} self | ||
* @api public | ||
*/ | ||
to(String name) { | ||
rooms = this.rooms?.isNotEmpty == true ? this.rooms : []; | ||
if (!rooms.contains(name)) this.rooms.add(name); | ||
/// Targets a room when emitting. | ||
/// | ||
/// @param {String} name | ||
/// @return {Namespace} self | ||
/// @api public | ||
Namespace to(String name) { | ||
rooms = rooms?.isNotEmpty == true ? rooms : []; | ||
if (!rooms.contains(name)) rooms.add(name); | ||
return this; | ||
} | ||
|
||
/** | ||
* Adds a new client. | ||
* | ||
* @return {Socket} | ||
* @api private | ||
*/ | ||
add(Client client, query, fn) { | ||
_logger.fine('adding socket to nsp ${this.name}'); | ||
var socket = new Socket(this, client, query); | ||
/// Adds a client. | ||
/// | ||
/// @return {Socket} | ||
/// @api private | ||
Socket add(Client client, query, Function fn) { | ||
_logger.fine('adding socket to nsp ${name}'); | ||
var socket = Socket(this, client, query); | ||
var self = this; | ||
this.run(socket, (err) { | ||
run(socket, (err) { | ||
// don't use Timer.run() here | ||
scheduleMicrotask(() { | ||
if ('open' == client.conn.readyState) { | ||
|
@@ -166,83 +151,72 @@ class Namespace extends EventEmitter { | |
return socket; | ||
} | ||
|
||
/** | ||
* Removes a client. Called by each `Socket`. | ||
* | ||
* @api private | ||
*/ | ||
remove(socket) { | ||
if (this.sockets.contains(socket)) { | ||
this.sockets.remove(socket); | ||
/// Removes a client. Called by each `Socket`. | ||
/// | ||
/// @api private | ||
void remove(socket) { | ||
if (sockets.contains(socket)) { | ||
sockets.remove(socket); | ||
} else { | ||
_logger.fine('ignoring remove for ${socket.id}'); | ||
} | ||
} | ||
|
||
/** | ||
* Emits to all clients. | ||
* | ||
* @return {Namespace} self | ||
* @api public | ||
*/ | ||
emit(ev, [dynamic arg]) { | ||
if (events.contains(ev)) { | ||
super.emit(ev, arg); | ||
/// Emits to all clients. | ||
/// | ||
/// @api public | ||
@override | ||
void emit(String event, [dynamic argument]) { | ||
if (events.contains(event)) { | ||
super.emit(event, argument); | ||
} else { | ||
// @todo check how to handle it with Dart | ||
// if (hasBin(args)) { parserType = ParserType.binaryEvent; } // binary | ||
|
||
List data = arg == null ? [ev] : [ev, arg]; | ||
// ignore: omit_local_variable_types | ||
List data = argument == null ? [event] : [event, argument]; | ||
|
||
Map packet = {'type': EVENT, 'data': data}; | ||
final packet = {'type': EVENT, 'data': data}; | ||
|
||
this | ||
.adapter | ||
.broadcast(packet, {'rooms': this.rooms, 'flags': this.flags}); | ||
adapter.broadcast(packet, {'rooms': rooms, 'flags': flags}); | ||
|
||
this.rooms = null; | ||
this.flags = null; | ||
rooms = null; | ||
flags = null; | ||
} | ||
} | ||
|
||
/** | ||
* Sends a `message` event to all clients. | ||
* | ||
* @return {Namespace} self | ||
* @api public | ||
*/ | ||
send([args]) { | ||
/// Sends a `message` event to all clients. | ||
/// | ||
/// @return {Namespace} self | ||
/// @api public | ||
void send([args]) { | ||
write(args); | ||
} | ||
|
||
write([args]) { | ||
this.emit('message', args); | ||
return this; | ||
void write([args]) { | ||
emit('message', args); | ||
} | ||
|
||
/** | ||
* Gets a list of clients. | ||
* | ||
* @return {Namespace} self | ||
* @api public | ||
*/ | ||
clients(fn([_])) { | ||
this.adapter.clients(this.rooms, fn); | ||
this.rooms = []; | ||
/// Gets a list of clients. | ||
/// | ||
/// @return {Namespace} self | ||
/// @api public | ||
/// | ||
/// TODO: Fix this description or code. Add type parameters to [fn([_])] | ||
/// | ||
Namespace clients(fn([_])) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @jumperchen Referenced in issue #14. Please follow up on that before merging this PR There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ok, I will review it later. |
||
adapter.clients(rooms, fn); | ||
rooms = []; | ||
return this; | ||
} | ||
|
||
/** | ||
* Sets the compress flag. | ||
* | ||
* @param {Boolean} if `true`, compresses the sending data | ||
* @return {Socket} self | ||
* @api public | ||
*/ | ||
compress(compress) { | ||
this.flags = this.flags.isEmpty ? this.flags : {}; | ||
this.flags['compress'] = compress; | ||
return this; | ||
/// Sets the compress flag. | ||
/// | ||
/// @param {Boolean} if `true`, compresses the sending data | ||
/// @api public | ||
void compress(compress) { | ||
flags = flags.isEmpty ? flags : {}; | ||
flags['compress'] = compress; | ||
} | ||
} | ||
|
||
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please verify that this still doesn't what's intended