Commit cd5b852c authored by Alejandro Celaya's avatar Alejandro Celaya Committed by Alejandro Celaya

Migrate PersistedDefaultsService to TS

parent 7ffe7fc4
import type { SidebarStore } from '../store';
import type { Key } from '../store/modules/defaults';
import { entries } from '../util/collections'; import { entries } from '../util/collections';
import { watch } from '../util/watch'; import { watch } from '../util/watch';
import type { LocalStorageService } from './local-storage';
/** const DEFAULT_KEYS: Record<Key, string> = {
* @typedef {import('../store/modules/defaults').Key} Key
*/
/** @type {Record<Key, string>} */
const DEFAULT_KEYS = {
annotationPrivacy: 'hypothesis.privacy', annotationPrivacy: 'hypothesis.privacy',
focusedGroup: 'hypothesis.groups.focus', focusedGroup: 'hypothesis.groups.focus',
}; };
...@@ -18,11 +16,10 @@ const DEFAULT_KEYS = { ...@@ -18,11 +16,10 @@ const DEFAULT_KEYS = {
* @inject * @inject
*/ */
export class PersistedDefaultsService { export class PersistedDefaultsService {
/** private _storage: LocalStorageService;
* @param {import('./local-storage').LocalStorageService} localStorage private _store: SidebarStore;
* @param {import('../store').SidebarStore} store
*/ constructor(localStorage: LocalStorageService, store: SidebarStore) {
constructor(localStorage, store) {
this._storage = localStorage; this._storage = localStorage;
this._store = store; this._store = store;
} }
...@@ -36,12 +33,12 @@ export class PersistedDefaultsService { ...@@ -36,12 +33,12 @@ export class PersistedDefaultsService {
/** /**
* Store subscribe callback for persisting changes to defaults. It will only * Store subscribe callback for persisting changes to defaults. It will only
* persist defaults that it "knows about" via `DEFAULT_KEYS`. * persist defaults that it "knows about" via `DEFAULT_KEYS`.
*
* @param {Record<Key, any>} defaults
* @param {Record<Key, any>} prevDefaults
*/ */
const persistChangedDefaults = (defaults, prevDefaults) => { const persistChangedDefaults = (
for (let [defaultKey, newValue] of entries(defaults)) { defaults: Record<Key, any>,
prevDefaults: Record<Key, any>
) => {
for (const [defaultKey, newValue] of entries(defaults)) {
if ( if (
prevDefaults[defaultKey] !== newValue && prevDefaults[defaultKey] !== newValue &&
defaultKey in DEFAULT_KEYS defaultKey in DEFAULT_KEYS
...@@ -52,7 +49,7 @@ export class PersistedDefaultsService { ...@@ -52,7 +49,7 @@ export class PersistedDefaultsService {
}; };
// Read persisted defaults into the store // Read persisted defaults into the store
for (let [defaultKey, key] of entries(DEFAULT_KEYS)) { for (const [defaultKey, key] of entries(DEFAULT_KEYS)) {
// `localStorage.getItem` will return `null` for a non-existent key // `localStorage.getItem` will return `null` for a non-existent key
const defaultValue = this._storage.getItem(key); const defaultValue = this._storage.getItem(key);
this._store.setDefault(defaultKey, defaultValue); this._store.setDefault(defaultKey, defaultValue);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment