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: Use Promise instead of onChanged listener Created April 1, 2016, 10:04 a.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)
@@ -58,61 +59,94 @@
readFromFile: function(file, listener, callback)
{
- function onLoaded(entry)
+ loadFile(file).catch(() => null)
Sebastian Noack 2016/04/01 10:49:44 The logic here is wrong. In the case of readFromFi
+ .then(entry =>
{
- for (let line of entry.content)
- listener.process(line);
+ if (!entry)
+ return;
+ 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));
- loadFile(file, onLoaded, callback);
+ ext.storage.get(keys, items =>
+ {
+ for (let key of keys)
+ for (let line of items[key])
+ listener.process(line);
+
+ listener.process(null);
+ callback(null);
+ });
+ }
+ });
},
-
+
Sebastian Noack 2016/04/01 10:49:44 Nit: You added trailing whitespaces to this line.
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")
{
- 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);
Sebastian Noack 2016/04/01 10:49:44 Nit: You added trailing whitespaces to this line.
+ }
},
statFile: function(file, callback)
{
- function onLoaded(entry)
+ loadFile(file).catch((val) => null)
+ .then(entry =>
{
- callback(null, {
- exists: true,
- lastModified: entry.lastModified
- });
- }
-
- loadFile(file, onLoaded, callback);
- }
+ if (entry)
+ callback(null, {
+ exists: true,
+ lastModified: entry.lastModified
+ });
+ });
+ },
};
« 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