angular-bacon
angular-bacon copied to clipboard
`$watchAsProperty` may emit duplicate value changes upon start
Consider the following lines of code:
$scope.test = 'initial';
var property = $scope.$watchAsProperty('test');
// Property will emit the scope variable's initial value
property.onValue(function (value) {
console.log("I will be invoked twice with the same value:", value);
});
// Running the digest loop will run the Angular watch that feeds `property` and the same value will be emitted once more.
$scope.$digest();
It will log twice with value 'initial'
. I understand why that technically happens, but I consider it very much counter-intuitive.
In my opinion, how one expects $watchAsProperty
to behave is very obvious and intuitive: it should represent an Angular scope variable as Bacon property. In the context of this issue that means that it should only emit a value change once for the scope variable that is assigned to 'initial'
at the very beginning and is left untouched from that point on.
I suggest changing the implementation of $watchAsProperty
to check for equality in its Angular watch function, as also referred to in the Angular documentation (see last paragraph of $watch
section and second example).
More specifically I suggest replacing the current watch function with:
scope[watchMethod](watchExp, function(newValue, oldValue) {
if (newValue !== oldValue) {
return bus.push(newValue);
}
}, objectEquality);
This would prevent the confusingly duplicate value change events.