diff --git a/lib/edit/attachmentsStore.js b/lib/edit/attachmentsStore.js index 685fddb..1cf1d42 100644 --- a/lib/edit/attachmentsStore.js +++ b/lib/edit/attachmentsStore.js @@ -161,11 +161,13 @@ define([], function() var index = objectStore.index("featureId"); var keyRange = IDBKeyRange.only(featureId); var deletedCount = 0; - index.openKeyCursor(keyRange).onsuccess = function(evt) + index.openCursor(keyRange).onsuccess = function(evt) { var cursor = evt.target.result; if(cursor) { + var attachment = cursor.value; + this._revokeLocalURL(attachment); objectStore.delete(cursor.primaryKey); deletedCount++; cursor.continue(); @@ -174,41 +176,62 @@ define([], function() { callback(deletedCount); } - }; + }.bind(this); }; this.delete = function(attachmentId, callback) { console.assert(this._db !== null, "indexeddb not initialized"); - var request = this._db.transaction([OBJECT_STORE_NAME],"readwrite") - .objectStore(OBJECT_STORE_NAME) - .delete(attachmentId); - request.onsuccess = function(event) + // before deleting an attachment we must revoke the blob URL that it contains + // in order to free memory in the browser + this.retrieve(attachmentId, function(success, attachment) { - setTimeout(function(){callback(true);},1); - }; - request.onerror = function(err) - { - callback(false,err); - }; + if( !success ) + { + callback(false,"attachment " + attachmentId + " not found"); + return; + } + + this._revokeLocalURL(attachment); + + var request = this._db.transaction([OBJECT_STORE_NAME],"readwrite") + .objectStore(OBJECT_STORE_NAME) + .delete(attachmentId); + request.onsuccess = function(event) + { + setTimeout(function(){callback(true);},1); + }; + request.onerror = function(err) + { + callback(false,err); + }; + }.bind(this)); }; this.deleteAll = function(callback) { console.assert(this._db !== null, "indexeddb not initialized"); - var request = this._db.transaction([OBJECT_STORE_NAME],"readwrite") - .objectStore(OBJECT_STORE_NAME) - .clear(); - request.onsuccess = function(event) + this.getAllAttachments(function(attachments) { - callback(true); - }; - request.onerror = function(err) - { - callback(false,err); - }; + attachments.forEach(function(attachment) + { + this._revokeLocalURL(attachment); + },this); + + var request = this._db.transaction([OBJECT_STORE_NAME],"readwrite") + .objectStore(OBJECT_STORE_NAME) + .clear(); + request.onsuccess = function(event) + { + callback(true); + }; + request.onerror = function(err) + { + callback(false,err); + }; + }.bind(this)); }; this.replaceFeatureId = function(featureLayerUrl, oldId, newId, callback) @@ -277,6 +300,18 @@ define([], function() }; }; + // internal methods + + this._createLocalURL = function(attachment) + { + + }; + + this._revokeLocalURL = function(attachment) + { + window.URL.revokeObjectURL(attachment.url); + }; + this.init = function(callback) { console.log("init AttachmentStore");