Skip to content

Commit

Permalink
feat(client): cache DAOs accessed through the Client API (JS-38)
Browse files Browse the repository at this point in the history
  • Loading branch information
Benjamin Reed committed Jul 31, 2019
1 parent af75a11 commit 76c9205
Showing 1 changed file with 36 additions and 9 deletions.
45 changes: 36 additions & 9 deletions src/Client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,16 @@ import {Category} from 'typescript-logging';
import {IHasHTTP} from './api/IHasHTTP';
import {IOnmsHTTP} from './api/IOnmsHTTP';

import {OnmsAuthConfig} from './api/OnmsAuthConfig';
import {OnmsHTTPOptions} from './api/OnmsHTTPOptions';
import {OnmsError} from './api/OnmsError';
import {OnmsResult} from './api/OnmsResult';
import {OnmsVersion} from './api/OnmsVersion';
import {ServerType, ServerTypes} from './api/ServerType';
import {ServerTypes} from './api/ServerType';
import {TicketerConfig} from './api/TicketerConfig';

import {OnmsServer} from './api/OnmsServer';
import {ServerMetadata} from './api/ServerMetadata';

import {BaseDAO} from './dao/BaseDAO';
import {AlarmDAO} from './dao/AlarmDAO';
import {EventDAO} from './dao/EventDAO';
import {FlowDAO} from './dao/FlowDAO';
Expand Down Expand Up @@ -109,6 +108,12 @@ export class Client implements IHasHTTP {
/** The remote server to connect to */
public server?: OnmsServer;

/**
* A cache of initialized DAOs, kept until server configuration changes
* @hidden
*/
private daos = new Map() as Map<string, BaseDAO>;

/**
* Construct a new OpenNMS client.
* @constructor
Expand Down Expand Up @@ -148,27 +153,49 @@ export class Client implements IHasHTTP {
}

/** Get an alarm DAO for querying alarms. */
public alarms() {
return new AlarmDAO(this);
public alarms(): AlarmDAO {
return this.getDao('alarms', AlarmDAO) as AlarmDAO;
}

/** Get an event DAO for querying events. */
public events() {
return new EventDAO(this);
return this.getDao('events', EventDAO) as EventDAO;
}

/** Get a node DAO for querying nodes. */
public nodes() {
return new NodeDAO(this);
return this.getDao('nodes', NodeDAO) as NodeDAO;
}

/** Get a flow DAO for querying flows. */
public flows() {
return new FlowDAO(this);
return this.getDao('flows', FlowDAO) as FlowDAO;
}

/** Get a situationFeedback DAO for submitting and querying correlation feedback. */
public situationfeedback() {
return new SituationFeedbackDAO(this);
return this.getDao('situationfeedback', SituationFeedbackDAO) as SituationFeedbackDAO;
}

/**
* A convenience method to validate cached DAOs and create them if they don't exist
* @hidden
* @param key a unique key used for caching the DAO
* @param daoClass the DAO class to retrieve or create
*/
private getDao(
key: string,
daoClass: typeof AlarmDAO | typeof EventDAO | typeof NodeDAO | typeof FlowDAO | typeof SituationFeedbackDAO,
) {
const existing = this.daos.get(key);
if (existing) {
if (existing.server && existing.server.equals(this.server)) {
return existing;
}
}
const dao = new daoClass(this);
dao.http = this.http;
this.daos.set(key, dao);
return dao;
}
}

0 comments on commit 76c9205

Please sign in to comment.