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 +