Concepts Hook Registry - osama1998H/Moca GitHub Wiki

Hook Registry

Priority-ordered hooks, dependency DAG, DocEventDispatcher, and hook types.

The Hook Registry allows apps to extend document behavior, add middleware, schedule jobs, and modify UI context -- all with explicit priority ordering and dependency resolution.

How Hooks Work

  1. Apps register hooks in their hooks.go file
  2. Each hook declares a numeric priority and optional dependencies
  3. At runtime, the DocEventDispatcher fires hooks in dependency-resolved, priority-sorted order

Hook Types

Type Fires When Example
Document Event Document lifecycle (BeforeInsert, AfterSave, etc.) Validate customer credit limit before save
Scheduler Cron schedule triggers Send daily digest emails
API Middleware HTTP request pipeline Add custom auth header validation
UI Context Desk page load Inject sidebar items based on user role

Priority & Dependency Resolution

  • Hooks declare numeric priority (lower = runs first)
  • Optional dependency declarations form a DAG
  • Kahn's algorithm resolves execution order
  • Circular dependencies are detected and rejected at registration time

Registration

// apps/crm/hooks.go
func init() {
    hooks.Register(hooks.Hook{
        Event:    "BeforeSave",
        DocType:  "Sales Order",
        Handler:  validateCreditLimit,
        Priority: 10,
    })
}

Related