Sourcing::Plugin::Memory
In-memory implementation of the Plugin interface. Suitable for testing and development.
Attributes
| Attribute | Type | Description |
|---|---|---|
$.supplier | Supplier | Emits events to the supply |
$.supply | Supply | Tap that pushes events to @!events |
@.events | Array | All emitted events in order |
%.store | Hash | Cached projection state: %!store{TypeName}{IDs} → {data, last-id} |
Methods
multi method emit($event)
Simple emit to the supplier without version checking.
multi method emit($event, :$type, :%ids!, :$current-version!)
Emit with optimistic locking. Uses CAS (compare-and-swap) for atomic version checking. Throws X::OptimisticLocked if the expected version doesn't match the stored version.
Also validates that $type is an aggregation — projections cannot emit events.
method get-events(%ids, %map)
Filters events by type map keys and ID field values.
method get-events-after(Int $id, %ids, %map)
Gets events after a specific version ID. Uses .skip($id + 1) on the event array.
method number-of-events
Returns total event count (@!events.elems).
multi method store-cached-data($proj where *.HOW.^can("data-to-store"), UInt :$last-id!)
Stores using the projection's custom data-to-store method for selective serialization.
multi method store-cached-data($proj, Int :$last-id!)
Stores by extracting all public attributes from the projection.
multi method store-cached-data(Mu:U $proj, %ids, %data, Int :$last-id!)
Core storage method. Stores in %!store{ProjectionName}{IDs} → {data, last-id}.
method get-cached-data(Mu:U $proj, %ids) is rw
Retrieves cached state. Returns a Hash with last-id (atomicint, default -1) and data (Hash of projection attributes).
Usage
use Sourcing::Plugin::Memory;
# Activate the plugin
Sourcing::Plugin::Memory.use;
# Access the plugin instance
my $memory = $*SourcingConfig;
# Inspect events
say "Total events: ", $memory.events.elems;
say "First event: ", $memory.events[0].^name;
See Also
- Sourcing::Plugin — the abstract interface
- Writing a Plugin Guide — how to implement your own plugin
- Exceptions — X::OptimisticLocked