Initial commit
This commit is contained in:
@@ -0,0 +1,56 @@
|
||||
using System.Collections.Generic;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace RudderSdk.Core;
|
||||
|
||||
/// <summary>Session of a scenario store-offer node; resolve it with a purchase or a decline.</summary>
|
||||
public sealed class StoreOfferSession
|
||||
{
|
||||
internal StoreOfferSession(ScenarioNodeContext context)
|
||||
{
|
||||
Context = context;
|
||||
Data = context.AsObjectDictionary();
|
||||
}
|
||||
|
||||
/// <summary>Underlying node context.</summary>
|
||||
public ScenarioNodeContext Context { get; }
|
||||
|
||||
/// <summary>Node id.</summary>
|
||||
public string Id => Context.NodeId;
|
||||
|
||||
/// <summary>Node data payload.</summary>
|
||||
public IReadOnlyDictionary<string, object> Data { get; }
|
||||
|
||||
/// <summary>True after the session was resolved once.</summary>
|
||||
public bool IsResolved { get; private set; }
|
||||
|
||||
/// <summary>Reads a typed value from the node data.</summary>
|
||||
public T Get<T>(string key, T defaultValue = default!) => Context.Get(key, defaultValue);
|
||||
|
||||
/// <summary>Resolves the offer as purchased.</summary>
|
||||
public Task PurchaseAsync(CancellationToken cancellationToken = default) => ResolveAsync("onPurchase", cancellationToken);
|
||||
|
||||
/// <summary>Resolves the offer as purchased (fire-and-forget).</summary>
|
||||
public void Purchase() => Resolve("onPurchase");
|
||||
|
||||
/// <summary>Resolves the offer as declined.</summary>
|
||||
public Task DeclineAsync(CancellationToken cancellationToken = default) => ResolveAsync("onDecline", cancellationToken);
|
||||
|
||||
/// <summary>Resolves the offer as declined (fire-and-forget).</summary>
|
||||
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);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user