solid-alive 0.2.0 → 0.2.2
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/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 +11 -6
- package/dist/types/index.d.ts +3 -2
- package/dist/types/useAlive.d.ts +1 -1
- package/package.json +1 -1
- package/{README.md → readme.md} +32 -36
- package/dist/types/AliveTransfer.d.ts +0 -8
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 a,createRoot as i,createComputed as l,createEffect as s,onCleanup as d}from"solid-js";var v=r({elements:{},info:{frozen:!1},setInfo:()=>{},insertElement:()=>{},onDeactivated:()=>{},onActivated:()=>{},removeAliveElements:()=>{},setCurrentComponentId:()=>{},insertCacheCb:()=>{}});function c(r){let[a,i]=t(),[l,s]=t({frozen:!0}),d="",c=new Map,f=new Map,u={onActivated:{},onDeactivated:{}};var m=e=>{var t;if(Reflect.has(a,e)){var n=null===(t=a[e])||void 0===t?void 0:t.subIds;null==n||n.forEach((t=>t!==e&&m(t))),i((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(i){var n=i.stack.split("\n"),r=n[0].includes("@")?5:6,o=n[r].trim(),a=o.includes("@")?o.split("@"):o.split(" ").slice(1);e=a[0],t=a[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(v.Provider,{value:{info:l,elements:a,setInfo:s,onActivated:e=>{h("onActivated",e)},onDeactivated:e=>{h("onDeactivated",e)},insertElement:e=>{let t=e.id;var n=Object.values(a).find((e=>{var n;return null===(n=e.subIds)||void 0===n?void 0:n.has(t)}));i([t],Object.assign(Object.assign(Object.assign({},a[t]),e),{fatherId:null==n?void 0:n.id}))},removeAliveElements:e=>{if(Array.isArray(e))for(const t of e)m(t);else if(!e)for(const e of Object.values(a))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(a,e)&&i(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){return function(r){var{info:o,elements:c,setInfo:u,insertElement:m,setCurrentComponentId:A,insertCacheCb:h}=a(v);Reflect.has(c,t)||(u("frozen",!1),A(t),i((o=>{m({id:t,dispose:o,owner:null,element:e(r),subIds:Array.isArray(n)?new Set(n):null})})));var p=e=>{var t=c[e].fatherId;return t?p(t):e};return l((()=>{var e;if(c[t].loaded)return;let n=null===(e=c[t])||void 0===e?void 0:e.element;for(;"function"==typeof n;)n=n();(Array.isArray(n)||n instanceof HTMLElement)&&h(t)})),f.size&&!f.has(p(t))&&f.clear(),s((()=>{var e;u("frozen",!1),f.has(t)||c[t].loaded&&(f.add(t),u("frozen",!0),null===(e=c[t].onActivated)||void 0===e||e.forEach((e=>e())),u("frozen",!1))})),d((()=>{var e;o.frozen||(u("frozen",!0),null===(e=c[t].onDeactivated)||void 0===e||e.forEach((e=>e())))})),c[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}=a(v);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 I(){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,I 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>): <T>(props: T) => JSX.Element;
|
package/dist/types/default.d.ts
CHANGED
|
@@ -7,12 +7,15 @@ export interface ProveiderProps {
|
|
|
7
7
|
export interface NodeInfo {
|
|
8
8
|
id: string
|
|
9
9
|
loaded?: boolean
|
|
10
|
-
|
|
11
|
-
|
|
10
|
+
owner: any,
|
|
11
|
+
props?:any
|
|
12
|
+
component?: ((props:any) => JSX.Element) | null
|
|
13
|
+
element?: JSX.Element | null
|
|
14
|
+
subIds?: Set<string> | null
|
|
12
15
|
dispose?: (() => void) | null
|
|
13
16
|
onActivated?: null | Set<() => void>
|
|
14
17
|
onDeactivated?: null | Set<() => void>
|
|
15
|
-
|
|
18
|
+
fatherId?: string
|
|
16
19
|
}
|
|
17
20
|
|
|
18
21
|
export interface SetElement {
|
|
@@ -36,15 +39,17 @@ export interface ContextProps {
|
|
|
36
39
|
insertElement: (d: NodeInfo) => void
|
|
37
40
|
onActivated: (cb: () => void) => void
|
|
38
41
|
onDeactivated: (cb: () => void) => void
|
|
39
|
-
|
|
42
|
+
removeAliveElements: (ids?: Array<IAliveElementIds>) => void
|
|
40
43
|
setCurrentComponentId: (id: string | symbol) => void
|
|
41
|
-
insertCacheCb: (id: string) => void
|
|
44
|
+
insertCacheCb: (id: string ) => void
|
|
42
45
|
}
|
|
43
46
|
|
|
44
47
|
interface IActive {
|
|
45
|
-
[key:string]: string
|
|
48
|
+
[key: string]: string
|
|
46
49
|
}
|
|
47
50
|
export interface IPrevCall {
|
|
48
51
|
onActivated: IActive
|
|
49
52
|
onDeactivated: IActive
|
|
50
53
|
}
|
|
54
|
+
|
|
55
|
+
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
package/{README.md → readme.md}
RENAMED
|
@@ -5,14 +5,16 @@
|
|
|
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()
|
|
13
|
+
|
|
12
14
|
- 子父 缓存/删除 问题
|
|
13
|
-
- 如果某组件下有子组件,在父的
|
|
15
|
+
- 如果某组件下有子组件,在父的 aliveTransfer中,
|
|
14
16
|
第三个参数,为对象 写上子组件的唯一id: {children:['/childrenId','asf',...]}
|
|
15
|
-
- 使用见下图, 也可用
|
|
17
|
+
- 使用见下图, 也可用 removeAliveElements 删除
|
|
16
18
|
|
|
17
19
|
|
|
18
20
|
|
|
@@ -32,10 +34,10 @@ render(() =>
|
|
|
32
34
|
, root!)
|
|
33
35
|
```
|
|
34
36
|
|
|
35
|
-
2 /App.tsx ,在 solid-alive 中 有
|
|
37
|
+
2 /App.tsx ,在 solid-alive 中 有 aliveTransfer, 参数: JSX , id:string, children?:[string..]
|
|
36
38
|
```jsx
|
|
37
39
|
import { Route, Router } from '@solidjs/router';
|
|
38
|
-
import {
|
|
40
|
+
import { aliveTransfer } from 'solid-alive';
|
|
39
41
|
|
|
40
42
|
import Home from './views/Home';
|
|
41
43
|
import Client from './views/Client';
|
|
@@ -44,14 +46,12 @@ import Team from './views/Blog/Team';
|
|
|
44
46
|
import Blog from './views/Blog';
|
|
45
47
|
import Single from './views/Blog/Single';
|
|
46
48
|
|
|
47
|
-
const AboutTsf = () => AliveTransfer(About, '/about');
|
|
48
49
|
|
|
49
|
-
|
|
50
|
-
const
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
const
|
|
54
|
-
const TeamTsf = () => AliveTransfer(Team, '/team');
|
|
50
|
+
const HomeTransfer = aliveTransfer(Home, '/')
|
|
51
|
+
const AboutTransfer = aliveTransfer(About, '/about')
|
|
52
|
+
const ShopTransfer = aliveTransfer(Shop, '/shop', ['/shopPage'])
|
|
53
|
+
const ShopPageTransfer = aliveTransfer(ShopPage, '/shopPage')
|
|
54
|
+
const BlogTransfer = aliveTransfer(Blog, '/blog', ['contact', '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,14 @@ 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
|
-
let divRef: Element | undefined = undefined
|
|
94
92
|
const click = () => {
|
|
95
|
-
|
|
96
|
-
//
|
|
93
|
+
removeAliveElements(['/about']) // delete '/about'; 删除 /about
|
|
94
|
+
// removeAliveElements() // delete all alive element; 会删除所有缓存的组件
|
|
97
95
|
}
|
|
98
96
|
|
|
99
97
|
//todo call 这个会被调用
|
|
@@ -112,18 +110,22 @@ export default function Single() {
|
|
|
112
110
|
return (
|
|
113
111
|
<div>
|
|
114
112
|
<h2>Single</h2>
|
|
113
|
+
{ /** 这个 AliveComponent 最好是在 未缓存的组件 中使用, 不然生命周期不同步 */ }
|
|
114
|
+
<AliveComponent id={'home1'}>
|
|
115
|
+
<Home />
|
|
116
|
+
</AliveComponent>
|
|
115
117
|
<input type="text" style={{ border: '2px solid red' }} />
|
|
116
118
|
</div>
|
|
117
119
|
)
|
|
118
120
|
}
|
|
119
121
|
```
|
|
120
122
|
|
|
121
|
-
|
|
123
|
+
3 动态生成 路由
|
|
122
124
|
```tsx
|
|
123
|
-
/**
|
|
125
|
+
/** App.tsx */
|
|
124
126
|
import { createEffect, lazy, type Component } from 'solid-js'
|
|
125
127
|
import { Route, Router } from '@solidjs/router'
|
|
126
|
-
import { useAlive,
|
|
128
|
+
import { useAlive, aliveTransfer } from "solid-alive"
|
|
127
129
|
|
|
128
130
|
const modules = import.meta.glob<{ default: Component<any> }>([
|
|
129
131
|
'./views/**/**.tsx',
|
|
@@ -132,14 +134,13 @@ const modules = import.meta.glob<{ default: Component<any> }>([
|
|
|
132
134
|
|
|
133
135
|
const transferRouter = (data: MenuData[]) =>
|
|
134
136
|
data.flatMap(item => {
|
|
135
|
-
let
|
|
136
|
-
if (!
|
|
137
|
-
const Transfer = (
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
)
|
|
137
|
+
let module = modules[`./views${item.realPath}`]
|
|
138
|
+
if (!module) return []
|
|
139
|
+
const Transfer = aliveTransfer(
|
|
140
|
+
lazy(module),
|
|
141
|
+
item.path,
|
|
142
|
+
item.children?.map(item => item.path)
|
|
143
|
+
)
|
|
143
144
|
return (
|
|
144
145
|
<Route component={Transfer} path={item.path.split('/').at(-1)}>
|
|
145
146
|
{item.children ? transferRouter(item.children) : null}
|
|
@@ -157,9 +158,6 @@ const App: Component = () => {
|
|
|
157
158
|
path: '/about',
|
|
158
159
|
realPath: '/About/index.tsx',
|
|
159
160
|
parentId: null,
|
|
160
|
-
hidden: 0,
|
|
161
|
-
sort: 2,
|
|
162
|
-
cache: 1
|
|
163
161
|
}
|
|
164
162
|
const addRoute =()=>{
|
|
165
163
|
aliveFrozen() // 暂时不响应下面数据变化
|
|
@@ -172,13 +170,11 @@ const App: Component = () => {
|
|
|
172
170
|
<Router>
|
|
173
171
|
<Route component={Client}>
|
|
174
172
|
{/* treeData 将 data变成 树结构数据 */}
|
|
175
|
-
{transferRouter(treeData(data
|
|
173
|
+
{transferRouter(treeData(data))}
|
|
176
174
|
</Route>
|
|
177
175
|
</Router>
|
|
178
176
|
)
|
|
179
177
|
}
|
|
180
178
|
|
|
181
179
|
export default App
|
|
182
|
-
|
|
183
|
-
|
|
184
180
|
```
|
|
@@ -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;
|