1. 1 : export { WrapperObject };
  2. 2 :
  3. 3 : /**
  4. 4 : * @class
  5. 5 : * @classdesc Contient une instance d'un objet, utile pour la création d'un singleton.
  6. 6 : * @template {!Tt} T
  7. 7 : */
  8. 8 : class WrapperObject {
  9. 9 : /**
  10. 10 : * Instance de la classe
  11. 11 : * @private
  12. 12 : * @type {?T}
  13. 13 : */
  14. 14 : #_instance = null;
  15. 15 : /**
  16. 16 : * Arguments par défaut
  17. 17 : * @private
  18. 18 : * @type {?{TypeOfItem: typeof T, args:any[]}}
  19. 19 : */
  20. 20 : #_args = null;
  21. 21 :
  22. 22 : /**
  23. 23 : * Constructeur de la classe
  24. 24 : * @param {typeof T} TypeOfItem Classe
  25. 25 : * @param {...any} args Argument pour instancier la classe
  26. 26 : */
  27. 27 : constructor(TypeOfItem, ...args) {
  28. 28 : this.#_args = {
  29. 29 : TypeOfItem,
  30. 30 : args,
  31. 31 : };
  32. 32 : }
  33. 33 :
  34. 34 : /**
  35. 35 : * Renvoie un instance de classe
  36. 36 : * @type {T}
  37. 37 : * @readonly
  38. 38 : */
  39. 39 : get Instance() {
  40. 40 : if (!this.#_instance) {
  41. 41 : const { TypeOfItem, args } = this.#_args;
  42. 42 : this.#_instance = new TypeOfItem(...args);
  43. 43 : this.#_args = undefined;
  44. 44 : }
  45. 45 :
  46. 46 : return this.#_instance;
  47. 47 : }
  48. 48 :
  49. 49 : /**
  50. 50 : * Contient une instance d'un objet, utile pour la création d'un singleton.
  51. 51 : * @static
  52. 52 : * @param {typeof T} typeofitem
  53. 53 : * @param {...any} args
  54. 54 : * @returns {WrapperObject<T>}
  55. 55 : */
  56. 56 : static Create(typeofitem, ...args) {
  57. 57 : return new WrapperObject(typeofitem, ...args);
  58. 58 : }
  59. 59 : }