In this documentation, we will focus on the common API. This module includes the core of BatchJS. This API allows you to create your own custom jobs and steps using the common interface.
abstract
extends EventEmitter
Abstract base class for all jobs.
export class JobImplementation extends Job {
protected _steps() {
return [new PassingStepFirst(), new PassingStepSecond()];
}
}
const job = new JobImplementation("My job");
job.on("stepStart", (step:step) => {
console.log(`Starting step ${step.name}`);
})
job.run()
.then(() => {
console.log("Job completed successfully");
})
.catch((error) => {
console.log("Job completed with errors");
});
>> Starting step PassingStepFirst
>> Starting step PassingStepSecond
>> Job completed successfully
Name | Description | Type |
---|---|---|
name | The name to assign to the Step. | string |
protected
static
abstract
Abstract method that most be implemented by the job in order to returns an ordered array of steps that make up the job.
Type | Description |
---|---|
Array.<Step> |
Asynchronously runs the job by executing each step in sequence.
Type | Description |
---|---|
Promise.<void> | A Promise that resolves when all steps are successfully executed or rejects if an error occurs. |
Adds an event listener to the specified event type.
Name | Description | Type |
---|---|---|
event | start, end, stepStart, stepEnd | |
listener | function |
Type | Description |
---|---|
this | allowing to chain |
Emits an event of the specified type to the listeners.
Name | Description | Type |
---|---|---|
event | start, end, stepStart, stepEnd | |
args | Data to sent to the listeners depending on the event type | Array.<JobEventEmitters> |
Type | Description |
---|---|
boolean |
Adds an event listener to the specified event type.
Name | Description | Type |
---|---|---|
event | start, end, stepStart, stepEnd | |
listener | function |
Type | Description |
---|---|
this | allowing to chain |
Adds a one time event listener to the specified event type.
Name | Description | Type |
---|---|---|
event | start, end, stepStart, stepEnd | |
listener | function |
Type | Description |
---|---|
this | allowing to chain |
Adds an event listener to the specified event type to the beginning of the listeners array.
Name | Description | Type |
---|---|---|
event | start, end, stepStart, stepEnd | |
listener | function |
Type | Description |
---|---|
this | allowing to chain |
Adds a one time event listener to the specified event type to the beginning of the listeners array.
Name | Description | Type |
---|---|---|
event | start, end, stepStart, stepEnd | |
listener | function |
Type | Description |
---|---|
this | allowing to chain |
Removes an event listener to the specified event type.
Name | Description | Type |
---|---|---|
event | start, end, stepStart, stepEnd | |
listener | function |
Type | Description |
---|---|
this | allowing to chain |
abstract
Abstract base class for all steps.
class StepImplementation extends Step {
constructor(name: string = "MockPassingStep") {
super(name);
}
protected _reader() {
return new Readable({
objectMode: true,
read() {
this.push("data");
this.push(null);
}
});
}
protected _processors() {
const opts: TransformOptions = {
objectMode: true,
transform(chunk: unknown, encoding: BufferEncoding, callback: TransformCallback) {
this.push(chunk);
callback();
}
};
return [new Transform(opts), new Transform(opts)];
}
protected _writer() {
return new Writable({
objectMode: true,
write(chunk: unknown, encoding: BufferEncoding, callback: TransformCallback) {
callback();
}
});
}
}
const step = new StepImplementation("StepImplementation");
step.run()
.then(() => {
console.log("Step completed successfully");
})
.catch((error) => {
console.log("Step completed with errors");
});
>> Step completed successfully
Name | Description | Type |
---|---|---|
name | The name to assign to the Step. | string |
protected
static
abstract
Abstract method that must be implemented by the step in order to defined the reader stream.
Type | Description |
---|---|
Readable |
protected
static
abstract
Abstract method that must be implemented by the step in order to process the data from the reader stream and push it to the writer stream. Processors are defined in an ordered array to be chained on the runner.
Type | Description |
---|---|
Array.<Duplex> |
protected
static
abstract
Abstract method that must be implemented by the step in order to defined the writer stream.
Type | Description |
---|---|
Writable |
Executes the step by connecting streams, processing data, and listening for events.
Type | Description |
---|---|
Promise.<void> | A Promise that resolves when the step execution is completed, and rejects if an error occurs. |