UNPKG

zustand-storage

Version:

A universal solution combining @aivron/sync-storage and zust-api for React (web & desktop). It merges local persistence with a Zustand-inspired API to provide core storage operations, bulk actions, JSON support, TTL, and integrated React hooks.

80 lines (79 loc) 2.35 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); var sync_storage_1 = require("@aivron/sync-storage"); /** * In-memory fallback storage if neither @aivron/sync-storage nor window.localStorage is available. */ var fallbackData = {}; /** * Fallback storage engine implementation using an in-memory object. */ var fallbackStorage = { getItem: function (key) { return fallbackData[key] || null; }, setItem: function (key, value) { fallbackData[key] = value; }, removeItem: function (key) { delete fallbackData[key]; } }; /** * Default storage engine that attempts to use @aivron/sync-storage first, * then falls back to window.localStorage, and finally uses an in-memory fallback. */ var defaultStorageEngine = { getItem: function (key) { try { // Use getStorageItem from @aivron/sync-storage if available. if (typeof sync_storage_1.getStorageItem === "function") { return (0, sync_storage_1.getStorageItem)(key); } } catch (e) { // If error, try next option. } try { return window.localStorage.getItem(key); } catch (e) { return fallbackStorage.getItem(key); } }, setItem: function (key, value) { try { if (typeof sync_storage_1.setStorageItem === "function") { (0, sync_storage_1.setStorageItem)(key, value); return; } } catch (e) { // Fall through to next option. } try { window.localStorage.setItem(key, value); } catch (e) { fallbackStorage.setItem(key, value); } }, removeItem: function (key) { try { if (typeof sync_storage_1.removeStorageItem === "function") { (0, sync_storage_1.removeStorageItem)(key); return; } } catch (e) { // Fall through to next option. } try { window.localStorage.removeItem(key); } catch (e) { fallbackStorage.removeItem(key); } } }; exports.default = defaultStorageEngine;