mirror of
https://github.com/usebruno/bruno.git
synced 2026-06-23 12:45:38 +00:00
refactor: remove statistics tracking from HookManager and HooksRuntime
This commit is contained in:
@@ -60,14 +60,7 @@ class HookManager {
|
||||
this.listeners = {};
|
||||
// Track cleanup functions for proper resource disposal
|
||||
this._cleanupFunctions = [];
|
||||
this._disposed = false;
|
||||
this._state = HookManager.State.ACTIVE;
|
||||
// Track execution statistics
|
||||
this._stats = {
|
||||
totalCalls: 0,
|
||||
totalErrors: 0,
|
||||
lastCallTime: null
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -83,15 +76,7 @@ class HookManager {
|
||||
* @returns {boolean} True if disposed
|
||||
*/
|
||||
get isDisposed() {
|
||||
return this._disposed;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get execution statistics
|
||||
* @returns {object} Statistics object
|
||||
*/
|
||||
get stats() {
|
||||
return { ...this._stats };
|
||||
return this._state === HookManager.State.DISPOSED;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -111,10 +96,9 @@ class HookManager {
|
||||
* Should be called when the HookManager is no longer needed
|
||||
*/
|
||||
dispose() {
|
||||
if (this._disposed) {
|
||||
if (this._state === HookManager.State.DISPOSED) {
|
||||
return;
|
||||
}
|
||||
this._disposed = true;
|
||||
this._state = HookManager.State.DISPOSED;
|
||||
|
||||
// Call all registered cleanup functions
|
||||
@@ -138,7 +122,7 @@ class HookManager {
|
||||
* @throws {Error} If HookManager is disposed
|
||||
*/
|
||||
_validateState(operation) {
|
||||
if (this._disposed) {
|
||||
if (this._state === HookManager.State.DISPOSED) {
|
||||
throw new Error(`Cannot ${operation}: HookManager has been disposed`);
|
||||
}
|
||||
}
|
||||
@@ -161,7 +145,7 @@ class HookManager {
|
||||
*/
|
||||
async call(pattern, data, options = {}) {
|
||||
// Validate state - but allow calls on disposed manager to fail gracefully
|
||||
if (this._disposed) {
|
||||
if (this._state === HookManager.State.DISPOSED) {
|
||||
console.warn('HookManager.call() called on disposed instance');
|
||||
if (options.collectErrors) {
|
||||
return {
|
||||
@@ -183,10 +167,6 @@ class HookManager {
|
||||
let handlersExecuted = 0;
|
||||
let handlersFailed = 0;
|
||||
|
||||
// Update stats
|
||||
this._stats.totalCalls++;
|
||||
this._stats.lastCallTime = Date.now();
|
||||
|
||||
const patternList = [].concat(pattern).map((d) => String(d).trim());
|
||||
const hasWildcard = patternList.includes('*');
|
||||
|
||||
@@ -202,7 +182,6 @@ class HookManager {
|
||||
|
||||
if (!result.success) {
|
||||
handlersFailed++;
|
||||
this._stats.totalErrors++;
|
||||
|
||||
if (collectErrors && result.error) {
|
||||
errors.push(result.error);
|
||||
|
||||
@@ -26,20 +26,6 @@ class HooksRuntime {
|
||||
*/
|
||||
constructor(props) {
|
||||
this.runtime = props?.runtime || 'quickjs';
|
||||
// Track statistics for performance monitoring
|
||||
this._stats = {
|
||||
vmCreations: 0,
|
||||
runs: 0,
|
||||
skippedRuns: 0
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* Get execution statistics
|
||||
* @returns {object} Statistics about hook execution
|
||||
*/
|
||||
get stats() {
|
||||
return { ...this._stats };
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -126,7 +112,6 @@ class HooksRuntime {
|
||||
|
||||
// Lazy VM creation: If no hooks file, return early without creating a VM
|
||||
if (this._isEmptyContent(hooksFile)) {
|
||||
this._stats.skippedRuns++;
|
||||
return {
|
||||
hookManager: activeHookManager,
|
||||
envVariables: cleanJson(envVariables),
|
||||
@@ -142,9 +127,6 @@ class HooksRuntime {
|
||||
};
|
||||
}
|
||||
|
||||
this._stats.runs++;
|
||||
this._stats.vmCreations++;
|
||||
|
||||
// Execute hooks script
|
||||
// Note: Hooks need the VM to persist so registered handlers can be called later
|
||||
// The cleanup function is registered with the HookManager and called when dispose() is invoked
|
||||
|
||||
@@ -25,14 +25,6 @@ describe('HookManager', () => {
|
||||
expect(hookManager.state).toBe(HookManager.State.ACTIVE);
|
||||
expect(hookManager.isDisposed).toBe(false);
|
||||
});
|
||||
|
||||
it('should initialize stats', () => {
|
||||
expect(hookManager.stats).toEqual({
|
||||
totalCalls: 0,
|
||||
totalErrors: 0,
|
||||
lastCallTime: null
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
describe('on()', () => {
|
||||
@@ -146,13 +138,6 @@ describe('HookManager', () => {
|
||||
expect(normalHandler).toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it('should update stats after call', async () => {
|
||||
hookManager.on('test', jest.fn());
|
||||
await hookManager.call('test', {});
|
||||
expect(hookManager.stats.totalCalls).toBe(1);
|
||||
expect(hookManager.stats.lastCallTime).not.toBeNull();
|
||||
});
|
||||
|
||||
it('should collect errors when collectErrors is true', async () => {
|
||||
const errorHandler = jest.fn(() => { throw new Error('Test error'); });
|
||||
hookManager.on('test', errorHandler);
|
||||
|
||||
Reference in New Issue
Block a user