/* @nexim/alpine v1.1.3 */ "use strict"; var __create = Object.create; var __defProp = Object.defineProperty; var __getOwnPropDesc = Object.getOwnPropertyDescriptor; var __getOwnPropNames = Object.getOwnPropertyNames; var __getProtoOf = Object.getPrototypeOf; var __hasOwnProp = Object.prototype.hasOwnProperty; var __export = (target, all) => { for (var name in all) __defProp(target, name, { get: all[name], enumerable: true }); }; var __copyProps = (to, from, except, desc) => { if (from && typeof from === "object" || typeof from === "function") { for (let key of __getOwnPropNames(from)) if (!__hasOwnProp.call(to, key) && key !== except) __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable }); } return to; }; var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps( // If the importer is in node compatibility mode or this is not an ESM // file that has been converted to a CommonJS file using a Babel- // compatible transform (i.e. "__esModule" has not been set), then set // "default" to the CommonJS "module.exports" for node compatibility. isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target, mod )); var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod); // src/main.ts var main_exports = {}; __export(main_exports, { AlpineStore: () => AlpineStore, AlpineStoreWithBackup: () => AlpineStoreWithBackup, alpineStoreGenerator: () => alpineStoreGenerator }); module.exports = __toCommonJS(main_exports); var import_package_tracer = require("@alwatr/package-tracer"); // src/store/store-generator.ts var import_logger = require("@alwatr/logger"); var import_alpinejs = __toESM(require("alpinejs"), 1); var logger = (0, import_logger.createLogger)("@nexim/alpine"); function alpineStoreGenerator(config) { logger.logMethodArgs?.("alpineStoreGenerator", { config }); import_alpinejs.default.store(config.name, config.defaultValue); const store = import_alpinejs.default.store(config.name); return store; } // src/store/store.ts var import_logger2 = require("@alwatr/logger"); var AlpineStore = class { /** * Provides a Alpine.js pure store implementation with logger. * * @param config - Configuration object. * * @example * ``` * import {AlpineStore} from '@nexim/alpine'; * * const {store} = new AlpineStore({ * name: 'myStore', * defaultValue: {data: 'root'}, * }); * console.log(store.data); // Output: { data: 'root' } * * store.data = 'user'; * console.log(store.data); // Output: { data: 'user' } * ``` */ constructor(config) { this.logger_ = (0, import_logger2.createLogger)(`${"@nexim/alpine"}:${config.name}`); this.logger_.logMethodArgs?.("constructor", config); this.store = alpineStoreGenerator(config); } }; // src/store/store-with-backup.ts var import_local_storage = require("@alwatr/local-storage"); var import_parse_duration = require("@alwatr/parse-duration"); var schemaVersion = 1; var AlpineStoreWithBackup = class extends AlpineStore { /** * Provides a Alpine.js store implementation with backup and expiration. * * @param config__ - Configuration object. * * @example * ``` * import {AlpineStoreWithBackup} from '@nexim/alpine'; * * const storeWithBackup = new AlpineStoreWithBackup({ * name: 'myStoreWithBackup', * version: 1, * defaultValue: {data: 'root'}, * expireDuration: '1d', * }); * * storeWithBackup.store.data = 'user'; * * storeWithBackup.save(); * console.log(storeWithBackup.store.data); // Output: { data: 'user' } * * storeWithBackup.clear(); * console.log(storeWithBackup.store.data); // Output: { data: 'root' } * ``` */ constructor(config__) { super(config__); this.config__ = config__; /** * Keys for storing data and expire time in local storage with version. */ this.localStorageKey__ = { data: `[${"@nexim/alpine"}:data:sv${schemaVersion}]:${this.config__.name}`, expireTime: `[${"@nexim/alpine"}:expire-time:sv${schemaVersion}]:${this.config__.name}` }; if (this.config__.expireDuration != null) { this.handleDataExpiration__(); } this.load__(); } /** * Saves the current data to local storage. If the data is null, it clears the stored data. * * Also updates the expiration time. */ save() { this.logger_.logMethodArgs?.("save", { data: this.store.data }); if (this.store.data === null) { this.clear(); return; } import_local_storage.localJsonStorage.setItem(this.localStorageKey__.data, this.store.data, this.config__.version); this.updateExpireTime__(); } /** * Clears the stored data. */ clear() { this.logger_.logMethod?.("clear"); import_local_storage.localJsonStorage.removeItem(this.localStorageKey__.data, this.config__.version); import_local_storage.localJsonStorage.removeItem(this.localStorageKey__.expireTime, this.config__.version); this.store = this.config__.defaultValue; } /** * Handles the expiration duration by checking if the stored data has expired. * If expired, it clears the stored data. */ handleDataExpiration__() { this.logger_.logMethod?.("handleDataExpiration__"); const expireDuration = import_local_storage.localJsonStorage.getItem( this.localStorageKey__.expireTime, { time: -1 }, this.config__.version ).time; if (expireDuration !== -1 && expireDuration < Date.now()) { this.clear(); } } /** * Loads data from local storage and updates the store's data. * * When data is not found or invalid in local storage, it uses the default value. * * FIXME: remove `NonNullable` from ``, after local storage new version. */ load__() { this.logger_.logMethod?.("load__"); const newData = import_local_storage.localJsonStorage.getItem( this.localStorageKey__.data, this.config__.defaultValue.data, this.config__.version ); this.store.data = newData; } /** * Updates the expiration time in local storage to the current time plus the configured expiration duration. */ updateExpireTime__() { if (this.config__.expireDuration == null) return; this.logger_.logMethod?.("updateExpireTime__"); const newExpireTime = Date.now() + (0, import_parse_duration.parseDuration)(this.config__.expireDuration); import_local_storage.localJsonStorage.setItem(this.localStorageKey__.expireTime, { time: newExpireTime }, this.config__.version); this.logger_.logOther?.("updated_expire_time", { newExpireTime }); } }; // src/main.ts __dev_mode__: import_package_tracer.packageTracer.add("@nexim/alpine", "1.1.3"); /*! For license information please see main.cjs.LEGAL.txt */ //# sourceMappingURL=main.cjs.map