fix(creditcall): restore progress updates across transactions

This commit is contained in:
2026-11-16 14:27:45 +00:00
parent 3d4a765d5b
commit c8924bcbb9
4 changed files with 356 additions and 296 deletions
+20 -80
View File
@@ -12,37 +12,28 @@ namespace Creditcall.ChipDna.Client
internal static class TransactionExecution
{
internal static Dictionary<string, string> Run(Action start,
Func<Task<Dictionary<string, string>>> completion, Action suppress, TimeSpan timeout)
Func<Task<Dictionary<string, string>>> completion, TimeSpan timeout)
{
try
start();
var task = completion();
// Task.Wait intentionally retains its existing exception behavior.
if (!task.Wait(timeout))
{
start();
var task = completion();
// Task.Wait intentionally retains its existing exception behavior.
if (!task.Wait(timeout))
return new Dictionary<string, string>
{
suppress();
return new Dictionary<string, string>
{
{ ParameterKeys.TransactionResult, "TIMEOUT" },
{ ParameterKeys.Errors, "Transaction timed out waiting for device response" }
};
}
try { return task.Result; }
catch (AggregateException agg)
{
var first = agg.Flatten().InnerExceptions[0];
return new Dictionary<string, string>
{
{ ParameterKeys.TransactionResult, "ERROR" },
{ ParameterKeys.Errors, $"Transaction task faulted: {first.Message}" }
};
}
{ ParameterKeys.TransactionResult, "TIMEOUT" },
{ ParameterKeys.Errors, "Transaction timed out waiting for device response" }
};
}
catch
try { return task.Result; }
catch (AggregateException agg)
{
suppress();
throw;
var first = agg.Flatten().InnerExceptions[0];
return new Dictionary<string, string>
{
{ ParameterKeys.TransactionResult, "ERROR" },
{ ParameterKeys.Errors, $"Transaction task faulted: {first.Message}" }
};
}
}
}
@@ -50,10 +41,7 @@ namespace Creditcall.ChipDna.Client
internal sealed class TransactionProgressTap
{
private readonly object gate = new object();
private readonly HashSet<string> references = new HashSet<string>(StringComparer.Ordinal);
private Action<string, string> listener;
private string reference;
private bool suppressed;
internal Dictionary<string, string> Observe(Action<string, string> callback, Func<Dictionary<string, string>> execute)
{
@@ -61,57 +49,11 @@ namespace Creditcall.ChipDna.Client
try { return execute(); }
finally { lock (gate) { listener -= callback; } }
}
internal void Begin(string value)
{
lock (gate)
{
if (suppressed) return;
// The existing reference generation can repeat within one second.
// A repeated reference cannot prove callback ownership.
if (reference != null || string.IsNullOrEmpty(value) || !references.Add(value))
suppressed = true;
reference = value;
}
}
internal void End() { lock (gate) { reference = null; } }
internal void Suppress() { lock (gate) { suppressed = true; reference = null; } }
internal void SuppressIfActive()
{
lock (gate)
{
if (reference == null) return;
suppressed = true;
reference = null;
}
}
internal void StartFailed(string errors)
{
lock (gate)
{
if (suppressed) return;
// SDK 3.17 StartCommand returns this exact error before SendRequest.
// No other error category proves that dispatch never happened.
if (errors == ClientErrorCodes.ClientNotConnectedToServer.ToString())
{
if (reference != null) references.Remove(reference);
reference = null;
return;
}
suppressed = true;
reference = null;
}
}
internal void Report(string source, EventParameters parameters)
{
lock (gate)
{
if (suppressed || reference == null || parameters == null) return;
string eventReference;
parameters.GetValue(ParameterKeys.Reference, out eventReference);
bool hasReference = !string.IsNullOrEmpty(eventReference);
if (hasReference && eventReference != reference) return;
if (listener == null || parameters == null) return;
string value;
switch (source)
@@ -128,10 +70,8 @@ namespace Creditcall.ChipDna.Client
}
if (!TransactionStreamFields.IsProgress(source, value)) return;
// Normal SDK 3.17 progress has no reference. The serial active window
// provides best-effort UI ownership, not a financial decision: a queued
// callback, including removal, may briefly show stale progress in a later window.
// The listener only attempts a bounded, nonblocking enqueue.
// Progress is display-only, including late SDK callbacks. The active
// request's listener only attempts a bounded enqueue, never socket I/O.
listener?.Invoke(source, value);
}
}