Skip to content

Commit d7be397

Browse files
committed
Code polishing
Added storage versioning for cleaner upgrades in the future Convert all promise usage into async/await Perform initialization based on runtime event, not when opening preferences page (every time at that!) Improve input validation checks Fix name edit box size
1 parent 91102d4 commit d7be397

File tree

5 files changed

+222
-202
lines changed

5 files changed

+222
-202
lines changed

background.js

Lines changed: 59 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,68 @@
1-
function handleCommand(command)
1+
const STORAGE_VERSION = 2;
2+
3+
async function initialize(details)
24
{
3-
console.log("(background) handling command: " + command);
5+
console.log("(init) Initializing...");
46

5-
if(command.startsWith(PREFIX_PRESET))
7+
// Preset initialization, if needed
8+
let storage = await browser.storage.local.get(["presets","version"]);
9+
10+
if(!Array.isArray(storage.presets))
11+
{
12+
console.log("(init) No presets found, initializing...");
13+
storage.presets = [];
14+
await browser.storage.local.set({"presets": storage.presets});
15+
}
16+
else
617
{
7-
browser.storage.local.get("presets").then((obj) => {
8-
if(obj != undefined)
18+
if(storage.version === undefined)
19+
{
20+
// Had non-versioned preset storage, so treat it as version 1
21+
storage.version = 1;
22+
}
23+
}
24+
25+
if(Number.isInteger(storage.version))
26+
{
27+
if(storage.version < STORAGE_VERSION)
28+
{
29+
// Storage is old, do any necessary upgrades to remain compatible
30+
31+
if(storage.version == 1)
932
{
10-
let presets = obj.presets;
11-
let presetNum = parseInt(command.substring(PREFIX_PRESET.length));
12-
if(presetNum <= presets.length)
13-
{
14-
let presetId = presets[presetNum - 1].id;
15-
applyPreset(presetId);
16-
}
33+
console.log("(update) Migrating storage from version 1 to 2...");
34+
await browser.storage.local.set({"version": 2});
35+
storage.version = 2;
1736
}
18-
});
37+
38+
// (future storage updates here)
39+
}
40+
}
41+
else
42+
{
43+
console.log("(init) No storage version tag found, initializing...");
44+
await browser.storage.local.set({"version": STORAGE_VERSION});
45+
}
46+
47+
console.log("(init) Initialization complete, " + storage.presets.length + " presets found");
48+
}
49+
50+
async function handleCommand(command)
51+
{
52+
console.log("(background) handling command: " + command);
53+
54+
if(command.startsWith(PREFIX_PRESET))
55+
{
56+
let presetNum = parseInt(command.substring(PREFIX_PRESET.length));
57+
let storage = await browser.storage.local.get("presets");
58+
let presets = storage.presets;
59+
if(presetNum <= presets.length)
60+
{
61+
let presetId = presets[presetNum - 1].id;
62+
applyPreset(presetId);
63+
}
1964
}
2065
}
2166

67+
browser.runtime.onInstalled.addListener(initialize);
2268
browser.commands.onCommand.addListener(handleCommand);

common.js

Lines changed: 12 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,22 @@
11
const PREFIX_PRESET = "preset-";
22

3-
function applyPreset(presetId)
3+
async function applyPreset(presetId)
44
{
55
console.log("(common) applying preset: " + presetId);
66

7-
browser.storage.local.get("presets").then((obj) => {
8-
if(obj != undefined)
7+
let storage = await browser.storage.local.get("presets");
8+
let presets = storage.presets;
9+
for(let i = 0;i < presets.length;i++)
10+
{
11+
if(presets[i].id == presetId)
912
{
10-
let presets = obj.presets;
11-
for(let i = 0;i < presets.length;i++)
12-
{
13-
if(presets[i].id == presetId)
14-
{
15-
resizeWindow(presets[i].width,presets[i].height);
16-
break;
17-
}
18-
}
13+
resizeWindow(presets[i].width,presets[i].height);
14+
break;
1915
}
20-
});
16+
}
2117
}
2218

23-
function resizeWindow(newWidth, newHeight)
19+
async function resizeWindow(newWidth, newHeight)
2420
{
2521
console.log("(common) resizing to: " + newWidth + "x" + newHeight);
2622

@@ -29,7 +25,6 @@ function resizeWindow(newWidth, newHeight)
2925
"height": parseInt(newHeight)
3026
};
3127

32-
browser.windows.getCurrent().then((currentWindow) => {
33-
browser.windows.update(currentWindow.id, updateInfo);
34-
});
28+
let currentWindow = await browser.windows.getCurrent();
29+
browser.windows.update(currentWindow.id, updateInfo);
3530
}

manifest.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"manifest_version": 2,
33
"name": "WindowSizer",
4-
"version": "0.6.1",
4+
"version": "0.7.0",
55
"author": "Dr. Cheap",
66
"description": "Quickly resize the window to one of your favorite sizes",
77
"homepage_url": "https://addons.mozilla.org/firefox/addon/windowsizer",

0 commit comments

Comments
 (0)