UNPKG

lambda-remote-context

Version:

Lifecycle management (RemoteContext) and wrapping (RemoteClient) for third party adapters

47 lines (46 loc) 1.5 kB
/** * Provides lifecycle hooks for RemoteClient instances added via addClient(client): * * - initialise() which calls client.initializeClient() on each client * * - cleanUp() which calls client.cleanUp() on each client. * */ export default class RemoteContext { constructor() { this.clients = []; this.runAllThrowFirst = async (onClient) => { // Collect cleanup ops const ops = this.clients.map((client) => onClient(client)); // Execute all and wait until all cleanup ops are complete const outcomes = await Promise.allSettled(ops); // Verify all cleanups were successful, otherwise throw for (let outcome of outcomes) { if (outcome.status === 'rejected') { throw outcome.reason; } } }; } /** * Add a client to have this RemoteContext manage initialisation and cleanup. * (chainable method) */ addClient(client) { this.clients.push(client); return this; } /** * Initialize all clients, synchronously in the order the were added */ async initialize() { await this.runAllThrowFirst((client) => client.initializeClient()); } /** * Cleanup all RemoteClients, in parallel. * @throws The error from the earliest failed cleanup in the list */ async cleanUp() { await this.runAllThrowFirst((client) => client.cleanUp()); } }