If you're using KO 3.4.0 or later, you have the option to implement deferred updates on a computed property that depends on both start
and end
, ensuring changes are detected even when these values change in complementary ways:
let eitherX = 1;
const either = ko.computed(() => {
start();
end();
return ++eitherX;
});
either.extend({deferred: true});
Check out this Live Example:
const start = ko.observable(10);
const end = ko.observable(20);
let eitherX = 1;
const either = ko.computed(() => {
start();
end();
return ++eitherX;
});
either.extend({deferred: true});
console.log("Subscribing");
// Enabling deferred updates
either.subscribe(() => console.log("notified"));
console.log("changing start");
start(11);
console.log("changing end");
end(19);
<script src="https://cdnjs.cloudflare.com/ajax/libs/knockout/3.4.2/knockout-min.js"></script>
In prior versions of Knockout, you can utilize a rate limit set at 0
:
let eitherX = 1;
const either = ko.computed(() => {
start();
end();
return ++eitherX;
});
either.extend({rateLimit: 0});
Here's how it works in action:
const start = ko.observable(10);
const end = ko.observable(20);
let eitherX = 1;
const either = ko.computed(() => {
start();
end();
return ++eitherX;
});
either.extend({rateLimit: 0});
console.log("Subscribing");
// Applying rate limit
either.subscribe(() => console.log("notified"));
console.log("changing start");
start(11);
console.log("changing end");
end(19);
<script src="https://cdnjs.cloudflare.com/ajax/libs/knockout/3.4.2/knockout-min.js"></script>
To ensure proper reactivity, include eitherX
in your setup so that the value of either
is always distinct upon recomputation. Otherwise, if you solely rely on start
and end
in defining either
, any changes made to one while simultaneously modifying the other will not trigger notifications due to unaltered output.