deliverSignal method

Future<void> deliverSignal({
  1. required String workflowExecutionId,
  2. required String signalName,
  3. Object? payload,
})

Delivers a signal to a waiting workflow.

If a completer is registered (workflow is actively waiting), the signal is delivered immediately. Otherwise the signal remains PENDING in the store for later consumption by waitForSignal.

Implementation

Future<void> deliverSignal({
  required String workflowExecutionId,
  required String signalName,
  Object? payload,
}) async {
  final key = _compositeKey(workflowExecutionId, signalName);

  // Check if there's an active waiter
  final completer = _completers.remove(key);
  if (completer != null && !completer.isCompleted) {
    // Find the pending signal in the store and mark as DELIVERED
    final signals = await _store.loadPendingSignals(
      workflowExecutionId,
      signalName: signalName,
    );
    for (final s in signals) {
      await _store.saveSignal(
        s.copyWith(
          status: SignalStatus.delivered,
          payload: payload?.toString(),
        ),
      );
    }

    // Cancel timeout timer
    _timeoutTimers[key]?.cancel();
    _timeoutTimers.remove(key);

    // Resume execution
    final execution = await _store.loadExecution(workflowExecutionId);
    if (execution != null && execution.status is Suspended) {
      await _store.saveExecution(
        execution.copyWith(
          status: const Running(),
          updatedAt: utcNow(),
        ),
      );
    }

    completer.complete(payload?.toString());
  }
  // If no active waiter, the signal was already saved by DurableEngineImpl.sendSignal()
  // and will be picked up when waitForSignal is called.
}