watcher-helper.js 3.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. "use strict";
  2. /*!
  3. * devextreme-angular
  4. * Version: 19.1.16
  5. * Build date: Tue Oct 18 2022
  6. *
  7. * Copyright (c) 2012 - 2022 Developer Express Inc. ALL RIGHTS RESERVED
  8. *
  9. * This software may be modified and distributed under the terms
  10. * of the MIT license. See the LICENSE file in the root of the project for details.
  11. *
  12. * https://github.com/DevExpress/devextreme-angular
  13. */
  14. Object.defineProperty(exports, "__esModule", { value: true });
  15. var core_1 = require("@angular/core");
  16. var commonUtils = require("devextreme/core/utils/common");
  17. var WatcherHelper = (function () {
  18. function WatcherHelper() {
  19. this._watchers = [];
  20. }
  21. WatcherHelper.prototype.getWatchMethod = function () {
  22. var _this = this;
  23. var watchMethod = function (valueGetter, valueChangeCallback, options) {
  24. var oldValue = valueGetter();
  25. options = options || {};
  26. if (!options.skipImmediate) {
  27. valueChangeCallback(oldValue);
  28. }
  29. var watcher = function () {
  30. var newValue = valueGetter();
  31. if (_this._isDifferentValues(oldValue, newValue, options.deep)) {
  32. valueChangeCallback(newValue);
  33. oldValue = newValue;
  34. }
  35. };
  36. _this._watchers.push(watcher);
  37. return function () {
  38. var index = _this._watchers.indexOf(watcher);
  39. if (index !== -1) {
  40. _this._watchers.splice(index, 1);
  41. }
  42. };
  43. };
  44. return watchMethod;
  45. };
  46. WatcherHelper.prototype._isDifferentValues = function (oldValue, newValue, deepCheck) {
  47. var comparableNewValue = this._toComparable(newValue);
  48. var comparableOldValue = this._toComparable(oldValue);
  49. var isObjectValues = comparableNewValue instanceof Object && comparableOldValue instanceof Object;
  50. if (deepCheck && isObjectValues) {
  51. return this._checkObjectsFields(newValue, oldValue);
  52. }
  53. return comparableNewValue !== comparableOldValue;
  54. };
  55. WatcherHelper.prototype._toComparable = function (value) {
  56. if (value instanceof Date) {
  57. return value.getTime();
  58. }
  59. return value;
  60. };
  61. WatcherHelper.prototype._checkObjectsFields = function (checkingFromObject, checkingToObject) {
  62. for (var field in checkingFromObject) {
  63. var oldValue = this._toComparable(checkingFromObject[field]);
  64. var newValue = this._toComparable(checkingToObject[field]);
  65. var isEqualObjects = false;
  66. if (typeof oldValue === 'object' && typeof newValue === 'object') {
  67. isEqualObjects = commonUtils.equalByValue(oldValue, newValue);
  68. }
  69. if (oldValue !== newValue && !isEqualObjects) {
  70. return true;
  71. }
  72. }
  73. };
  74. WatcherHelper.prototype.checkWatchers = function () {
  75. for (var _i = 0, _a = this._watchers; _i < _a.length; _i++) {
  76. var watcher = _a[_i];
  77. watcher();
  78. }
  79. };
  80. WatcherHelper.decorators = [
  81. { type: core_1.Injectable },
  82. ];
  83. return WatcherHelper;
  84. }());
  85. exports.WatcherHelper = WatcherHelper;
  86. //# sourceMappingURL=watcher-helper.js.map