github.com/hernad/nomad@v1.6.112/ui/app/utils/properties/local-storage.js (about)

     1  /**
     2   * Copyright (c) HashiCorp, Inc.
     3   * SPDX-License-Identifier: MPL-2.0
     4   */
     5  
     6  import { computed } from '@ember/object';
     7  
     8  // An Ember.Computed property that persists set values in localStorage
     9  // and will attempt to get its initial value from localStorage before
    10  // falling back to a default.
    11  //
    12  // ex. showTutorial: localStorageProperty('nomadTutorial', true),
    13  export default function localStorageProperty(localStorageKey, defaultValue) {
    14    return computed({
    15      get() {
    16        const persistedValue = window.localStorage.getItem(localStorageKey);
    17        return persistedValue ? JSON.parse(persistedValue) : defaultValue;
    18      },
    19      set(key, value) {
    20        window.localStorage.setItem(localStorageKey, JSON.stringify(value));
    21        return value;
    22      },
    23    });
    24  }