Skip to content

Commit

Permalink
Merge pull request #1974 from ThomasMichon/array-subscriptions
Browse files Browse the repository at this point in the history
Restore original notifySubscribers method when unsubscribing from arrayChange events
  • Loading branch information
mbest committed Jan 14, 2016
2 parents e9e653e + 756d06e commit d608c4c
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 1 deletion.
8 changes: 8 additions & 0 deletions spec/observableArrayChangeTrackingBehaviors.js
Original file line number Diff line number Diff line change
Expand Up @@ -274,6 +274,14 @@ describe('Observable Array change tracking', function() {
expect(source.getSubscriptionsCount()).toBe(0);
});

it('should restore previous subscription notifications', function () {
var source = ko.observableArray();
var notifySubscribers = source.notifySubscribers;
var arrayChange = source.subscribe(function () { }, null, 'arrayChange');
arrayChange.dispose();
expect(source.notifySubscribers).toBe(notifySubscribers);
});

it('Should support tracking of a computed observable using extender', function() {
var myArray = ko.observable(['Alpha', 'Beta', 'Gamma']),
myComputed = ko.computed(function() {
Expand Down
7 changes: 6 additions & 1 deletion src/subscribables/observableArray.changeTracking.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ ko.extenders['trackArrayChanges'] = function(target, options) {
cachedDiff = null,
arrayChangeSubscription,
pendingNotifications = 0,
underlyingNotifySubscribersFunction,
underlyingBeforeSubscriptionAddFunction = target.beforeSubscriptionAdd,
underlyingAfterSubscriptionRemoveFunction = target.afterSubscriptionRemove;

Expand All @@ -31,6 +32,10 @@ ko.extenders['trackArrayChanges'] = function(target, options) {
if (underlyingAfterSubscriptionRemoveFunction)
underlyingAfterSubscriptionRemoveFunction.call(target, event);
if (event === arrayChangeEventName && !target.hasSubscriptionsForEvent(arrayChangeEventName)) {
if (underlyingNotifySubscribersFunction) {
target['notifySubscribers'] = underlyingNotifySubscribersFunction;
underlyingNotifySubscribersFunction = undefined;
}
arrayChangeSubscription.dispose();
trackingChanges = false;
}
Expand All @@ -45,7 +50,7 @@ ko.extenders['trackArrayChanges'] = function(target, options) {
trackingChanges = true;

// Intercept "notifySubscribers" to track how many times it was called.
var underlyingNotifySubscribersFunction = target['notifySubscribers'];
underlyingNotifySubscribersFunction = target['notifySubscribers'];
target['notifySubscribers'] = function(valueToNotify, event) {
if (!event || event === defaultEvent) {
++pendingNotifications;
Expand Down

0 comments on commit d608c4c

Please sign in to comment.