context.js 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. /*
  2. * Copyright (C) 2024 Puter Technologies Inc.
  3. *
  4. * This file is part of Phoenix Shell.
  5. *
  6. * Phoenix Shell is free software: you can redistribute it and/or modify
  7. * it under the terms of the GNU Affero General Public License as published
  8. * by the Free Software Foundation, either version 3 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU Affero General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Affero General Public License
  17. * along with this program. If not, see <https://www.gnu.org/licenses/>.
  18. */
  19. export class Context {
  20. constructor (values) {
  21. for ( const k in values ) this[k] = values[k];
  22. }
  23. sub (newValues) {
  24. if ( newValues === undefined ) newValues = {};
  25. const sub = Object.create(this);
  26. const alreadyApplied = {};
  27. for ( const k in sub ) {
  28. if ( sub[k] instanceof Context ) {
  29. const newValuesForK =
  30. newValues.hasOwnProperty(k)
  31. ? newValues[k] : undefined;
  32. sub[k] = sub[k].sub(newValuesForK);
  33. alreadyApplied[k] = true;
  34. }
  35. }
  36. for ( const k in newValues ) {
  37. if ( alreadyApplied[k] ) continue;
  38. sub[k] = newValues[k];
  39. }
  40. return sub;
  41. }
  42. }