using System.Collections.Generic; using System.Threading; using System.Threading.Tasks; namespace RudderSdk.Core; /// Session of a scenario store-offer node; resolve it with a purchase or a decline. public sealed class StoreOfferSession { internal StoreOfferSession(ScenarioNodeContext context) { Context = context; Data = context.AsObjectDictionary(); } /// Underlying node context. public ScenarioNodeContext Context { get; } /// Node id. public string Id => Context.NodeId; /// Node data payload. public IReadOnlyDictionary Data { get; } /// True after the session was resolved once. public bool IsResolved { get; private set; } /// Reads a typed value from the node data. public T Get(string key, T defaultValue = default!) => Context.Get(key, defaultValue); /// Resolves the offer as purchased. public Task PurchaseAsync(CancellationToken cancellationToken = default) => ResolveAsync("onPurchase", cancellationToken); /// Resolves the offer as purchased (fire-and-forget). public void Purchase() => Resolve("onPurchase"); /// Resolves the offer as declined. public Task DeclineAsync(CancellationToken cancellationToken = default) => ResolveAsync("onDecline", cancellationToken); /// Resolves the offer as declined (fire-and-forget). public void Decline() => Resolve("onDecline"); private async Task ResolveAsync(string handle, CancellationToken cancellationToken) { if (IsResolved) return; IsResolved = true; await Context.CompleteAsync(handle, cancellationToken).ConfigureAwait(false); } private void Resolve(string handle) { if (IsResolved) return; IsResolved = true; Context.Complete(handle); } }