Skip to content

Instantly share code, notes, and snippets.

@haampie
Last active August 18, 2016 23:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save haampie/8dd4c00c4b825d4d2c933fced3c9ed50 to your computer and use it in GitHub Desktop.
Save haampie/8dd4c00c4b825d4d2c933fced3c9ed50 to your computer and use it in GitHub Desktop.
Sync / Async event handlers
<?php
// In requests:
final class SomeSubscriber {
public function applyDomainEvent(DomainEvent $xyz) {
// Maybe fire another event to get some consistency.
}
}
final class SyncSubscriberNotifier implements EventSubscriberNotifier {}
$notifier = new SyncSubscriberNotifier(new LaravelContainerResolver);
$notifier->register(SomeSubscriber::class);
// Handle some message synchronous, others asynchronous.
$bus = new MiddlewareBus([
new FinishBeforeHandlingOthers,
$notifier,
new PublishAsync, // terminates the pipeline (maybe conditionally), and puts a serialized event in an envelope with some metadata
]);
$bus->dispatch(new DomainMessage());
<?php
// In workers:
final class SomeProjector {
public function applyDomainEvent(DomainEvent $xyz) {
// Do some heavy database stuff that can be eventually consistent, or some networking
}
}
final class DelayedSubscribersNotifier implements EventSubscriberNotifier {}
$notifier = new DelayedSubscribersNotifier(new LaravelContainerResolver);
$notifier->register(SomeProjector::class);
// Handle some message synchronous, other asynchronous.
$bus = new MiddlewareBus([
new FinishBeforeHandlingOthers,
$notifier,
// No need to publish async things.
]);
$deserializer = ...;
$loop->listen('DomainMessageReceived', function(MessageEnvelope $envelope) use ($deserializer, $bus) {
$message = $envelope->deserialize($deserializer);
$bus->dispatch($message);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment