Skip to content

Instantly share code, notes, and snippets.

@bChiquet
Created January 18, 2017 18:25
Show Gist options
  • Save bChiquet/5481e3a1e885441a9d7f53851c3854e9 to your computer and use it in GitHub Desktop.
Save bChiquet/5481e3a1e885441a9d7f53851c3854e9 to your computer and use it in GitHub Desktop.
Reactive event sourcing in 45 lines
event_reaction_map = {}
def apply_effects_of(event):
if event in event_reaction_map:
for reaction in event_reaction_map[event]:
reaction()
def react_to(event):
def reaction_subscription(reaction):
subscribe_reaction(reaction)
return reaction
def subscribe_reaction(reaction):
if event in event_reaction_map:
event_reaction_map[event].append(reaction)
else:
event_reaction_map[event] = [reaction]
return reaction_subscription
@react_to("complaint") # Decorators are just syntactic sugar. Comment this line and uncomment the line below for proof.
def complaint_reaction():
print("That's outrageous !")
events.happening("reaction")
# complaint_reaction=react_to("complaint")(complaint_reaction)
@react_to("reaction")
def reaction_followup():
print("You said it, mate !")
class Events:
def __init__(self):
self.events = []
def happening(self, event):
self.events.append(event)
apply_effects_of(event)
events = Events()
events.happening("complaint")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment