Skip to content

Commit

Permalink
fixed unsubscribe bug
Browse files Browse the repository at this point in the history
  • Loading branch information
justinjung04 committed Jun 17, 2016
1 parent 1cc92f6 commit 127e8a9
Showing 1 changed file with 11 additions and 3 deletions.
14 changes: 11 additions & 3 deletions src/treeful-node.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ export default class TreefulNode {
let _data = data;
let _callbacks = [];
let _children = {};
let _hashId = 0;

this.addNode = (node) => {
const branch = {};
Expand All @@ -26,14 +27,21 @@ export default class TreefulNode {
this.getCallbacks = () => _callbacks;

this.subscribe = (callback, ignoreChildren = false) => {
_callbacks.push({callback, ignoreChildren});
const hashId = _hashId;
_callbacks.push({hashId, callback, ignoreChildren});
_hashId++;
return () => {
unsubscribe(_callbacks.length - 1);
unsubscribe(hashId);
};
};

const unsubscribe = (index) => {
_callbacks.splice(index, 1);
for(let i=0; i<_callbacks.length; i++) {
if(_callbacks[i].hashId == index) {
_callbacks.splice(i, 1);
break;
}
}
};

const callCallbacks = (data, id, self) => {
Expand Down

0 comments on commit 127e8a9

Please sign in to comment.