@hotmeshio/hotmesh
Version:
Serverless Workflow
55 lines (54 loc) • 2.07 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.AbstractConnection = void 0;
const logger_1 = require("../logger");
/**
* Abstract class for creating connections to different backend providers.
* All implementations should extend this class and implement
* the following steps:
*
* 1) Add the provider to ./providers/<name>.ts
* 2) Update ./factory.ts to reference the provider
* 3) Register the tag with the `Provider` type in ./types/provider.ts.
* 4) Create the specific provider type file at ./types/<name>.ts
* 5) Update ./modules/utils.ts (identifyProvider) with logic to resolve the provider by inspecting the class/import
*/
class AbstractConnection {
constructor() {
this.connection = null;
this.id = null;
}
async disconnect() {
if (this.connection) {
await this.closeConnection(this.connection);
this.connection = null;
}
if (this.id) {
AbstractConnection.instances.delete(this.id);
}
}
static async connect(id, client, options, //user
config) {
if (AbstractConnection.instances.has(id)) {
return AbstractConnection.instances.get(id);
}
const instance = new this();
const opts = options ? { ...options } : { ...instance.defaultOptions };
instance.connection = await instance.createConnection(client, opts, config);
instance.id = id;
AbstractConnection.instances.set(id, instance);
return instance;
}
static async disconnectAll() {
if (!this.disconnecting) {
this.disconnecting = true;
await Promise.all(Array.from(this.instances.values()).map((instance) => instance.disconnect()));
this.instances.clear();
this.disconnecting = false;
}
}
}
exports.AbstractConnection = AbstractConnection;
AbstractConnection.logger = new logger_1.LoggerService('hotmesh', 'system');
AbstractConnection.disconnecting = false;
AbstractConnection.instances = new Map();