Event System Overview
The event system uses a publish-subscribe pattern where:- Emitters publish events when actions occur
- Subscribers listen for specific events and react
- Events are stored in the database and processed asynchronously
Available Events
EverShop provides built-in events for common operations. All events are type-safe and provide structured data.Product Events
product_created
Fired when a new product is created.ProductRow
product_updated
Fired when a product is updated.ProductRow
product_deleted
Fired when a product is deleted.ProductRow
product_image_added
Fired when an image is added to a product. Data Type:ProductImageRow
Category Events
category_created
Fired when a new category is created.CategoryRow
category_updated
Fired when a category is updated. Data Type:CategoryRow
category_deleted
Fired when a category is deleted. Data Type:CategoryRow
Customer Events
customer_registered
Fired when a customer registers on the storefront.CustomerRow
customer_created
Fired when a customer is created by admin. Data Type:CustomerRow
customer_updated
Fired when a customer is updated. Data Type:CustomerRow
customer_deleted
Fired when a customer is deleted. Data Type:CustomerRow
Order Events
order_created
Fired when a new order is created. Data Type:OrderRow
order_placed
Fired when an order is successfully placed.OrderRow
Inventory Events
inventory_updated
Fired when product inventory is updated.{ old: ProductInventoryRow, new: ProductInventoryRow }
Creating Event Subscribers
Directory Structure
Subscribers are organized by event name:Basic Subscriber
Type-Safe Subscriber
Using Helper Function
Accessing Services
Emitting Custom Events
Extensions can emit custom events for other extensions to consume.Basic Emit
Type-Safe Custom Events
Register your custom events in a type declaration file:Cron Jobs
Scheduled tasks are registered via the bootstrap file and executed by the cron worker.Register a Cron Job
*/1 * * * *- Every minute0 * * * *- Every hour0 0 * * *- Daily at midnight0 0 * * 0- Weekly on Sunday0 0 1 * *- Monthly on the 1st
Create Cron Handler
Manage Jobs Programmatically
Best Practices
Event Subscribers
- Keep it Fast: Subscribers should execute quickly. Offload heavy work to queues
- Handle Errors: Always wrap in try-catch to prevent one subscriber from breaking others
- Be Idempotent: Handle duplicate events gracefully
- Log Important Actions: Use the logger for debugging
Cron Jobs
- Use Appropriate Schedules: Don’t run intensive tasks too frequently
- Add Timeout Handling: Prevent long-running jobs from overlapping
- Log Execution: Track when jobs run and how long they take
- Handle Failures Gracefully: Catch errors and alert if needed
Custom Events
- Use Clear Names: Choose descriptive event names (snake_case)
- Document Data Structure: Define what data your events provide
- Version Your Events: Include version info if data structure might change
- Type Your Events: Use TypeScript declarations for type safety
Next Steps
- Review Extension Structure for organizing subscribers
- Check Creating Extensions for setup guide
- Explore the sample extension for real-world examples