Commit 73c43685 authored by Tim Kinnane's avatar Tim Kinnane
Browse files

fix(docs): Configs and build restore docs generation

Now `yarn build` will complete test, coverage, compile and document
parent 71f6e279
......@@ -12,7 +12,8 @@
],
"exclude": [
"**/*.d.ts",
"**/*.spec.ts"
"**/*.spec.ts",
"src/config/*"
],
"reporter": [
"lcovonly",
......
TN:
SF:/Volumes/x/code/rocketchat-bot-driver/src/index.ts
FN:2,(anonymous_0)
FNF:1
FNH:0
FNDA:0,(anonymous_0)
DA:2,0
DA:3,0
DA:4,0
DA:5,0
DA:6,0
DA:7,0
DA:9,0
DA:10,0
DA:11,0
DA:12,0
LF:10
LH:0
BRDA:2,0,0,0
BRDA:2,0,1,0
BRDA:2,0,2,0
BRDA:3,1,0,0
BRDA:3,1,1,0
BRDA:3,2,0,0
BRDA:3,2,1,0
BRDA:5,3,0,0
BRDA:5,3,1,0
BRDA:5,4,0,0
BRDA:5,4,1,0
BRF:11
BRH:0
end_of_record
TN:
SF:/Volumes/x/code/rocketchat-bot-driver/src/lib/asteroidInterfaces.ts
FNF:0
FNH:0
DA:2,0
LF:1
LH:0
DA:1,1
DA:2,1
DA:3,1
LF:3
LH:3
BRF:0
BRH:0
end_of_record
......
/// <reference types="node" />
import { EventEmitter } from 'events';
/**
* Patch in mock Asteroid type
* @todo Update with typing from definately typed (when available)
*/
export interface IAsteroid extends EventEmitter {
ddp: {
on: (event: string, func: (doc: any) => void) => void;
};
connect: () => void;
disconnect: () => void;
call: (method: string, params: any) => any;
apply: (method: string, params: any[]) => any;
subscribe: (name: string, params: any) => any;
subscriptions: ISubscription[];
unsubscribe: (id: string) => void;
createUser: (options: IUserOptions) => Promise<string>;
loginWithPassword: (options: IUserOptions) => Promise<string>;
login: (params: any) => Promise<string>;
logout: () => void;
}
/**
* Patch in Asteroid subscription type
* @todo Update with typing from definately typed (when available)
*/
export interface ISubscription extends EventEmitter {
id: string;
}
/**
* Patch in Asteroid user options type
* @todo Update with typing from definately typed (when available)
*/
export interface IUserOptions {
username?: string;
email?: string;
password: string;
}
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
//# sourceMappingURL=asteroidInterfaces.js.map
\ No newline at end of file
{"version":3,"file":"asteroidInterfaces.js","sourceRoot":"","sources":["../../src/config/asteroidInterfaces.ts"],"names":[],"mappings":"","sourcesContent":["import { EventEmitter } from 'events'\n\n/**\n * Patch in mock Asteroid type\n * @todo Update with typing from definately typed (when available)\n */\nexport interface IAsteroid extends EventEmitter {\n ddp: { on: (event: string, func: (doc: any) => void) => void }\n connect: () => void,\n disconnect: () => void,\n call: (method: string, params: any) => any\n apply: (method: string, params: any[]) => any\n subscribe: (name: string, params: any) => any\n subscriptions: ISubscription[],\n unsubscribe: (id: string) => void,\n createUser: (options: IUserOptions) => Promise<string>,\n loginWithPassword: (options: IUserOptions) => Promise<string>,\n login: (params: any) => Promise<string>,\n logout: () => void\n}\n\n/**\n * Patch in Asteroid subscription type\n * @todo Update with typing from definately typed (when available)\n */\nexport interface ISubscription extends EventEmitter {\n id: string\n}\n\n/**\n * Patch in Asteroid user options type\n * @todo Update with typing from definately typed (when available)\n */\nexport interface IUserOptions {\n username?: string,\n email?: string,\n password: string\n}\n"]}
\ No newline at end of file
export * from './lib/Driver';
import * as driver from './lib/driver';
import * as methodCache from './lib/methodCache';
declare const _default: {
driver: typeof driver;
methodCache: typeof methodCache;
};
export default _default;
"use strict";
function __export(m) {
for (var p in m) if (!exports.hasOwnProperty(p)) exports[p] = m[p];
var __importStar = (this && this.__importStar) || function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k in mod) if (Object.hasOwnProperty.call(mod, k)) result[k] = mod[k];
result["default"] = mod;
return result;
}
Object.defineProperty(exports, "__esModule", { value: true });
__export(require("./lib/Driver"));
const driver = __importStar(require("./lib/driver"));
const methodCache = __importStar(require("./lib/methodCache"));
exports.default = {
driver,
methodCache
};
//# sourceMappingURL=index.js.map
\ No newline at end of file
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";;;;;AAAA,kCAA4B","sourcesContent":["export * from './lib/Driver'\n"]}
\ No newline at end of file
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";;;;;;;;;AAAA,qDAAsC;AACtC,+DAAgD;AAChD,kBAAe;IACb,MAAM;IACN,WAAW;CACZ,CAAA","sourcesContent":["import * as driver from './lib/driver'\nimport * as methodCache from './lib/methodCache'\nexport default {\n driver,\n methodCache\n}\n"]}
\ No newline at end of file
/// <reference types="node" />
import { EventEmitter } from 'events';
import { IAsteroid } from '../config/asteroidInterfaces';
/**
* Main interface for interacting with Rocket.Chat
* @param asteroid An Asteroid instance to connect to Meteor server
*/
export declare class Driver extends EventEmitter {
host: string;
private asteroid;
/**
* Creates a new driver instance with given options or defaults
* @param host Rocket.Chat instance Host URL:PORT (without protocol)
*/
constructor(host?: string);
* Connection options type
* @param host Rocket.Chat instance Host URL:PORT (without protocol)
* @param timeout How long to wait (ms) before abandonning connection
*/
export interface IOptions {
host?: string;
timeout?: number;
}
export declare const defaults: IOptions;
/**
* Error-first callback param type
*/
export interface ICallback {
(error: Error | null, result?: any): void;
}
/**
* Event Emitter for listening to connection
* @example
* import { driver } from 'rocketchat-bot-driver'
* driver.connect()
* driver.events.on('connected', () => console.log('driver connected'))
*/
export declare const events: EventEmitter;
/**
* An Asteroid instance for interacting with Rocket.Chat
*/
export declare let asteroid: IAsteroid;
/**
* Initialise asteroid instance with given options or defaults
* @example <caption>Use with callback</caption>
* import { driver } from 'rocketchat-bot-driver'
* driver.connect({}, (err, asteroid) => {
* if (err) throw err
* else constole.log(asteroid)
* })
* @example <caption>Using promise</caption>
* import { driver } from 'rocketchat-bot-driver'
* driver.connect()
* .then((asteroid) => {
* console.log(asteroid)
* })
* .catch((err) => {
* console.error(err)
* })
*/
export declare function connect(options?: IOptions, callback?: ICallback): Promise<any>;
......@@ -2,32 +2,76 @@
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
}
var __importStar = (this && this.__importStar) || function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k in mod) if (Object.hasOwnProperty.call(mod, k)) result[k] = mod[k];
result["default"] = mod;
return result;
}
Object.defineProperty(exports, "__esModule", { value: true });
// @ts-ignore // Asteroid is not typed
const asteroid_1 = require("asteroid");
const events_1 = require("events");
const ws_1 = __importDefault(require("ws"));
const methodCache = __importStar(require("./methodCache"));
const Asteroid = asteroid_1.createClass();
exports.defaults = {
host: 'localhost:3000',
timeout: 20 * 1000 // 20 seconds
};
/**
* Event Emitter for listening to connection
* @example
* import { driver } from 'rocketchat-bot-driver'
* driver.connect()
* driver.events.on('connected', () => console.log('driver connected'))
*/
exports.events = new events_1.EventEmitter();
/**
* Main interface for interacting with Rocket.Chat
* @param asteroid An Asteroid instance to connect to Meteor server
* Initialise asteroid instance with given options or defaults
* @example <caption>Use with callback</caption>
* import { driver } from 'rocketchat-bot-driver'
* driver.connect({}, (err, asteroid) => {
* if (err) throw err
* else constole.log(asteroid)
* })
* @example <caption>Using promise</caption>
* import { driver } from 'rocketchat-bot-driver'
* driver.connect()
* .then((asteroid) => {
* console.log(asteroid)
* })
* .catch((err) => {
* console.error(err)
* })
*/
class Driver extends events_1.EventEmitter {
/**
* Creates a new driver instance with given options or defaults
* @param host Rocket.Chat instance Host URL:PORT (without protocol)
*/
// @ts-ignore // host is unused (doesn't notice use in template literal)
constructor(host = 'localhost:3000') {
super();
this.host = host;
this.asteroid = new Asteroid({
endpoint: `ws://${host}/websocket`,
function connect(options = {}, callback) {
return new Promise((resolve, reject) => {
options = Object.assign(exports.defaults, options);
exports.asteroid = new Asteroid({
endpoint: `ws://${options.host}/websocket`,
SocketConstructor: ws_1.default
});
this.asteroid.on('connected', () => this.emit('connected'));
this.asteroid.on('reconnected', () => this.emit('reconnected'));
}
methodCache.use(exports.asteroid); // init instance for later caching method calls
exports.asteroid.on('connected', () => exports.events.emit('connected'));
exports.asteroid.on('reconnected', () => exports.events.emit('reconnected'));
let cancelled = false;
const rejectionTimeout = setTimeout(() => {
cancelled = true;
const err = new Error('Asteroid connection timeout');
// if no callback available, reject the promise
// else, return callback using "error-first-pattern"
return callback ? callback(err, exports.asteroid) : reject(err);
}, options.timeout);
exports.asteroid.once('connected', () => {
// cancel connection and don't resolve if already rejected
if (cancelled)
return exports.asteroid.disconnect();
clearTimeout(rejectionTimeout);
return (callback !== undefined) ? callback(null, exports.asteroid) : resolve(exports.asteroid);
});
});
}
exports.Driver = Driver;
//# sourceMappingURL=Driver.js.map
\ No newline at end of file
exports.connect = connect;
//# sourceMappingURL=driver.js.map
\ No newline at end of file
{"version":3,"file":"Driver.js","sourceRoot":"","sources":["../../src/lib/Driver.ts"],"names":[],"mappings":";;;;;AAAA,sCAAsC;AACtC,uCAAsC;AACtC,mCAAqC;AACrC,4CAAmB;AACnB,MAAM,QAAQ,GAAG,sBAAW,EAAE,CAAA;AAE9B;;;GAGG;AACH,YAAoB,SAAQ,qBAAY;IAGtC;;;OAGG;IACH,wEAAwE;IACxE,YAAoB,OAAO,gBAAgB;QACzC,KAAK,EAAE,CAAA;QADW,SAAI,GAAJ,IAAI,CAAmB;QAEzC,IAAI,CAAC,QAAQ,GAAG,IAAI,QAAQ,CAAC;YAC3B,QAAQ,EAAE,QAAQ,IAAI,YAAY;YAClC,iBAAiB,EAAE,YAAE;SACtB,CAAC,CAAA;QACF,IAAI,CAAC,QAAQ,CAAC,EAAE,CAAC,WAAW,EAAE,GAAG,EAAE,CAAC,IAAI,CAAC,IAAI,CAAC,WAAW,CAAC,CAAC,CAAA;QAC3D,IAAI,CAAC,QAAQ,CAAC,EAAE,CAAC,aAAa,EAAE,GAAG,EAAE,CAAC,IAAI,CAAC,IAAI,CAAC,aAAa,CAAC,CAAC,CAAA;IACjE,CAAC;CACF;AAjBD,wBAiBC","sourcesContent":["// @ts-ignore // Asteroid is not typed\nimport { createClass } from 'asteroid'\nimport { EventEmitter } from 'events'\nimport ws from 'ws'\nconst Asteroid = createClass()\n\n/**\n * Main interface for interacting with Rocket.Chat\n * @param asteroid An Asteroid instance to connect to Meteor server\n */\nexport class Driver extends EventEmitter {\n private asteroid: any /** @TODO update with Asteroid type (submit to tsd) */\n\n /**\n * Creates a new driver instance with given options or defaults\n * @param host Rocket.Chat instance Host URL:PORT (without protocol)\n */\n // @ts-ignore // host is unused (doesn't notice use in template literal)\n constructor (public host = 'localhost:3000') {\n super()\n this.asteroid = new Asteroid({\n endpoint: `ws://${host}/websocket`,\n SocketConstructor: ws\n })\n this.asteroid.on('connected', () => this.emit('connected'))\n this.asteroid.on('reconnected', () => this.emit('reconnected'))\n }\n}\n"]}
\ No newline at end of file
{"version":3,"file":"driver.js","sourceRoot":"","sources":["../../src/lib/driver.ts"],"names":[],"mappings":";;;;;;;;;;;;AAAA,sCAAsC;AACtC,uCAAsC;AACtC,mCAAqC;AACrC,4CAA0B;AAC1B,2DAA4C;AAE5C,MAAM,QAAQ,GAAG,sBAAW,EAAE,CAAA;AAWjB,QAAA,QAAQ,GAAa;IAChC,IAAI,EAAE,gBAAgB;IACtB,OAAO,EAAE,EAAE,GAAG,IAAI,CAAC,aAAa;CACjC,CAAA;AASD;;;;;;GAMG;AACU,QAAA,MAAM,GAAG,IAAI,qBAAY,EAAE,CAAA;AAOxC;;;;;;;;;;;;;;;;;GAiBG;AACH,iBAAyB,UAAoB,EAAE,EAAE,QAAoB;IACnE,MAAM,CAAC,IAAI,OAAO,CAAY,CAAC,OAAO,EAAE,MAAM,EAAE,EAAE;QAChD,OAAO,GAAG,MAAM,CAAC,MAAM,CAAC,gBAAQ,EAAE,OAAO,CAAC,CAAA;QAC1C,gBAAQ,GAAG,IAAI,QAAQ,CAAC;YACtB,QAAQ,EAAE,QAAQ,OAAO,CAAC,IAAI,YAAY;YAC1C,iBAAiB,EAAE,YAAS;SAC7B,CAAC,CAAA;QACF,WAAW,CAAC,GAAG,CAAC,gBAAQ,CAAC,CAAA,CAAC,+CAA+C;QACzE,gBAAQ,CAAC,EAAE,CAAC,WAAW,EAAE,GAAG,EAAE,CAAC,cAAM,CAAC,IAAI,CAAC,WAAW,CAAC,CAAC,CAAA;QACxD,gBAAQ,CAAC,EAAE,CAAC,aAAa,EAAE,GAAG,EAAE,CAAC,cAAM,CAAC,IAAI,CAAC,aAAa,CAAC,CAAC,CAAA;QAC5D,IAAI,SAAS,GAAG,KAAK,CAAA;QACrB,MAAM,gBAAgB,GAAG,UAAU,CAAC,GAAG,EAAE;YACvC,SAAS,GAAG,IAAI,CAAA;YAChB,MAAM,GAAG,GAAG,IAAI,KAAK,CAAC,6BAA6B,CAAC,CAAA;YACpD,+CAA+C;YAC/C,oDAAoD;YACpD,MAAM,CAAC,QAAQ,CAAC,CAAC,CAAC,QAAQ,CAAC,GAAG,EAAE,gBAAQ,CAAC,CAAC,CAAC,CAAC,MAAM,CAAC,GAAG,CAAC,CAAA;QACzD,CAAC,EAAE,OAAO,CAAC,OAAO,CAAC,CAAA;QACnB,gBAAQ,CAAC,IAAI,CAAC,WAAW,EAAE,GAAG,EAAE;YAC9B,0DAA0D;YAC1D,EAAE,CAAC,CAAC,SAAS,CAAC;gBAAC,MAAM,CAAC,gBAAQ,CAAC,UAAU,EAAE,CAAA;YAC3C,YAAY,CAAC,gBAAgB,CAAC,CAAA;YAC9B,MAAM,CAAC,CAAC,QAAQ,KAAK,SAAS,CAAC,CAAC,CAAC,CAAC,QAAQ,CAAC,IAAI,EAAE,gBAAQ,CAAC,CAAC,CAAC,CAAC,OAAO,CAAC,gBAAQ,CAAC,CAAA;QAChF,CAAC,CAAC,CAAA;IACJ,CAAC,CAAC,CAAA;AACJ,CAAC;AAzBD,0BAyBC","sourcesContent":["// @ts-ignore // Asteroid is not typed\nimport { createClass } from 'asteroid'\nimport { EventEmitter } from 'events'\nimport WebSocket from 'ws'\nimport * as methodCache from './methodCache'\nimport { IAsteroid } from '../config/asteroidInterfaces'\nconst Asteroid = createClass()\n\n/**\n * Connection options type\n * @param host Rocket.Chat instance Host URL:PORT (without protocol)\n * @param timeout How long to wait (ms) before abandonning connection\n */\nexport interface IOptions {\n host?: string,\n timeout?: number\n}\nexport const defaults: IOptions = {\n host: 'localhost:3000',\n timeout: 20 * 1000 // 20 seconds\n}\n\n/**\n * Error-first callback param type\n */\nexport interface ICallback {\n (error: Error | null, result?: any): void\n}\n\n/**\n * Event Emitter for listening to connection\n * @example\n * import { driver } from 'rocketchat-bot-driver'\n * driver.connect()\n * driver.events.on('connected', () => console.log('driver connected'))\n */\nexport const events = new EventEmitter()\n\n/**\n * An Asteroid instance for interacting with Rocket.Chat\n */\nexport let asteroid: IAsteroid\n\n/**\n * Initialise asteroid instance with given options or defaults\n * @example <caption>Use with callback</caption>\n * import { driver } from 'rocketchat-bot-driver'\n * driver.connect({}, (err, asteroid) => {\n * if (err) throw err\n * else constole.log(asteroid)\n * })\n * @example <caption>Using promise</caption>\n * import { driver } from 'rocketchat-bot-driver'\n * driver.connect()\n * .then((asteroid) => {\n * console.log(asteroid)\n * })\n * .catch((err) => {\n * console.error(err)\n * })\n */\nexport function connect (options: IOptions = {}, callback?: ICallback): Promise<any> {\n return new Promise<IAsteroid>((resolve, reject) => {\n options = Object.assign(defaults, options)\n asteroid = new Asteroid({\n endpoint: `ws://${options.host}/websocket`,\n SocketConstructor: WebSocket\n })\n methodCache.use(asteroid) // init instance for later caching method calls\n asteroid.on('connected', () => events.emit('connected'))\n asteroid.on('reconnected', () => events.emit('reconnected'))\n let cancelled = false\n const rejectionTimeout = setTimeout(() => {\n cancelled = true\n const err = new Error('Asteroid connection timeout')\n // if no callback available, reject the promise\n // else, return callback using \"error-first-pattern\"\n return callback ? callback(err, asteroid) : reject(err)\n }, options.timeout)\n asteroid.once('connected', () => {\n // cancel connection and don't resolve if already rejected\n if (cancelled) return asteroid.disconnect()\n clearTimeout(rejectionTimeout)\n return (callback !== undefined) ? callback(null, asteroid) : resolve(asteroid)\n })\n })\n}\n"]}
\ No newline at end of file
/// <reference types="lru-cache" />
import LRU from 'lru-cache';
export declare const results: Map<string, LRU.Cache<string, any>>;
export declare const defaults: LRU.Options;
/**
* Set the instance to call methods on, with cached results
* @param instanceToUse Instance of a class
*/
export declare function use(instanceToUse: object): void;
/**
* Setup a cache for a method call
* @param method Method name, for index of cached results
* @param max Maximum size of cache
* @param maxAge Maximum age of cache
*/
export declare function create(method: string, options?: LRU.Options): LRU.Cache<string, any> | undefined;
/**
* Get results of a prior method call or call and cache - always a promise
* @param method Method name, to call on instance in use
* @param key Key to pass to method call and save results against
*/
export declare function call(method: string, key: string): Promise<any>;
/**
* Get results of a prior method call
* @param method Method name for cache to get
* @param key Key for method result set to return
*/
export declare function get(method: string, key: string): any;
/**
* Clear a cached method call's results (all or only for given key)
* @param method Method name for cache to clear
* @param key Key for method result set to clear
*/
export declare function clear(method: string, key?: string): void;
/**
* Clear cached results for all methods
*/
export declare function clearAll(): void;
"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
}
Object.defineProperty(exports, "__esModule", { value: true });
const lru_cache_1 = __importDefault(require("lru-cache"));
/** @TODO: Remove ! post-fix expression when TypeScript #9619 resolved */
let instance;
exports.results = new Map();
exports.defaults = {
max: 100,
maxAge: 300 * 1000
};
/**
* Set the instance to call methods on, with cached results
* @param instanceToUse Instance of a class
*/
function use(instanceToUse) {
instance = instanceToUse;
}
exports.use = use;
/**
* Setup a cache for a method call
* @param method Method name, for index of cached results
* @param max Maximum size of cache
* @param maxAge Maximum age of cache
*/
function create(method, options = {}) {
options = Object.assign(exports.defaults, options);
exports.results.set(method, new lru_cache_1.default(options));
return exports.results.get(method);
}
exports.create = create;
/**
* Get results of a prior method call or call and cache - always a promise
* @param method Method name, to call on instance in use
* @param key Key to pass to method call and save results against
*/
function call(method, key) {
if (!exports.results.has(method))
create(method); // create as needed
const methodCache = exports.results.get(method);
let callResults;
if (methodCache.has(key)) {
// return from cache if key has been used on method before
callResults = methodCache.get(key);
}
else {
// call and cache for next time, returning results
callResults = instance[method].call(instance, key);
methodCache.set(key, callResults);
}
return Promise.resolve(callResults);
}
exports.call = call;
/**
* Get results of a prior method call
* @param method Method name for cache to get
* @param key Key for method result set to return
*/
function get(method, key) {
if (exports.results.has(method))
return exports.results.get(method).get(key);
}
exports.get = get;
/**
* Clear a cached method call's results (all or only for given key)
* @param method Method name for cache to clear
* @param key Key for method result set to clear
*/
function clear(method, key) {
if (exports.results.has(method)) {
if (key)
return exports.results.get(method).del(key);
else
return exports.results.get(method).reset();
}
}
exports.clear = clear;
/**
* Clear cached results for all methods
*/
function clearAll() {
exports.results.forEach((cache) => cache.reset());
}
exports.clearAll = clearAll;
//# sourceMappingURL=methodCache.js.map
\ No newline at end of file
{"version":3,"file":"methodCache.js","sourceRoot":"","sources":["../../src/lib/methodCache.ts"],"names":[],"mappings":";;;;;AAAA,0DAA2B;AAE3B,yEAAyE;AACzE,IAAI,QAAa,CAAA;AACJ,QAAA,OAAO,GAAwC,IAAI,GAAG,EAAE,CAAA;AACxD,QAAA,QAAQ,GAAgB;IACnC,GAAG,EAAE,GAAG;IACR,MAAM,EAAE,GAAG,GAAG,IAAI;CACnB,CAAA;AAED;;;GAGG;AACH,aAAqB,aAAqB;IACxC,QAAQ,GAAG,aAAa,CAAA;AAC1B,CAAC;AAFD,kBAEC;AAED;;;;;GAKG;AACH,gBAAwB,MAAc,EAAE,UAAuB,EAAE;IAC/D,OAAO,GAAG,MAAM,CAAC,MAAM,CAAC,gBAAQ,EAAE,OAAO,CAAC,CAAA;IAC1C,eAAO,CAAC,GAAG,CAAC,MAAM,EAAE,IAAI,mBAAG,CAAC,OAAO,CAAC,CAAC,CAAA;IACrC,MAAM,CAAC,eAAO,CAAC,GAAG,CAAC,MAAM,CAAC,CAAA;AAC5B,CAAC;AAJD,wBAIC;AAED;;;;GAIG;AACH,cAAsB,MAAc,EAAE,GAAW;IAC/C,EAAE,CAAC,CAAC,CAAC,eAAO,CAAC,GAAG,CAAC,MAAM,CAAC,CAAC;QAAC,MAAM,CAAC,MAAM,CAAC,CAAA,CAAC,mBAAmB;IAC5D,MAAM,WAAW,GAAG,eAAO,CAAC,GAAG,CAAC,MAAM,CAAE,CAAA;IACxC,IAAI,WAAW,CAAA;IAEf,EAAE,CAAC,CAAC,WAAW,CAAC,GAAG,CAAC,GAAG,CAAC,CAAC,CAAC,CAAC;QACzB,0DAA0D;QAC1D,WAAW,GAAG,WAAW,CAAC,GAAG,CAAC,GAAG,CAAC,CAAA;IACpC,CAAC;IAAC,IAAI,CAAC,CAAC;QACN,kDAAkD;QAClD,WAAW,GAAG,QAAQ,CAAC,MAAM,CAAC,CAAC,IAAI,CAAC,QAAQ,EAAE,GAAG,CAAC,CAAA;QAClD,WAAW,CAAC,GAAG,CAAC,GAAG,EAAE,WAAW,CAAC,CAAA;IACnC,CAAC;IACD,MAAM,CAAC,OAAO,CAAC,OAAO,CAAC,WAAW,CAAC,CAAA;AACrC,CAAC;AAdD,oBAcC;AAED;;;;GAIG;AACH,aAAqB,MAAc,EAAE,GAAW;IAC9C,EAAE,CAAC,CAAC,eAAO,CAAC,GAAG,CAAC,MAAM,CAAC,CAAC;QAAC,MAAM,CAAC,eAAO,CAAC,GAAG,CAAC,MAAM,CAAE,CAAC,GAAG,CAAC,GAAG,CAAC,CAAA;AAC/D,CAAC;AAFD,kBAEC;AAED;;;;GAIG;AACH,eAAuB,MAAc,EAAE,GAAY;IACjD,EAAE,CAAC,CAAC,eAAO,CAAC,GAAG,CAAC,MAAM,CAAC,CAAC,CAAC,CAAC;QACxB,EAAE,CAAC,CAAC,GAAG,CAAC;YAAC,MAAM,CAAC,eAAO,CAAC,GAAG,CAAC,MAAM,CAAE,CAAC,GAAG,CAAC,GAAG,CAAC,CAAA;QAC7C,IAAI;YAAC,MAAM,CAAC,eAAO,CAAC,GAAG,CAAC,MAAM,CAAE,CAAC,KAAK,EAAE,CAAA;IAC1C,CAAC;AACH,CAAC;AALD,sBAKC;AAED;;GAEG;AACH;IACE,eAAO,CAAC,OAAO,CAAC,CAAC,KAAK,EAAE,EAAE,CAAC,KAAK,CAAC,KAAK,EAAE,CAAC,CAAA;AAC3C,CAAC;AAFD,4BAEC","sourcesContent":["import LRU from 'lru-cache'\n\n/** @TODO: Remove ! post-fix expression when TypeScript #9619 resolved */\nlet instance: any\nexport const results: Map<string, LRU.Cache<string, any>> = new Map()\nexport const defaults: LRU.Options = {\n max: 100,\n maxAge: 300 * 1000\n}\n\n/**\n * Set the instance to call methods on, with cached results\n * @param instanceToUse Instance of a class\n */\nexport function use (instanceToUse: object) {\n instance = instanceToUse\n}\n\n/**\n * Setup a cache for a method call\n * @param method Method name, for index of cached results\n * @param max Maximum size of cache\n * @param maxAge Maximum age of cache\n */\nexport function create (method: string, options: LRU.Options = {}) {\n options = Object.assign(defaults, options)\n results.set(method, new LRU(options))\n return results.get(method)\n}\n\n/**\n * Get results of a prior method call or call and cache - always a promise\n * @param method Method name, to call on instance in use\n * @param key Key to pass to method call and save results against\n */\nexport function call (method: string, key: string) {\n if (!results.has(method)) create(method) // create as needed\n const methodCache = results.get(method)!\n let callResults\n\n if (methodCache.has(key)) {\n // return from cache if key has been used on method before\n callResults = methodCache.get(key)\n } else {\n // call and cache for next time, returning results\n callResults = instance[method].call(instance, key)\n methodCache.set(key, callResults)\n }\n return Promise.resolve(callResults)\n}\n\n/**\n * Get results of a prior method call\n * @param method Method name for cache to get\n * @param key Key for method result set to return\n */\nexport function get (method: string, key: string) {\n if (results.has(method)) return results.get(method)!.get(key)\n}\n\n/**\n * Clear a cached method call's results (all or only for given key)\n * @param method Method name for cache to clear\n * @param key Key for method result set to clear\n */\nexport function clear (method: string, key?: string) {\n if (results.has(method)) {\n if (key) return results.get(method)!.del(key)\n else return results.get(method)!.reset()\n }\n}\n\n/**\n * Clear cached results for all methods\n */\nexport function clearAll () {\n results.forEach((cache) => cache.reset())\n}\n"]}
\ No newline at end of file
This diff is collapsed.
This diff is collapsed.
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment