pad_cookie: Move initial cookie read+save to `init()`

Benefits of this change:
  * It avoids race conditions with tests that clear cookies.
  * Any attempt to get or set a value before `init()` is called will
    throw an error, ensuring the API is used properly.
  * Improved readability: It's easier to understand what the
    `pad.noCookie` check is doing.
pull/4987/head
Richard Hansen 2021-03-25 23:21:27 -04:00 committed by webzwo0i
parent aeee5c0b69
commit 0df41a9a78
1 changed files with 4 additions and 3 deletions

View File

@ -21,15 +21,16 @@ const Cookies = require('./pad_utils').Cookies;
exports.padcookie = new class { exports.padcookie = new class {
constructor() { constructor() {
this.cookieName_ = window.location.protocol === 'https:' ? 'prefs' : 'prefsHttp'; this.cookieName_ = window.location.protocol === 'https:' ? 'prefs' : 'prefsHttp';
}
init() {
const prefs = this.readPrefs_() || {}; const prefs = this.readPrefs_() || {};
delete prefs.userId; delete prefs.userId;
delete prefs.name; delete prefs.name;
delete prefs.colorId; delete prefs.colorId;
this.prefs_ = prefs; this.prefs_ = prefs;
this.savePrefs_(); this.savePrefs_();
} // Re-read the saved cookie to test if cookies are enabled.
init() {
if (this.readPrefs_() == null) { if (this.readPrefs_() == null) {
$.gritter.add({ $.gritter.add({
title: 'Error', title: 'Error',