Skip to content

Commit

Permalink
Merge pull request #1449 from ProjectMirador/fix-recursion-errors
Browse files Browse the repository at this point in the history
Fix recursion errors
  • Loading branch information
aeschylus authored Jun 19, 2017
2 parents 27fae8e + 722a84f commit 1a2937f
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 2 deletions.
35 changes: 34 additions & 1 deletion js/src/utils/saveController.js
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@
this.init(newConfig);
};


$.SaveController.prototype = {

init: function(config) {
Expand Down Expand Up @@ -372,14 +373,46 @@

},

cleanup: function(obj) {

/**
* Setup an array-based implementation of a Set
* to track the objects we have
* already cloned - this will generically clean circular refs.
**/
var clonedSet = [];

function cloner(obj) {

if(obj === null || typeof(obj) != 'object') {
return obj;
}

if (clonedSet.indexOf(obj) === -1) {
clonedSet.push(obj);
var temp = Array.isArray(obj) ? [] : {};
for(var key in obj) {
if (obj.hasOwnProperty(key)) {
temp[key] = cloner(obj[key]);
}
}
return temp;
}

return undefined;
}

return cloner(obj);
},

save: function() {
var _this = this;

// the hash must be stringified because
// localStorage is a key:value store that
// only accepts strings.

localStorage.setItem(_this.sessionID, JSON.stringify(_this.currentConfig));
localStorage.setItem(_this.sessionID, JSON.stringify(_this.cleanup(_this.currentConfig)));
}

};
Expand Down
2 changes: 1 addition & 1 deletion js/src/viewer/bookmarkPanel.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@

onConfigUpdated: function() {
var _this = this;
_this.storageModule.save(_this.state.currentConfig)
_this.storageModule.save(_this.state.cleanup(_this.state.currentConfig))
.then(function(blobId) {
var bookmarkURL = window.location.href.replace(window.location.hash, '') + "?json="+blobId;
_this.element.find('#share-url').val(bookmarkURL).focus().select();
Expand Down
26 changes: 26 additions & 0 deletions spec/utils/saveController.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,32 @@ describe('SaveController', function () {
});
});

describe('Test Cleaning Up Objects', function () {
it('should remove circular references', function () {

var saveController = new Mirador.SaveController(this.config);

var object_a = { valid: 'This is a valid value' };
var object_b = { valid: 'This is a valid value', invalid: object_a };
object_a.object_b = object_b;

var cleaned = saveController.cleanup(object_a);

/**
* The original object should be untouched.
*/
expect(object_a.object_b.invalid).not.toBeUndefined();

/**
* object_a reference in cleaned should be the only
* value missing.
*/
expect(cleaned.valid).toBe('This is a valid value');
expect(cleaned.object_b.invalid).toBeUndefined();
expect(cleaned.object_b.valid).toBe('This is a valid value');
});
});

describe('Event handling', function () {
xit('should handle windowUpdated', function () {
});
Expand Down

0 comments on commit 1a2937f

Please sign in to comment.