pinia-plugin-persist-uni
Advanced tools
Comparing version 1.0.2 to 1.0.3
import { PiniaPluginContext } from 'pinia'; | ||
export interface PersistStrategy { | ||
key?: string; | ||
storage?: Storage; | ||
paths?: string[]; | ||
@@ -8,2 +9,3 @@ } | ||
enabled: true; | ||
H5Storage?: Storage; | ||
strategies?: PersistStrategy[]; | ||
@@ -10,0 +12,0 @@ } |
const updateStorage = (strategy, store) => { | ||
const storage = strategy.storage || sessionStorage; | ||
const storeKey = strategy.key || store.$id; | ||
@@ -8,4 +9,6 @@ if (strategy.paths) { | ||
}, {}); | ||
storage.setItem(storeKey, JSON.stringify(partialState)); | ||
uni.setStorage({ key: storeKey, data: JSON.stringify(partialState) }); | ||
} else { | ||
storage.setItem(storeKey, JSON.stringify(store.$state)); | ||
uni.setStorage({ key: storeKey, data: JSON.stringify(store.$state) }); | ||
@@ -15,13 +18,18 @@ } | ||
var index = ({ options, store }) => { | ||
var _a, _b, _c, _d; | ||
var _a, _b, _c, _d, _e; | ||
if ((_a = options.persist) == null ? void 0 : _a.enabled) { | ||
const defaultStrat = [ | ||
{ | ||
key: store.$id | ||
key: store.$id, | ||
storage: ((_b = options.persist) == null ? void 0 : _b.H5Storage) || sessionStorage | ||
} | ||
]; | ||
const strategies = ((_c = (_b = options.persist) == null ? void 0 : _b.strategies) == null ? void 0 : _c.length) ? (_d = options.persist) == null ? void 0 : _d.strategies : defaultStrat; | ||
const strategies = ((_d = (_c = options.persist) == null ? void 0 : _c.strategies) == null ? void 0 : _d.length) ? (_e = options.persist) == null ? void 0 : _e.strategies : defaultStrat; | ||
strategies.forEach((strategy) => { | ||
var _a2; | ||
const storage = strategy.storage || ((_a2 = options.persist) == null ? void 0 : _a2.H5Storage) || sessionStorage; | ||
const storeKey = strategy.key || store.$id; | ||
const storageResult = uni.getStorageSync(storeKey); | ||
let storageResult; | ||
storageResult = storage.getItem(storeKey); | ||
storageResult = uni.getStorageSync(storeKey); | ||
if (storageResult) { | ||
@@ -28,0 +36,0 @@ store.$patch(JSON.parse(storageResult)); |
@@ -1,1 +0,1 @@ | ||
(function(t,i){typeof exports=="object"&&typeof module!="undefined"?i(exports):typeof define=="function"&&define.amd?define(["exports"],i):(t=typeof globalThis!="undefined"?globalThis:t||self,i(t.piniaPersist={}))})(this,function(t){"use strict";const i=(s,e)=>{const a=s.key||e.$id;if(s.paths){const u=s.paths.reduce((n,d)=>(n[d]=e.$state[d],n),{});uni.setStorage({key:a,data:JSON.stringify(u)})}else uni.setStorage({key:a,data:JSON.stringify(e.$state)})};var c=({options:s,store:e})=>{var a,u,n,d;if((a=s.persist)!=null&&a.enabled){const p=[{key:e.$id}],f=(n=(u=s.persist)==null?void 0:u.strategies)!=null&&n.length?(d=s.persist)==null?void 0:d.strategies:p;f.forEach(r=>{const l=r.key||e.$id,o=uni.getStorageSync(l);o&&(e.$patch(JSON.parse(o)),i(r,e))}),e.$subscribe(()=>{f.forEach(r=>{i(r,e)})})}};t.default=c,t.updateStorage=i,Object.defineProperties(t,{__esModule:{value:!0},[Symbol.toStringTag]:{value:"Module"}})}); | ||
(function(s,i){typeof exports=="object"&&typeof module!="undefined"?i(exports):typeof define=="function"&&define.amd?define(["exports"],i):(s=typeof globalThis!="undefined"?globalThis:s||self,i(s.piniaPersist={}))})(this,function(s){"use strict";const i=(t,e)=>{const n=t.storage||sessionStorage,a=t.key||e.$id;if(t.paths){const r=t.paths.reduce((o,d)=>(o[d]=e.$state[d],o),{});n.setItem(a,JSON.stringify(r)),uni.setStorage({key:a,data:JSON.stringify(r)})}else n.setItem(a,JSON.stringify(e.$state)),uni.setStorage({key:a,data:JSON.stringify(e.$state)})};var p=({options:t,store:e})=>{var n,a,r,o,d;if((n=t.persist)!=null&&n.enabled){const l=[{key:e.$id,storage:((a=t.persist)==null?void 0:a.H5Storage)||sessionStorage}],f=(o=(r=t.persist)==null?void 0:r.strategies)!=null&&o.length?(d=t.persist)==null?void 0:d.strategies:l;f.forEach(g=>{var c;const h=g.storage||((c=t.persist)==null?void 0:c.H5Storage)||sessionStorage,S=g.key||e.$id;let u;u=h.getItem(S),u=uni.getStorageSync(S),u&&(e.$patch(JSON.parse(u)),i(g,e))}),e.$subscribe(()=>{f.forEach(g=>{i(g,e)})})}};s.default=p,s.updateStorage=i,Object.defineProperties(s,{__esModule:{value:!0},[Symbol.toStringTag]:{value:"Module"}})}); |
{ | ||
"name": "pinia-plugin-persist-uni", | ||
"version": "1.0.2", | ||
"version": "1.0.3", | ||
"repository": { | ||
@@ -5,0 +5,0 @@ "type": "git", |
110
README.md
@@ -1,8 +0,108 @@ | ||
# pinia-plugin-persist-uni | ||
--- | ||
theme: channing-cyan | ||
highlight: atom-one-dark | ||
--- | ||
![Build](https://github.com/Allen-1998/pinia-plugin-persist-uni/actions/workflows/build.yml/badge.svg) | ||
![Docs](https://github.com/Allen-1998/pinia-plugin-persist-uni/actions/workflows/docs.yml/badge.svg) | ||
# 前言 | ||
Persist VueJs Pinia state data in sessionStorage or other storages, heavilly influenced by [vuex-persistedstate](https://github.com/robinvdvleuten/vuex-persistedstate) and [pinia-plugin-persist](https://github.com/Seb-L/pinia-plugin-persist). | ||
> 尤雨溪在 3 月 24 日晚与掘金合作的直播[链接](https://live.juejin.cn/4354/vue3)中提到,pinia 就是实际上的 vuex5,作为新一代的状态管理器,更友好的 ts 支持,更轻量的打包体积,更简化的模块管理,无疑会在将来的市场中备受欢迎。 | ||
Documentation: [HERE](https://Allen-1998.github.io/pinia-plugin-persist-uni/) | ||
pinia 的优点相比也不用多说了,但也正是由于其处于一个新生的阶段,周边生态还不够完善,在本人搭建项目的过程中便遇到了 pinia 在 uniapp 中数据持久化的问题。 | ||
市场上目前也有一些数据持久化的插件,例如 `vuex-persistedstate`,`pinia-plugin-persist`,但是服务于 pinia 和 uniapp 的却没有,其中`pinia-plugin-persist`虽然同样可以满足需求,但是由于其默认数据持久化的方式是 sessionStorage,使用时需要重复的配置,作为一个有手的程序员,当然不能忍,于是便有了`pinia-plugin-persist-uni`。 | ||
# 使用说明 | ||
## 安装 | ||
`npm i pinia-plugin-persist-uni` | ||
## 配置 | ||
### Vue2 | ||
```typescript | ||
import Vue from 'vue' | ||
import vueCompositionApi from '@vue/composition-api' | ||
import { createPinia } from 'pinia' | ||
import piniaPersist from 'pinia-plugin-persist-uni' | ||
import App from './App.vue' | ||
const pinia = createPinia() | ||
pinia.use(piniaPersist) | ||
Vue.use(vueCompositionApi) | ||
Vue.use(pinia) | ||
new Vue({ | ||
pinia, | ||
render: (h) => h(App), | ||
}).$mount('#app') | ||
``` | ||
### Vue3 | ||
```typescript | ||
import { createApp } from 'vue' | ||
import { createPinia } from 'pinia' | ||
import piniaPersist from 'pinia-plugin-persist-uni' | ||
const pinia = createPinia() | ||
pinia.use(piniaPersist) | ||
createApp({}).use(pinia).mount('#app') | ||
``` | ||
### Typescript | ||
```json | ||
// tsconfig.json | ||
{ | ||
"compilerOptions": { | ||
"types": ["pinia-plugin-persist-uni"] | ||
} | ||
} | ||
``` | ||
## 基本用法 | ||
通过在你的 stroe 中配置 persist, 将会通过 uniAppStorage 来持久化存储你的数据. | ||
请配置 id,用于持久化存储时的 key。 | ||
```typescript | ||
// store/user.ts | ||
import { defineStore } from 'pinia' | ||
export const useUserStore = defineStore('storeUser', { | ||
state: () => { | ||
id: 'user', | ||
return { | ||
firstName: 'allen', | ||
lastName: 'ttk', | ||
accessToken: 'xxxxxxxxxxxxx', | ||
} | ||
}, | ||
actions: { | ||
setToken(value: string) { | ||
this.accessToken = value | ||
}, | ||
}, | ||
persist: { | ||
enabled: true, | ||
}, | ||
}) | ||
``` | ||
## 总结 | ||
新技术会带给我们更良好的开发体验,但是我们同样应该关注其社区环境,并力所能及的贡献出自己的一份力量。本插件开发的新路历程也是基于目前`pinia`的生态环境中没有专门服务于`uniapp`的`数据持久化`插件。 | ||
该项目也是参考了`vuex-persistedstate`和`pinia-plugin-persist`,保持了使用习惯的同时又简化了使用配置。同时在搭建项目的过程中也接触到了`github-pages`以及`github actions`的配置使用,实现了说明文档自动部署和 npm 自动发包,可谓是收获满满。 | ||
目前源码已发布至 github,详情见 [源码地址](https://github.com/Allen-1998/pinia-plugin-persist-uni) [说明文档](https://allen-1998.github.io/pinia-plugin-persist-uni/) [使用案例](https://github.com/Allen-1998/uni-vue3-vite-ts-pinia),对你有帮助或者喜欢的话请点个 Star。 | ||
## 参考 | ||
- [vuex-persistedstate](https://github.com/robinvdvleuten/vuex-persistedstate) | ||
- [pinia-plugin-persist](https://github.com/Seb-L/pinia-plugin-persist) |
Unpublished package
Supply chain riskPackage version was not found on the registry. It may exist on a different registry and need to be configured to pull from that registry.
Found 1 instance in 1 package
Unpopular package
QualityThis package is not very popular.
Found 1 instance in 1 package
9537
70
0
109
1