Reference Implementation:
MythicFramework/resources/[mythic]/mythic-base/core/sh_proxy.luaThis file contains the complete proxy system implementation.What is the Proxy Pattern?
The proxy pattern in Mythic Framework provides a centralized component registry that:- Registers components from any resource
- Fetches components for use by other resources
- Extends existing components with new functionality
- Resolves dependencies automatically
The COMPONENTS Global Table
At the heart of the proxy system is the globalCOMPONENTS table:
The Four Core Exports
The proxy system provides four essential exports:1. RegisterComponent
Purpose: Register a new component or override an existing one Location:mythic-base/core/sh_proxy.lua:32
component_name(string) - Unique name for the componentcomponent_data(table) - The component object with methods and data
_protected = true to prevent other resources from overriding them:
_required to enforce that certain methods must exist:
2. FetchComponent
Purpose: Retrieve a component for use Location:mythic-base/core/sh_proxy.lua:67
component_name(string) - Name of the component to fetch
- Component object or
nilif not found
3. ExtendComponent
Purpose: Add new functionality to an existing component without modifying it Location:mythic-base/core/sh_proxy.lua:76
component_name(string) - Name of component to extendextension_data(table) - Methods/data to add
-
Adding helper methods:
-
Adding configuration:
-
Monkey-patching:
ExtendComponent cannot modify protected components. It will fail with a warning.
4. RequestDependencies
Purpose: Asynchronously wait for dependencies to load before initializing Location:mythic-base/core/sh_proxy.lua:96
component_name(string) - Name of your component (for logging)dependencies(table) - Array of component names to wait forcallback(function) - Called when dependencies are ready (or failed)
- Polls for dependencies every 100ms
- Times out after 50 attempts (~5 seconds)
- Calls callback with any errors
- Tracks which components depend on others for updates
Component Lifecycle
Understanding when components are registered and available:Real-World Examples
Example 1: Simple Utility Component
Example 2: Complex Feature Component
Example 3: Extending Core Component
Dependency Updates
When a component is extended or re-registered, all resources that depend on it are notified:Best Practices
1. Always Use RequestDependencies
1. Always Use RequestDependencies
Never assume components are available immediately:
2. Fetch Components Once
2. Fetch Components Once
Don’t fetch on every use:
3. Use Protected for Core Components
3. Use Protected for Core Components
Prevent accidental overrides:
4. Name Components Consistently
4. Name Components Consistently
Use PascalCase for component names:
5. Document Component APIs
5. Document Component APIs
Add comments explaining methods:
Common Patterns
Singleton Pattern
Factory Pattern
Next Steps
Component System
Learn more about component-based architecture
Resource Structure
How to structure resources using components
Event System
Complement components with events
API Reference
Complete API documentation