send method

Future<WireSendResults> send(
  1. String signal, [
  2. dynamic payload,
  3. dynamic scope
])

Implementation

Future<WireSendResults> send(String signal, [payload, scope]) async {
  bool noMoreSubscribers = true;
  // print('> Wire -> WireCommunicateLayer: send - hasSignal($signal) = ${hasSignal(signal)}');
  final results = [];
  if (hasSignal(signal)) {
    final hasWires = _wireIdsBySignal.containsKey(signal);
    // print('> Wire -> WireCommunicateLayer: send - hasWires = ${hasWires}');
    if (hasWires) {
      final wiresToRemove = <Wire<dynamic>>[];
      final isLookingInScope = scope != null;
      await Future.forEach<int>(_wireIdsBySignal[signal]!, (wireId) async {
        final wire = _wireById[wireId]!;
        if (isLookingInScope && wire.scope != scope) return;
        noMoreSubscribers = wire.withReplies && --wire.replies == 0;
        // print('> \t\t wireId = ${wireId} | noMoreSubscribers = ${noMoreSubscribers}');
        if (noMoreSubscribers) wiresToRemove.add(wire);
        final resultData = await wire.transfer(payload);
        if (resultData != null) results.add(resultData);
      });
      if (wiresToRemove.isNotEmpty) {
        await Future.forEach(wiresToRemove, (Wire<dynamic> wire) async {
          noMoreSubscribers = await _removeWire(wire);
        });
      }
    }
  }
  return WireSendResults(results, noMoreSubscribers);
}