All files / src/system cache.ts

100% Statements 23/23
100% Branches 0/0
100% Functions 8/8
100% Lines 23/23

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69  19x 19x 19x   19x   19x                               118x 118x 118x       130x 130x   130x       118x   118x 472x 472x   472x         472x   472x   472x       602x       118x       602x       19x  
import Node from 'type/node';
import GenericTypeGenerator from 'system/node-generator/type/generic';
import AccountTypeGenerator from 'system/node-generator/type/account';
import SystemAccountGenerator from 'system/node-generator/account/system';
import { GeneratorConstructor } from 'system/node-generator';
import AnonymousAccountGenerator from 'system/node-generator/account/anonymous';
 
const GENERATORS: GeneratorConstructor[] = [
	GenericTypeGenerator,
	AccountTypeGenerator,
	SystemAccountGenerator,
	AnonymousAccountGenerator
];
 
interface NodeCache {
	[composite_key: string]: Node;
}
 
class SystemCache {
	private hostname: string;
	private nodes: NodeCache;
 
	public constructor(hostname: string) {
		this.hostname = hostname;
		this.nodes = {};
		this.generateNodes();
	}
 
	public fetchNode(type_id: string, node_id: string): Node | undefined {
		const cache_key = this.buildCacheKey(type_id, node_id);
		const nodes = this.getNodes();
 
		return nodes[cache_key];
	}
 
	private generateNodes(): void {
		const hostname = this.getHostname();
 
		GENERATORS.forEach((generator_constructor) => {
			const generator = new generator_constructor(hostname);
			const node = generator.generate();
 
			this.addNode(node);
		});
	}
 
	private addNode(node: Node): void {
		const cache_key = this.buildCacheKey(node.type_id, node.id);
 
		const nodes = this.getNodes();
 
		nodes[cache_key] = node;
	}
 
	private buildCacheKey(type_id: string, node_id: string): string {
		return `${type_id}/${node_id}`;
	}
 
	private getHostname(): string {
		return this.hostname;
	}
 
	private getNodes(): NodeCache {
		return this.nodes;
	}
}
 
export default SystemCache;