@brushes/context 0.0.4 → 0.0.5
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.js +1 -1
- package/dist/store/rootStore.d.ts +1 -0
- package/package.json +1 -1
- package/src/store/rootStore.tsx +30 -31
package/dist/index.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
import{jsx as o}from"react/jsx-runtime";import{createStore as
|
|
1
|
+
import{jsx as o}from"react/jsx-runtime";import{createStore as e,useStore as r}from"zustand";import{createContext as t,useRef as n,useContext as s}from"react";import{persist as i,createJSONStorage as c}from"zustand/middleware";function u(o,e){var r={};for(var t in o)Object.prototype.hasOwnProperty.call(o,t)&&e.indexOf(t)<0&&(r[t]=o[t]);if(null!=o&&"function"==typeof Object.getOwnPropertySymbols){var n=0;for(t=Object.getOwnPropertySymbols(o);n<t.length;n++)e.indexOf(t[n])<0&&Object.prototype.propertyIsEnumerable.call(o,t[n])&&(r[t[n]]=o[t[n]])}return r}"function"==typeof SuppressedError&&SuppressedError;const a={contractSettlOpno:0,upmMap:{},rebMoney:0,goodsCamount:0,shoppingCountPrice:0,totalDiscountPrice:0,accountsSumPrice:0,discount:0,freight:0,comDisMoney:0,copyComDisMoney:0,shoppingType:"",promotionCode:"",promotionCodes:""},d=t(null);function l(r){var{children:t}=r,s=u(r,["children"]);const i=n();return i.current||(i.current=(o=>{const r={moduleStore:{_ocDiscount:[],_ocPoints:[],_couponList:[],_selectCoupon:[],_shoppingList:[],_contractGoodsList:[],_orderDomainStr:[],_ocContractSettlList:[],_orderAddressInfo:{freight:0,goodsReceiptMem:"",goodsReceiptPhone:"",goodsReceiptArrdess:"",areaCode:""},defaultValue:void 0,_skuInfo:{},params:{},breadList:[],open:!1}};return e()(e=>Object.assign(Object.assign(Object.assign({},r),o),{setModuleStore:o=>e(e=>({moduleStore:Object.assign(e.moduleStore,o)}))}))})(s)),o(d.Provider,{value:i.current,children:t})}function p(o){const e=s(d);if(!e)throw new Error("Missing ModuleContext.Provider in the tree");return r(e,o)}let f=localStorage;const m=t(null);function g(r){var{children:t}=r,s=u(r,["children"]);const a=n();return a.current||(a.current=(o=>{const r={rootStore:{safe:0,_userInfo:{},_historyList:[],_themeColor:{colorPrimary:"#1677ff",colorBgTextHover:"#e6r4ff"},_orderCount:{},_cart:0,_webStore:{},_menuChildren:[]}};return e()(i(e=>Object.assign(Object.assign(Object.assign({},r),o),{setModuleRootStore:o=>e(e=>({rootStore:Object.assign(e.rootStore,o)}))}),{name:"root-storage",storage:c(()=>f)}))})(s)),o(m.Provider,{value:a.current,children:t})}function h(o){const e=s(m);if(!e)throw new Error("Missing RootModuleContext.Provider in the tree");return r(e,o)}export{l as ModuleProvider,g as ModuleRootProvider,a as initialValueOrder,p as useModuleContext,h as useModuleRootContext};
|
package/package.json
CHANGED
package/src/store/rootStore.tsx
CHANGED
|
@@ -1,39 +1,38 @@
|
|
|
1
1
|
import { createStore } from "zustand";
|
|
2
2
|
import { persist, createJSONStorage } from "zustand/middleware";
|
|
3
|
-
import { getTaro } from "@brushes/utils";
|
|
4
3
|
|
|
5
|
-
|
|
6
|
-
const Taro = getTaro();
|
|
4
|
+
let localStorageImpl: any = localStorage;
|
|
7
5
|
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
6
|
+
export const initPersist = (Taro: any) => {
|
|
7
|
+
if (Taro) {
|
|
8
|
+
localStorageImpl = {
|
|
9
|
+
getItem: (key: string) => {
|
|
10
|
+
try {
|
|
11
|
+
const value = Taro.getStorageSync(key);
|
|
12
|
+
return value !== "" ? value : null;
|
|
13
|
+
} catch (error) {
|
|
14
|
+
console.error(`读取存储失败 [${key}]`, error);
|
|
15
|
+
return null;
|
|
16
|
+
}
|
|
17
|
+
},
|
|
18
|
+
setItem: (key: string, value: any) => {
|
|
19
|
+
try {
|
|
20
|
+
Taro.setStorageSync(key, value);
|
|
21
|
+
} catch (error) {
|
|
22
|
+
console.error(`写入存储失败 [${key}]`, error);
|
|
23
|
+
}
|
|
24
|
+
},
|
|
25
|
+
removeItem: (key: string) => {
|
|
26
|
+
try {
|
|
27
|
+
Taro.removeStorageSync(key);
|
|
28
|
+
} catch (error) {
|
|
29
|
+
console.error(`删除存储失败 [${key}]`, error);
|
|
30
|
+
}
|
|
31
|
+
},
|
|
32
|
+
};
|
|
33
|
+
}
|
|
33
34
|
};
|
|
34
35
|
|
|
35
|
-
const localImpl = localStorage ? localStorage : miniProgramStorage();
|
|
36
|
-
|
|
37
36
|
export type moduleType = {
|
|
38
37
|
[v: string]: any;
|
|
39
38
|
safe?: number;
|
|
@@ -85,7 +84,7 @@ export const createRootModuleStore = (initProps?: Partial<moduleRootStore>) => {
|
|
|
85
84
|
}),
|
|
86
85
|
{
|
|
87
86
|
name: "root-storage", // 存储的 key 名
|
|
88
|
-
storage: createJSONStorage(() =>
|
|
87
|
+
storage: createJSONStorage(() => localStorageImpl), // (optional) by default, 'localStorage' is used
|
|
89
88
|
},
|
|
90
89
|
),
|
|
91
90
|
);
|