No puede seleccionar más de 25 temas Los temas deben comenzar con una letra o número, pueden incluir guiones ('-') y pueden tener hasta 35 caracteres de largo.
 
 
 

202 líneas
3.2 KiB

  1. module.exports = {
  2. type: 'syncer',
  3. locked: false,
  4. buffer: [],
  5. o: {
  6. components: []
  7. },
  8. oSelf: {
  9. components: []
  10. },
  11. reset: function () {
  12. this.o = {
  13. components: []
  14. };
  15. this.oSelf = {
  16. components: []
  17. };
  18. this.locked = false;
  19. this.buffer.forEach(q => {
  20. const [ method, ...rest ] = q;
  21. this[method].apply(this, rest);
  22. });
  23. this.buffer = [];
  24. },
  25. queue: function (args) {
  26. this.buffer.push(args);
  27. },
  28. get: function (self) {
  29. let o = this.o;
  30. if (self)
  31. o = this.oSelf;
  32. let keys = Object.keys(o);
  33. if (o.components.length === 0) {
  34. if (keys.length === 1)
  35. return null;
  36. delete o.components;
  37. }
  38. o.id = this.obj.id;
  39. return o;
  40. },
  41. set: function (self, cpnType, property, value) {
  42. if (this.locked) {
  43. this.queue(['set', self, cpnType, property, value]);
  44. return;
  45. }
  46. let o = this.o;
  47. if (self)
  48. o = this.oSelf;
  49. if (cpnType) {
  50. let cpn = o.components.find(c => (c.type === cpnType));
  51. if (!cpn) {
  52. cpn = {
  53. type: cpnType
  54. };
  55. o.components.push(cpn);
  56. }
  57. cpn[property] = value;
  58. } else
  59. o[property] = value;
  60. },
  61. setComponent: function (self, cpnType, cpn) {
  62. if (this.locked) {
  63. this.queue(['setComponent', self, cpnType, cpn]);
  64. return;
  65. }
  66. let o = this.o;
  67. if (self)
  68. o = this.oSelf;
  69. let exists = o.components.find(c => c.type === cpnType);
  70. if (exists)
  71. extend(exists, cpn);
  72. else
  73. o.components.push(cpn);
  74. },
  75. setObject: function (self, cpnType, object, property, value) {
  76. if (this.locked) {
  77. this.queue(['setObject', self, cpnType, object, property, value]);
  78. return;
  79. }
  80. let o = this.o;
  81. if (self)
  82. o = this.oSelf;
  83. let cpn = o.components.find(c => (c.type === cpnType));
  84. if (!cpn) {
  85. cpn = {
  86. type: cpnType
  87. };
  88. o.components.push(cpn);
  89. }
  90. let obj = cpn[object];
  91. if (!obj) {
  92. obj = {};
  93. cpn[object] = obj;
  94. }
  95. obj[property] = value;
  96. },
  97. setArray: function (self, cpnType, property, value, noDuplicate) {
  98. if (this.locked) {
  99. this.queue(['setArray', self, cpnType, property, value, noDuplicate]);
  100. return;
  101. }
  102. let o = this.o;
  103. if (self)
  104. o = this.oSelf;
  105. let cpn = o.components.find(c => (c.type === cpnType));
  106. if (!cpn) {
  107. cpn = {
  108. type: cpnType
  109. };
  110. o.components.push(cpn);
  111. }
  112. if (!cpn[property])
  113. cpn[property] = [];
  114. if ((noDuplicate) && (cpn[property].find(f => (f === value))))
  115. return;
  116. cpn[property].push(value);
  117. },
  118. setSelfArray: function (self, property, value) {
  119. if (this.locked) {
  120. this.queue(['setSelfArray', self, property, value]);
  121. return;
  122. }
  123. let o = this.o;
  124. if (self)
  125. o = this.oSelf;
  126. if (!o.has(property))
  127. o[property] = [];
  128. o[property].push(value);
  129. },
  130. delete: function (self, cpnType, property) {
  131. let o = this.o;
  132. if (self)
  133. o = this.oSelf;
  134. if (cpnType) {
  135. let cpn = o.components.find(c => (c.type === cpnType));
  136. if (!cpn)
  137. return;
  138. delete cpn[property];
  139. } else
  140. delete o[property];
  141. },
  142. deleteFromArray: function (self, cpnType, property, cbMatch) {
  143. let o = this.o;
  144. if (self)
  145. o = this.oSelf;
  146. let cpn = o.components.find(c => (c.type === cpnType));
  147. if (!cpn)
  148. return;
  149. else if (!cpn[property])
  150. return;
  151. cpn[property].spliceWhere(cbMatch);
  152. }
  153. };