Simplify base classes by using stream _construct methods
Signed-off-by: Eric Peterson <ericpeterson@spotify.com>
This commit is contained in:
@@ -0,0 +1,5 @@
|
||||
---
|
||||
'@backstage/plugin-search-backend-node': patch
|
||||
---
|
||||
|
||||
Updated indexer and decorator base classes to take advantage of features introduced in Node.js v16; be sure you are running a [supported version of Node.js](https://backstage.io/docs/releases/v1.8.0#node-16-and-18).
|
||||
@@ -34,26 +34,10 @@ export type BatchSearchEngineOptions = {
|
||||
export abstract class BatchSearchEngineIndexer extends Writable {
|
||||
private batchSize: number;
|
||||
private currentBatch: IndexableDocument[] = [];
|
||||
private initialized: Promise<undefined | Error>;
|
||||
|
||||
constructor(options: BatchSearchEngineOptions) {
|
||||
super({ objectMode: true });
|
||||
this.batchSize = options.batchSize;
|
||||
|
||||
// @todo Once node v15 is minimum, convert to _construct implementation.
|
||||
this.initialized = new Promise(done => {
|
||||
// Necessary to allow concrete implementation classes to construct
|
||||
// themselves before calling their initialize() methods.
|
||||
setImmediate(async () => {
|
||||
try {
|
||||
await this.initialize();
|
||||
done(undefined);
|
||||
} catch (e) {
|
||||
assertError(e);
|
||||
done(e);
|
||||
}
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -73,6 +57,20 @@ export abstract class BatchSearchEngineIndexer extends Writable {
|
||||
*/
|
||||
public abstract finalize(): Promise<void>;
|
||||
|
||||
/**
|
||||
* Encapsulates initialization logic.
|
||||
* @internal
|
||||
*/
|
||||
async _construct(done: (error?: Error | null | undefined) => void) {
|
||||
try {
|
||||
await this.initialize();
|
||||
done();
|
||||
} catch (e) {
|
||||
assertError(e);
|
||||
done(e);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Encapsulates batch stream write logic.
|
||||
* @internal
|
||||
@@ -82,13 +80,6 @@ export abstract class BatchSearchEngineIndexer extends Writable {
|
||||
_e: any,
|
||||
done: (error?: Error | null) => void,
|
||||
) {
|
||||
// Wait for init before proceeding. Throw error if initialization failed.
|
||||
const maybeError = await this.initialized;
|
||||
if (maybeError) {
|
||||
done(maybeError);
|
||||
return;
|
||||
}
|
||||
|
||||
this.currentBatch.push(doc);
|
||||
if (this.currentBatch.length < this.batchSize) {
|
||||
done();
|
||||
@@ -111,12 +102,6 @@ export abstract class BatchSearchEngineIndexer extends Writable {
|
||||
*/
|
||||
async _final(done: (error?: Error | null) => void) {
|
||||
try {
|
||||
const maybeError = await this.initialized;
|
||||
if (maybeError) {
|
||||
done(maybeError);
|
||||
return;
|
||||
}
|
||||
|
||||
// Index any remaining documents.
|
||||
if (this.currentBatch.length) {
|
||||
await this.index(this.currentBatch);
|
||||
|
||||
@@ -24,25 +24,8 @@ import { Transform } from 'stream';
|
||||
* @public
|
||||
*/
|
||||
export abstract class DecoratorBase extends Transform {
|
||||
private initialized: Promise<undefined | Error>;
|
||||
|
||||
constructor() {
|
||||
super({ objectMode: true });
|
||||
|
||||
// @todo Once node v15 is minimum, convert to _construct implementation.
|
||||
this.initialized = new Promise(done => {
|
||||
// Necessary to allow concrete implementation classes to construct
|
||||
// themselves before calling their initialize() methods.
|
||||
setImmediate(async () => {
|
||||
try {
|
||||
await this.initialize();
|
||||
done(undefined);
|
||||
} catch (e) {
|
||||
assertError(e);
|
||||
done(e);
|
||||
}
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -68,6 +51,20 @@ export abstract class DecoratorBase extends Transform {
|
||||
*/
|
||||
public abstract finalize(): Promise<void>;
|
||||
|
||||
/**
|
||||
* Encapsulates initialization logic.
|
||||
* @internal
|
||||
*/
|
||||
async _construct(done: (error?: Error | null | undefined) => void) {
|
||||
try {
|
||||
await this.initialize();
|
||||
done();
|
||||
} catch (e) {
|
||||
assertError(e);
|
||||
done(e);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Encapsulates simple transform stream logic.
|
||||
* @internal
|
||||
@@ -77,13 +74,6 @@ export abstract class DecoratorBase extends Transform {
|
||||
_: any,
|
||||
done: (error?: Error | null) => void,
|
||||
) {
|
||||
// Wait for init before proceeding. Throw error if initialization failed.
|
||||
const maybeError = await this.initialized;
|
||||
if (maybeError) {
|
||||
done(maybeError);
|
||||
return;
|
||||
}
|
||||
|
||||
try {
|
||||
const decorated = await this.decorate(document);
|
||||
|
||||
|
||||
Reference in New Issue
Block a user