Last active
August 18, 2016 23:21
-
-
Save haampie/8dd4c00c4b825d4d2c933fced3c9ed50 to your computer and use it in GitHub Desktop.
Sync / Async event handlers
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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()); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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