@ahoo-wang/fetcher-storage
v6.0.1
Published
A lightweight, cross-environment storage library with key-based storage and automatic environment detection. Provides consistent API for browser localStorage and in-memory storage with change notifications.
Maintainers
Readme
@ahoo-wang/fetcher-storage
使用序列化、缓存和变更通知存储一个类型化值。它适合符合浏览器 Storage 模型的小型应用
偏好或令牌;可查询或事务性数据应使用数据库。
安装
pnpm add @ahoo-wang/fetcher-eventbus @ahoo-wang/fetcher-storagePeer 依赖为 @ahoo-wang/fetcher-eventbus。
示例
import { InMemoryStorage, KeyStorage } from '@ahoo-wang/fetcher-storage';
interface Preferences {
theme: 'light' | 'dark';
}
const preferences = new KeyStorage<Preferences>({
key: 'preferences',
storage: new InMemoryStorage(),
defaultValue: { theme: 'light' },
});
const removeListener = preferences.addListener({
name: 'apply-theme',
handle: ({ newValue }) => console.log(newValue?.theme),
});
preferences.set({ theme: 'dark' });
removeListener();
preferences.destroy();核心能力
- 浏览器
localStorage与内存回退。 - 每个
KeyStorage实例缓存一个类型化值;reload()从存储重新读取。 - 默认 JSON 序列化,并支持按需自定义序列化器。
- 类型化
{ oldValue, newValue }通知。 - 显式移除监听并清理所有者生命周期;
destroy()关闭存储自己创建的事件总线。
