blob: fb8d5f475b93c1e9753cae1968487ce527a5b6a0 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
|
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
"use strict";
const { XPCOMUtils } = ChromeUtils.importESModule(
"resource://gre/modules/XPCOMUtils.sys.mjs"
);
const lazy = {};
ChromeUtils.defineESModuleGetters(lazy, {
JSONFile: "resource://gre/modules/JSONFile.sys.mjs",
});
var EXPORTED_SYMBOLS = ["Storage"];
// Lazy-load JSON file that backs Storage instances.
XPCOMUtils.defineLazyGetter(lazy, "lazyStore", async function() {
const path = PathUtils.join(
PathUtils.profileDir,
"shield-recipe-client.json"
);
const store = new lazy.JSONFile({ path });
await store.load();
return store;
});
var Storage = class {
constructor(prefix) {
this.prefix = prefix;
}
/**
* Clear ALL storage data and save to the disk.
*/
static async clearAllStorage() {
const store = await lazy.lazyStore;
store.data = {};
store.saveSoon();
}
/**
* Sets an item in the prefixed storage.
* @returns {Promise}
* @resolves With the stored value, or null.
* @rejects Javascript exception.
*/
async getItem(name) {
const store = await lazy.lazyStore;
const namespace = store.data[this.prefix] || {};
return namespace[name] || null;
}
/**
* Sets an item in the prefixed storage.
* @returns {Promise}
* @resolves When the operation is completed successfully
* @rejects Javascript exception.
*/
async setItem(name, value) {
const store = await lazy.lazyStore;
if (!(this.prefix in store.data)) {
store.data[this.prefix] = {};
}
store.data[this.prefix][name] = value;
store.saveSoon();
}
/**
* Removes a single item from the prefixed storage.
* @returns {Promise}
* @resolves When the operation is completed successfully
* @rejects Javascript exception.
*/
async removeItem(name) {
const store = await lazy.lazyStore;
if (this.prefix in store.data) {
delete store.data[this.prefix][name];
store.saveSoon();
}
}
/**
* Clears all storage for the prefix.
* @returns {Promise}
* @resolves When the operation is completed successfully
* @rejects Javascript exception.
*/
async clear() {
const store = await lazy.lazyStore;
store.data[this.prefix] = {};
store.saveSoon();
}
};
|