1.0.22 • Published 1 year ago
aging-storage v1.0.22
aging-storage
基于 LocalStorage 的封装,使得键值对支持设置过期时间,同时存储的值支持多种类型(包括string | number | boolean | object | Date | null | undefined),无需转成字符串再存储。并且借用 storage 事件,实现同域下浏览器标签页之间的自定义事件传递。
Installation
npm install aging-storage
Usage
import AgingStorage from "aging-storage";
const agingStorage = new AgingStorage();
// or
const agingStorage = new AgingStorage({
preKey: 'aging',
listen: true,
eventKey: 'storage_event'
});
API
/**
* 从 LocalStorage 获得对应键值的数据。
* @param key 键的名称
*/
getItem: (key: string) => string | number | object | undefined;
/**
* 将数据以键值对的形式存到 LocalStorage 中,并且可以指定过期时间。
* @param key 键的名称
* @param value 值数据,支持字符串、数字、对象、undefined
* @param expire 有效时长,单位:s
*/
setItem: (key: string, value: string | number | object | undefined, expire?: number) => void;
/**
* 删除 LocalStorage 中给定键的数据条目,支持指定只删除过期时间前的数据条目。
* @param key 键的名称
* @param deadline 过期时间或13位时间戳
*/
removeItem: (key: string, deadline?: string | number) => void;
/**
* 批量删除 LocalStorage 中给定键的数据条目,支持指定只删除过期时间前的数据条目。
* @param keyList 一个包含键的名称和过期时间或13位时间戳的数组对象
*/
batchRemoveItem: (keyList: Array<{ key: string; deadline?: string | number }>) => void;
/**
* 在当前浏览器标签页触发一个自定义事件。第二个参数将会传递给事件监听器的回调函数。
* (只有当 new AgingStorage 时选项 listen 设成 true 时有效。)
* @param event 自定义事件名
* @param params 回调函数的入参
*/
emit: (event: string, params?: any) => void;
/**
* 注册监听一个来自其他浏览器标签页的自定义事件。
* (只有当 new AgingStorage 时选项 listen 设成 true 时有效。)
* @param event 自定义事件名
* @param callback 回调函数
*/
on: (event: string, callback: (params?: any) => void) => void;
/**
* 取消监听一个来自其他浏览器标签页的自定义事件。
* (只有当 new AgingStorage 时选项 listen 设成 true 时有效。)
* @param event 自定义事件名
* @param callback 回调函数
*/
off: (event?: string, callback?: Function) => void;
1.0.22
1 year ago
1.0.21
1 year ago
1.0.20
1 year ago
1.0.19
1 year ago
1.0.18
1 year ago
1.0.17
1 year ago
1.0.16
2 years ago
1.0.15
2 years ago
1.0.14
2 years ago
1.0.13
2 years ago
1.0.12
2 years ago
1.0.11
2 years ago
1.0.10
2 years ago
1.0.9
2 years ago
1.0.8
2 years ago
1.0.7
2 years ago
1.0.6
2 years ago
1.0.5
2 years ago
1.0.4
2 years ago
1.0.3
2 years ago
1.0.2
2 years ago
1.0.1
2 years ago
1.0.0
2 years ago