@plotinus/matrix-package-observable-coordinator
Version:
Observable coordinator pattern components using IntrospectableBaseCommunicationComponent and proper presentation architecture
49 lines (48 loc) • 2.28 kB
JavaScript
// packages/package-observable-coordinator/src/components/worker/worker.component.ts
import { IntrospectableBaseCommunicationComponent } from '@matrix/presentation';
import { logInfo, logDebug } from '@matrix/logger';
export class WorkerComponent extends IntrospectableBaseCommunicationComponent {
onInit() {
super.onInit();
// Initialize observable state
this.setState({
workerId: this.id,
status: 'initializing',
currentJob: null,
completedJobs: []
});
logInfo(`Worker:${this.id}: Initialized with observable state. Worker ID set to '${this.id}'.`);
}
startExecution() {
const currentState = this.state;
logInfo(`Worker:${currentState.workerId}: Starting execution. Emitting 'Registered' event.`);
this.setState({ status: 'registered' });
this.emitEvent('Registered', { workerId: currentState.workerId });
}
// DSL binding: onProcessJob="processJobHandler" (defined on worker-definition tag)
processJobHandler(job) {
const currentState = this.state;
logInfo(`Worker:${currentState.workerId}: Received 'ProcessJob' command. Starting processing.`);
logDebug(`Worker:${currentState.workerId}: Job details`, job);
// Update state to show processing
this.setState({
status: 'processing',
currentJob: job
});
// Simulate asynchronous work
const processingTime = 1000; // Simulate 1 second
logDebug(`Worker:${currentState.workerId}: Simulating work for job ${job.id} (${processingTime}ms)...`);
setTimeout(() => {
const updatedState = this.state;
const completedJobs = [...updatedState.completedJobs, job.id];
this.setState({
status: 'idle',
currentJob: null,
completedJobs: completedJobs
});
logInfo(`Worker:${updatedState.workerId}: Job processing completed for job ${job.id}. Emitting 'JobCompleted'.`);
this.emitEvent('JobCompleted', { workerId: updatedState.workerId, jobId: job.id });
}, processingTime);
}
}
WorkerComponent.dslTag = 'worker-definition'; // Used by NodeFactory for registration