solid-alive 0.1.9 → 0.2.1
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 +29 -32
- package/dist/index.js +1 -1
- package/dist/types/AliveComponent.d.ts +6 -0
- package/dist/types/AliveProvider.d.ts +1 -1
- package/dist/types/aliveTransfer.d.ts +8 -0
- package/dist/types/default.d.ts +16 -4
- package/dist/types/index.d.ts +3 -2
- package/dist/types/useAlive.d.ts +1 -1
- package/package.json +1 -1
- package/dist/types/AliveTransfer.d.ts +0 -8
package/README.md
CHANGED
|
@@ -5,14 +5,15 @@
|
|
|
5
5
|
### 描述(describe)
|
|
6
6
|
- 用于 solid 组件缓存,只测试过2级路由缓存
|
|
7
7
|
- AliveProvider
|
|
8
|
+
- AliveComponent 不要在 有缓存 的组件中使用
|
|
8
9
|
- 在 useAlive
|
|
9
|
-
-
|
|
10
|
-
|
|
10
|
+
- removeAliveElements: 函数, 可传一个参数, 不传就删除所有缓存 :
|
|
11
|
+
removeAliveElements(['/home'])
|
|
11
12
|
- aliveForzen: 暂时不响应 路由数据变化, aliveForzen()
|
|
12
13
|
- 子父 缓存/删除 问题
|
|
13
14
|
- 如果某组件下有子组件,在父的 AliveTransfer中,
|
|
14
15
|
第三个参数,为对象 写上子组件的唯一id: {children:['/childrenId','asf',...]}
|
|
15
|
-
- 使用见下图, 也可用 -
|
|
16
|
+
- 使用见下图, 也可用 -removeAliveElements 删除
|
|
16
17
|
|
|
17
18
|
|
|
18
19
|
|
|
@@ -21,7 +22,7 @@
|
|
|
21
22
|
```jsx
|
|
22
23
|
import { render } from 'solid-js/web'
|
|
23
24
|
import App from './App'
|
|
24
|
-
import {
|
|
25
|
+
import { aliveProvider } from 'solid-alive'
|
|
25
26
|
|
|
26
27
|
const root = document.getElementById('root')
|
|
27
28
|
|
|
@@ -35,7 +36,7 @@ render(() =>
|
|
|
35
36
|
2 /App.tsx ,在 solid-alive 中 有 AliveTransfer, 参数: JSX , id:string, children?:[string..]
|
|
36
37
|
```jsx
|
|
37
38
|
import { Route, Router } from '@solidjs/router';
|
|
38
|
-
import {
|
|
39
|
+
import { aliveTransfer } from 'solid-alive';
|
|
39
40
|
|
|
40
41
|
import Home from './views/Home';
|
|
41
42
|
import Client from './views/Client';
|
|
@@ -44,14 +45,13 @@ import Team from './views/Blog/Team';
|
|
|
44
45
|
import Blog from './views/Blog';
|
|
45
46
|
import Single from './views/Blog/Single';
|
|
46
47
|
|
|
47
|
-
const AboutTsf = () => AliveTransfer(About, '/about');
|
|
48
48
|
|
|
49
|
-
|
|
50
|
-
const
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
const
|
|
54
|
-
const
|
|
49
|
+
const HomeTransfer = aliveTransfer(Home, '/')
|
|
50
|
+
const AboutTransfer = aliveTransfer(About, '/about')
|
|
51
|
+
const ShopTransfer = aliveTransfer(Shop, '/shop',['/shopPage'])
|
|
52
|
+
const ShopPageTransfer = aliveTransfer(ShopPage, '/shopPage')
|
|
53
|
+
const BlogTransfer = aliveTransfer(Blog, '/blog', ['contact', 'single'])
|
|
54
|
+
const SingleTsf = aliveTransfer(Single,'single')
|
|
55
55
|
|
|
56
56
|
export default function App() {
|
|
57
57
|
return (
|
|
@@ -63,7 +63,6 @@ export default function App() {
|
|
|
63
63
|
<Route component={AboutTsf} path={'/about'} />
|
|
64
64
|
{/* 父子 缓存 Parent Child Nesting */}
|
|
65
65
|
<Route component={BlogTsf} path={'/blog'}>
|
|
66
|
-
<Route component={TeamTsf} path={'team'} />
|
|
67
66
|
<Route component={SingleTsf} path={'single'} />
|
|
68
67
|
</Route>
|
|
69
68
|
</Route>
|
|
@@ -85,15 +84,15 @@ export default function Blog(props: any) {
|
|
|
85
84
|
|
|
86
85
|
- 子 /views/Blog/Single/index.tsx 中
|
|
87
86
|
```tsx
|
|
88
|
-
import { onActivated,onDeactivated,useAlive } from "solid-alive"
|
|
87
|
+
import { onActivated,onDeactivated,useAlive, AliveComponent } from "solid-alive"
|
|
89
88
|
|
|
90
89
|
export default function Single() {
|
|
91
|
-
const {
|
|
90
|
+
const { removeAliveElements,aliveFrozen } = useAlive()
|
|
92
91
|
|
|
93
92
|
let divRef: Element | undefined = undefined
|
|
94
93
|
const click = () => {
|
|
95
|
-
|
|
96
|
-
//
|
|
94
|
+
removeAliveElements(['/about']) // delete '/about'; 删除 /about
|
|
95
|
+
// removeAliveElements() // delete all alive element; 会删除所有缓存的组件
|
|
97
96
|
}
|
|
98
97
|
|
|
99
98
|
//todo call 这个会被调用
|
|
@@ -112,15 +111,19 @@ export default function Single() {
|
|
|
112
111
|
return (
|
|
113
112
|
<div>
|
|
114
113
|
<h2>Single</h2>
|
|
114
|
+
{ /** 这个 AliveComponent 最好是在 未缓存的组件 中使用, 不然生命周期不同步 */ }
|
|
115
|
+
<AliveComponent id={'home1'}>
|
|
116
|
+
<Home />
|
|
117
|
+
</AliveComponent>
|
|
115
118
|
<input type="text" style={{ border: '2px solid red' }} />
|
|
116
119
|
</div>
|
|
117
120
|
)
|
|
118
121
|
}
|
|
119
122
|
```
|
|
120
123
|
|
|
121
|
-
|
|
124
|
+
3 动态生成 路由
|
|
122
125
|
```tsx
|
|
123
|
-
/**
|
|
126
|
+
/** App.tsx */
|
|
124
127
|
import { createEffect, lazy, type Component } from 'solid-js'
|
|
125
128
|
import { Route, Router } from '@solidjs/router'
|
|
126
129
|
import { useAlive,AliveTransfer } from "solid-alive"
|
|
@@ -132,14 +135,13 @@ const modules = import.meta.glob<{ default: Component<any> }>([
|
|
|
132
135
|
|
|
133
136
|
const transferRouter = (data: MenuData[]) =>
|
|
134
137
|
data.flatMap(item => {
|
|
135
|
-
let
|
|
136
|
-
if (!
|
|
137
|
-
const Transfer = (
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
)
|
|
138
|
+
let module = modules[`./views${item.realPath}`]
|
|
139
|
+
if (!module) return []
|
|
140
|
+
const Transfer = aliveTransfer(
|
|
141
|
+
lazy(module),
|
|
142
|
+
item.path,
|
|
143
|
+
item.children?.map(item => item.path)
|
|
144
|
+
)
|
|
143
145
|
return (
|
|
144
146
|
<Route component={Transfer} path={item.path.split('/').at(-1)}>
|
|
145
147
|
{item.children ? transferRouter(item.children) : null}
|
|
@@ -157,9 +159,6 @@ const App: Component = () => {
|
|
|
157
159
|
path: '/about',
|
|
158
160
|
realPath: '/About/index.tsx',
|
|
159
161
|
parentId: null,
|
|
160
|
-
hidden: 0,
|
|
161
|
-
sort: 2,
|
|
162
|
-
cache: 1
|
|
163
162
|
}
|
|
164
163
|
const addRoute =()=>{
|
|
165
164
|
aliveFrozen() // 暂时不响应下面数据变化
|
|
@@ -179,6 +178,4 @@ const App: Component = () => {
|
|
|
179
178
|
}
|
|
180
179
|
|
|
181
180
|
export default App
|
|
182
|
-
|
|
183
|
-
|
|
184
181
|
```
|
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 t,produce as n}from"solid-js/store";import{createContext as r,on as o,useContext as i,createRoot as a,createComputed as l,createEffect as v,onCleanup as d}from"solid-js";var s=r({elements:{},info:{frozen:!1},setInfo:()=>{},insertElement:()=>{},onDeactivated:()=>{},onActivated:()=>{},removeAliveElements:()=>{},setCurrentComponentId:()=>{},insertCacheCb:()=>{}});function c(r){let[i,a]=t(),[l,v]=t({frozen:!0}),d="",c=new Map,f=new Map,u={onActivated:{},onDeactivated:{}};var m=e=>{var t;if(Reflect.has(i,e)){var n=null===(t=i[e])||void 0===t?void 0:t.subIds;null==n||n.forEach((t=>t!==e&&m(t))),a((t=>{var n,r;return null===(r=(n=t[e]).dispose)||void 0===r||r.call(n),t[e]=null,delete t[e],t}))}},A=e=>{var{caller:t,path:n}=function(){var e=null,t=null;try{throw new Error}catch(a){var n=a.stack.split("\n"),r=n[0].includes("@")?5:6,o=n[r].trim(),i=o.includes("@")?o.split("@"):o.split(" ").slice(1);e=i[0],t=i[1]}return{caller:e,path:t}}(),r=u[e];return(!r[t]||r[t]===n)&&(u[e][t]=n,!0)},h=(e,t)=>{if(!l.frozen&&t&&A(e)){var n={onActivated:c,onDeactivated:f}[e],r=n.get(d)||new Set;r.add(o([],t))&&n.set(d,r)}};return e(s.Provider,{value:{info:l,elements:i,setInfo:v,onActivated:e=>{h("onActivated",e)},onDeactivated:e=>{h("onDeactivated",e)},insertElement:e=>{let t=e.id;a([t],Object.assign(Object.assign({},i[t]),e))},removeAliveElements:e=>{if(e&&Array.isArray(e))for(const t of e)m(t);else for(const e of Object.values(i))m(e.id)},setCurrentComponentId:e=>{d=e},insertCacheCb:e=>{let t=c.get(e),r=f.get(e);c.delete(e),f.delete(e),u.onActivated={},u.onDeactivated={},Reflect.has(i,e)&&a(n((n=>{n[e].onActivated=t,n[e].onDeactivated=r,n[e].loaded=!0})))}},get children(){return r.children}})}let f=new Set([]);function u(e,t,n,r){return function(o){var{info:c,elements:u,setInfo:m,insertElement:A,setCurrentComponentId:h,insertCacheCb:p}=i(s);Reflect.has(u,t)||(m("frozen",!1),h(t),a((i=>{A({id:t,dispose:i,owner:null,component:r?e:null,element:e(o),subIds:Array.isArray(n)?new Set(n):null})})));var E=e=>{var t;if(u[e].isTop)return e;var n=null===(t=Object.values(u).find((t=>{var n;return null===(n=t.subIds)||void 0===n?void 0:n.has(e)})))||void 0===t?void 0:t.id;return n&&(n=E(n)),n||e};return f.has(E(t))||f.clear(),l((()=>{var e;if(!u[t].loaded){let n=null===(e=u[t])||void 0===e?void 0:e.element;for(;"function"==typeof n;)n=n();n instanceof HTMLElement&&p(t)}})),v((()=>{var e;m("frozen",!1),f.has(t)||u[t].loaded&&(f.add(t),m("frozen",!0),null===(e=u[t].onActivated)||void 0===e||e.forEach((e=>e())),m("frozen",!1))})),d((()=>{var e;c.frozen||(m("frozen",!0),null===(e=u[t].onDeactivated)||void 0===e||e.forEach((e=>e())))})),u[t].element}}function m(t){var n=u((()=>t.children),t.id,t.subIds);return e(n,{})}function A(){var{onActivated:e,onDeactivated:t,removeAliveElements:n,setInfo:r}=i(s);return{onActivated:e,onDeactivated:t,removeAliveElements:n,setInfo:r}}function h(e){var{onActivated:t}=A();t(e)}function p(e){var{onDeactivated:t}=A();t(e)}function E(){var{removeAliveElements:e,setInfo:t}=A();return{removeAliveElements:e,aliveFrozen:()=>t("frozen",!0)}}export{m as AliveComponent,c as AliveProvider,u as aliveTransfer,h as onActivated,p as onDeactivated,E as useAlive};
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import { JSX } from 'solid-js';
|
|
2
|
+
/**
|
|
3
|
+
* @description Alive 组件用的 转换函数
|
|
4
|
+
* @param { ()=> JSX.Element } Component,
|
|
5
|
+
* @param { string } id string,自己的id 值,一定要唯一
|
|
6
|
+
* @param { Array<string> } [subIds] [string,...], 子组件的 id值 可不传,这样默认销毁时不会去干掉子组件,
|
|
7
|
+
*/
|
|
8
|
+
export default function aliveTransfer(Component: <T>(props: T) => JSX.Element, id: string, subIds?: Array<string>, saveComponent?: boolean): <T>(props: T) => JSX.Element;
|
package/dist/types/default.d.ts
CHANGED
|
@@ -7,8 +7,10 @@ export interface ProveiderProps {
|
|
|
7
7
|
export interface NodeInfo {
|
|
8
8
|
id: string
|
|
9
9
|
loaded?: boolean
|
|
10
|
-
|
|
11
|
-
|
|
10
|
+
owner: any,
|
|
11
|
+
component?: ((props:any) => JSX.Element) | null
|
|
12
|
+
element?: JSX.Element | null
|
|
13
|
+
subIds?: Set<string> | null
|
|
12
14
|
dispose?: (() => void) | null
|
|
13
15
|
onActivated?: null | Set<() => void>
|
|
14
16
|
onDeactivated?: null | Set<() => void>
|
|
@@ -36,7 +38,17 @@ export interface ContextProps {
|
|
|
36
38
|
insertElement: (d: NodeInfo) => void
|
|
37
39
|
onActivated: (cb: () => void) => void
|
|
38
40
|
onDeactivated: (cb: () => void) => void
|
|
39
|
-
|
|
41
|
+
removeAliveElements: (ids?: Array<IAliveElementIds>) => void
|
|
40
42
|
setCurrentComponentId: (id: string | symbol) => void
|
|
41
|
-
insertCacheCb: (id: string) => void
|
|
43
|
+
insertCacheCb: (id: string ) => void
|
|
42
44
|
}
|
|
45
|
+
|
|
46
|
+
interface IActive {
|
|
47
|
+
[key: string]: string
|
|
48
|
+
}
|
|
49
|
+
export interface IPrevCall {
|
|
50
|
+
onActivated: IActive
|
|
51
|
+
onDeactivated: IActive
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
export type IAliveElementIds = string
|
package/dist/types/index.d.ts
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import AliveProvider from './AliveProvider';
|
|
2
|
-
import
|
|
2
|
+
import aliveTransfer from './aliveTransfer';
|
|
3
|
+
import { AliveComponent } from './AliveComponent';
|
|
3
4
|
import { onActivated, onDeactivated, useAlive } from './useAlive';
|
|
4
|
-
export { AliveProvider,
|
|
5
|
+
export { AliveComponent, AliveProvider, aliveTransfer, onActivated, onDeactivated, useAlive, };
|
package/dist/types/useAlive.d.ts
CHANGED
|
@@ -5,6 +5,6 @@ export declare function onDeactivated(fn: () => void): void;
|
|
|
5
5
|
* @returns aliveFrozen 让 alive 暂时失去响应, 一般在加新增路由数据时使用
|
|
6
6
|
*/
|
|
7
7
|
export declare function useAlive(): {
|
|
8
|
-
|
|
8
|
+
removeAliveElements: (ids?: Array<import("./default").IAliveElementIds>) => void;
|
|
9
9
|
aliveFrozen: () => void;
|
|
10
10
|
};
|
package/package.json
CHANGED
|
@@ -1,8 +0,0 @@
|
|
|
1
|
-
import { JSX } from 'solid-js';
|
|
2
|
-
/**
|
|
3
|
-
* @description Alive 组件用的 转换函数
|
|
4
|
-
* @param { ()=> JSX.Element } Componet,
|
|
5
|
-
* @param { string } id string,自己的id 值,一定要唯一
|
|
6
|
-
* @param { Array<string> } [children] [string,...], 子组件的 id值 可不传,这样默认销毁时不会去干掉子组件,
|
|
7
|
-
*/
|
|
8
|
-
export default function AliveTransfer(Component: () => JSX.Element, id: string, children?: Array<string> | null): JSX.Element;
|