DependenciesBlock.js 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. /*
  2. MIT License http://www.opensource.org/licenses/mit-license.php
  3. Author Tobias Koppers @sokra
  4. */
  5. "use strict";
  6. const DependenciesBlockVariable = require("./DependenciesBlockVariable");
  7. function disconnect(i) {
  8. i.disconnect();
  9. }
  10. function unseal(i) {
  11. i.unseal();
  12. }
  13. class DependenciesBlock {
  14. constructor() {
  15. this.dependencies = [];
  16. this.blocks = [];
  17. this.variables = [];
  18. }
  19. addBlock(block) {
  20. this.blocks.push(block);
  21. block.parent = this;
  22. }
  23. addVariable(name, expression, dependencies) {
  24. for(let v of this.variables) {
  25. if(v.name === name && v.expression === expression) {
  26. return;
  27. }
  28. }
  29. this.variables.push(new DependenciesBlockVariable(name, expression, dependencies));
  30. }
  31. addDependency(dependency) {
  32. this.dependencies.push(dependency);
  33. }
  34. updateHash(hash) {
  35. function updateHash(i) {
  36. i.updateHash(hash);
  37. }
  38. this.dependencies.forEach(updateHash);
  39. this.blocks.forEach(updateHash);
  40. this.variables.forEach(updateHash);
  41. }
  42. disconnect() {
  43. this.dependencies.forEach(disconnect);
  44. this.blocks.forEach(disconnect);
  45. this.variables.forEach(disconnect);
  46. }
  47. unseal() {
  48. this.blocks.forEach(unseal);
  49. }
  50. hasDependencies(filter) {
  51. if(filter) {
  52. if(this.dependencies.some(filter)) {
  53. return true;
  54. }
  55. } else {
  56. if(this.dependencies.length > 0) {
  57. return true;
  58. }
  59. }
  60. return this.blocks.concat(this.variables).some(item => item.hasDependencies(filter));
  61. }
  62. sortItems() {
  63. this.blocks.forEach(block => block.sortItems());
  64. }
  65. }
  66. module.exports = DependenciesBlock;