@nexim/alpine 1.1.4 → 1.1.5
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +4 -0
- package/dist/main.cjs +4 -4
- package/dist/main.cjs.map +2 -2
- package/dist/main.mjs +4 -4
- package/dist/main.mjs.map +2 -2
- package/dist/store/store-with-backup.d.ts.map +1 -1
- package/package.json +3 -3
package/CHANGELOG.md
CHANGED
|
@@ -3,6 +3,10 @@
|
|
|
3
3
|
All notable changes to this project will be documented in this file.
|
|
4
4
|
See [Conventional Commits](https://conventionalcommits.org) for commit guidelines.
|
|
5
5
|
|
|
6
|
+
## [1.1.5](https://github.com/the-nexim/nanolib/compare/@nexim/alpine@1.1.4...@nexim/alpine@1.1.5) (2025-04-27)
|
|
7
|
+
|
|
8
|
+
**Note:** Version bump only for package @nexim/alpine
|
|
9
|
+
|
|
6
10
|
## [1.1.4](https://github.com/the-nexim/nanolib/compare/@nexim/alpine@1.1.3...@nexim/alpine@1.1.4) (2025-04-21)
|
|
7
11
|
|
|
8
12
|
**Note:** Version bump only for package @nexim/alpine
|
package/dist/main.cjs
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
/* @nexim/alpine v1.1.
|
|
1
|
+
/* @nexim/alpine v1.1.5 */
|
|
2
2
|
"use strict";
|
|
3
3
|
var __create = Object.create;
|
|
4
4
|
var __defProp = Object.defineProperty;
|
|
@@ -39,8 +39,8 @@ module.exports = __toCommonJS(main_exports);
|
|
|
39
39
|
var import_package_tracer = require("@alwatr/package-tracer");
|
|
40
40
|
|
|
41
41
|
// src/store/store-generator.ts
|
|
42
|
-
var import_alpinejs = __toESM(require("alpinejs"), 1);
|
|
43
42
|
var import_logger = require("@alwatr/logger");
|
|
43
|
+
var import_alpinejs = __toESM(require("alpinejs"), 1);
|
|
44
44
|
var logger = (0, import_logger.createLogger)("@nexim/alpine");
|
|
45
45
|
function alpineStoreGenerator(config) {
|
|
46
46
|
logger.logMethodArgs?.("alpineStoreGenerator", { config });
|
|
@@ -79,8 +79,8 @@ var AlpineStore = class {
|
|
|
79
79
|
};
|
|
80
80
|
|
|
81
81
|
// src/store/store-with-backup.ts
|
|
82
|
-
var import_parse_duration = require("@alwatr/parse-duration");
|
|
83
82
|
var import_local_storage = require("@alwatr/local-storage");
|
|
83
|
+
var import_parse_duration = require("@alwatr/parse-duration");
|
|
84
84
|
var schemaVersion = 1;
|
|
85
85
|
var AlpineStoreWithBackup = class extends AlpineStore {
|
|
86
86
|
/**
|
|
@@ -190,5 +190,5 @@ var AlpineStoreWithBackup = class extends AlpineStore {
|
|
|
190
190
|
};
|
|
191
191
|
|
|
192
192
|
// src/main.ts
|
|
193
|
-
__dev_mode__: import_package_tracer.packageTracer.add("@nexim/alpine", "1.1.
|
|
193
|
+
__dev_mode__: import_package_tracer.packageTracer.add("@nexim/alpine", "1.1.5");
|
|
194
194
|
//# sourceMappingURL=main.cjs.map
|
package/dist/main.cjs.map
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"version": 3,
|
|
3
3
|
"sources": ["../src/main.ts", "../src/store/store-generator.ts", "../src/store/store.ts", "../src/store/store-with-backup.ts"],
|
|
4
|
-
"sourcesContent": ["import { packageTracer } from '@alwatr/package-tracer';\n\n__dev_mode__: packageTracer.add(__package_name__, __package_version__);\n\nexport * from './store/store-generator.js';\nexport * from './store/store.js';\nexport * from './store/store-with-backup.js';\n", "import
|
|
5
|
-
"mappings": ";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,4BAA8B;;;ACA9B,
|
|
4
|
+
"sourcesContent": ["import { packageTracer } from '@alwatr/package-tracer';\n\n__dev_mode__: packageTracer.add(__package_name__, __package_version__);\n\nexport * from './store/store-generator.js';\nexport * from './store/store.js';\nexport * from './store/store-with-backup.js';\n", "import { createLogger } from '@alwatr/logger';\nimport alpine from 'alpinejs';\n\nconst logger = createLogger(__package_name__);\n\n/**\n * alpineStoreGenerator Options.\n *\n * @typeParam T - The type of the default value.\n */\nexport type AlpineStoreGeneratorOptions<T extends DictionaryReq> = {\n /**\n * The name of the store.\n */\n name: string;\n\n /**\n * The default value of the store.\n */\n defaultValue: T;\n};\n\n/**\n * Generates an Alpine.js store with default value.\n *\n * @typeParam T - The type of the data.\n *\n * @param config - The configuration object for the store.\n * @returns - The initialized store instance.\n *\n * @remarks\n *\n * This function uses Alpine.js to create a reactive store with a default value.\n * The store is identified by a unique name and can be accessed and manipulated\n * throughout the application. Alpine.js stores provide a simple way to manage\n * data in your application, making it easy to keep your UI in sync with your data.\n *\n * @see {@link https://alpinejs.dev/globals/alpine-store}\n *\n * @example\n * ```ts\n * const store = alpineStoreGenerator({\n * name: 'user',\n * defaultValue: {type: 'root'},\n * });\n *\n * console.log(store.type); // Output: root\n *```\n */\nexport function alpineStoreGenerator<T extends DictionaryReq>(config: AlpineStoreGeneratorOptions<T>): T {\n logger.logMethodArgs?.('alpineStoreGenerator', { config });\n\n alpine.store(config.name, config.defaultValue);\n\n // Get store Proxy\n const store = alpine.store(config.name) as T;\n return store;\n}\n", "import { type AlwatrLogger, createLogger } from '@alwatr/logger';\n\nimport { alpineStoreGenerator } from './store-generator.js';\n\n/**\n * AlpineStore Options.\n *\n * @typeParam T - The type of the store value.\n */\nexport type AlpineStoreOptions<T extends DictionaryReq> = {\n /**\n * The name of the store.\n */\n name: string;\n\n /**\n * The default value of the store.\n */\n defaultValue: T;\n};\n\n/**\n * Provides a Alpine.js pure store implementation with logger.\n */\nexport class AlpineStore<T extends DictionaryReq> {\n /**\n * The store's data.\n */\n store: T;\n\n protected logger_: AlwatrLogger;\n\n /**\n * Provides a Alpine.js pure store implementation with logger.\n *\n * @param config - Configuration object.\n *\n * @example\n * ```ts\n * import {AlpineStore} from '@nexim/alpine';\n *\n * const {store} = new AlpineStore({\n * name: 'myStore',\n * defaultValue: {data: 'root'},\n * });\n * console.log(store.data); // Output: { data: 'root' }\n *\n * store.data = 'user';\n * console.log(store.data); // Output: { data: 'user' }\n * ```\n */\n constructor(config: AlpineStoreOptions<T>) {\n this.logger_ = createLogger(`${__package_name__}:${config.name}`);\n this.logger_.logMethodArgs?.('constructor', config);\n\n this.store = alpineStoreGenerator(config);\n }\n}\n", "import { localJsonStorage } from '@alwatr/local-storage';\nimport { type Duration, parseDuration } from '@alwatr/parse-duration';\n\nimport { AlpineStore } from './store.js';\n\n/**\n * Type for the store's data to extends from them.\n */\nexport type AlpineStoreWithBackupType = {\n data: DictionaryReq | null;\n};\n\n/**\n * AlpineStoreWithBackup Options.\n *\n * @typeParam T - The type of the store value.\n */\nexport type AlpineStoreWithBackupOptions<T extends AlpineStoreWithBackupType> = {\n /**\n * The name of the store.\n */\n name: string;\n\n /**\n * The version of the store.\n */\n version: number;\n\n /**\n * The default value of the store.\n */\n defaultValue: T;\n\n /**\n * Optional. The duration after which the store expires.\n */\n expireDuration?: Duration;\n};\n\n/**\n * Version of the schema for storing data in local storage.\n *\n * Change when this schema changes.\n */\nconst schemaVersion = 1;\n\n/**\n * Provides a Alpine.js store implementation with backup and expiration.\n */\nexport class AlpineStoreWithBackup<T extends AlpineStoreWithBackupType> extends AlpineStore<T> {\n /**\n * Keys for storing data and expire time in local storage with version.\n */\n private localStorageKey__ = {\n data: `[${__package_name__}:data:sv${schemaVersion.toString()}]:${this.config__.name}`,\n expireTime: `[${__package_name__}:expire-time:sv${schemaVersion.toString()}]:${this.config__.name}`,\n };\n\n /**\n * Provides a Alpine.js store implementation with backup and expiration.\n *\n * @param config__ - Configuration object.\n *\n * @example\n * ```ts\n * import {AlpineStoreWithBackup} from '@nexim/alpine';\n *\n * const storeWithBackup = new AlpineStoreWithBackup({\n * name: 'myStoreWithBackup',\n * version: 1,\n * defaultValue: {data: 'root'},\n * expireDuration: '1d',\n * });\n *\n * storeWithBackup.store.data = 'user';\n *\n * storeWithBackup.save();\n * console.log(storeWithBackup.store.data); // Output: { data: 'user' }\n *\n * storeWithBackup.clear();\n * console.log(storeWithBackup.store.data); // Output: { data: 'root' }\n * ```\n */\n constructor(private config__: AlpineStoreWithBackupOptions<T>) {\n super(config__);\n\n if (this.config__.expireDuration != null) {\n this.handleDataExpiration__();\n }\n\n this.load__();\n }\n\n /**\n * Saves the current data to local storage. If the data is null, it clears the stored data.\n *\n * Also updates the expiration time.\n */\n save(): void {\n this.logger_.logMethodArgs?.('save', { data: this.store.data });\n\n if (this.store.data === null) {\n this.clear();\n return;\n }\n\n localJsonStorage.setItem(this.localStorageKey__.data, this.store.data, this.config__.version);\n this.updateExpireTime__();\n }\n\n /**\n * Clears the stored data.\n */\n clear(): void {\n this.logger_.logMethod?.('clear');\n\n localJsonStorage.removeItem(this.localStorageKey__.data, this.config__.version);\n localJsonStorage.removeItem(this.localStorageKey__.expireTime, this.config__.version);\n\n this.store = this.config__.defaultValue;\n }\n\n /**\n * Handles the expiration duration by checking if the stored data has expired.\n * If expired, it clears the stored data.\n */\n private handleDataExpiration__(): void {\n this.logger_.logMethod?.('handleDataExpiration__');\n\n // FIXME: use null if not set, after local storage new version.\n const expireDuration = localJsonStorage.getItem<{ time: number }>(\n this.localStorageKey__.expireTime,\n { time: -1 },\n this.config__.version,\n ).time;\n\n if (expireDuration !== -1 && expireDuration < Date.now()) {\n this.clear();\n }\n }\n\n /**\n * Loads data from local storage and updates the store's data.\n *\n * When data is not found or invalid in local storage, it uses the default value.\n *\n * FIXME: remove `NonNullable` from `<T['data']>`, after local storage new version.\n */\n private load__(): void {\n this.logger_.logMethod?.('load__');\n\n const newData = localJsonStorage.getItem<NonNullable<T['data']>>(\n this.localStorageKey__.data,\n this.config__.defaultValue.data as NonNullable<T['data']>,\n this.config__.version,\n ) as T['data'];\n this.store.data = newData;\n }\n\n /**\n * Updates the expiration time in local storage to the current time plus the configured expiration duration.\n */\n private updateExpireTime__(): void {\n if (this.config__.expireDuration == null) return;\n this.logger_.logMethod?.('updateExpireTime__');\n\n const newExpireTime = Date.now() + parseDuration(this.config__.expireDuration);\n localJsonStorage.setItem(this.localStorageKey__.expireTime, { time: newExpireTime }, this.config__.version);\n\n this.logger_.logOther?.('updated_expire_time', { newExpireTime });\n }\n}\n"],
|
|
5
|
+
"mappings": ";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,4BAA8B;;;ACA9B,oBAA6B;AAC7B,sBAAmB;AAEnB,IAAM,aAAS,4BAAa,eAAgB;AA8CrC,SAAS,qBAA8C,QAA2C;AACvG,SAAO,gBAAgB,wBAAwB,EAAE,OAAO,CAAC;AAEzD,kBAAAA,QAAO,MAAM,OAAO,MAAM,OAAO,YAAY;AAG7C,QAAM,QAAQ,gBAAAA,QAAO,MAAM,OAAO,IAAI;AACtC,SAAO;AACT;;;ACzDA,IAAAC,iBAAgD;AAwBzC,IAAM,cAAN,MAA2C;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EA2BhD,YAAY,QAA+B;AACzC,SAAK,cAAU,6BAAa,GAAG,eAAgB,IAAI,OAAO,IAAI,EAAE;AAChE,SAAK,QAAQ,gBAAgB,eAAe,MAAM;AAElD,SAAK,QAAQ,qBAAqB,MAAM;AAAA,EAC1C;AACF;;;ACzDA,2BAAiC;AACjC,4BAA6C;AA2C7C,IAAM,gBAAgB;AAKf,IAAM,wBAAN,cAAyE,YAAe;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAkC7F,YAAoB,UAA2C;AAC7D,UAAM,QAAQ;AADI;AA9BpB;AAAA;AAAA;AAAA,SAAQ,oBAAoB;AAAA,MAC1B,MAAM,IAAI,eAAgB,WAAW,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B,KAAK,SAAS,aAAa;AAAA,MAC3B,KAAK,SAAS;AAAA,IAChB;AACA,SAAK,MAAM,OAAO;AAAA,EACpB;AAAA;AAAA;AAAA;AAAA,EAKQ,qBAA2B;AACjC,QAAI,KAAK,SAAS,kBAAkB,KAAM;AAC1C,SAAK,QAAQ,YAAY,oBAAoB;AAE7C,UAAM,gBAAgB,KAAK,IAAI,QAAI,qCAAc,KAAK,SAAS,cAAc;AAC7E,0CAAiB,QAAQ,KAAK,kBAAkB,YAAY,EAAE,MAAM,cAAc,GAAG,KAAK,SAAS,OAAO;AAE1G,SAAK,QAAQ,WAAW,uBAAuB,EAAE,cAAc,CAAC;AAAA,EAClE;AACF;;;AHzKA,aAAc,qCAAc,IAAI,iBAAkB,OAAmB;",
|
|
6
6
|
"names": ["alpine", "import_logger"]
|
|
7
7
|
}
|
package/dist/main.mjs
CHANGED
|
@@ -1,11 +1,11 @@
|
|
|
1
|
-
/* @nexim/alpine v1.1.
|
|
1
|
+
/* @nexim/alpine v1.1.5 */
|
|
2
2
|
|
|
3
3
|
// src/main.ts
|
|
4
4
|
import { packageTracer } from "@alwatr/package-tracer";
|
|
5
5
|
|
|
6
6
|
// src/store/store-generator.ts
|
|
7
|
-
import alpine from "alpinejs";
|
|
8
7
|
import { createLogger } from "@alwatr/logger";
|
|
8
|
+
import alpine from "alpinejs";
|
|
9
9
|
var logger = createLogger("@nexim/alpine");
|
|
10
10
|
function alpineStoreGenerator(config) {
|
|
11
11
|
logger.logMethodArgs?.("alpineStoreGenerator", { config });
|
|
@@ -44,8 +44,8 @@ var AlpineStore = class {
|
|
|
44
44
|
};
|
|
45
45
|
|
|
46
46
|
// src/store/store-with-backup.ts
|
|
47
|
-
import { parseDuration } from "@alwatr/parse-duration";
|
|
48
47
|
import { localJsonStorage } from "@alwatr/local-storage";
|
|
48
|
+
import { parseDuration } from "@alwatr/parse-duration";
|
|
49
49
|
var schemaVersion = 1;
|
|
50
50
|
var AlpineStoreWithBackup = class extends AlpineStore {
|
|
51
51
|
/**
|
|
@@ -155,7 +155,7 @@ var AlpineStoreWithBackup = class extends AlpineStore {
|
|
|
155
155
|
};
|
|
156
156
|
|
|
157
157
|
// src/main.ts
|
|
158
|
-
__dev_mode__: packageTracer.add("@nexim/alpine", "1.1.
|
|
158
|
+
__dev_mode__: packageTracer.add("@nexim/alpine", "1.1.5");
|
|
159
159
|
export {
|
|
160
160
|
AlpineStore,
|
|
161
161
|
AlpineStoreWithBackup,
|
package/dist/main.mjs.map
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"version": 3,
|
|
3
3
|
"sources": ["../src/main.ts", "../src/store/store-generator.ts", "../src/store/store.ts", "../src/store/store-with-backup.ts"],
|
|
4
|
-
"sourcesContent": ["import { packageTracer } from '@alwatr/package-tracer';\n\n__dev_mode__: packageTracer.add(__package_name__, __package_version__);\n\nexport * from './store/store-generator.js';\nexport * from './store/store.js';\nexport * from './store/store-with-backup.js';\n", "import
|
|
5
|
-
"mappings": ";;;AAAA,SAAS,qBAAqB;;;ACA9B,
|
|
4
|
+
"sourcesContent": ["import { packageTracer } from '@alwatr/package-tracer';\n\n__dev_mode__: packageTracer.add(__package_name__, __package_version__);\n\nexport * from './store/store-generator.js';\nexport * from './store/store.js';\nexport * from './store/store-with-backup.js';\n", "import { createLogger } from '@alwatr/logger';\nimport alpine from 'alpinejs';\n\nconst logger = createLogger(__package_name__);\n\n/**\n * alpineStoreGenerator Options.\n *\n * @typeParam T - The type of the default value.\n */\nexport type AlpineStoreGeneratorOptions<T extends DictionaryReq> = {\n /**\n * The name of the store.\n */\n name: string;\n\n /**\n * The default value of the store.\n */\n defaultValue: T;\n};\n\n/**\n * Generates an Alpine.js store with default value.\n *\n * @typeParam T - The type of the data.\n *\n * @param config - The configuration object for the store.\n * @returns - The initialized store instance.\n *\n * @remarks\n *\n * This function uses Alpine.js to create a reactive store with a default value.\n * The store is identified by a unique name and can be accessed and manipulated\n * throughout the application. Alpine.js stores provide a simple way to manage\n * data in your application, making it easy to keep your UI in sync with your data.\n *\n * @see {@link https://alpinejs.dev/globals/alpine-store}\n *\n * @example\n * ```ts\n * const store = alpineStoreGenerator({\n * name: 'user',\n * defaultValue: {type: 'root'},\n * });\n *\n * console.log(store.type); // Output: root\n *```\n */\nexport function alpineStoreGenerator<T extends DictionaryReq>(config: AlpineStoreGeneratorOptions<T>): T {\n logger.logMethodArgs?.('alpineStoreGenerator', { config });\n\n alpine.store(config.name, config.defaultValue);\n\n // Get store Proxy\n const store = alpine.store(config.name) as T;\n return store;\n}\n", "import { type AlwatrLogger, createLogger } from '@alwatr/logger';\n\nimport { alpineStoreGenerator } from './store-generator.js';\n\n/**\n * AlpineStore Options.\n *\n * @typeParam T - The type of the store value.\n */\nexport type AlpineStoreOptions<T extends DictionaryReq> = {\n /**\n * The name of the store.\n */\n name: string;\n\n /**\n * The default value of the store.\n */\n defaultValue: T;\n};\n\n/**\n * Provides a Alpine.js pure store implementation with logger.\n */\nexport class AlpineStore<T extends DictionaryReq> {\n /**\n * The store's data.\n */\n store: T;\n\n protected logger_: AlwatrLogger;\n\n /**\n * Provides a Alpine.js pure store implementation with logger.\n *\n * @param config - Configuration object.\n *\n * @example\n * ```ts\n * import {AlpineStore} from '@nexim/alpine';\n *\n * const {store} = new AlpineStore({\n * name: 'myStore',\n * defaultValue: {data: 'root'},\n * });\n * console.log(store.data); // Output: { data: 'root' }\n *\n * store.data = 'user';\n * console.log(store.data); // Output: { data: 'user' }\n * ```\n */\n constructor(config: AlpineStoreOptions<T>) {\n this.logger_ = createLogger(`${__package_name__}:${config.name}`);\n this.logger_.logMethodArgs?.('constructor', config);\n\n this.store = alpineStoreGenerator(config);\n }\n}\n", "import { localJsonStorage } from '@alwatr/local-storage';\nimport { type Duration, parseDuration } from '@alwatr/parse-duration';\n\nimport { AlpineStore } from './store.js';\n\n/**\n * Type for the store's data to extends from them.\n */\nexport type AlpineStoreWithBackupType = {\n data: DictionaryReq | null;\n};\n\n/**\n * AlpineStoreWithBackup Options.\n *\n * @typeParam T - The type of the store value.\n */\nexport type AlpineStoreWithBackupOptions<T extends AlpineStoreWithBackupType> = {\n /**\n * The name of the store.\n */\n name: string;\n\n /**\n * The version of the store.\n */\n version: number;\n\n /**\n * The default value of the store.\n */\n defaultValue: T;\n\n /**\n * Optional. The duration after which the store expires.\n */\n expireDuration?: Duration;\n};\n\n/**\n * Version of the schema for storing data in local storage.\n *\n * Change when this schema changes.\n */\nconst schemaVersion = 1;\n\n/**\n * Provides a Alpine.js store implementation with backup and expiration.\n */\nexport class AlpineStoreWithBackup<T extends AlpineStoreWithBackupType> extends AlpineStore<T> {\n /**\n * Keys for storing data and expire time in local storage with version.\n */\n private localStorageKey__ = {\n data: `[${__package_name__}:data:sv${schemaVersion.toString()}]:${this.config__.name}`,\n expireTime: `[${__package_name__}:expire-time:sv${schemaVersion.toString()}]:${this.config__.name}`,\n };\n\n /**\n * Provides a Alpine.js store implementation with backup and expiration.\n *\n * @param config__ - Configuration object.\n *\n * @example\n * ```ts\n * import {AlpineStoreWithBackup} from '@nexim/alpine';\n *\n * const storeWithBackup = new AlpineStoreWithBackup({\n * name: 'myStoreWithBackup',\n * version: 1,\n * defaultValue: {data: 'root'},\n * expireDuration: '1d',\n * });\n *\n * storeWithBackup.store.data = 'user';\n *\n * storeWithBackup.save();\n * console.log(storeWithBackup.store.data); // Output: { data: 'user' }\n *\n * storeWithBackup.clear();\n * console.log(storeWithBackup.store.data); // Output: { data: 'root' }\n * ```\n */\n constructor(private config__: AlpineStoreWithBackupOptions<T>) {\n super(config__);\n\n if (this.config__.expireDuration != null) {\n this.handleDataExpiration__();\n }\n\n this.load__();\n }\n\n /**\n * Saves the current data to local storage. If the data is null, it clears the stored data.\n *\n * Also updates the expiration time.\n */\n save(): void {\n this.logger_.logMethodArgs?.('save', { data: this.store.data });\n\n if (this.store.data === null) {\n this.clear();\n return;\n }\n\n localJsonStorage.setItem(this.localStorageKey__.data, this.store.data, this.config__.version);\n this.updateExpireTime__();\n }\n\n /**\n * Clears the stored data.\n */\n clear(): void {\n this.logger_.logMethod?.('clear');\n\n localJsonStorage.removeItem(this.localStorageKey__.data, this.config__.version);\n localJsonStorage.removeItem(this.localStorageKey__.expireTime, this.config__.version);\n\n this.store = this.config__.defaultValue;\n }\n\n /**\n * Handles the expiration duration by checking if the stored data has expired.\n * If expired, it clears the stored data.\n */\n private handleDataExpiration__(): void {\n this.logger_.logMethod?.('handleDataExpiration__');\n\n // FIXME: use null if not set, after local storage new version.\n const expireDuration = localJsonStorage.getItem<{ time: number }>(\n this.localStorageKey__.expireTime,\n { time: -1 },\n this.config__.version,\n ).time;\n\n if (expireDuration !== -1 && expireDuration < Date.now()) {\n this.clear();\n }\n }\n\n /**\n * Loads data from local storage and updates the store's data.\n *\n * When data is not found or invalid in local storage, it uses the default value.\n *\n * FIXME: remove `NonNullable` from `<T['data']>`, after local storage new version.\n */\n private load__(): void {\n this.logger_.logMethod?.('load__');\n\n const newData = localJsonStorage.getItem<NonNullable<T['data']>>(\n this.localStorageKey__.data,\n this.config__.defaultValue.data as NonNullable<T['data']>,\n this.config__.version,\n ) as T['data'];\n this.store.data = newData;\n }\n\n /**\n * Updates the expiration time in local storage to the current time plus the configured expiration duration.\n */\n private updateExpireTime__(): void {\n if (this.config__.expireDuration == null) return;\n this.logger_.logMethod?.('updateExpireTime__');\n\n const newExpireTime = Date.now() + parseDuration(this.config__.expireDuration);\n localJsonStorage.setItem(this.localStorageKey__.expireTime, { time: newExpireTime }, this.config__.version);\n\n this.logger_.logOther?.('updated_expire_time', { newExpireTime });\n }\n}\n"],
|
|
5
|
+
"mappings": ";;;AAAA,SAAS,qBAAqB;;;ACA9B,SAAS,oBAAoB;AAC7B,OAAO,YAAY;AAEnB,IAAM,SAAS,aAAa,eAAgB;AA8CrC,SAAS,qBAA8C,QAA2C;AACvG,SAAO,gBAAgB,wBAAwB,EAAE,OAAO,CAAC;AAEzD,SAAO,MAAM,OAAO,MAAM,OAAO,YAAY;AAG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KA,aAAc,eAAc,IAAI,iBAAkB,OAAmB;",
|
|
6
6
|
"names": ["createLogger", "createLogger"]
|
|
7
7
|
}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"store-with-backup.d.ts","sourceRoot":"","sources":["../../src/store/store-with-backup.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"store-with-backup.d.ts","sourceRoot":"","sources":["../../src/store/store-with-backup.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,KAAK,QAAQ,EAAiB,MAAM,wBAAwB,CAAC;AAEtE,OAAO,EAAE,WAAW,EAAE,MAAM,YAAY,CAAC;AAEzC;;GAEG;AACH,MAAM,MAAM,yBAAyB,GAAG;IACtC,IAAI,EAAE,aAAa,GAAG,IAAI,CAAC;CAC5B,CAAC;AAEF;;;;GAIG;AACH,MAAM,MAAM,4BAA4B,CAAC,CAAC,SAAS,yBAAyB,IAAI;IAC9E;;OAEG;IACH,IAAI,EAAE,MAAM,CAAC;IAEb;;OAEG;IACH,OAAO,EAAE,MAAM,CAAC;IAEhB;;OAEG;IACH,YAAY,EAAE,CAAC,CAAC;IAEhB;;OAEG;IACH,cAAc,CAAC,EAAE,QAAQ,CAAC;CAC3B,CAAC;AASF;;GAEG;AACH,qBAAa,qBAAqB,CAAC,CAAC,SAAS,yBAAyB,CAAE,SAAQ,WAAW,CAAC,CAAC,CAAC;IAkChF,OAAO,CAAC,QAAQ;IAjC5B;;OAEG;IACH,OAAO,CAAC,iBAAiB,CAGvB;IAEF;;;;;;;;;;;;;;;;;;;;;;;;OAwBG;gBACiB,QAAQ,EAAE,4BAA4B,CAAC,CAAC,CAAC;IAU7D;;;;OAIG;IACH,IAAI,IAAI,IAAI;IAYZ;;OAEG;IACH,KAAK,IAAI,IAAI;IASb;;;OAGG;IACH,OAAO,CAAC,sBAAsB;IAe9B;;;;;;OAMG;IACH,OAAO,CAAC,MAAM;IAWd;;OAEG;IACH,OAAO,CAAC,kBAAkB;CAS3B"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@nexim/alpine",
|
|
3
|
-
"version": "1.1.
|
|
3
|
+
"version": "1.1.5",
|
|
4
4
|
"description": "Utility functions to enhance Alpine.js usage with backup support.",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"alpinejs",
|
|
@@ -59,7 +59,7 @@
|
|
|
59
59
|
"@types/alpinejs": "^3.13.11",
|
|
60
60
|
"ava": "^6.2.0",
|
|
61
61
|
"typedoc": "^0.28.3",
|
|
62
|
-
"typedoc-plugin-markdown": "^4.6.
|
|
62
|
+
"typedoc-plugin-markdown": "^4.6.3",
|
|
63
63
|
"typedoc-plugin-no-inherit": "^1.6.1",
|
|
64
64
|
"typescript": "^5.8.3",
|
|
65
65
|
"wireit": "^0.14.12"
|
|
@@ -114,5 +114,5 @@
|
|
|
114
114
|
"command": "typedoc"
|
|
115
115
|
}
|
|
116
116
|
},
|
|
117
|
-
"gitHead": "
|
|
117
|
+
"gitHead": "958e5c52a3b48808a4db41686795f8b1bfbb4b37"
|
|
118
118
|
}
|