UNPKG

@opentap/runner-client

Version:

This is the web client for the OpenTAP Runner.

184 lines (183 loc) 9.23 kB
import { JSONCodec } from 'nats.ws'; import { RunnerEvent, TestPlanRunCompletedEventArgs, TestPlanRunStartEventArgs, TestStepRunCompletedEventArgs, TestStepRunStartEventArgs, AssetUpdatedEvent, } from './DTOs'; import { BaseClient } from './BaseClient'; export class SystemClient extends BaseClient { constructor(baseSubject, options) { super(baseSubject, options); } /** * Subscribe to the lifetime event. * @param listener * @param {SubscriptionOptions} options * @returns {Subscription} */ subscribeLifetimeEventListener(listener, options) { const subject = 'Runner.*.Events.Lifetime'; return this.subscribe(subject, Object.assign(Object.assign({}, options), { callback: (error, message) => { if (error) { listener(this.natsErrorHandler(error, subject), null); return; } try { const jsonCodec = JSONCodec(); const data = jsonCodec.decode(message === null || message === void 0 ? void 0 : message.data); listener(null, data); } catch (error) { listener(this.natsErrorHandler(error, subject), null); } } })); } /** * Subscribe to the runner updated events for the given Runner ID. * Wildcard can be used to receive all runner updated events. * @param {string} runnerId * @param {(error:ErrorResponse|null,message:RunnerEvent|null)=>void} listener * @param {SubscriptionOptions} options? */ subscribeRunnerUpdatedEvents(runnerId, listener, options) { const subject = `RunnerRegistry.${runnerId}.Events.Updated`; return this.subscribe(subject, Object.assign(Object.assign({}, options), { callback: (error, message) => { if (error) { listener(this.natsErrorHandler(error, subject), null); return; } try { const jsonCodec = JSONCodec(); const data = RunnerEvent.fromJS(jsonCodec.decode(message === null || message === void 0 ? void 0 : message.data)); listener(null, data); } catch (error) { listener(this.natsErrorHandler(error, subject), null); } } })); } /** * Subscribe to the test step run start events for the given Runner and Run ID. * Wildcard can be used for the Runner or Run ID to receive all test step run start events. * @param {string} runnerId * @param {string} runId * @param {(error:ErrorResponse|null,message:TestStepRunStartEventArgs|null)=>void} listener * @param {SubscriptionOptions} options? */ subscribeTestStepRunStartEventListener(runnerId, runId, listener, options) { const subject = `Runner.${runnerId}.Runs.${runId}.Events.TestStepRunStart`; return this.subscribe(subject, Object.assign(Object.assign({}, options), { callback: (error, message) => { if (error) { listener(this.natsErrorHandler(error, subject), null); return; } try { const jsonCodec = JSONCodec(); const testStepRunStartEventArgsJs = jsonCodec.decode(message === null || message === void 0 ? void 0 : message.data); const testStepRunStartEventArgs = TestStepRunStartEventArgs.fromJS(testStepRunStartEventArgsJs); listener(null, testStepRunStartEventArgs); } catch (error) { listener(this.natsErrorHandler(error, subject), null); } } })); } /** * Subscribe to the test step run completed events for the given Runner and Run ID. * Wildcard can be used for the Runner or Run ID to receive all test step run completed events. * @param {string} runnerId * @param {string} runId * @param {(error:ErrorResponse|null,message:TestStepRunStartEventArgs|null)=>void} listener * @param {SubscriptionOptions} options? */ subscribeTestStepRunCompletedEventListener(runnerId, runId, listener, options) { const subject = `Runner.${runnerId}.Runs.${runId}.Events.TestStepRunCompleted`; return this.subscribe(subject, Object.assign(Object.assign({}, options), { callback: (error, message) => { if (error) { listener(this.natsErrorHandler(error, subject), null); return; } try { const jsonCodec = JSONCodec(); const testStepRunCompletedEventArgsJs = jsonCodec.decode(message === null || message === void 0 ? void 0 : message.data); const testStepRunCompletedEventArgs = TestStepRunCompletedEventArgs.fromJS(testStepRunCompletedEventArgsJs); listener(null, testStepRunCompletedEventArgs); } catch (error) { listener(this.natsErrorHandler(error, subject), null); } } })); } /** * Subscribe to the test plan run start events for the given Runner and Run ID. * Wildcard can be used for the Runner or Run ID to receive all test plan run start events. * @param {string} runnerId * @param {string} runId * @param {(error:ErrorResponse|null,message:TestStepRunStartEventArgs|null)=>void} listener * @param {SubscriptionOptions} options? */ subscribeTestPlanRunStartEventListener(runnerId, runId, listener, options) { const subject = `Runner.${runnerId}.Runs.${runId}.Events.TestPlanRunStart`; return this.subscribe(subject, Object.assign(Object.assign({}, options), { callback: (error, message) => { if (error) { listener(this.natsErrorHandler(error, subject), null); return; } try { const jsonCodec = JSONCodec(); const testPlanRunStartEventArgsJs = jsonCodec.decode(message === null || message === void 0 ? void 0 : message.data); const testPlanRunStartEventArgs = TestPlanRunStartEventArgs.fromJS(testPlanRunStartEventArgsJs); listener(null, testPlanRunStartEventArgs); } catch (error) { listener(this.natsErrorHandler(error, subject), null); } } })); } /** * Subscribe to the test plan run completed events for the given Runner and Run ID. * Wildcard can be used for the Runner or Run ID to receive all test plan run completed events. * @param {string} runnerId * @param {string} runId * @param {(error:ErrorResponse|null,message:TestStepRunStartEventArgs|null)=>void} listener * @param {SubscriptionOptions} options? */ subscribeTestPlanRunCompletedEventListener(runnerId, runId, listener, options) { const subject = `Runner.${runnerId}.Runs.${runId}.Events.TestPlanRunCompleted`; return this.subscribe(subject, Object.assign(Object.assign({}, options), { callback: (error, message) => { if (error) { listener(this.natsErrorHandler(error, subject), null); return; } try { const jsonCodec = JSONCodec(); const testPlanRunCompletedEventArgsJs = jsonCodec.decode(message === null || message === void 0 ? void 0 : message.data); const testPlanRunCompletedEventArgs = TestPlanRunCompletedEventArgs.fromJS(testPlanRunCompletedEventArgsJs); listener(null, testPlanRunCompletedEventArgs); } catch (error) { listener(this.natsErrorHandler(error, subject), null); } } })); } /** * Subscribe to the asset update events for the given Runner ID. * Wildcard can be used to receive all asset update events. * @param {string} runnerId * @param {(error:ErrorResponse|null,message:AssetUpdatedEvent|null)=>void} listener * @param {SubscriptionOptions} options? */ subscribeAssetUpdatedEvents(runnerId, listener, options) { const subject = `RunnerRegistry.${runnerId}.Events.AssetsUpdated`; return this.subscribe(subject, Object.assign(Object.assign({}, options), { callback: (error, message) => { if (error) { listener(this.natsErrorHandler(error, subject), null); return; } try { const jsonCodec = JSONCodec(); const data = AssetUpdatedEvent.fromJS(jsonCodec.decode(message === null || message === void 0 ? void 0 : message.data)); listener(null, data); } catch (error) { listener(this.natsErrorHandler(error, subject), null); } } })); } }