solid-alive 0.1.8 → 0.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.
@@ -4,12 +4,11 @@
4
4
  - pnpm add solid-alive / npm i solid-alive / yarn add solid-alive
5
5
  ### 描述(describe)
6
6
  - 用于 solid 组件缓存,只测试过2级路由缓存
7
+ - AliveProvider
7
8
  - 在 useAlive
8
9
  - removeAliveElement: 函数, 可传一个参数, 不传就删除所有缓存 :
9
10
  removeAliveElement('/home')
10
- - onActivated / onDeactivated: 函数,只能传一个函数
11
- - onActivated(()=> console.log('actived'))
12
- - <table><tr><td bgcolor=#ff0>现在多个onActivated/onDeactivated 会被保存,在一个组件 内不要有多个onActivated/onDeactivated函数</td></tr></table>
11
+ - aliveForzen: 暂时不响应 路由数据变化, aliveForzen()
13
12
  - 子父 缓存/删除 问题
14
13
  - 如果某组件下有子组件,在父的 AliveTransfer中,
15
14
  第三个参数,为对象 写上子组件的唯一id: {children:['/childrenId','asf',...]}
@@ -18,6 +17,7 @@
18
17
 
19
18
 
20
19
  ### 使用(use)
20
+ 1 /index.tsx,AliveProvider将app 包裹
21
21
  ```jsx
22
22
  import { render } from 'solid-js/web'
23
23
  import App from './App'
@@ -85,10 +85,10 @@ export default function Blog(props: any) {
85
85
 
86
86
  - 子 /views/Blog/Single/index.tsx 中
87
87
  ```tsx
88
- import { useAlive , onActivated, onDeactivated} from "solid-alive"
88
+ import { onActivated,onDeactivated,useAlive } from "solid-alive"
89
89
 
90
90
  export default function Single() {
91
- const { removeAliveElement } = useAlive()
91
+ const { removeAliveElement,aliveFrozen } = useAlive()
92
92
 
93
93
  let divRef: Element | undefined = undefined
94
94
  const click = () => {
@@ -98,10 +98,10 @@ export default function Single() {
98
98
 
99
99
  //todo call 这个会被调用
100
100
  onActivated(()=>{
101
- console.log('Single-activeated-1')
101
+ console.log('Single-activeated-1')
102
102
  })
103
103
 
104
- //todo call 这个依然会被调用
104
+ //todo no call 这个不会被调用
105
105
  onActivated(()=>{
106
106
  console.log('Single-activeated-2')
107
107
  })
@@ -113,20 +113,72 @@ export default function Single() {
113
113
  <div>
114
114
  <h2>Single</h2>
115
115
  <input type="text" style={{ border: '2px solid red' }} />
116
- <div
117
- style={{ height: '500px', width: '200px',border:'1px solid green',overflow:'auto'}}
118
- >
119
- <div style="height:900px;border:1px solid red">
120
- Lorem ipsum dolor sit amet consectetur adipisicing elit. Veniam earum
121
- aspernatur omnis, fugiat doloremque repellat facilis fugit nisi magni
122
- provident hic aliquid nostrum reiciendis, dolores rem, quasi dolor
123
- officia? Quidem? Sit alias tempore ab provident ea aliquid nostrum
124
- quaerat? Natus aut dignissimos, illo nisi officiis adipisci ipsam
125
- totam quasi ratione laboriosam eius recusandae asperiores nobis quis
126
- assumenda odio consectetur animi. Debitis architecto mollitia sapiente
127
- </div>
128
- </div>
129
116
  </div>
130
117
  )
131
118
  }
119
+ ```
120
+
121
+
122
+ ```tsx
123
+ /** 动态路由 App.tsx */
124
+ import { createEffect, lazy, type Component } from 'solid-js'
125
+ import { Route, Router } from '@solidjs/router'
126
+ import { useAlive,AliveTransfer } from "solid-alive"
127
+
128
+ const modules = import.meta.glob<{ default: Component<any> }>([
129
+ './views/**/**.tsx',
130
+ './views/**.tsx'
131
+ ])
132
+
133
+ const transferRouter = (data: MenuData[]) =>
134
+ data.flatMap(item => {
135
+ let m = modules[`./views${item.realPath}`]
136
+ if (!m) return []
137
+ const Transfer = (props: any) =>
138
+ AliveTransfer(
139
+ lazy(m).bind(null, props),
140
+ item.path,
141
+ item.children?.map(item => item.path)
142
+ )
143
+ return (
144
+ <Route component={Transfer} path={item.path.split('/').at(-1)}>
145
+ {item.children ? transferRouter(item.children) : null}
146
+ </Route>
147
+ )
148
+ })
149
+
150
+ const App: Component = () => {
151
+ const { aliveFrozen } = useAlive()
152
+
153
+ const [data, setData] = createStore<MenuData[]>([])
154
+ const a: MenuData = {
155
+ id: 2,
156
+ text: 'ABOUT',
157
+ path: '/about',
158
+ realPath: '/About/index.tsx',
159
+ parentId: null,
160
+ hidden: 0,
161
+ sort: 2,
162
+ cache: 1
163
+ }
164
+ const addRoute =()=>{
165
+ aliveFrozen() // 暂时不响应下面数据变化
166
+ setData(d => {
167
+ return [...d, a]
168
+ })
169
+ }
170
+
171
+ return (
172
+ <Router>
173
+ <Route component={Client}>
174
+ {/* treeData 将 data变成 树结构数据 */}
175
+ {transferRouter(treeData(data, 'id', 'parentId'))}
176
+ </Route>
177
+ </Router>
178
+ )
179
+ }
180
+
181
+ export default App
182
+
183
+
132
184
  ```
package/dist/index.js CHANGED
@@ -1 +1 @@
1
- import{createComponent as e}from"solid-js/web";import{createStore as t,produce as n}from"solid-js/store";import{createContext as l,on as o,useContext as i,createRoot as a,createEffect as r,onCleanup as d}from"solid-js";var v=l({elements:{},closeSymbol:Symbol("close"),insertElement:()=>{},onDeactivated:()=>{},onActivated:()=>{},removeAliveElement:()=>{},setElement:()=>{},setCurrentComponentId:()=>{}});function c(l){var[i,a]=t();let r=Symbol("close"),d=r,c=new Map,s=new Map;var u=e=>{var t;if(Reflect.has(i,e)){var n=null===(t=i[e])||void 0===t?void 0:t.children;null==n||n.forEach((t=>t!==e&&u(t))),a((t=>{var n,l;return t[e].onDeactivated=null,t[e].component=null,null===(l=(n=t[e]).dispose)||void 0===l||l.call(n),t[e].dispose=null,t[e].onActivated=null,t[e].onDeactivated=null,t[e].id="",t[e].children=null,t[e]=null,delete t[e],t}))}},m=(e,t)=>{if(d!==r&&t){var n={onActivated:c,onDeactivated:s}[e],l=n.get(d)||new Set;l.size<100&&l.add(o([],t))&&n.set(d,l)}};return e(v.Provider,{value:{elements:i,closeSymbol:r,onActivated:e=>{m("onActivated",e)},onDeactivated:e=>{m("onDeactivated",e)},insertElement:e=>{let t=e.id,n=c.get(t),l=s.get(t);c.delete(t),s.delete(t),a([t],Object.assign(Object.assign(Object.assign({},i[t]),e),{onActivated:n,onDeactivated:l}))},setElement:(e,t,l)=>{Reflect.has(i,e)&&a(n((n=>{n[e][t]=l})))},removeAliveElement:e=>{if(null==e)for(const e of Object.values(i))u(e.id);else u(e)},setCurrentComponentId:e=>{d=e}},get children(){return l.children}})}function s(t,n,l){var{elements:o,closeSymbol:c,insertElement:s,setCurrentComponentId:u}=i(v);return Reflect.has(o,n)||(u(n),s({id:n,children:Array.isArray(l)?new Set(l):null}),a((l=>{var i;s({id:n,dispose:l,component:e(t,{}),onDeactivated:null,onActivated:null});var a=null===(i=Object.values(o).find((e=>{var t;return null===(t=e.children)||void 0===t?void 0:t.has(n)})))||void 0===i?void 0:i.id;a&&u(a)}))),r((()=>{var e;null===(e=o[n].onActivated)||void 0===e||e.forEach((e=>e()))})),d((()=>{var e;u(c),null===(e=o[n].onDeactivated)||void 0===e||e.forEach((e=>e()))})),o[n].component}function u(){var{onActivated:e,onDeactivated:t,removeAliveElement:n}=i(v);return{onActivated:e,onDeactivated:t,removeAliveElement:n}}function m(e){var{onActivated:t}=u();t(e)}function f(e){var{onDeactivated:t}=u();t(e)}export{c as AliveProvider,s as AliveTransfer,m as onActivated,f as onDeactivated,u as useAlive};
1
+ import{createComponent as e}from"solid-js/web";import{createStore as n,produce as t}from"solid-js/store";import{createContext as o,on as r,useContext as i,createRoot as l,createEffect as a,onCleanup as v}from"solid-js";var d=o({elements:{},info:{frozen:!1},setInfo:()=>{},insertElement:()=>{},onDeactivated:()=>{},onActivated:()=>{},removeAliveElement:()=>{},setCurrentComponentId:()=>{},insertCacheCb:()=>{}});function c(o){let[i,l]=n(),[a,v]=n({frozen:!0}),c="",s=new Map,f=new Map,u={onActivated:{},onDeactivated:{}};var m=e=>{var n;if(Reflect.has(i,e)){var t=null===(n=i[e])||void 0===n?void 0:n.children;null==t||t.forEach((n=>n!==e&&m(n))),l((n=>{var t,o;return n[e].onDeactivated=null,n[e].component=null,null===(o=(t=n[e]).dispose)||void 0===o||o.call(t),n[e].dispose=null,n[e].onActivated=null,n[e].onDeactivated=null,n[e].children=null,n[e]=null,delete n[e],n}))}},p=e=>{var{caller:n,path:t}=function(){var e=null,n=null;try{throw new Error}catch(l){var t=l.stack.split("\n"),o=t[0].includes("@")?5:6,r=t[o].trim(),i=r.includes("@")?r.split("@"):r.split(" ").slice(1);e=i[0],n=i[1]}return{caller:e,path:n}}(),o=u[e];return o[n]&&o[n]!==t?(console.warn(`[solid-alive]:检测到多个${e}函数 ${t}`),!1):(u[e][n]=t,!0)},h=(e,n)=>{if(!a.frozen&&n&&p(e)){var t={onActivated:s,onDeactivated:f}[e],o=t.get(c)||new Set;o.add(r([],n))&&t.set(c,o)}};return e(d.Provider,{value:{info:a,elements:i,setInfo:v,onActivated:e=>{h("onActivated",e)},onDeactivated:e=>{h("onDeactivated",e)},insertElement:e=>{let n=e.id;l([n],Object.assign(Object.assign({},i[n]),e))},removeAliveElement:e=>{if(null==e)for(const e of Object.values(i))m(e.id);else m(e)},setCurrentComponentId:e=>{c=e},insertCacheCb:e=>{let n=s.get(e),o=f.get(e);s.delete(e),f.delete(e),u.onActivated={},u.onDeactivated={},Reflect.has(i,e)&&l(t((t=>{t[e].onActivated=n,t[e].onDeactivated=o,t[e].loaded=!0})))}},get children(){return o.children}})}let s=new Set([]);function f(n,t,o){var{info:r,elements:c,setInfo:f,insertElement:u,setCurrentComponentId:m,insertCacheCb:p}=i(d);Reflect.has(c,t)||(f("frozen",!1),m(t),l((r=>{u({id:t,dispose:r,component:e(n,{}),children:Array.isArray(o)?new Set(o):null})})));var h=e=>{var n;if(c[e].isTop)return e;var t=null===(n=Object.values(c).find((n=>{var t;return null===(t=n.children)||void 0===t?void 0:t.has(e)})))||void 0===n?void 0:n.id;return t&&(t=h(t)),t||e};return s.has(h(t))||s.clear(),a((()=>{var e,n;if(f("frozen",!1),!s.has(t))if(c[t].loaded)s.add(t),f("frozen",!0),null===(e=c[t].onActivated)||void 0===e||e.forEach((e=>e())),f("frozen",!1);else{let e=null===(n=c[t])||void 0===n?void 0:n.component;for(;"function"==typeof e;)e=e();e instanceof HTMLElement&&p(t)}})),v((()=>{var e;r.frozen||(f("frozen",!0),null===(e=c[t].onDeactivated)||void 0===e||e.forEach((e=>e())),f("frozen",!1))})),c[t].component}function u(){var{onActivated:e,onDeactivated:n,removeAliveElement:t,setInfo:o}=i(d);return{onActivated:e,onDeactivated:n,removeAliveElement:t,setInfo:o}}function m(e){var{onActivated:n}=u();n(e)}function p(e){var{onDeactivated:n}=u();n(e)}function h(){var{removeAliveElement:e,setInfo:n}=u();return{removeAliveElement:e,aliveFrozen:()=>n("frozen",!0)}}export{c as AliveProvider,f as AliveTransfer,m as onActivated,p as onDeactivated,h as useAlive};
@@ -1,4 +1,4 @@
1
- import { ProveiderProps } from './default';
1
+ import { ProveiderProps } from "./default";
2
2
  /**
3
3
  * @description Alive
4
4
  * @param children jsx.element
@@ -1,3 +1,3 @@
1
- import { ContextProps } from './default';
1
+ import { ContextProps } from "./default";
2
2
  declare var Context: import("solid-js").Context<ContextProps>;
3
3
  export default Context;
@@ -1,34 +1,50 @@
1
1
  import { JSX } from 'solid-js'
2
2
 
3
- export interface ProveiderProps{
3
+ export interface ProveiderProps {
4
4
  children: JSX.Element
5
5
  }
6
6
 
7
7
  export interface NodeInfo {
8
8
  id: string
9
- component?: JSX.Element
9
+ loaded?: boolean
10
+ component?: JSX.Element | null
10
11
  children?: Set<string> | null
11
12
  dispose?: (() => void) | null
12
13
  onActivated?: null | Set<() => void>
13
14
  onDeactivated?: null | Set<() => void>
15
+ isTop?: boolean | null
14
16
  }
15
17
 
16
-
17
18
  export interface SetElement {
18
- <T extends keyof NodeInfo>(id: string, prop: T, v: NodeInfo[T]): void
19
+ (id: string, values: NodeInfo): void
19
20
  }
20
21
 
21
22
  export interface StoreProps {
22
- [propsName: string]: NodeInfo
23
+ [key: string]: NodeInfo
24
+ }
25
+
26
+ export interface IInfo {
27
+ frozen: boolean
23
28
  }
24
29
 
30
+ export type TSetInfo = <T extends keyof IInfo>(key: T, value: IInfo[T]) => void
31
+
25
32
  export interface ContextProps {
26
33
  elements: StoreProps
27
- closeSymbol: symbol
34
+ info: IInfo
35
+ setInfo: TSetInfo
28
36
  insertElement: (d: NodeInfo) => void
29
37
  onActivated: (cb: () => void) => void
30
38
  onDeactivated: (cb: () => void) => void
31
39
  removeAliveElement: (id?: string) => void
32
- setElement: SetElement
33
40
  setCurrentComponentId: (id: string | symbol) => void
41
+ insertCacheCb: (id: string) => void
42
+ }
43
+
44
+ interface IActive {
45
+ [key:string]: string
46
+ }
47
+ export interface IPrevCall {
48
+ onActivated: IActive
49
+ onDeactivated: IActive
34
50
  }
@@ -1,4 +1,4 @@
1
1
  import AliveProvider from './AliveProvider';
2
2
  import AliveTransfer from './AliveTransfer';
3
- import { onActivated, onDeactivated, useAlive } from './alive';
3
+ import { onActivated, onDeactivated, useAlive } from './useAlive';
4
4
  export { AliveProvider, AliveTransfer, onActivated, onDeactivated, useAlive };
@@ -1,12 +1,10 @@
1
+ export declare function onActivated(fn: () => void): void;
2
+ export declare function onDeactivated(fn: () => void): void;
1
3
  /**
2
- * @returns onActivated 激活缓存组件时触发,
3
- * @returns onDeactivated 退出缓存组件时触发,
4
4
  * @returns removeAliveElement 删除缓存组件,
5
+ * @returns aliveFrozen 让 alive 暂时失去响应, 一般在加新增路由数据时使用
5
6
  */
6
7
  export declare function useAlive(): {
7
- onActivated: (cb: () => void) => void;
8
- onDeactivated: (cb: () => void) => void;
9
8
  removeAliveElement: (id?: string) => void;
9
+ aliveFrozen: () => void;
10
10
  };
11
- export declare function onActivated(fn: () => void): void;
12
- export declare function onDeactivated(fn: () => void): void;
@@ -0,0 +1,4 @@
1
+ export declare function getCallerFunctionName(): {
2
+ caller: any;
3
+ path: any;
4
+ };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "solid-alive",
3
- "version": "0.1.8",
3
+ "version": "0.2.0",
4
4
  "author": "1iuxs",
5
5
  "description": "solid-alive",
6
6
  "type": "module",