strata.js 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  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 DelegatingPStratumImplAPI {
  20. constructor (facade) {
  21. this.facade = facade;
  22. }
  23. get delegate () {
  24. return this.facade.delegate;
  25. }
  26. }
  27. export class DelegatingPStratumImplType {
  28. constructor (facade) {
  29. this.facade = facade;
  30. }
  31. getImplAPI () {
  32. return new DelegatingPStratumImplAPI(this.facade);
  33. }
  34. }
  35. export class TerminalPStratumImplType {
  36. getImplAPI () {
  37. return {};
  38. }
  39. }
  40. export class PStratum {
  41. constructor (impl) {
  42. this.impl = impl;
  43. const implTypeClass = this.impl.constructor.TYPE
  44. ?? DelegatingPStratumImplType;
  45. this.implType = new implTypeClass(this);
  46. this.api = this.implType.getImplAPI();
  47. this.lookValue = null;
  48. this.seqNo = 0;
  49. this.history = [];
  50. // TODO: make this configurable
  51. this.historyOn = ! this.impl.reach;
  52. }
  53. setDelegate (delegate) {
  54. this.delegate = delegate;
  55. }
  56. look () {
  57. if ( this.looking ) {
  58. return this.lookValue;
  59. }
  60. this.looking = true;
  61. this.lookValue = this.impl.next(this.api);
  62. return this.lookValue;
  63. }
  64. next () {
  65. this.seqNo++;
  66. let toReturn;
  67. if ( this.looking ) {
  68. this.looking = false;
  69. toReturn = this.lookValue;
  70. } else {
  71. toReturn = this.impl.next(this.api);
  72. }
  73. this.history.push(toReturn.value);
  74. return toReturn;
  75. }
  76. fork () {
  77. const forkImpl = this.impl.fork(this.api);
  78. const fork = new PStratum(forkImpl);
  79. // DRY: sync state
  80. fork.looking = this.looking;
  81. fork.lookValue = this.lookValue;
  82. fork.seqNo = this.seqNo;
  83. fork.history = [...this.history];
  84. return fork;
  85. }
  86. join (friend) {
  87. // DRY: sync state
  88. this.looking = friend.looking;
  89. this.lookValue = friend.lookValue;
  90. this.seqNo = friend.seqNo;
  91. this.history = friend.history;
  92. this.impl.join(this.api, friend.impl);
  93. }
  94. reach (start, end) {
  95. if ( this.impl.reach ) {
  96. return this.impl.reach(this.api, start, end)
  97. }
  98. if ( this.historyOn ) {
  99. return this.history.slice(start, end);
  100. }
  101. }
  102. }