Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code

Unified Diff: lib/io.js

Issue 29339112: Issue 3716 - Split up files stored in storage.local (Closed)
Patch Set: Fix the promise logic. Remove whitespace changes. Created April 1, 2016, 12:41 p.m.
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « chrome/ext/background.js ('k') | lib/prefs.js » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: lib/io.js
===================================================================
--- a/lib/io.js
+++ b/lib/io.js
@@ -15,40 +15,41 @@
* along with Adblock Plus. If not, see <http://www.gnu.org/licenses/>.
*/
+"use strict";
+
const keyPrefix = "file:";
-function fileToKey(file)
+function fileToKey(file, chunk)
{
- return keyPrefix + (file instanceof FakeFile ? file.path : file.spec);
+ let key = keyPrefix;
+
+ if (file instanceof FakeFile)
+ key += file.path;
+ else
+ key += file.spec;
+
+ if (typeof chunk != "undefined")
+ key += ":" + chunk;
+
+ return key;
}
-function loadFile(file, successCallback, errorCallback)
+function loadFile(file)
{
- let key = fileToKey(file);
-
- ext.storage.get([key], function(items)
+ return new Promise((resolve, reject) =>
{
- let entry = items[key];
-
- if (entry)
- successCallback(entry);
- else
- errorCallback(new Error("File doesn't exist"));
+ let key = fileToKey(file);
+ ext.storage.get([key], items =>
+ {
+ let entry = items[key];
+ if (entry)
+ resolve(entry);
+ else
+ reject(new Error("File doesn't exist"));
+ });
});
}
-function saveFile(file, data, callback)
-{
- ext.storage.set(
- fileToKey(file),
- {
- content: data,
- lastModified: Date.now()
- },
- callback
- );
-}
-
exports.IO =
{
resolveFilePath: function(path)
@@ -60,47 +61,79 @@
{
function onLoaded(entry)
{
- for (let line of entry.content)
- listener.process(line);
+ if ("content" in entry)
+ {
+ for (let line of entry.content)
+ listener.process(line);
- listener.process(null);
- callback(null);
+ listener.process(null);
+ callback(null);
+ }
+ else
+ {
+ let keys = [];
+ for (let i = 0; i < entry.chunks; i++)
+ keys.push(fileToKey(file, i));
+
+ ext.storage.get(keys, items =>
+ {
+ for (let key of keys)
+ for (let line of items[key])
+ listener.process(line);
+
+ listener.process(null);
+ callback(null);
+ });
+ }
}
- loadFile(file, onLoaded, callback);
+ loadFile(file).then(onLoaded, callback);
},
writeToFile: function(file, data, callback)
{
- saveFile(file, data, callback);
- },
+ let items = {};
+ let entry = items[fileToKey(file)] = {lastModified: Date.now()};
- copyFile: function(fromFile, toFile, callback)
- {
- function onLoaded(entry)
+ if (typeof browser == "object")
Oleksandr 2016/04/01 12:45:10 I was using this to test in Chrome. Just noticed t
{
- saveFile(toFile, entry.content, callback);
- }
+ loadFile(file).catch(() => null)
+ .then(oldEntry =>
+ {
+ let quota = 1024 * 1024 / 2 - 1000;
+ let chunks = [];
+ let chunk = [];
+ let chunkSize = 0;
- loadFile(fromFile, onLoaded, callback);
- },
+ for (let line of data)
+ {
+ if (line.length + chunkSize > quota)
+ {
+ chunks.push(chunk);
+ chunk = [];
+ chunkSize = 0;
+ }
- renameFile: function(fromFile, newName, callback)
- {
- function onLoaded()
- {
- ext.storage.remove(fileToKey(fromFile), function()
- {
- ext.storage.set(keyPrefix + newName, entry, callback);
+ chunk.push(line);
+ chunkSize += line.length;
+ }
+ chunks.push(chunk);
+
+ for (let i = 0; i < chunks.length; i++)
+ items[fileToKey(file, i)] = chunks[i];
+ entry.chunks = chunks.length;
+ ext.storage.set(items, callback);
+
+ if (oldEntry && "chunks" in oldEntry)
+ for (let i = entry.chunks; i < oldEntry.chunks; i++)
+ ext.storage.remove(fileToKey(file, i));
});
}
-
- loadFile(fromFile, onLoaded, callback);
- },
-
- removeFile: function(file, callback)
- {
- ext.storage.remove(fileToKey(file), callback);
+ else
+ {
+ entry.content = data;
+ ext.storage.set(items, callback);
+ }
},
statFile: function(file, callback)
@@ -113,6 +146,6 @@
});
}
- loadFile(file, onLoaded, callback);
- }
+ loadFile(file).then(onLoaded, callback);
+ },
Sebastian Noack 2016/04/01 13:01:05 Nit: You added a redundant comma here. There shoul
};
« no previous file with comments | « chrome/ext/background.js ('k') | lib/prefs.js » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld