Skip to content
This repository has been archived by the owner on Mar 13, 2018. It is now read-only.

ArrayReduction does not update PathObservers #18

Closed
peterwmwong opened this issue Jul 6, 2013 · 1 comment
Closed

ArrayReduction does not update PathObservers #18

peterwmwong opened this issue Jul 6, 2013 · 1 comment

Comments

@peterwmwong
Copy link
Contributor

When items are added or removed from array being watched by ArrayReduction, all the PathObservers that watched items indexed AFTER the add/remove index are out of sync.

Let's look at an example...

var values = ['a','b','c'];
var calls = [];
var reduction = new ArrayReduction(
  values,
  '',
  function(sum,cur){
    sum.push(cur);
    return sum;
  },
  calls
);
calls.splice(0);

// Internal view of observers.path for each item by index:
// array index    observer.path
// -----------    -------------
// 0              0
// 1              1             <---- To be spliced
// 2              2

values.splice(1,1);
reduction.deliver();

// Internal view of observers.path for each item by index:
// array index    observer.path
// -----------    -------------
// 0              0
// 1              2             <---- INCORRECT PATH, SHOULD BE 1


// == EXPECTED ==
// > Calls to reduce function: ["a", "c"]
// === ACTUAL ===
// > Calls to reduce function: ["a", "c", "a", "c", undefined]
console.log('Calls to reduce function:', calls);

... Notice the difference in EXPECTED and ACTUAL output.
What appears to be happening is the PathObserver that was watching the [2] element (which is now the [1] element), incorrectly causes a another reduce() because it sees a change in value ('c' -> undefined).

I believe the problem is that ArrayReduction's handleSplice() does not loop over and update the paths in observers. https://github.com/rafaelw/ChangeSummary/blob/08cb2c69b5daba0cbd5e806820311a76ea279858/util/array_reduction.js#L56

@rafaelw
Copy link
Contributor

rafaelw commented Jul 12, 2013

Well done!

@rafaelw rafaelw closed this as completed Jul 12, 2013
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants