Skip to content

Commit

Permalink
Make sure that rate-limit supersedes deferred updates.
Browse files Browse the repository at this point in the history
  • Loading branch information
mbest committed May 27, 2015
1 parent 6d5d786 commit 048179c
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 0 deletions.
60 changes: 60 additions & 0 deletions spec/asyncBehaviors.js
Original file line number Diff line number Diff line change
Expand Up @@ -826,6 +826,42 @@ describe('Deferred', function() {
expect(dependentComputed()).toEqual('C');
});

it('Should delay update of dependent pure computed observable', function() {
var data = ko.observable('A'),
deferredComputed = ko.computed(data).extend({deferred:true}),
dependentComputed = ko.pureComputed(deferredComputed);

expect(dependentComputed()).toEqual('A');

data('B');
expect(deferredComputed()).toEqual('B');
expect(dependentComputed()).toEqual('A');

data('C');
expect(dependentComputed()).toEqual('A');

jasmine.Clock.tick(1);
expect(dependentComputed()).toEqual('C');
});

it('Should delay update of dependent rate-limited computed observable', function() {
var data = ko.observable('A'),
deferredComputed = ko.computed(data).extend({deferred:true}),
dependentComputed = ko.computed(deferredComputed).extend({rateLimit: 500});

expect(dependentComputed()).toEqual('A');

data('B');
expect(deferredComputed()).toEqual('B');
expect(dependentComputed()).toEqual('A');

data('C');
expect(dependentComputed()).toEqual('A');

jasmine.Clock.tick(1); // Cause the "change" notification to propogate to the rate-limited computed
expect(dependentComputed()).toEqual('C');
});

it('Should *not* delay update of dependent deferred computed observable', function () {
var data = ko.observable('A'),
timesEvaluated = 0,
Expand Down Expand Up @@ -865,6 +901,30 @@ describe('Deferred', function() {
expect(notifySpy.argsForCall).toEqual([ ['B'] ]);
});

it('Is superseded by rate-limit', function() {
this.restoreAfter(ko.options, 'deferUpdates');
ko.options.deferUpdates = true;

var data = ko.observable('A'),
deferredComputed = ko.computed(data),
dependentComputed = ko.computed(deferredComputed).extend({rateLimit: 500}),
notifySpy = jasmine.createSpy('notifySpy'),
subscription = dependentComputed.subscribe(notifySpy);

expect(dependentComputed()).toEqual('A');

data('B');
expect(deferredComputed()).toEqual('B');
expect(dependentComputed()).toEqual('A'); // rate-limited computed doesn't respond to "dirty" events

jasmine.Clock.tick(1); // notifies "change" event to the rate-limited computed
expect(dependentComputed()).toEqual('B'); // updated on-demand
expect(notifySpy).not.toHaveBeenCalled(); // but no notification

jasmine.Clock.tick(500);
expect(notifySpy.argsForCall).toEqual([ ['B'] ]); // rate-limited computed notifies after the specified delay
});

it('Should minimize evaluation at the end of a complex graph', function() {
this.restoreAfter(ko.options, 'deferUpdates');
ko.options.deferUpdates = true;
Expand Down
3 changes: 3 additions & 0 deletions src/subscribables/extenders.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,9 @@ ko.extenders = {
method = options['method'];
}

// rateLimit supersedes deferred updates
target._deferUpdates = false;

limitFunction = method == 'notifyWhenChangesStop' ? debounce : throttle;
target.limit(function(callback) {
return limitFunction(callback, timeout);
Expand Down

0 comments on commit 048179c

Please sign in to comment.