MobileFirstLLC/shortcuts-for-chrome

View on GitHub
src/shared/storage.js

Summary

Maintainability
A
0 mins
Test Coverage
/**
 * @class Storage
 *
 * @classdesc Application storage for persisting data. Persisted data
 * includes: pinned links (user preference) and recently used links
 * (based on user behavior). This storage is stored in chrome sync
 * storage, which is specific to current user, and will sync between
 * devices if user is signed in and sync is enabled.
 *
 * @see {@link https://developer.chrome.com/docs/extensions/reference/storage/#usage | Chrome storage}
 *
 * !!! info "Required Permissions"
 *     This feature requires `storage` permission in extension manifest.
 */
export default class Storage {

    /**
     * @constant
     * @static
     * @enum
     * @memberOf Storage
     * @description List of storage keys. Only these keys can be stored
     * in this storage.
     * @returns {{Object}}
     */
    static get keys() {
        return {pinned: 'pinned', recent: 'recent'};
    }

    /**
     * @memberOf Storage
     * @static
     * @description
     *
     * !!! example "Get values from storage"
     *     ```js linenums="0"
     *     Storage.get([Storage.keys.recent], items => {
     *       // do something with items
     *     });
     *     ```
     *
     * @param {string|Array<string>|Object} keys Must be one of:
     * A single key to get, list of keys to get, or a dictionary
     * specifying default values (see description of the object).
     * An empty list or object will return an empty result object.
     * Pass in null to get the entire contents of storage.
     *
     * @param {function} callback - Function to call with result.
     */
    static get(keys, callback) {
        chrome.storage.sync.get(keys, callback);
    }

    /**
     * @memberOf Storage
     * @static
     * @description
     *
     * !!! example "Save value to storage"
     *     ```{ .js linenums="0" }
     *     Storage.save(Storage.keys.recent, recentObj, callback);
     *     ```
     *
     * @param {string} key - One of  {@link Storage.keys}.
     *
     * @param {*} value - Value to save.
     *
     * @param {function?} callback - Called after save operation has
     * completed.
     */
    static save(key, value, callback = () => false) {
        chrome.storage.sync.set({[key]: value}, callback);
    }
}