feat(core): Replace custom class with a record type
This commit is contained in:
parent
59b90307c2
commit
edf1d0b257
@ -1,34 +1,9 @@
|
|||||||
import 'dart:async';
|
import 'dart:async';
|
||||||
import 'package:meta/meta.dart';
|
|
||||||
import 'package:moxxmpp/src/stringxml.dart';
|
import 'package:moxxmpp/src/stringxml.dart';
|
||||||
import 'package:synchronized/synchronized.dart';
|
import 'package:synchronized/synchronized.dart';
|
||||||
|
|
||||||
/// A surrogate key for awaiting stanzas.
|
/// (JID we sent a stanza to, the id of the sent stanza, the tag of the sent stanza).
|
||||||
@immutable
|
typedef _StanzaCompositeKey = (String?, String, String);
|
||||||
class _StanzaSurrogateKey {
|
|
||||||
const _StanzaSurrogateKey(this.sentTo, this.id, this.tag);
|
|
||||||
|
|
||||||
/// The JID the original stanza was sent to. We expect the result to come from the
|
|
||||||
/// same JID.
|
|
||||||
final String? sentTo;
|
|
||||||
|
|
||||||
/// The ID of the original stanza. We expect the result to have the same ID.
|
|
||||||
final String id;
|
|
||||||
|
|
||||||
/// The tag name of the stanza.
|
|
||||||
final String tag;
|
|
||||||
|
|
||||||
@override
|
|
||||||
int get hashCode => sentTo.hashCode ^ id.hashCode ^ tag.hashCode;
|
|
||||||
|
|
||||||
@override
|
|
||||||
bool operator ==(Object other) {
|
|
||||||
return other is _StanzaSurrogateKey &&
|
|
||||||
other.sentTo == sentTo &&
|
|
||||||
other.id == id &&
|
|
||||||
other.tag == tag;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/// This class handles the await semantics for stanzas. Stanzas are given a "unique"
|
/// This class handles the await semantics for stanzas. Stanzas are given a "unique"
|
||||||
/// key equal to the tuple (to, id, tag) with which their response is identified.
|
/// key equal to the tuple (to, id, tag) with which their response is identified.
|
||||||
@ -40,7 +15,7 @@ class _StanzaSurrogateKey {
|
|||||||
/// This class also handles some "edge cases" of RFC 6120, like an empty "from" attribute.
|
/// This class also handles some "edge cases" of RFC 6120, like an empty "from" attribute.
|
||||||
class StanzaAwaiter {
|
class StanzaAwaiter {
|
||||||
/// The pending stanzas, identified by their surrogate key.
|
/// The pending stanzas, identified by their surrogate key.
|
||||||
final Map<_StanzaSurrogateKey, Completer<XMLNode>> _pending = {};
|
final Map<_StanzaCompositeKey, Completer<XMLNode>> _pending = {};
|
||||||
|
|
||||||
/// The critical section for accessing [StanzaAwaiter._pending].
|
/// The critical section for accessing [StanzaAwaiter._pending].
|
||||||
final Lock _lock = Lock();
|
final Lock _lock = Lock();
|
||||||
@ -54,7 +29,7 @@ class StanzaAwaiter {
|
|||||||
Future<Future<XMLNode>> addPending(String? to, String id, String tag) async {
|
Future<Future<XMLNode>> addPending(String? to, String id, String tag) async {
|
||||||
final completer = await _lock.synchronized(() {
|
final completer = await _lock.synchronized(() {
|
||||||
final completer = Completer<XMLNode>();
|
final completer = Completer<XMLNode>();
|
||||||
_pending[_StanzaSurrogateKey(to, id, tag)] = completer;
|
_pending[(to, id, tag)] = completer;
|
||||||
return completer;
|
return completer;
|
||||||
});
|
});
|
||||||
|
|
||||||
@ -68,7 +43,7 @@ class StanzaAwaiter {
|
|||||||
final id = stanza.attributes['id'] as String?;
|
final id = stanza.attributes['id'] as String?;
|
||||||
if (id == null) return false;
|
if (id == null) return false;
|
||||||
|
|
||||||
final key = _StanzaSurrogateKey(
|
final key = (
|
||||||
stanza.attributes['from'] as String?,
|
stanza.attributes['from'] as String?,
|
||||||
id,
|
id,
|
||||||
stanza.tag,
|
stanza.tag,
|
||||||
@ -92,7 +67,7 @@ class StanzaAwaiter {
|
|||||||
final id = stanza.attributes['id'] as String?;
|
final id = stanza.attributes['id'] as String?;
|
||||||
if (id == null) return false;
|
if (id == null) return false;
|
||||||
|
|
||||||
final key = _StanzaSurrogateKey(
|
final key = (
|
||||||
stanza.attributes['from'] as String?,
|
stanza.attributes['from'] as String?,
|
||||||
id,
|
id,
|
||||||
stanza.tag,
|
stanza.tag,
|
||||||
|
@ -5,6 +5,8 @@ import 'package:moxxmpp/src/awaiter.dart';
|
|||||||
import 'package:moxxmpp/src/parser.dart';
|
import 'package:moxxmpp/src/parser.dart';
|
||||||
import 'package:synchronized/synchronized.dart';
|
import 'package:synchronized/synchronized.dart';
|
||||||
|
|
||||||
|
/// A queue for incoming [XMPPStreamObject]s to ensure "in order"
|
||||||
|
/// processing (except for stanzas that are awaited).
|
||||||
class IncomingStanzaQueue {
|
class IncomingStanzaQueue {
|
||||||
IncomingStanzaQueue(this._callback, this._stanzaAwaiter);
|
IncomingStanzaQueue(this._callback, this._stanzaAwaiter);
|
||||||
|
|
||||||
@ -90,6 +92,7 @@ class IncomingStanzaQueue {
|
|||||||
}
|
}
|
||||||
|
|
||||||
object as XMPPStreamElement;
|
object as XMPPStreamElement;
|
||||||
|
// TODO: Check the from attribute to ensure that it is matched correctly.
|
||||||
return _stanzaAwaiter.isAwaited(object.node);
|
return _stanzaAwaiter.isAwaited(object.node);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user