@@ -32,14 +32,15 @@ import * as TBApi from './tbapi';
3232import * as TBCore from './tbcore.js' ;
3333import { delay } from './tbhelpers.js' ;
3434import TBListener from './tblistener.js' ;
35- import TBLog from './tblog' ;
3635import TBModule from './tbmodule.jsx' ;
37- import * as TBStorage from './tbstorage.js' ;
3836
3937import AppRoot from './AppRoot' ;
4038import { initializeObserver } from './frontends' ;
39+ import { getCache , setCache } from './util/cache' ;
4140import { documentInteractive } from './util/dom' ;
41+ import createLogger from './util/logging' ;
4242import { isUserLoggedInQuick } from './util/platform' ;
43+ import { getSettingAsync , setSettingAsync , updateSettings } from './util/settings' ;
4344import { reactRenderer } from './util/ui_interop' ;
4445
4546import Achievements from './modules/achievements.js' ;
@@ -162,12 +163,12 @@ async function checkLoadConditions (tries = 3) {
162163
163164 // Write a setting and read back its value, if this fails something is wrong
164165 let echoValue = Math . random ( ) ;
165- let echoResult : number ;
166166 try {
167- echoResult = await TBStorage . setSettingAsync ( 'Utils' , 'echoTest' , echoValue ) ;
167+ await setSettingAsync ( 'Utils' , 'echoTest' , echoValue ) ;
168168 } catch ( error ) {
169169 throw new Error ( 'Failed to write to settings' , { cause : error } ) ;
170170 }
171+ const echoResult = await getSettingAsync ( 'Utils' , 'echoTest' ) ;
171172 if ( echoResult !== echoValue ) {
172173 throw new Error ( `Settings read/write inconsistent: expected ${ echoValue } , received ${ echoResult } ` ) ;
173174 }
@@ -185,11 +186,11 @@ async function doSettingsUpdates () {
185186 const currentUser = await TBApi . getCurrentUser ( ) ;
186187 let lastVersion = await TBCore . getLastVersion ( ) ;
187188
188- const cacheName = await TBStorage . getCache ( 'Utils' , 'cacheName' , '' ) ;
189+ const cacheName = await getCache ( 'Utils' , 'cacheName' , '' ) ;
189190
190191 // Update cache if we're logged in as someone else
191192 if ( cacheName !== currentUser ) {
192- await TBStorage . setCache ( SETTINGS_NAME , 'cacheName' , currentUser ) ;
193+ await setCache ( SETTINGS_NAME , 'cacheName' , currentUser ) ;
193194
194195 // Force refresh of timed cache
195196 browser . runtime . sendMessage ( {
@@ -200,46 +201,46 @@ async function doSettingsUpdates () {
200201 // Extra checks on old faults
201202 if ( typeof lastVersion !== 'number' ) {
202203 lastVersion = parseInt ( lastVersion ) ;
203- await TBStorage . setSettingAsync ( SETTINGS_NAME , 'lastVersion' , lastVersion ) ;
204+ await setSettingAsync ( SETTINGS_NAME , 'lastVersion' , lastVersion ) ;
204205 }
205206
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 ) ;
208209
209210 if ( typeof shortLength !== 'number' ) {
210211 shortLength = parseInt ( shortLength ) ;
211- await TBStorage . setSettingAsync ( SETTINGS_NAME , 'shortLength' , shortLength ) ;
212+ await setSettingAsync ( SETTINGS_NAME , 'shortLength' , shortLength ) ;
212213 }
213214
214215 if ( typeof longLength !== 'number' ) {
215216 longLength = parseInt ( longLength ) ;
216- await TBStorage . setSettingAsync ( SETTINGS_NAME , 'longLength' , longLength ) ;
217+ await setSettingAsync ( SETTINGS_NAME , 'longLength' , longLength ) ;
217218 }
218219
219220 // First run changes for all releases.
220221 if ( TBCore . shortVersion > lastVersion ) {
221222 // 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.
223224 TBCore . getToolboxDevs ( ) ; // always repopulate tb devs for each version change
224225
225226 // 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.
226227
227228 // Start: version changes.
228229 // reportsThreshold should be 0 by default
229230 if ( lastVersion < 50101 ) {
230- await TBStorage . setSettingAsync ( 'QueueTools' , 'reportsThreshold' , 0 ) ;
231+ await setSettingAsync ( 'QueueTools' , 'reportsThreshold' , 0 ) ;
231232 }
232233
233234 // Clean up removed settings - it doesn't really matter what version
234235 // we're coming from, we just want to make sure these removed settings
235236 // aren't cluttering up storage
236- await Promise . all ( [
237+ const keysToDelete = [
237238 // 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',
240241
241242 // Beta mode setting removed in favor of dedicated beta builds #917
242- TBStorage . setSettingAsync ( SETTINGS_NAME , ' betaMode', undefined ) ,
243+ 'Toolbox.Utils. betaMode',
243244
244245 // (old) modmail pro removed in v7, RIP old modmail
245246 ...[
@@ -265,26 +266,27 @@ async function doSettingsUpdates () {
265266 'entryProcessRate' ,
266267 'chunkProcessSize' ,
267268 'twoPhaseProcessing' ,
268- ] . map ( setting => TBStorage . setSettingAsync ( ' ModMail' , setting , undefined ) ) ,
269+ ] . map ( setting => `Toolbox. ModMail. ${ setting } ` ) ,
269270
270271 // new reddit is dead, long live shreddit i guess. the setting to
271272 // skip the new reddit lightbox when viewing comments no longer
272273 // 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 ] ) ) ) ;
275277
276278 // End: version changes.
277279
278280 // 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' , '' ) ;
280282 if ( settingSubEnabled ) {
281283 TBCore . setWikiPrivate ( 'tbsettings' , settingSubEnabled , false ) ;
282284 }
283285
284286 // 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 ) ;
288290 }
289291}
290292
@@ -295,13 +297,13 @@ async function doSettingsUpdates () {
295297 }
296298
297299 // Create a logger
298- const logger = TBLog ( 'Init' ) ;
300+ const log = createLogger ( 'Init' ) ;
299301
300302 // Ensure that other conditions are met, and return early if not
301303 try {
302304 await checkLoadConditions ( ) ;
303305 } catch ( error ) {
304- logger . error ( 'Load condition not met:' , ( error as Error ) . message ) ;
306+ log . error ( 'Load condition not met:' , error ) ;
305307 return ;
306308 }
307309
@@ -391,7 +393,7 @@ async function doSettingsUpdates () {
391393 OldReddit ,
392394 ]
393395 ) {
394- logger . debug ( 'Registering module' , m ) ;
396+ log . debug ( 'Registering module' , m ) ;
395397 TBModule . register_module ( m ) ;
396398 }
397399
0 commit comments