Skip to content

Commit

Permalink
chore: Use SentObject instead of map.
Browse files Browse the repository at this point in the history
  • Loading branch information
cloudwebrtc committed Dec 10, 2021
1 parent bc0a21a commit 1875f59
Showing 1 changed file with 42 additions and 32 deletions.
74 changes: 42 additions & 32 deletions lib/src/peer.dart
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,22 @@ export 'transports/NativeTransport.dart'

final logger = new Logger('Peer');

class SentObject {
final int id;
final String method;
final Function(dynamic) resolve;
final Function(dynamic) reject;
final Timer timer;
final Function() close;
SentObject(
{required this.id,
required this.method,
required this.resolve,
required this.reject,
required this.timer,
required this.close});
}

class Peer extends EnhancedEventEmitter {
// Closed flag.
bool _closed = false;
Expand All @@ -20,7 +36,7 @@ class Peer extends EnhancedEventEmitter {
dynamic _data = {};

// Map of pending sent request objects indexed by request id.
var _sents = Map<String, dynamic>();
var _sents = Map<String, SentObject>();

// Transport.
late TransportInterface _transport;
Expand Down Expand Up @@ -72,33 +88,29 @@ class Peer extends EnhancedEventEmitter {
await this._transport.send(request);

int timeout = (1500 * (15 + (0.1 * this._sents.length))).toInt();
final sent = {
'id': request['id'],
'method': request['method'],
'resolve': (data2) {
final sent = _sents.remove(requestId);
if (sent == null) return;
sent['timer'].cancel();
completer.complete(data2);
},
'reject': (error) {
final sent = _sents.remove(requestId);
if (sent == null) return;
sent['timer'].cancel();
completer.completeError(error);
},
'timer': new Timer.periodic(new Duration(milliseconds: timeout),
(Timer timer) {
if (this._sents.remove(requestId) == null) return;

completer.completeError('request timeout');
}),
'close': () {
var handler = _sents[requestId];
handler['timer'].cancel();
completer.completeError('peer closed');
}
};
final sent = SentObject(
id: request['id'],
method: request['method'],
resolve: (data2) {
final sent = _sents.remove(requestId);
sent?.timer.cancel();
completer.complete(data2);
},
reject: (error) {
final sent = _sents.remove(requestId);
sent?.timer.cancel();
completer.completeError(error);
},
timer: new Timer.periodic(new Duration(milliseconds: timeout),
(Timer timer) {
if (this._sents.remove(requestId) == null) return;
completer.completeError('request timeout');
}),
close: () {
final sent = _sents[requestId];
sent?.timer.cancel();
completer.completeError('peer closed');
});

_sents[requestId] = sent;
return completer.future;
Expand Down Expand Up @@ -215,15 +227,13 @@ class Peer extends EnhancedEventEmitter {
}

if (response['ok'] != null && response['ok'] == true) {
var resolve = sent['resolve'];
resolve(response['data']);
sent.resolve(response['data']);
} else {
var error = {
'code': response['errorCode'] ?? 500,
'error': response['errorReason'] ?? ''
};
var reject = sent['reject'];
reject(error);
sent.reject(error);
}
}

Expand Down

0 comments on commit 1875f59

Please sign in to comment.