Skip to content
Snippets Groups Projects
Select Git revision
  • cb1f0038b696c9862ca83acc013aa394a7970657
  • master default protected
  • v1.22.9
  • v1.22.8
  • v1.22.7
  • v1.22.6
  • v1.22.5
  • v1.22.4
  • v1.22.3
  • v1.22.1
  • v1.22.0
  • v1.21.0
  • v1.20.5
  • v1.20.4
  • v1.20.3
  • v1.20.2
  • v1.20.1
  • v1.20.0
  • v1.19.0
  • v1.18.3
  • v1.18.2
  • v1.18.1
22 results

issue-17_test.go

Blame
  • storage.mjs 2.90 KiB
    /**
     * Copyright schukai GmbH and contributors 2022. All Rights Reserved.
     * Node module: @schukai/monster
     * This file is licensed under the AGPLv3 License.
     * License text available at https://www.gnu.org/licenses/agpl-3.0.en.html
     */
    
    import {internalSymbol,instanceSymbol} from "../../constants.mjs";
    import {validateString} from "../../types/validate.mjs";
    import {Datasource} from "../datasource.mjs";
    
    export {Storage, storageObjectSymbol}
    
    /**
     * @private
     * @type {symbol}
     */
    const storageObjectSymbol = Symbol.for  ('@schukai/monster/data/datasource/storage/@@storageObject')
    
    /**
     * The class represents a record.
     * 
     * @license AGPLv3
     * @since 1.22.0
     * @copyright schukai GmbH
     * @memberOf Monster.Data.Datasource
     * @summary The Storage class encapsulates the access to data objects over WebStorageAPI.
     */
    class Storage extends Datasource {
    
        /**
         *
         * @param {string} key LocalStorage Key
         * @throws {TypeError} value is not a string
         */
        constructor(key) {
            super();
            this.setOption('key', validateString(key));
        }
    
        /**
         * This method is called by the `instanceof` operator.
         * @returns {symbol}
         * @since 2.1.0
         */
        static get [instanceSymbol]() {
            return Symbol.for("@schukai/monster/data/datasource/storage");
        }    
    
        /**
         * @property {string} key=undefined LocalStorage Key
         */
        get defaults() {
            return Object.assign({}, super.defaults, {
                key: undefined,
            });
        }
    
        /**
         * @throws {Error} this method must be implemented by derived classes.
         * @return {external:Storage}
         * @private
         */
        [storageObjectSymbol]() {
            throw new Error("this method must be implemented by derived classes")
        }
    
        /**
         * @return {Promise}
         * @throws {Error} the options does not contain a valid json definition
         * @throws {TypeError} value is not a object
         * @throws {Error} the data cannot be read
         */
        read() {
            const self = this;
    
            const storage = self[storageObjectSymbol]();
    
            return new Promise(function (resolve) {
                const data = JSON.parse(storage.getItem(self.getOption('key')));
                self.set(data??{});
                resolve();
            })
    
        }
    
        /**
         * @return {Storage}
         * @throws {Error} the data cannot be written
         */
        write() {
            const self = this;
    
            const storage = self[storageObjectSymbol]();
    
            return new Promise(function (resolve) {
    
                const data = self.get();
                if (data === undefined) {
                    storage.removeItem(self.getOption('key'));
                } else {
                    storage.setItem(self.getOption('key'), JSON.stringify(data));
                }
    
                resolve();
            })
        }
    
        /**
         * @return {Storage}
         */
        getClone() {
            const self=this;
            return new Storage(self[internalSymbol].getRealSubject()['options'].key);
        } 
        
    }