@@ -32,14 +32,15 @@ import * as TBApi from './tbapi';
32
32
import * as TBCore from './tbcore.js' ;
33
33
import { delay } from './tbhelpers.js' ;
34
34
import TBListener from './tblistener.js' ;
35
- import TBLog from './tblog' ;
36
35
import TBModule from './tbmodule.jsx' ;
37
- import * as TBStorage from './tbstorage.js' ;
38
36
39
37
import AppRoot from './AppRoot' ;
40
38
import { initializeObserver } from './frontends' ;
39
+ import { getCache , setCache } from './util/cache' ;
41
40
import { documentInteractive } from './util/dom' ;
41
+ import createLogger from './util/logging' ;
42
42
import { isUserLoggedInQuick } from './util/platform' ;
43
+ import { getSettingAsync , setSettingAsync , updateSettings } from './util/settings' ;
43
44
import { reactRenderer } from './util/ui_interop' ;
44
45
45
46
import Achievements from './modules/achievements.js' ;
@@ -162,12 +163,12 @@ async function checkLoadConditions (tries = 3) {
162
163
163
164
// Write a setting and read back its value, if this fails something is wrong
164
165
let echoValue = Math . random ( ) ;
165
- let echoResult : number ;
166
166
try {
167
- echoResult = await TBStorage . setSettingAsync ( 'Utils' , 'echoTest' , echoValue ) ;
167
+ await setSettingAsync ( 'Utils' , 'echoTest' , echoValue ) ;
168
168
} catch ( error ) {
169
169
throw new Error ( 'Failed to write to settings' , { cause : error } ) ;
170
170
}
171
+ const echoResult = await getSettingAsync ( 'Utils' , 'echoTest' ) ;
171
172
if ( echoResult !== echoValue ) {
172
173
throw new Error ( `Settings read/write inconsistent: expected ${ echoValue } , received ${ echoResult } ` ) ;
173
174
}
@@ -185,11 +186,11 @@ async function doSettingsUpdates () {
185
186
const currentUser = await TBApi . getCurrentUser ( ) ;
186
187
let lastVersion = await TBCore . getLastVersion ( ) ;
187
188
188
- const cacheName = await TBStorage . getCache ( 'Utils' , 'cacheName' , '' ) ;
189
+ const cacheName = await getCache ( 'Utils' , 'cacheName' , '' ) ;
189
190
190
191
// Update cache if we're logged in as someone else
191
192
if ( cacheName !== currentUser ) {
192
- await TBStorage . setCache ( SETTINGS_NAME , 'cacheName' , currentUser ) ;
193
+ await setCache ( SETTINGS_NAME , 'cacheName' , currentUser ) ;
193
194
194
195
// Force refresh of timed cache
195
196
browser . runtime . sendMessage ( {
@@ -200,46 +201,46 @@ async function doSettingsUpdates () {
200
201
// Extra checks on old faults
201
202
if ( typeof lastVersion !== 'number' ) {
202
203
lastVersion = parseInt ( lastVersion ) ;
203
- await TBStorage . setSettingAsync ( SETTINGS_NAME , 'lastVersion' , lastVersion ) ;
204
+ await setSettingAsync ( SETTINGS_NAME , 'lastVersion' , lastVersion ) ;
204
205
}
205
206
206
- let shortLength = await TBStorage . getSettingAsync ( SETTINGS_NAME , 'shortLength' , 15 ) ;
207
- let longLength = await TBStorage . getSettingAsync ( SETTINGS_NAME , 'longLength' , 45 ) ;
207
+ let shortLength = await getSettingAsync ( SETTINGS_NAME , 'shortLength' , 15 ) ;
208
+ let longLength = await getSettingAsync ( SETTINGS_NAME , 'longLength' , 45 ) ;
208
209
209
210
if ( typeof shortLength !== 'number' ) {
210
211
shortLength = parseInt ( shortLength ) ;
211
- await TBStorage . setSettingAsync ( SETTINGS_NAME , 'shortLength' , shortLength ) ;
212
+ await setSettingAsync ( SETTINGS_NAME , 'shortLength' , shortLength ) ;
212
213
}
213
214
214
215
if ( typeof longLength !== 'number' ) {
215
216
longLength = parseInt ( longLength ) ;
216
- await TBStorage . setSettingAsync ( SETTINGS_NAME , 'longLength' , longLength ) ;
217
+ await setSettingAsync ( SETTINGS_NAME , 'longLength' , longLength ) ;
217
218
}
218
219
219
220
// First run changes for all releases.
220
221
if ( TBCore . shortVersion > lastVersion ) {
221
222
// These need to happen for every version change
222
- await TBStorage . setSettingAsync ( SETTINGS_NAME , 'lastVersion' , TBCore . shortVersion ) ; // set last version to this version.
223
+ await setSettingAsync ( SETTINGS_NAME , 'lastVersion' , TBCore . shortVersion ) ; // set last version to this version.
223
224
TBCore . getToolboxDevs ( ) ; // always repopulate tb devs for each version change
224
225
225
226
// This should be a per-release section of stuff we want to change in each update. Like setting/converting data/etc. It should always be removed before the next release.
226
227
227
228
// Start: version changes.
228
229
// reportsThreshold should be 0 by default
229
230
if ( lastVersion < 50101 ) {
230
- await TBStorage . setSettingAsync ( 'QueueTools' , 'reportsThreshold' , 0 ) ;
231
+ await setSettingAsync ( 'QueueTools' , 'reportsThreshold' , 0 ) ;
231
232
}
232
233
233
234
// Clean up removed settings - it doesn't really matter what version
234
235
// we're coming from, we just want to make sure these removed settings
235
236
// aren't cluttering up storage
236
- await Promise . all ( [
237
+ const keysToDelete = [
237
238
// Some new modmail settings were removed in 5.7.0
238
- TBStorage . setSettingAsync ( ' NewModMail' , ' searchhelp', undefined ) ,
239
- TBStorage . setSettingAsync ( ' NewModMail' , ' checkForNewMessages', undefined ) ,
239
+ 'Toolbox. NewModMail. searchhelp',
240
+ 'Toolbox. NewModMail. checkForNewMessages',
240
241
241
242
// Beta mode setting removed in favor of dedicated beta builds #917
242
- TBStorage . setSettingAsync ( SETTINGS_NAME , ' betaMode', undefined ) ,
243
+ 'Toolbox.Utils. betaMode',
243
244
244
245
// (old) modmail pro removed in v7, RIP old modmail
245
246
...[
@@ -265,26 +266,27 @@ async function doSettingsUpdates () {
265
266
'entryProcessRate' ,
266
267
'chunkProcessSize' ,
267
268
'twoPhaseProcessing' ,
268
- ] . map ( setting => TBStorage . setSettingAsync ( ' ModMail' , setting , undefined ) ) ,
269
+ ] . map ( setting => `Toolbox. ModMail. ${ setting } ` ) ,
269
270
270
271
// new reddit is dead, long live shreddit i guess. the setting to
271
272
// skip the new reddit lightbox when viewing comments no longer
272
273
// applies to anything, remove it
273
- TBStorage . setSettingAsync ( 'Comments' , 'commentsAsFullPage' , undefined ) ,
274
- ] ) ;
274
+ 'Toolbox.Comments.commentsAsFullPage' ,
275
+ ] ;
276
+ await updateSettings ( Object . fromEntries ( keysToDelete . map ( key => [ key , undefined ] ) ) ) ;
275
277
276
278
// End: version changes.
277
279
278
280
// This is a super extra check to make sure the wiki page for settings export really is private.
279
- const settingSubEnabled = await TBStorage . getSettingAsync ( 'Utils' , 'settingSub' , '' ) ;
281
+ const settingSubEnabled = await getSettingAsync ( 'Utils' , 'settingSub' , '' ) ;
280
282
if ( settingSubEnabled ) {
281
283
TBCore . setWikiPrivate ( 'tbsettings' , settingSubEnabled , false ) ;
282
284
}
283
285
284
286
// These two should be left for every new release. If there is a new beta feature people want, it should be opt-in, not left to old settings.
285
- // TBStorage.setSetting ('Notifier', 'lastSeenModmail', now); // don't spam 100 new mod mails on first install.
286
- // TBStorage.setSetting ('Notifier', 'modmailCount', 0);
287
- await TBStorage . setSettingAsync ( SETTINGS_NAME , 'debugMode' , false ) ;
287
+ // await setSettingAsync ('Notifier', 'lastSeenModmail', now); // don't spam 100 new mod mails on first install.
288
+ // await setSettingAsync ('Notifier', 'modmailCount', 0);
289
+ await setSettingAsync ( SETTINGS_NAME , 'debugMode' , false ) ;
288
290
}
289
291
}
290
292
@@ -295,13 +297,13 @@ async function doSettingsUpdates () {
295
297
}
296
298
297
299
// Create a logger
298
- const logger = TBLog ( 'Init' ) ;
300
+ const log = createLogger ( 'Init' ) ;
299
301
300
302
// Ensure that other conditions are met, and return early if not
301
303
try {
302
304
await checkLoadConditions ( ) ;
303
305
} catch ( error ) {
304
- logger . error ( 'Load condition not met:' , ( error as Error ) . message ) ;
306
+ log . error ( 'Load condition not met:' , error ) ;
305
307
return ;
306
308
}
307
309
@@ -391,7 +393,7 @@ async function doSettingsUpdates () {
391
393
OldReddit ,
392
394
]
393
395
) {
394
- logger . debug ( 'Registering module' , m ) ;
396
+ log . debug ( 'Registering module' , m ) ;
395
397
TBModule . register_module ( m ) ;
396
398
}
397
399
0 commit comments