stage-hooks
Stage Hooks - AOP (Aspect-Oriented Programming) Lifecycle Hooks for Polypoint
Provides before/after/around hooks for any Stage method with automatic garbage collection and optimal performance characteristics.
Usage: const stage = new Stage()
// Add hooks to any method
stage.hooks.draw.before(() => {
console.log('Before draw')
})
stage.hooks.draw.after((result) => {
console.log('After draw', result)
})
// Chaining
stage.hooks.clear
.before(() => console.log('1'))
.after(() => console.log('2'))
// Around (wrapping)
stage.hooks.draw.around(function(original, args) {
this.ctx.save()
const result = original.apply(this, args)
this.ctx.restore()
return result
})
// Management
const myHook = () => console.log('my hook')
stage.hooks.draw.before(myHook)
stage.hooks.draw.remove('before', myHook)
stage.hooks.draw.clear()
// Inspection
console.log(stage.hooks.draw.list())
// Execute as normal - hooks run automatically
stage.draw()
Performance: - Setup (adding hooks): ~7ns per hook - Execution (no hooks): ~0.1ns overhead (fast path) - Execution (with 5 hooks): ~20-25ns - Memory: Automatic cleanup via WeakMap when stage is GC'd
Features: - Auto-discovery: Any method is automatically hookable - Memory safe: Uses WeakMap for automatic garbage collection - Performant: Direct wrapping, no proxy overhead on execution - Chainable: All hook methods return the lifecycle object - Manageable: Remove, clear, and list hooks
Meta Data
| filepath_exists | True |
| path | stage-hooks |
| filepath | stage-hooks.js |
| clean_files | () |