/*! * devextreme-angular-test * Version: 17.2.8 * Build date: Mon Feb 05 2018 * * Copyright (c) 2012 - 2018 Developer Express Inc. ALL RIGHTS RESERVED * * This software may be modified and distributed under the terms * of the MIT license. See the LICENSE file in the root of the project for details. * * https://github.com/DevExpress/devextreme-angular */ import { Injectable } from '@angular/core'; @Injectable() export class WatcherHelper { private _watchers: any[] = []; getWatchMethod() { let watchMethod = (valueGetter, valueChangeCallback, options) => { let oldValue = valueGetter(); options = options || {}; if (!options.skipImmediate) { valueChangeCallback(oldValue); } let watcher = () => { let newValue = valueGetter(); if (this._isDifferentValues(oldValue, newValue, options.deep)) { valueChangeCallback(newValue); oldValue = newValue; } }; this._watchers.push(watcher); return () => { let index = this._watchers.indexOf(watcher); if (index !== -1) { this._watchers.splice(index, 1); } }; }; return watchMethod; } private _isDifferentValues(oldValue: any, newValue: any, deepCheck: boolean) { let comparableNewValue = this._toComparable(newValue); let comparableOldValue = this._toComparable(oldValue); let isObjectValues = comparableNewValue instanceof Object && comparableOldValue instanceof Object; if (deepCheck && isObjectValues) { return this._checkObjectsFields(newValue, oldValue); } return comparableNewValue !== comparableOldValue; } private _toComparable(value) { if (value instanceof Date) { return value.getTime(); } return value; } private _checkObjectsFields(checkingFromObject: Object, checkingToObject: Object) { for (let field in checkingFromObject) { let oldValue = this._toComparable(checkingFromObject[field]); let newValue = this._toComparable(checkingToObject[field]); if (newValue !== oldValue) { return true; } } } checkWatchers() { for (let watcher of this._watchers) { watcher(); } } }