ryan-mars / stochastic

TypeScript framework for building event-driven services. Easily go from Event Storming → Code.
MIT License
6 stars 1 forks source link

Refactor Policy and ReadModel to use base class EventHandler #46

Open ryan-mars opened 3 years ago

ryan-mars commented 3 years ago

ReadModel (projections) and Policy are the only "event handler" components we have right now. It is conceivable that developers will want to respond to events in ways we have not yet conceived. It would be nice if there was a base class EventHandler they could use or extend to suit their needs.

ryan-mars commented 3 years ago

Policy signature:

const MyPolicy = new Policy(
  {
    __filename,
    events: [],
    commands: []
  },
  async (event, commands) => {

  }
)

ReadModel signature

const MyReadModel = new ByKeyReadModel( 
  {    
    __filename,    
    events: [],    
    shape: ...,    
    key: "flightNo"
  },  
  (events, status) => {    
    return { ...status, ... } 
  }
);

Proposed EventHandler signature

const MyEventHandler = new EventHandler(
  {
    __filename,
    events: [],
  },
  async (event) => {

  }
)
ryan-mars commented 3 years ago

Furthermore, I wonder if it makes sense to create a base MessageHandler component for Query and Command.

With MessageHandler and EventHandler my gut says if we're missing something at a conceptual level these two components could cover a lot of behavior.

ryan-mars commented 3 years ago

@sam-goodwin Can we consider this done?