|
| 1 | +namespace Subscriptions.Actors |
| 2 | + |
| 3 | +open Dapr.Client |
| 4 | +open Dapr.Actors |
| 5 | +open Dapr.Actors.Runtime |
| 6 | +open FSharpx.Control |
| 7 | +open System.Collections.Generic |
| 8 | +open System.Threading.Tasks |
| 9 | +open Shared.Config |
| 10 | +open Shared.Extensions |
| 11 | +open Shared |
| 12 | + |
| 13 | +type Subscriptions = Dictionary<string, Subscription> |
| 14 | + |
| 15 | +module SubscriptionActor = |
| 16 | + |
| 17 | + /// <summary> |
| 18 | + /// The name of the actor. |
| 19 | + /// </summary> |
| 20 | + [<Literal>] |
| 21 | + let Name = "SubscriptionActor" |
| 22 | + |
| 23 | + /// <summary> |
| 24 | + /// The ID of the actor. |
| 25 | + /// </summary> |
| 26 | + let ID = ActorId("subscription") |
| 27 | + |
| 28 | +/// <summary> |
| 29 | +/// An actor that is responsible for handling the subscriptions in the state store. |
| 30 | +/// </summary> |
| 31 | +type ISubscriptionActor = |
| 32 | + inherit IActor |
| 33 | + |
| 34 | + /// <summary> |
| 35 | + /// Adds the given subscriptions to the state store. |
| 36 | + /// </summary> |
| 37 | + /// <param name="subscription">The subscription data.</param> |
| 38 | + /// <returns>A task that represents the asynchronous operation.</returns> |
| 39 | + abstract Subscribe: subscription: Subscription -> Task<Subscriptions> |
| 40 | + |
| 41 | + /// <summary> |
| 42 | + /// Removes the subscription for the given email from the state store. |
| 43 | + /// </summary> |
| 44 | + /// <param name="email">The email to unsubscribe.</param> |
| 45 | + /// <returns>A task that represents the asynchronous operation.</returns> |
| 46 | + abstract Unsubscribe: email: string -> Task<Subscriptions> |
| 47 | + |
| 48 | +/// <summary> |
| 49 | +/// Concrete subscription actor implementation. |
| 50 | +/// |
| 51 | +/// Storing all subscriptions under a single key, may be an overhead for a large set of subscriptions. For |
| 52 | +/// production usage, an other storage format may be better. |
| 53 | +/// </summary> |
| 54 | +[<Actor(TypeName = SubscriptionActor.Name)>] |
| 55 | +type SubscriptionActor(actorService: ActorService, actorId: ActorId, daprClient: DaprClient) = |
| 56 | + inherit Actor(actorService, actorId) |
| 57 | + |
| 58 | + interface ISubscriptionActor with |
| 59 | + |
| 60 | + /// <inheritdoc/> |
| 61 | + member _.Subscribe(subscription: Subscription) = |
| 62 | + async { |
| 63 | + let! subscriptions = |
| 64 | + daprClient.GetStateAsyncOr(StateStore.name, StateStore.subscriptions, Subscriptions()) |
| 65 | + |
| 66 | + let maybeValue = subscriptions.GetValueO(subscription.Email) |
| 67 | + |
| 68 | + return! |
| 69 | + match maybeValue with |
| 70 | + | Some(value) when value.Name = subscription.Name -> |
| 71 | + async { return subscriptions } |
| 72 | + | _ -> |
| 73 | + subscriptions.[subscription.Email] <- subscription |
| 74 | + |
| 75 | + daprClient.SaveStateAsync(StateStore.name, StateStore.subscriptions, subscriptions) |
| 76 | + |> Async.AwaitTask |
| 77 | + |> Async.map (fun _ -> subscriptions) |
| 78 | + } |
| 79 | + |> Async.StartAsTask |
| 80 | + |
| 81 | + /// <inheritdoc/> |
| 82 | + member _.Unsubscribe(email: string) = |
| 83 | + async { |
| 84 | + let! subscriptions = |
| 85 | + daprClient.GetStateAsyncOr(StateStore.name, StateStore.subscriptions, Subscriptions()) |
| 86 | + |
| 87 | + return! |
| 88 | + match subscriptions.Remove(email) with |
| 89 | + | true -> |
| 90 | + daprClient.SaveStateAsync(StateStore.name, StateStore.subscriptions, subscriptions) |
| 91 | + |> Async.AwaitTask |
| 92 | + |> Async.map (fun _ -> subscriptions) |
| 93 | + | false -> async { return subscriptions } |
| 94 | + } |
| 95 | + |> Async.StartAsTask |
0 commit comments