bowling-analysis-system
Version:
A comprehensive system for analyzing bowling techniques using video processing and metrics calculation
123 lines (109 loc) • 3.29 kB
JavaScript
/**
* Base Processor
*
* Provides foundation for all processors in the system
* @module core/BaseProcessor
*/
/**
* @class BaseProcessor
* @description Foundation class for all processors
*/
class BaseProcessor {
/**
* Create a new processor
* @param {string} name - Processor name
* @param {Object} config - Processor configuration
*/
constructor(name, config = {}) {
this.name = name;
this.config = { ...config };
this.middleware = [];
}
/**
* Initialize the processor
* @param {Object} config - Initialization configuration
* @returns {Promise<void>} Promise that resolves when initialization is complete
*/
async initialize(config = {}) {
// Update configuration
this.config = {
...this.config,
...config
};
// Perform initialization tasks
await this._init();
}
/**
* Process input data
* @param {*} input - Input data
* @param {Object} context - Processing context
* @returns {Promise<*>} Processing result
*/
async process(input, context = {}) {
// Execute pre-processing hooks
await this._preProcess(input, context);
// Apply middleware
let result = input;
for (const middleware of this.middleware) {
result = await middleware(result, context);
}
// Execute core processing logic
result = await this._process(result, context);
// Execute post-processing hooks
result = await this._postProcess(result, context);
return result;
}
/**
* Add middleware to the processor
* @param {Function} middleware - Middleware function
* @returns {BaseProcessor} This processor instance
*/
use(middleware) {
if (typeof middleware === 'function') {
this.middleware.push(middleware);
}
return this;
}
/**
* Processor initialization hook (to be implemented by subclasses)
* @param {Object} config - Initialization configuration
* @returns {Promise<void>} Promise that resolves when initialization is complete
* @protected
*/
async _init() {
// Default implementation does nothing
}
/**
* Pre-processing hook (to be implemented by subclasses)
* @param {*} input - Input data
* @param {Object} context - Processing context
* @returns {Promise<void>} Promise that resolves when pre-processing is complete
* @protected
*/
async _preProcess(input, context) {
// Default implementation does nothing
}
/**
* Core processing logic (to be implemented by subclasses)
* @param {*} input - Input data
* @param {Object} context - Processing context
* @returns {Promise<*>} Processing result
* @protected
*/
async _process(input, context) {
// Default implementation passes input through
return input;
}
/**
* Post-processing hook (to be implemented by subclasses)
* @param {*} result - Processing result
* @param {Object} context - Processing context
* @returns {Promise<*>} Post-processed result
* @protected
*/
async _postProcess(result, context) {
// Default implementation passes result through
return result;
}
}
module.exports = BaseProcessor;