From ea83219937cd893e19a109e09bd07a59a3590c10 Mon Sep 17 00:00:00 2001 From: Radu Popovici Date: Wed, 18 Mar 2020 23:46:29 +0200 Subject: [PATCH] Feature/evented fns (#70) * Evented addEvent * exec mapE listen Co-authored-by: Radu Popovici --- src/Core/NBB.Core.Evented.FSharp/Evented.fs | 10 +++++- .../NBB.Core.Evented.FSharp.Tests.fsproj | 1 + .../NBB.Core.Evented.FSharp.Tests/Sample.fs | 3 +- .../NBB.Core.Evented.FSharp.Tests/Sample2.fs | 31 +++++++++++++++++++ 4 files changed, 42 insertions(+), 3 deletions(-) create mode 100644 test/UnitTests/Core/NBB.Core.Evented.FSharp.Tests/Sample2.fs diff --git a/src/Core/NBB.Core.Evented.FSharp/Evented.fs b/src/Core/NBB.Core.Evented.FSharp/Evented.fs index fe473530..d688f883 100644 --- a/src/Core/NBB.Core.Evented.FSharp/Evented.fs +++ b/src/Core/NBB.Core.Evented.FSharp/Evented.fs @@ -2,6 +2,7 @@ type Evented<'a, 'e> = Evented of payload:'a * events:'e list +[] module Evented = let map func (Evented(value, events)) = Evented(func value, events) @@ -17,6 +18,10 @@ module Evented = let lift2 f = map f >> apply + let run (Evented(value, events): Evented<'a, 'e>) = (value, events) + let exec (Evented(_value, events): Evented<'a, 'e>) = events + let mapE (func:'e->'f) (Evented(value, events): Evented<'a,'e>) = Evented(value, events |> List.map func) + type Evented<'a, 'e> with static member Map (x, f) = Evented.map f x static member Return (x) = Evented.return' x @@ -32,7 +37,7 @@ module EventedBuilder = member _.Zero() = Evented.return' () [] -module Events = +module EventedExtensions = let evented = new EventedBuilder.EventedBuilder() let () = Evented.map @@ -40,6 +45,9 @@ module Events = let (>>=) evented func = Evented.bind func evented let (>=>) = Evented.composeK + let addEvent (ev:'e) = Evented((),[ev]) + let listen (Evented (value, events): Evented<'a,'e>) = Evented((value,events),events) + [] module List = let traverseEvented f list = diff --git a/test/UnitTests/Core/NBB.Core.Evented.FSharp.Tests/NBB.Core.Evented.FSharp.Tests.fsproj b/test/UnitTests/Core/NBB.Core.Evented.FSharp.Tests/NBB.Core.Evented.FSharp.Tests.fsproj index 1f4430c8..5fbdc40e 100644 --- a/test/UnitTests/Core/NBB.Core.Evented.FSharp.Tests/NBB.Core.Evented.FSharp.Tests.fsproj +++ b/test/UnitTests/Core/NBB.Core.Evented.FSharp.Tests/NBB.Core.Evented.FSharp.Tests.fsproj @@ -9,6 +9,7 @@ + diff --git a/test/UnitTests/Core/NBB.Core.Evented.FSharp.Tests/Sample.fs b/test/UnitTests/Core/NBB.Core.Evented.FSharp.Tests/Sample.fs index 7539d135..bb6ff3c5 100644 --- a/test/UnitTests/Core/NBB.Core.Evented.FSharp.Tests/Sample.fs +++ b/test/UnitTests/Core/NBB.Core.Evented.FSharp.Tests/Sample.fs @@ -1,4 +1,4 @@ -module Sample +namespace NBB.Core.Evented.FSharp.Tests open NBB.Core.Evented.FSharp open FSharpPlus @@ -10,7 +10,6 @@ module Domain = | Updated let create x = Evented(AggRoot x, [Added]) - let update (x:AggRoot) = Evented(x, [Updated]) let increment (AggRoot x) = AggRoot (x + 1) diff --git a/test/UnitTests/Core/NBB.Core.Evented.FSharp.Tests/Sample2.fs b/test/UnitTests/Core/NBB.Core.Evented.FSharp.Tests/Sample2.fs new file mode 100644 index 00000000..f666f0b3 --- /dev/null +++ b/test/UnitTests/Core/NBB.Core.Evented.FSharp.Tests/Sample2.fs @@ -0,0 +1,31 @@ +namespace NBB.Core.Evented.FSharp.Tests + +open NBB.Core.Evented.FSharp + +module Sample2 = + type Payment = { + Id: int + Amount: decimal + Cancelled: bool + } + + type DomainEvent = + | Added of Id:int * Amount:decimal + | Cancelled of Id:int + + let create (id,amount) = + evented{ + let payment = {Id=id; Amount=amount; Cancelled=false} + do! addEvent <| Added(id,amount) + return payment + } + + let cancell payment = + evented{ + let payment' = {payment with Cancelled=true} + do! addEvent <| Cancelled payment.Id + return payment' + } + + let createCancelled = create >=> cancell +