@lytjs/plugin-storage 4.0.5 → 4.2.0
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/dist/index.cjs +1 -1
- package/dist/types/index.d.ts +60 -0
- package/dist/types/index.d.ts.map +1 -0
- package/package.json +8 -6
package/dist/index.cjs
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
|
|
1
|
+
var b=Object.defineProperty;var J=Object.getOwnPropertyDescriptor;var N=Object.getOwnPropertyNames;var _=Object.prototype.hasOwnProperty;var F=(n,t)=>{for(var o in t)b(n,o,{get:t[o],enumerable:!0})},R=(n,t,o,l)=>{if(t&&typeof t=="object"||typeof t=="function")for(let g of N(t))!_.call(n,g)&&g!==o&&b(n,g,{get:()=>t[g],enumerable:!(l=J(t,g))||l.enumerable});return n};var j=n=>R(b({},"__esModule",{value:!0}),n);var B={};F(B,{createStorage:()=>A});module.exports=j(B);function w(n,t){return`${t}${n}`}function q(n,t){return n.startsWith(t)?n.slice(t.length):n}function x(n,t){let o=null;return function(...l){o&&clearTimeout(o),o=setTimeout(()=>n.apply(this,l),t)}}function A(n={}){let{prefix:t="lyt_",storage:o=typeof localStorage!="undefined"?localStorage:{},expire:l,debug:g=!1,serialize:P=JSON.stringify,deserialize:O=JSON.parse}=n,u=(...r)=>{};function c(r,e,i){try{let s={value:e,timestamp:Date.now()},a=i!=null?i:l;a&&(s.expire=Date.now()+a);let y=w(r,t);o.setItem(y,P(s)),u(`set ${r}`,e)}catch(s){}}function m(r,e){try{let i=w(r,t),s=o.getItem(i);if(!s)return e!=null?e:null;let a=O(s);return a.expire&&Date.now()>a.expire?(u(`${r} expired, removing`),h(r),e!=null?e:null):(u(`get ${r}`,a.value),a.value)}catch(i){return e!=null?e:null}}function h(r){try{let e=w(r,t);o.removeItem(e),u(`removed ${r}`)}catch(e){}}function $(){try{let r=T();for(let e of r)h(e);u("cleared all")}catch(r){}}function I(r){return m(r)!==null}function T(){try{let r=[];for(let e=0;e<o.length;e++){let i=o.key(e);i&&i.startsWith(t)&&r.push(q(i,t))}return r}catch(r){return[]}}function z(){return T().length}function W(r,e,i={}){let{immediate:s=!0,deep:a=!0,debounce:y=100}=i;if(typeof r.$watch=="function")return r.$watch(e,y>0?x(p=>{c(e,p)},y):p=>{c(e,p)},{immediate:s,deep:a});if(typeof globalThis.watch=="function"){let f=globalThis.watch;return f(()=>r[e],y>0?x(S=>{c(e,S)},y):S=>{c(e,S)},{immediate:s,deep:a})}let d=r[e],D=setInterval(()=>{let f=r[e];f!==d&&(d=f,c(e,f))},200);return s&&c(e,d),()=>clearInterval(D)}function K(r,e,i){let s=m(e,i);return s!==null&&(r[e]=s,u(`restored ${e}`,s)),r[e]}let v={install(r,e){r.config=r.config||{},r.config.globalProperties=r.config.globalProperties||{},r.config.globalProperties.$storage=v,typeof r.provide=="function"&&r.provide("storage",v)},set:c,get:m,remove:h,clear:$,has:I,keys:T,size:z,watch:W,restore:K,get storage(){return o}};return v}
|
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
/** 存储类型 */
|
|
2
|
+
type StorageType = 'local' | 'session';
|
|
3
|
+
/** 存储配置选项 */
|
|
4
|
+
interface StorageOptions {
|
|
5
|
+
/** key 前缀,默认 'lyt_' */
|
|
6
|
+
prefix?: string;
|
|
7
|
+
/** 使用的存储对象,默认 localStorage */
|
|
8
|
+
storage?: Storage;
|
|
9
|
+
/** 过期时间(毫秒),默认无过期 */
|
|
10
|
+
expire?: number;
|
|
11
|
+
/** 是否开启调试模式 */
|
|
12
|
+
debug?: boolean;
|
|
13
|
+
/** 数据序列化函数,默认 JSON.stringify */
|
|
14
|
+
serialize?: (value: any) => string;
|
|
15
|
+
/** 数据反序列化函数,默认 JSON.parse */
|
|
16
|
+
deserialize?: (value: string) => any;
|
|
17
|
+
}
|
|
18
|
+
/** 存储插件实例 */
|
|
19
|
+
interface StoragePlugin {
|
|
20
|
+
/** 安装到 Lyt 应用 */
|
|
21
|
+
install: (app: any, options?: any) => void;
|
|
22
|
+
/** 设置值 */
|
|
23
|
+
set<T = any>(key: string, value: T, expire?: number): void;
|
|
24
|
+
/** 获取值 */
|
|
25
|
+
get<T = any>(key: string, defaultValue?: T): T | null;
|
|
26
|
+
/** 删除值 */
|
|
27
|
+
remove(key: string): void;
|
|
28
|
+
/** 清空所有(仅当前前缀的) */
|
|
29
|
+
clear(): void;
|
|
30
|
+
/** 检查 key 是否存在 */
|
|
31
|
+
has(key: string): boolean;
|
|
32
|
+
/** 获取所有 key(仅当前前缀的) */
|
|
33
|
+
keys(): string[];
|
|
34
|
+
/** 获取存储使用情况 */
|
|
35
|
+
size(): number;
|
|
36
|
+
/** 监听响应式对象变化,自动保存 */
|
|
37
|
+
watch(target: any, key: string, options?: WatchOptions): () => void;
|
|
38
|
+
/** 从存储恢复响应式对象 */
|
|
39
|
+
restore<T = any>(target: any, key: string, defaultValue?: T): T;
|
|
40
|
+
/** 原始存储对象 */
|
|
41
|
+
readonly storage: Storage;
|
|
42
|
+
}
|
|
43
|
+
/** watch 配置选项 */
|
|
44
|
+
interface WatchOptions {
|
|
45
|
+
/** 是否立即执行一次保存 */
|
|
46
|
+
immediate?: boolean;
|
|
47
|
+
/** 深度监听 */
|
|
48
|
+
deep?: boolean;
|
|
49
|
+
/** 防抖延迟(毫秒) */
|
|
50
|
+
debounce?: number;
|
|
51
|
+
}
|
|
52
|
+
/**
|
|
53
|
+
* 创建本地存储持久化插件实例
|
|
54
|
+
* @param options 存储配置
|
|
55
|
+
* @returns StoragePlugin 插件实例
|
|
56
|
+
*/
|
|
57
|
+
declare function createStorage(options?: StorageOptions): StoragePlugin;
|
|
58
|
+
export { createStorage };
|
|
59
|
+
export type { StorageOptions, StoragePlugin, WatchOptions, StorageType };
|
|
60
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AAeA,WAAW;AACX,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AAI,CAAA;IACnE,iBAAiB;IACjB,OAAO,CAAC,CAAC,GAAG,GAAG,EAAE,MAAM,EAAE,GAAG,EAAE,GAAG,EAAE,MAAM,EAAE,YAAY,CAAC,EAAE,CAAC,GAAG,CAAC,CAAA;IAC/D,aAAa;IACb,QAAQ,CAAC,OAAO,EAAE,OAAO,CAAA;CAC1B;AAED,iBAAiB;AACjB,UAAU,YAAY;IACpB,iBAAiB;IACjB,SAAS,CAAC,EAAE,OAAO,CAAA;IACnB,WAAW;IACX,IAAI,CAAC,EAAE,OAAO,CAAA;IACd,eAAe;IACf,QAAQ,CAAC,EAAE,MAAM,CAAA;CAClB;AAkCD;;;;GAIG;AACH,iBAAS,aAAa,CAAC,OAAO,GAAE,cAAmB,GAAG,aAAa,CA8OlE;AAED,OAAO,EAAE,aAAa,EAAE,CAAA;AACxB,YAAY,EAAE,cAAc,EAAE,aAAa,EAAE,YAAY,EAAE,WAAW,EAAE,CAAA"}
|
package/package.json
CHANGED
|
@@ -1,14 +1,16 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@lytjs/plugin-storage",
|
|
3
|
-
"version": "4.0
|
|
3
|
+
"version": "4.2.0",
|
|
4
4
|
"description": "Lyt.js 本地存储持久化插件 - 自动保存响应式状态到 localStorage/sessionStorage",
|
|
5
|
-
"main": "./
|
|
6
|
-
"module": "./
|
|
5
|
+
"main": "./dist/index.cjs",
|
|
6
|
+
"module": "./dist/index.mjs",
|
|
7
|
+
"types": "./dist/types/index.d.ts",
|
|
7
8
|
"exports": {
|
|
8
9
|
".": {
|
|
9
|
-
"
|
|
10
|
-
"
|
|
11
|
-
"
|
|
10
|
+
"types": "./dist/types/index.d.ts",
|
|
11
|
+
"import": "./dist/index.mjs",
|
|
12
|
+
"require": "./dist/index.cjs",
|
|
13
|
+
"default": "./dist/index.mjs"
|
|
12
14
|
}
|
|
13
15
|
},
|
|
14
16
|
"sideEffects": false,
|