Commit 65d131cd authored by Robert Knight's avatar Robert Knight

Add types for "defaults" store module

parent ef9426db
import { watch } from '../util/watch';
/**
* @typedef {import('../store/modules/defaults').Key} Key
*/
/** @type {Record<Key, string>} */
const DEFAULT_KEYS = {
annotationPrivacy: 'hypothesis.privacy',
focusedGroup: 'hypothesis.groups.focus',
......@@ -46,7 +51,7 @@ export class PersistedDefaultsService {
Object.keys(DEFAULT_KEYS).forEach(defaultKey => {
// `localStorage.getItem` will return `null` for a non-existent key
const defaultValue = this._storage.getItem(DEFAULT_KEYS[defaultKey]);
this._store.setDefault(defaultKey, defaultValue);
this._store.setDefault(/** @type {Key} */ (defaultKey), defaultValue);
});
// Listen for changes to those defaults from the store and persist them
......
import * as util from '../util';
import { createStoreModule } from '../create-store';
import { createStoreModule, makeAction } from '../create-store';
/**
* A store module for managing client-side user-convenience defaults.
......@@ -21,16 +19,27 @@ const initialState = {
focusedGroup: /** @type {string|null} */ (null),
};
/**
* @typedef {keyof initialState} Key
* @typedef {typeof initialState} State
*/
const reducers = {
SET_DEFAULT: function (state, action) {
/**
* @param {State} state
* @param {{ defaultKey: Key, value: string|null }} action
*/
SET_DEFAULT(state, action) {
return { [action.defaultKey]: action.value };
},
};
const actions = util.actionTypes(reducers);
/**
* @param {Key} defaultKey
* @param {string|null} value
*/
function setDefault(defaultKey, value) {
return { type: actions.SET_DEFAULT, defaultKey, value };
return makeAction(reducers, 'SET_DEFAULT', { defaultKey, value });
}
/** Selectors */
......@@ -38,13 +47,14 @@ function setDefault(defaultKey, value) {
/**
* Retrieve the state's current value for `defaultKey`.
*
* @return {string|null} - The current value for `defaultKey` or `undefined` if it is not
* present
* @param {State} state
* @param {Key} defaultKey
*/
function getDefault(state, defaultKey) {
return state[defaultKey];
}
/** @param {State} state */
function getDefaults(state) {
return state;
}
......
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