From 0df41a9a78f6635ef901742150d042eaab2cc8d4 Mon Sep 17 00:00:00 2001 From: Richard Hansen Date: Thu, 25 Mar 2021 23:21:27 -0400 Subject: [PATCH] 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. --- src/static/js/pad_cookie.js | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/src/static/js/pad_cookie.js b/src/static/js/pad_cookie.js index 620d41735..a8f5ca743 100644 --- a/src/static/js/pad_cookie.js +++ b/src/static/js/pad_cookie.js @@ -21,15 +21,16 @@ const Cookies = require('./pad_utils').Cookies; exports.padcookie = new class { constructor() { this.cookieName_ = window.location.protocol === 'https:' ? 'prefs' : 'prefsHttp'; + } + + init() { const prefs = this.readPrefs_() || {}; delete prefs.userId; delete prefs.name; delete prefs.colorId; this.prefs_ = prefs; this.savePrefs_(); - } - - init() { + // Re-read the saved cookie to test if cookies are enabled. if (this.readPrefs_() == null) { $.gritter.add({ title: 'Error',