Skip to content

Commit

Permalink
feat(esgallery): mark stacks as processed
Browse files Browse the repository at this point in the history
  • Loading branch information
bounoable committed Nov 10, 2022
1 parent d0a8f7b commit 5d203a8
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 1 deletion.
5 changes: 5 additions & 0 deletions goes/esgallery/events.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,11 @@ const (
Cleared = "esgallery.cleared"
)

// Non-aggregate events
const (
StackProcessed = "esgallery.stack_processed"
)

// ProcessorTriggerEvents are the events that can trigger a [*Processor].
var ProcessorTriggerEvents = []string{
StackAdded,
Expand Down
23 changes: 22 additions & 1 deletion goes/esgallery/gallery.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"github.com/modernice/media-entity/gallery"
"github.com/modernice/media-entity/image"
"github.com/modernice/media-entity/internal/slicex"
"golang.org/x/exp/slices"
)

// Target is an event-sourced aggregate that acts as a gallery.
Expand Down Expand Up @@ -40,7 +41,15 @@ type ID = gallery.ID
type Gallery[StackID, ImageID ID, T Target] struct {
*gallery.Base[StackID, ImageID]

target T
target T
processedStacks []StackID
}

// ProcessedStacks returns ids of the stacks that have been processed by a post-processor.
func (g *Gallery[StackID, ImageID, T]) ProcessedStacks() []StackID {
out := make([]StackID, len(g.processedStacks))
copy(out, g.processedStacks)
return out
}

// New returns a new [*Gallery] that applies events and commands to the provided
Expand Down Expand Up @@ -74,6 +83,7 @@ func New[StackID, ImageID ID, T Target](target T) *Gallery[StackID, ImageID, T]
event.ApplyWith(target, g.untag, StackUntagged)
event.ApplyWith(target, g.sort, Sorted)
event.ApplyWith(target, g.clear, Cleared)
event.ApplyWith(target, g.stackProcessed, StackProcessed)

command.ApplyWith(target, func(load addStack[StackID, ImageID]) error {
_, err := g.NewStack(load.StackID, load.Image)
Expand Down Expand Up @@ -419,3 +429,14 @@ func (g *Gallery[StackID, ImageID, T]) Clear() {
func (g *Gallery[StackID, ImageID, T]) clear(event.Of[struct{}]) {
g.Base.Clear()
}

// MarkAsProcessed marks a [gallery.Stack] as being processed by a post-processor.
func (g *Gallery[StackID, ImageID, T]) MarkAsProcessed(stackID StackID) {
if !slices.Contains(g.ProcessedStacks(), stackID) {
aggregate.Next(g.target, StackProcessed, stackID)
}
}

func (g *Gallery[StackID, ImageID, T]) stackProcessed(evt event.Of[StackID]) {
g.processedStacks = append(g.processedStacks, evt.Data())
}
5 changes: 5 additions & 0 deletions goes/esgallery/processor.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,9 @@ type ProcessableGallery[StackID, ImageID ID] interface {

// Tag adds tags to a [gallery.Stack].
Tag(StackID, ...string) (gallery.Stack[StackID, ImageID], error)

// MarkAsProcessed marks a [gallery.Stack] as being processed by a post-processor.
MarkAsProcessed(StackID)
}

// WasProcessed returns whether the given [gallery.Stack] was processed by a [*PostProcessor].
Expand Down Expand Up @@ -153,6 +156,8 @@ func (r ProcessorResult[StackID, ImageID]) Apply(g ProcessableGallery[StackID, I
}
}

g.MarkAsProcessed(r.StackID)

return nil
}

Expand Down
7 changes: 7 additions & 0 deletions goes/esgallery/processor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import (
"github.com/modernice/media-entity/internal/testx"
imgtools "github.com/modernice/media-tools/image"
"golang.org/x/exp/maps"
"golang.org/x/exp/slices"
)

func TestProcessor_Process(t *testing.T) {
Expand Down Expand Up @@ -435,4 +436,10 @@ func testProcessorResult(
if len(storage.Files()) != 4 {
t.Fatalf("expected 4 files in storage; got %d\n%s", len(storage.Files()), maps.Keys(storage.Files()))
}

if result.Applied {
if !slices.Contains(g.ProcessedStacks(), stack.ID) {
t.Fatalf("expected ProcessedStacks() to contain stack %s\n%v", stack.ID, g.ProcessedStacks())
}
}
}

0 comments on commit 5d203a8

Please sign in to comment.