Permalink
Browse files

fix(subscriber-collection): fix removeSubscriber leak

fixes #585 
fixes #555
  • Loading branch information...
bigopon authored and jdanyow committed Sep 25, 2017
1 parent 2bc7378 commit 8fe6181857ab907b71f727002f7a3060e6b81eb3
Showing with 32 additions and 5 deletions.
  1. +12 −5 src/subscriber-collection.js
  2. +20 −0 test/subscriber-collection.spec.js
@@ -43,13 +43,20 @@ function removeSubscriber(context, callable) {
this._callable2 = null;
return true;
}
let rest = this._contextsRest;
let index;
if (!rest || !rest.length || (index = rest.indexOf(context)) === -1 || this._callablesRest[index] !== callable) { // eslint-disable-line no-cond-assign
const callables = this._callablesRest;
if (callables === undefined || callables.length === 0) {
return false;
}
const contexts = this._contextsRest;
let i = 0;
while (!(callables[i] === callable && contexts[i] === context) && callables.length > i) {
i++;
}
if (i >= callables.length) {
return false;
}
rest.splice(index, 1);
this._callablesRest.splice(index, 1);
contexts.splice(i, 1);
callables.splice(i, 1);
return true;
}
@@ -43,4 +43,24 @@ describe('subscriberCollection', () => {
expect(callable9.call).toHaveBeenCalledWith('9', 'new value2', 'old value2');
expect(callable10.call).toHaveBeenCalledWith('10', 'new value2', 'old value2');
});
it('removes subscribers', () => {
let observer = new Test();
let subscribers = [];
for (let i = 0, ii = 100; ii > i; ++i) {
observer.addSubscriber((i % 5).toString(), subscribers[i] = { i });
}
let removalCount = 0;
for (let i = 4, ii = subscribers.length; ii > i; i += 5) {
let result = observer.removeSubscriber((i % 5).toString(), subscribers[i]);
if (result) {
removalCount++;
}
}
expect(observer._callablesRest.length).toBe(subscribers.length - 3 - removalCount);
expect(observer.removeSubscriber('5', {})).toBe(false);
});
});

0 comments on commit 8fe6181

Please sign in to comment.