@brushes/context 0.0.4 → 0.0.6

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 CHANGED
@@ -1 +1 @@
1
- import{jsx as o}from"react/jsx-runtime";import{createStore as r,useStore as e}from"zustand";import{createContext as t,useRef as n,useContext as s}from"react";import{persist as c,createJSONStorage as i}from"zustand/middleware";import{getTaro as u}from"@brushes/utils";function a(o,r){var e={};for(var t in o)Object.prototype.hasOwnProperty.call(o,t)&&r.indexOf(t)<0&&(e[t]=o[t]);if(null!=o&&"function"==typeof Object.getOwnPropertySymbols){var n=0;for(t=Object.getOwnPropertySymbols(o);n<t.length;n++)r.indexOf(t[n])<0&&Object.prototype.propertyIsEnumerable.call(o,t[n])&&(e[t[n]]=o[t[n]])}return e}"function"==typeof SuppressedError&&SuppressedError;const l={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 p(e){var{children:t}=e,s=a(e,["children"]);const c=n();return c.current||(c.current=(o=>{const e={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 r()(r=>Object.assign(Object.assign(Object.assign({},e),o),{setModuleStore:o=>r(r=>({moduleStore:Object.assign(r.moduleStore,o)}))}))})(s)),o(d.Provider,{value:c.current,children:t})}function m(o){const r=s(d);if(!r)throw new Error("Missing ModuleContext.Provider in the tree");return e(r,o)}const f=localStorage||(()=>{const o=u();return{getItem:r=>{try{const e=o.getStorageSync(r);return""!==e?e:null}catch(o){return console.error(`读取存储失败 [${r}]`,o),null}},setItem:(r,e)=>{try{o.setStorageSync(r,e)}catch(o){console.error(`写入存储失败 [${r}]`,o)}},removeItem:r=>{try{o.removeStorageSync(r)}catch(o){console.error(`删除存储失败 [${r}]`,o)}}}})(),g=t(null);function h(e){var{children:t}=e,s=a(e,["children"]);const u=n();return u.current||(u.current=(o=>{const e={rootStore:{safe:0,_userInfo:{},_historyList:[],_themeColor:{colorPrimary:"#1677ff",colorBgTextHover:"#e6r4ff"},_orderCount:{},_cart:0,_webStore:{},_menuChildren:[]}};return r()(c(r=>Object.assign(Object.assign(Object.assign({},e),o),{setModuleRootStore:o=>r(r=>({rootStore:Object.assign(r.rootStore,o)}))}),{name:"root-storage",storage:i(()=>f)}))})(s)),o(g.Provider,{value:u.current,children:t})}function S(o){const r=s(g);if(!r)throw new Error("Missing RootModuleContext.Provider in the tree");return e(r,o)}export{p as ModuleProvider,h as ModuleRootProvider,l as initialValueOrder,m as useModuleContext,S as useModuleRootContext};
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 c}from"react";import{persist as s,createJSONStorage as i}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:""},l=t(null);function d(r){var{children:t}=r,c=u(r,["children"]);const s=n();return s.current||(s.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)}))}))})(c)),o(l.Provider,{value:s.current,children:t})}function p(o){const e=c(l);if(!e)throw new Error("Missing ModuleContext.Provider in the tree");return r(e,o)}let m=localStorage;const f=o=>{o&&(m={getItem:e=>{try{const r=o.getStorageSync(e);return""!==r?r:null}catch(o){return console.error(`读取存储失败 [${e}]`,o),null}},setItem:(e,r)=>{try{o.setStorageSync(e,r)}catch(o){console.error(`写入存储失败 [${e}]`,o)}},removeItem:e=>{try{o.removeStorageSync(e)}catch(o){console.error(`删除存储失败 [${e}]`,o)}}})},g=t(null);function h(r){var{children:t}=r,c=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()(s(e=>Object.assign(Object.assign(Object.assign({},r),o),{setModuleRootStore:o=>e(e=>({rootStore:Object.assign(e.rootStore,o)}))}),{name:"root-storage",storage:i(()=>m)}))})(c)),o(g.Provider,{value:a.current,children:t})}function S(o){const e=c(g);if(!e)throw new Error("Missing RootModuleContext.Provider in the tree");return r(e,o)}export{d as ModuleProvider,h as ModuleRootProvider,f as initPersist,a as initialValueOrder,p as useModuleContext,S as useModuleRootContext};
@@ -0,0 +1,5 @@
1
+ import { ModuleRootState, moduleRootStore } from "./rootStore";
2
+ export { initPersist } from "./rootStore";
3
+ type ModuleProviderProps = React.PropsWithChildren<Partial<moduleRootStore>>;
4
+ export declare function ModuleRootProvider({ children, ...props }: ModuleProviderProps): import("react/jsx-runtime").JSX.Element;
5
+ export declare function useModuleRootContext<T>(selector: (state: ModuleRootState) => T): T;
@@ -1,3 +1,4 @@
1
+ export declare const initPersist: (Taro: any) => void;
1
2
  export type moduleType = {
2
3
  [v: string]: any;
3
4
  safe?: number;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@brushes/context",
3
- "version": "0.0.4",
3
+ "version": "0.0.6",
4
4
  "description": "",
5
5
  "main": "src/index.ts",
6
6
  "scripts": {
@@ -7,6 +7,8 @@ import {
7
7
  import { useRef, useContext, createContext } from "react";
8
8
  import { useStore } from "zustand";
9
9
 
10
+ export { initPersist } from "./rootStore";
11
+
10
12
  const ModuleContext = createContext<ModuleRootStore | null>(null);
11
13
 
12
14
  type ModuleProviderProps = React.PropsWithChildren<Partial<moduleRootStore>>;
@@ -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
- const miniProgramStorage = () => {
6
- const Taro = getTaro();
4
+ let localStorageImpl: any = localStorage;
7
5
 
8
- return {
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
- };
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(() => localImpl), // (optional) by default, 'localStorage' is used
87
+ storage: createJSONStorage(() => localStorageImpl), // (optional) by default, 'localStorage' is used
89
88
  },
90
89
  ),
91
90
  );