base.js 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. /*---------------------------------------------------------------------------------------------
  2. * Copyright (c) Microsoft Corporation. All rights reserved.
  3. * Licensed under the MIT License. See License.txt in the project root for license information.
  4. *--------------------------------------------------------------------------------------------*/
  5. import { getLogger } from './logging.js';
  6. let _derived;
  7. /**
  8. * @internal
  9. * This is to allow splitting files.
  10. */
  11. export function _setDerived(derived) {
  12. _derived = derived;
  13. }
  14. export class ConvenientObservable {
  15. get TChange() { return null; }
  16. reportChanges() {
  17. this.get();
  18. }
  19. /** @sealed */
  20. read(reader) {
  21. if (reader) {
  22. return reader.readObservable(this);
  23. }
  24. else {
  25. return this.get();
  26. }
  27. }
  28. /** @sealed */
  29. map(fn) {
  30. return _derived(() => {
  31. const name = getFunctionName(fn);
  32. return name !== undefined ? name : `${this.debugName} (mapped)`;
  33. }, (reader) => fn(this.read(reader), reader));
  34. }
  35. }
  36. export class BaseObservable extends ConvenientObservable {
  37. constructor() {
  38. super(...arguments);
  39. this.observers = new Set();
  40. }
  41. /** @sealed */
  42. addObserver(observer) {
  43. const len = this.observers.size;
  44. this.observers.add(observer);
  45. if (len === 0) {
  46. this.onFirstObserverAdded();
  47. }
  48. }
  49. /** @sealed */
  50. removeObserver(observer) {
  51. const deleted = this.observers.delete(observer);
  52. if (deleted && this.observers.size === 0) {
  53. this.onLastObserverRemoved();
  54. }
  55. }
  56. onFirstObserverAdded() { }
  57. onLastObserverRemoved() { }
  58. }
  59. export function transaction(fn, getDebugName) {
  60. var _a, _b;
  61. const tx = new TransactionImpl(fn, getDebugName);
  62. try {
  63. (_a = getLogger()) === null || _a === void 0 ? void 0 : _a.handleBeginTransaction(tx);
  64. fn(tx);
  65. }
  66. finally {
  67. tx.finish();
  68. (_b = getLogger()) === null || _b === void 0 ? void 0 : _b.handleEndTransaction();
  69. }
  70. }
  71. export class TransactionImpl {
  72. constructor(fn, _getDebugName) {
  73. this.fn = fn;
  74. this._getDebugName = _getDebugName;
  75. this.updatingObservers = [];
  76. }
  77. getDebugName() {
  78. if (this._getDebugName) {
  79. return this._getDebugName();
  80. }
  81. return getFunctionName(this.fn);
  82. }
  83. updateObserver(observer, observable) {
  84. this.updatingObservers.push({ observer, observable });
  85. observer.beginUpdate(observable);
  86. }
  87. finish() {
  88. const updatingObservers = this.updatingObservers;
  89. // Prevent anyone from updating observers from now on.
  90. this.updatingObservers = null;
  91. for (const { observer, observable } of updatingObservers) {
  92. observer.endUpdate(observable);
  93. }
  94. }
  95. }
  96. export function getFunctionName(fn) {
  97. const fnSrc = fn.toString();
  98. // Pattern: /** @description ... */
  99. const regexp = /\/\*\*\s*@description\s*([^*]*)\*\//;
  100. const match = regexp.exec(fnSrc);
  101. const result = match ? match[1] : undefined;
  102. return result === null || result === void 0 ? void 0 : result.trim();
  103. }
  104. export function observableValue(name, initialValue) {
  105. return new ObservableValue(name, initialValue);
  106. }
  107. export class ObservableValue extends BaseObservable {
  108. constructor(debugName, initialValue) {
  109. super();
  110. this.debugName = debugName;
  111. this._value = initialValue;
  112. }
  113. get() {
  114. return this._value;
  115. }
  116. set(value, tx, change) {
  117. var _a;
  118. if (this._value === value) {
  119. return;
  120. }
  121. let _tx;
  122. if (!tx) {
  123. tx = _tx = new TransactionImpl(() => { }, () => `Setting ${this.debugName}`);
  124. }
  125. try {
  126. const oldValue = this._value;
  127. this._setValue(value);
  128. (_a = getLogger()) === null || _a === void 0 ? void 0 : _a.handleObservableChanged(this, { oldValue, newValue: value, change, didChange: true });
  129. for (const observer of this.observers) {
  130. tx.updateObserver(observer, this);
  131. observer.handleChange(this, change);
  132. }
  133. }
  134. finally {
  135. if (_tx) {
  136. _tx.finish();
  137. }
  138. }
  139. }
  140. toString() {
  141. return `${this.debugName}: ${this._value}`;
  142. }
  143. _setValue(newValue) {
  144. this._value = newValue;
  145. }
  146. }
  147. export function disposableObservableValue(name, initialValue) {
  148. return new DisposableObservableValue(name, initialValue);
  149. }
  150. export class DisposableObservableValue extends ObservableValue {
  151. _setValue(newValue) {
  152. if (this._value === newValue) {
  153. return;
  154. }
  155. if (this._value) {
  156. this._value.dispose();
  157. }
  158. this._value = newValue;
  159. }
  160. dispose() {
  161. var _a;
  162. (_a = this._value) === null || _a === void 0 ? void 0 : _a.dispose();
  163. }
  164. }