Skip to content

Commit

Permalink
Fix bug with all siblings in array being changed when one only one is…
Browse files Browse the repository at this point in the history
… edited
  • Loading branch information
maierson committed May 12, 2016
1 parent faf10d5 commit 908a2c9
Show file tree
Hide file tree
Showing 9 changed files with 492 additions and 373 deletions.
10 changes: 2 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -111,14 +111,7 @@ If you were to track all instances of an entity on each update the write penalty

###Data shape
This is not currently designed to work with cyclical data. It is best for non-cyclical objects received from the server in the form of json (or other non-cyclical fomats).
If a strong need arises for managing cyclical structures this might be an option for future development.

###Release Notes
5.1.0
* remove thread api, time travel works on main thread only to prevent mixing data from different cache nodes
* ```getCurrentIndex()``` change to ```index()``` with dual nature to get or set the current index.
* add ```node()``` api in order to locate cache nodes by their id
Replaces the threading option in a much simpler and intuitive manner since threads are only a way of jumping around in time on the cache.
It might happen later if there's a need.

###Documentation
* [Immutable data](https://maierson.gitbooks.io/one/content/immutable_data.html)
Expand All @@ -127,5 +120,6 @@ Replaces the threading option in a much simpler and intuitive manner since threa
* [Get](https://maierson.gitbooks.io/one/content/get.html)
* [Evict](https://maierson.gitbooks.io/one/content/evict.html)
* [Time travel](https://maierson.gitbooks.io/one/content/time_travel.html)
* [Release Notes](https://maierson.gitbooks.io/one/content/release_notes.html)


2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "one",
"version": "5.1.0",
"version": "5.1.1",
"description": "Browser application cache. It guarantees entity uniqueness across the entire cache.",
"main": "lib/index.js",
"scripts": {
Expand Down
7 changes: 6 additions & 1 deletion src/cache.js
Original file line number Diff line number Diff line change
Expand Up @@ -474,16 +474,20 @@ function getCache(debugParam = false) {
// at this point all items are added into the flush map - update their pointers if applicable
flushMap.forEach((item, key) => {
if (key !== UPDATED_KEY) {

// do not modify flush map on its own iteration but ok to pass along for reference
let refsFrom = item[REF_FROM];

// parentUid = uid of the item being targeted for ref update (ie the ref's parent)
for (let parentUid in refsFrom) {
if (refsFrom.hasOwnProperty(parentUid)) {

let paths = refsFrom[parentUid];
let parentItem = flushMap.get(parentUid);
if (!parentItem) {
parentItem = getLiveItem(parentUid);
}

/* only update if dirty - no need to iterate all paths - just check the first one
- if dirty then the parent entity needs to be cloned and updated anyways so pass in
the ref entity when cloning - it will be updated wherever it is encountered during cloning */
Expand All @@ -492,10 +496,11 @@ function getCache(debugParam = false) {
let targetRef = opath.get(parentItem[ENTITY], firstPath);
// check for dirty
let dirty = (targetRef && targetRef !== item[ENTITY]);

if (dirty === true) {
parentItem = ensureItem(parentItem[ENTITY], flushMap);
// the entity is still frozen here - clone it to update and freeze it deeply
parentItem[ENTITY] = deepClone(parentItem[ENTITY], item[ENTITY], true, true);
parentItem[ENTITY] = deepClone(parentItem[ENTITY], item[ENTITY], true);
}
}
}
Expand Down
17 changes: 10 additions & 7 deletions src/utils/clone.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ export function isEmpty(value) {
* @param force
* @returns {*}
*/
export function deepClone(obj, uidReference, freeze = true, force = false) {
export function deepClone(obj, uidReference, freeze = true) {
if (!obj || (!isObject(obj) && !isArray(obj))) {
return obj;
}
Expand All @@ -59,7 +59,7 @@ export function deepClone(obj, uidReference, freeze = true, force = false) {
let value = result[propName];
if (value) {
if (isArray(value)) {
result[propName] = deepCloneArray(value, uidReference, force);
result[propName] = deepCloneArray(value, uidReference, freeze);
} else if (isDate(value)) {
let date = new Date(value.getTime());
if (freeze === true) {
Expand All @@ -80,7 +80,7 @@ export function deepClone(obj, uidReference, freeze = true, force = false) {
//result[propName] = deepClone(value);
}
} else {
result[propName] = deepClone(value, uidReference, freeze, force);
result[propName] = deepClone(value, uidReference, freeze);
}
}
}
Expand All @@ -92,16 +92,19 @@ export function deepClone(obj, uidReference, freeze = true, force = false) {
return result;
}

function deepCloneArray(arr, uidReference, force) {
function deepCloneArray(arr, uidReference, freeze) {
return arr.map(item => {
if (isArray(item)) {
return deepCloneArray(item, uidReference, force);
return deepCloneArray(item, uidReference, freeze);
} else if (isObject(item)) {
// *** keep items inside clones as we're not editing them = must getEdit on item
if (hasUid(item) && force === false) {
if (hasUid(item)) {
if(uidReference && (item[config.prop.uidName] === uidReference[config.prop.uidName])){
return uidReference;
}
return item;
} else {
return deepClone(item, uidReference, force);
return deepClone(item, uidReference, freeze);
}
} else {
return item;
Expand Down
Loading

0 comments on commit 908a2c9

Please sign in to comment.