Path: redb/redb.Core/Data/TransactionCompletion.cs
Snapshot: redb, development snapshot 2026-12-06. This is the development state of the public mirrors; it may differ from the published packages.
License: Apache-2.0. Repository: https://github.com/redbase-app/redb
Page: https://redbase.app/src/dev-2026-12-06/redb/redb.Core/Data/TransactionCompletion.cs
----
using System;
using System.Collections.Generic;
using System.Runtime.CompilerServices;
namespace redb.Core.Data
{
///
/// Callbacks that run when an explicit redb transaction () ends, with whether it
/// committed. The provider transaction classes report the outcome once; the props and list caches use it to
/// publish what a transaction loaded only after its commit (). Entries are
/// held weakly by the transaction: a transaction nobody completes leaves nothing behind.
///
public static class TransactionCompletion
{
private static readonly ConditionalWeakTable>> Pending = new();
/// Runs with true when the transaction commits, false when it ends any other way.
public static void Register(IRedbTransaction transaction, Action onCompleted)
{
ArgumentNullException.ThrowIfNull(transaction);
ArgumentNullException.ThrowIfNull(onCompleted);
var callbacks = Pending.GetOrCreateValue(transaction);
lock (callbacks)
callbacks.Add(onCompleted);
}
/// Called by the transaction once, after it committed or rolled back.
public static void Completed(IRedbTransaction transaction, bool committed)
{
ArgumentNullException.ThrowIfNull(transaction);
if (!Pending.TryGetValue(transaction, out var callbacks))
return;
Pending.Remove(transaction);
Action[] snapshot;
lock (callbacks)
snapshot = callbacks.ToArray();
foreach (var callback in snapshot)
callback(committed);
}
}
}