AbstractCustomNode.ts 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. import { injectable, inject } from 'inversify';
  2. import { ServiceIdentifiers } from '../container/ServiceIdentifiers';
  3. import { ICustomNode } from '../interfaces/custom-nodes/ICustomNode';
  4. import { IOptions } from '../interfaces/options/IOptions';
  5. import { TStatement } from '../types/node/TStatement';
  6. import { NodeUtils } from '../node/NodeUtils';
  7. @injectable()
  8. export abstract class AbstractCustomNode implements ICustomNode {
  9. /**
  10. * @type {string}
  11. */
  12. protected cachedCode: string;
  13. /**
  14. * @type {TStatement[]}
  15. */
  16. protected cachedNode: TStatement[];
  17. /**
  18. * @type {IOptions}
  19. */
  20. protected readonly options: IOptions;
  21. /**
  22. * @param options
  23. */
  24. constructor (
  25. @inject(ServiceIdentifiers.IOptions) options: IOptions
  26. ) {
  27. this.options = options;
  28. }
  29. /**
  30. * @param args
  31. */
  32. public abstract initialize (...args: any[]): void;
  33. /**
  34. * @returns {string}
  35. */
  36. public getCode (): string {
  37. if (!this.cachedCode) {
  38. this.cachedCode = NodeUtils.convertStructureToCode(this.getNode());
  39. }
  40. return this.cachedCode;
  41. }
  42. /**
  43. * @returns {TStatement[]}
  44. */
  45. public getNode (): TStatement[] {
  46. if (!this.cachedNode) {
  47. this.cachedNode = this.getNodeStructure();
  48. }
  49. return this.cachedNode;
  50. }
  51. /**
  52. * @returns {TStatement[]}
  53. */
  54. protected abstract getNodeStructure (): TStatement[];
  55. }