solid-alive 0.1.8 → 0.1.9
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/{readme.md → README.md} +72 -20
- package/dist/index.js +1 -1
- package/dist/types/AliveProvider.d.ts +1 -1
- package/dist/types/context.d.ts +1 -1
- package/dist/types/default.d.ts +15 -7
- package/dist/types/index.d.ts +1 -1
- package/dist/types/{alive.d.ts → useAlive.d.ts} +4 -6
- package/dist/types/utils.d.ts +4 -0
- package/package.json +1 -1
package/{readme.md → README.md}
RENAMED
|
@@ -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
|
-
-
|
|
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 {
|
|
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
|
|
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 l,createRoot as i,createEffect as a,onCleanup as c}from"solid-js";var v=o({elements:{},info:{frozen:!1},setInfo:()=>{},insertElement:()=>{},onDeactivated:()=>{},onActivated:()=>{},removeAliveElement:()=>{},setCurrentComponentId:()=>{},insertCacheCb:()=>{}});function d(o){let[l,i]=n(),[a,c]=n({frozen:!0}),d="",s=new Map,f=new Map,u={cbType:"",caller:"",path:""};var m=e=>{var n;if(Reflect.has(l,e)){var t=null===(n=l[e])||void 0===n?void 0:n.children;null==t||t.forEach((n=>n!==e&&m(n))),i((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(i){var t=i.stack.split("\n"),o=t[0].includes("@")?5:6,r=t[o].trim(),l=r.includes("@")?r.split("@"):r.split(" ").slice(1);e=l[0],n=l[1]}return{caller:e,path:n}}();return u.caller&&u.cbType===e&&u.caller===n&&u.path!==t?(console.warn(`[solid-alive]:检测到多个${e}函数 ${t}`),!1):(u={caller:n,path:t,cbType:e},!0)},h=(e,n)=>{if(!a.frozen&&n&&p(e)){var t={onActivated:s,onDeactivated:f}[e],o=t.get(d)||new Set;o.add(r([],n))&&t.set(d,o)}};return e(v.Provider,{value:{info:a,elements:l,setInfo:(e,n)=>{c(e,n)},onActivated:e=>{h("onActivated",e)},onDeactivated:e=>{h("onDeactivated",e)},insertElement:e=>{let n=e.id;i([n],Object.assign(Object.assign({},l[n]),e))},removeAliveElement:e=>{if(null==e)for(const e of Object.values(l))m(e.id);else m(e)},setCurrentComponentId:e=>{d=e},insertCacheCb:e=>{let n=s.get(e),o=f.get(e);s.delete(e),f.delete(e),Reflect.has(l,e)&&i(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:d,setInfo:f,insertElement:u,setCurrentComponentId:m,insertCacheCb:p}=l(v);Reflect.has(d,t)||(f("frozen",!1),m(t),i((r=>{u({id:t,dispose:r,component:e(n,{}),children:Array.isArray(o)?new Set(o):null})})));var h=e=>{var n;if(d[e].isTop)return e;var t=null===(n=Object.values(d).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(d[t].loaded)s.add(t),f("frozen",!0),null===(e=d[t].onActivated)||void 0===e||e.forEach((e=>e())),f("frozen",!1);else{let e=null===(n=d[t])||void 0===n?void 0:n.component;for(;"function"==typeof e;)e=e();e instanceof HTMLElement&&p(t)}})),c((()=>{var e;r.frozen||(f("frozen",!0),null===(e=d[t].onDeactivated)||void 0===e||e.forEach((e=>e())),f("frozen",!1))})),d[t].component}function u(){var{onActivated:e,onDeactivated:n,removeAliveElement:t,setInfo:o}=l(v);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{d as AliveProvider,f as AliveTransfer,m as onActivated,p as onDeactivated,h as useAlive};
|
package/dist/types/context.d.ts
CHANGED
package/dist/types/default.d.ts
CHANGED
|
@@ -1,34 +1,42 @@
|
|
|
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
|
-
|
|
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
|
-
|
|
19
|
+
(id: string, values: NodeInfo): void
|
|
19
20
|
}
|
|
20
21
|
|
|
21
22
|
export interface StoreProps {
|
|
22
|
-
[
|
|
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
|
-
|
|
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
|
|
34
42
|
}
|
package/dist/types/index.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
1
|
import AliveProvider from './AliveProvider';
|
|
2
2
|
import AliveTransfer from './AliveTransfer';
|
|
3
|
-
import { onActivated, onDeactivated, useAlive } from './
|
|
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;
|