AbstractCustomNode.ts 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. import { inject, injectable } from 'inversify';
  2. import { ServiceIdentifiers } from '../container/ServiceIdentifiers';
  3. import { TIdentifierNamesGeneratorFactory } from '../types/container/generators/TIdentifierNamesGeneratorFactory';
  4. import { TStatement } from '../types/node/TStatement';
  5. import { ICustomNode } from '../interfaces/custom-nodes/ICustomNode';
  6. import { IIdentifierNamesGenerator } from '../interfaces/generators/identifier-names-generators/IIdentifierNamesGenerator';
  7. import { IOptions } from '../interfaces/options/IOptions';
  8. import { IRandomGenerator } from '../interfaces/utils/IRandomGenerator';
  9. import { GlobalVariableTemplate1 } from '../templates/GlobalVariableTemplate1';
  10. import { GlobalVariableTemplate2 } from '../templates/GlobalVariableTemplate2';
  11. @injectable()
  12. export abstract class AbstractCustomNode implements ICustomNode {
  13. /**
  14. * @type {string[]}
  15. */
  16. private static readonly globalVariableTemplateFunctions: string[] = [
  17. GlobalVariableTemplate1(),
  18. GlobalVariableTemplate2()
  19. ];
  20. /**
  21. * @type {TStatement[] | null}
  22. */
  23. protected cachedNode: TStatement[] | null = null;
  24. /**
  25. * @type {IIdentifierNamesGenerator}
  26. */
  27. protected readonly identifierNamesGenerator: IIdentifierNamesGenerator;
  28. /**
  29. * @type {IOptions}
  30. */
  31. protected readonly options: IOptions;
  32. /**
  33. * @type {IRandomGenerator}
  34. */
  35. protected readonly randomGenerator: IRandomGenerator;
  36. /**
  37. * @param {TIdentifierNamesGeneratorFactory} identifierNamesGeneratorFactory
  38. * @param {IRandomGenerator} randomGenerator
  39. * @param {IOptions} options
  40. */
  41. constructor (
  42. @inject(ServiceIdentifiers.Factory__IIdentifierNamesGenerator)
  43. identifierNamesGeneratorFactory: TIdentifierNamesGeneratorFactory,
  44. @inject(ServiceIdentifiers.IRandomGenerator) randomGenerator: IRandomGenerator,
  45. @inject(ServiceIdentifiers.IOptions) options: IOptions
  46. ) {
  47. this.identifierNamesGenerator = identifierNamesGeneratorFactory(options);
  48. this.randomGenerator = randomGenerator;
  49. this.options = options;
  50. }
  51. /**
  52. * @param {unknown[]} args
  53. */
  54. public abstract initialize (...args: unknown[]): void;
  55. /**
  56. * @returns {TStatement[]}
  57. */
  58. public getNode (): TStatement[] {
  59. if (!this.cachedNode) {
  60. this.cachedNode = this.getNodeStructure();
  61. }
  62. return this.cachedNode;
  63. }
  64. /**
  65. * @returns {string}
  66. */
  67. protected getGlobalVariableTemplate (): string {
  68. return this.randomGenerator
  69. .getRandomGenerator()
  70. .pickone(AbstractCustomNode.globalVariableTemplateFunctions);
  71. }
  72. /**
  73. * @returns {TStatement[]}
  74. */
  75. protected abstract getNodeStructure (): TStatement[];
  76. }