solid-alive 0.0.3
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.cjs +1 -0
- package/dist/index.js +1 -0
- package/dist/types/AliveProvider.d.ts +8 -0
- package/dist/types/AliveTransfer.d.ts +9 -0
- package/dist/types/context.d.ts +3 -0
- package/dist/types/default.d.ts +34 -0
- package/dist/types/index.d.ts +4 -0
- package/dist/types/useAlive.d.ts +6 -0
- package/dist/types/utils.d.ts +2 -0
- package/package.json +46 -0
- package/readme.md +118 -0
package/dist/index.cjs
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
"use strict";var e=require("solid-js/web"),t=require("solid-js/store"),o=require("solid-js"),n=o.createContext({elements:{},insertElement:()=>{},onDeactivated:()=>{},onActivated:()=>{},removeAliveElement:()=>{},saveScrollTop:()=>{},setCurrentComponentId:()=>{}});exports.AliveProvider=function(o){var[l,r]=t.createStore();let c=Symbol("close"),s=c,a=new Map,i=new Map;const d=e=>{let t=e.id,o=a.get(t),n=i.get(t);a.delete(t),i.delete(t),r([t],{...e,onActivated:o,onDeactivated:n})},v=e=>{if(e)Reflect.has(l,e)&&(e=>{var t=l[e]?.children;t?.forEach((t=>t!==e&&v(t))),r((t=>(t[e].onDeactivated?.(),t[e].component=null,t[e].dispose?.(),t[e].dispose=null,t[e].owner=null,t[e].onActivated=null,t[e].onDeactivated=null,t[e].scrollTop=0,t[e].id="",t[e].children=null,t[e]=null,delete t[e],{...t})))})(e);else for(const e of Object.values(l))v(e.id)};var u=e=>{s=e},m=(e,t)=>{Reflect.has(l,e)&&r((o=>(o[e].scrollTop=t??0,o)))},p=e=>{"string"==typeof s&&a.set(s,e)},f=e=>{"string"==typeof s&&i.set(s,e)};return e.createComponent(n.Provider,{get value(){return{saveBodyScrollTop:o.saveBodyScrollTop,elements:l,closeSymbol:c,onActivated:p,onDeactivated:f,insertElement:d,saveScrollTop:m,removeAliveElement:v,setCurrentComponentId:u}},get children(){return o.children}})},exports.AliveTransfer=function(t,l,r){var{elements:c,saveBodyScrollTop:s,closeSymbol:a,insertElement:i,saveScrollTop:d,setCurrentComponentId:v}=o.useContext(n);Reflect.has(c,l)||(v(l),o.createRoot((n=>{i({id:l,dispose:n,component:e.createComponent(t,{}),onDeactivated:null,onActivated:null,owner:o.getOwner,scrollTop:0,children:Array.isArray(r)?r:null})})));var u=e=>{if(s)if("cache"===e){var t=document.documentElement.scrollTop||document.body.scrollTop||0;c[l]?.scrollTop!==t&&d(l,t)}else"scroll"===e&&setTimeout((()=>{var e;e=c[l]?.scrollTop,document.documentElement.scrollTop=document.body.scrollTop=e||0}),100)};return o.createEffect((()=>{c[l]?.onActivated?.(),u("scroll")})),o.onCleanup((()=>{v(a??""),c[l]?.onDeactivated?.(),u("cache")})),c[l]?.owner&&o.runWithOwner(c[l].owner,(()=>c[l]?.component))},exports.useAlive=function(){var{onActivated:e,onDeactivated:t,removeAliveElement:l}=o.useContext(n);return{onActivated:e,onDeactivated:t,removeAliveElement:l}};
|
package/dist/index.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
import{createComponent as e}from"solid-js/web";import{createStore as o}from"solid-js/store";import{createContext as t,useContext as l,createRoot as n,getOwner as r,createEffect as c,onCleanup as s,runWithOwner as a}from"solid-js";var i=t({elements:{},insertElement:()=>{},onDeactivated:()=>{},onActivated:()=>{},removeAliveElement:()=>{},saveScrollTop:()=>{},setCurrentComponentId:()=>{}});function d(t){var[l,n]=o();let r=Symbol("close"),c=r,s=new Map,a=new Map;const d=e=>{let o=e.id,t=s.get(o),l=a.get(o);s.delete(o),a.delete(o),n([o],{...e,onActivated:t,onDeactivated:l})},v=e=>{if(e)Reflect.has(l,e)&&(e=>{var o=l[e]?.children;o?.forEach((o=>o!==e&&v(o))),n((o=>(o[e].onDeactivated?.(),o[e].component=null,o[e].dispose?.(),o[e].dispose=null,o[e].owner=null,o[e].onActivated=null,o[e].onDeactivated=null,o[e].scrollTop=0,o[e].id="",o[e].children=null,o[e]=null,delete o[e],{...o})))})(e);else for(const e of Object.values(l))v(e.id)};var m=e=>{c=e},p=(e,o)=>{Reflect.has(l,e)&&n((t=>(t[e].scrollTop=o??0,t)))},u=e=>{"string"==typeof c&&s.set(c,e)},f=e=>{"string"==typeof c&&a.set(c,e)};return e(i.Provider,{get value(){return{saveBodyScrollTop:t.saveBodyScrollTop,elements:l,closeSymbol:r,onActivated:u,onDeactivated:f,insertElement:d,saveScrollTop:p,removeAliveElement:v,setCurrentComponentId:m}},get children(){return t.children}})}function v(o,t,d){var{elements:v,saveBodyScrollTop:m,closeSymbol:p,insertElement:u,saveScrollTop:f,setCurrentComponentId:T}=l(i);Reflect.has(v,t)||(T(t),n((l=>{u({id:t,dispose:l,component:e(o,{}),onDeactivated:null,onActivated:null,owner:r,scrollTop:0,children:Array.isArray(d)?d:null})})));var A=e=>{if(m)if("cache"===e){var o=document.documentElement.scrollTop||document.body.scrollTop||0;v[t]?.scrollTop!==o&&f(t,o)}else"scroll"===e&&setTimeout((()=>{var e;e=v[t]?.scrollTop,document.documentElement.scrollTop=document.body.scrollTop=e||0}),100)};return c((()=>{v[t]?.onActivated?.(),A("scroll")})),s((()=>{T(p??""),v[t]?.onDeactivated?.(),A("cache")})),v[t]?.owner&&a(v[t].owner,(()=>v[t]?.component))}function m(){var{onActivated:e,onDeactivated:o,removeAliveElement:t}=l(i);return{onActivated:e,onDeactivated:o,removeAliveElement:t}}export{d as AliveProvider,v as AliveTransfer,m as useAlive};
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import { ProveiderProps } from './default';
|
|
2
|
+
/**
|
|
3
|
+
* @description Alive
|
|
4
|
+
* @param children jsx.element
|
|
5
|
+
* @param { boolean } rememberScrollTop true/false, 默认false, 不会记住body的scrollTop高度
|
|
6
|
+
* @returns jsx.element
|
|
7
|
+
*/
|
|
8
|
+
export default function AliveProvider(props: ProveiderProps): import("solid-js").JSX.Element;
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import { JSX } from 'solid-js';
|
|
2
|
+
/**
|
|
3
|
+
* @description ALive 组件用的 转换函数
|
|
4
|
+
* @param { JSX.Element } Componet()=> JSX.Element,
|
|
5
|
+
* @param { string } id string,自己的id 值,一定要唯一
|
|
6
|
+
* @param { Array<string> } [children] [string,...], 子组件的 id值 可不传,这样默认销毁时不会去干掉子组件,
|
|
7
|
+
* @returns { JSX.Element }
|
|
8
|
+
*/
|
|
9
|
+
export default function AliveTransfer(Component: () => JSX.Element, id: string, children?: Array<string>): JSX.Element;
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
import { getOwner } from 'solid-js/web'
|
|
2
|
+
import { JSX } from 'solid-js'
|
|
3
|
+
|
|
4
|
+
export interface ProveiderProps {
|
|
5
|
+
children: JSX.Element
|
|
6
|
+
saveBodyScrollTop?: boolean
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
export interface NodeInfo {
|
|
10
|
+
id: string
|
|
11
|
+
component: JSX.Element
|
|
12
|
+
dispose: (() => void) | null
|
|
13
|
+
owner: getOwner | null
|
|
14
|
+
onActivated: null | (() => void)
|
|
15
|
+
onDeactivated: null | (() => void)
|
|
16
|
+
scrollTop: number
|
|
17
|
+
children?: Array<string> | null
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
export interface StoreProps {
|
|
21
|
+
[propsName: string]: NodeInfo
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
export interface ContextProps {
|
|
25
|
+
elements: StoreProps
|
|
26
|
+
insertElement: (d: NodeInfo) => void
|
|
27
|
+
onActivated: (cb: () => void) => void
|
|
28
|
+
onDeactivated: (cb: () => void) => void
|
|
29
|
+
removeAliveElement: (id?: string) => void
|
|
30
|
+
saveScrollTop: (id: string, height: number) => void
|
|
31
|
+
setCurrentComponentId: (id: string | symbol) => void
|
|
32
|
+
saveBodyScrollTop?: boolean
|
|
33
|
+
closeSymbol?: symbol
|
|
34
|
+
}
|
package/package.json
ADDED
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "solid-alive",
|
|
3
|
+
"version": "0.0.3",
|
|
4
|
+
"description": "solid-alive",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"main": "dist/index.cjs",
|
|
7
|
+
"module": "dist/index.js",
|
|
8
|
+
"types": "dist/types/index.d.ts",
|
|
9
|
+
"scripts": {
|
|
10
|
+
"build": "tsc && rollup -c",
|
|
11
|
+
"test": "echo \"Error: no test specified\" && exit 1"
|
|
12
|
+
},
|
|
13
|
+
"keywords": [
|
|
14
|
+
"keep-alive",
|
|
15
|
+
"solid-js",
|
|
16
|
+
"keepalive",
|
|
17
|
+
"cache",
|
|
18
|
+
"solid-alive",
|
|
19
|
+
"solid-keep-alive",
|
|
20
|
+
"alive"
|
|
21
|
+
],
|
|
22
|
+
"files": [
|
|
23
|
+
"package.json",
|
|
24
|
+
"README.md",
|
|
25
|
+
"LICENSE",
|
|
26
|
+
"dist"
|
|
27
|
+
],
|
|
28
|
+
"author": "iuxs",
|
|
29
|
+
"license": "ISC",
|
|
30
|
+
"dependencies": {
|
|
31
|
+
"solid-js": "^1.8.16"
|
|
32
|
+
},
|
|
33
|
+
"devDependencies": {
|
|
34
|
+
"@rollup/plugin-json": "^6.1.0",
|
|
35
|
+
"@rollup/plugin-node-resolve": "^15.2.3",
|
|
36
|
+
"@rollup/plugin-typescript": "^11.1.6",
|
|
37
|
+
"rollup": "^4.14.0",
|
|
38
|
+
"rollup-plugin-cleanup": "^3.2.1",
|
|
39
|
+
"rollup-plugin-css-only": "^4.5.2",
|
|
40
|
+
"rollup-plugin-serve": "^1.1.1",
|
|
41
|
+
"rollup-plugin-terser": "^7.0.2",
|
|
42
|
+
"rollup-plugin-typescript2": "^0.36.0",
|
|
43
|
+
"rollup-preset-solid": "^2.0.1",
|
|
44
|
+
"typescript": "^5.4.3"
|
|
45
|
+
}
|
|
46
|
+
}
|
package/readme.md
ADDED
|
@@ -0,0 +1,118 @@
|
|
|
1
|
+
# solid-alive
|
|
2
|
+
|
|
3
|
+
### 安装
|
|
4
|
+
- pnpm add solid-alive / npm i solid-alive / yarn add solid-alive
|
|
5
|
+
### 描述
|
|
6
|
+
- 用于 solid 组件缓存,只测试过2级路由缓存
|
|
7
|
+
- 在 useAlive 中有 onActivated, onDeactivated,removeAliveElement 三个函数使用
|
|
8
|
+
- removeAliveElement: 函数, 可传一个参数, 不传就删除所有缓存 :
|
|
9
|
+
removeAliveElement('/home')
|
|
10
|
+
- onActivated / onDeactivated: 函数,只能传一个函数,多次调用只有最一个会调用.
|
|
11
|
+
onActivated(()=> console.log('actived') )
|
|
12
|
+
- 可记住body的高度,但要在 AliveProvider 中加 saveBodyScrollTop参数,但组件内部的滚动条问题自己解决
|
|
13
|
+
- 子父 缓存/删除 问题
|
|
14
|
+
- 如果某组件下有子组件,在父的 AliveTransfer中,
|
|
15
|
+
第三个参数,为数组 写上子组件的唯一id: ['/childrenId','asf',...]
|
|
16
|
+
- 使用见下图, 也可用 removeAliveElement 删除
|
|
17
|
+
|
|
18
|
+
|
|
19
|
+
### 使用
|
|
20
|
+
1 /index.tsx,AliveProvider将app 包裹, saveBodyScrollTop : 记住body滚动高度
|
|
21
|
+
```jsx
|
|
22
|
+
import { render } from 'solid-js/web'
|
|
23
|
+
import App from './App'
|
|
24
|
+
import { AliveProvider } from 'solid-alive'
|
|
25
|
+
const root = document.getElementById('root')
|
|
26
|
+
|
|
27
|
+
render(() =>
|
|
28
|
+
<AliveProvider saveBodyScrollTop>
|
|
29
|
+
<App />
|
|
30
|
+
</AliveProvider>
|
|
31
|
+
, root!)
|
|
32
|
+
```
|
|
33
|
+
|
|
34
|
+
2 /App.tsx ,在 solid-alive 中 有 AliveTransfer, 参数: JSX , id:string, children?:[string..]
|
|
35
|
+
```jsx
|
|
36
|
+
import { Route, Router } from '@solidjs/router';
|
|
37
|
+
import { AliveTransfer } from 'solid-alive';
|
|
38
|
+
|
|
39
|
+
import Home from './views/Home';
|
|
40
|
+
import Client from './views/Client';
|
|
41
|
+
import About from './views/About';
|
|
42
|
+
import Team from './views/Blog/Team';
|
|
43
|
+
import Blog from './views/Blog';
|
|
44
|
+
import Single from './views/Blog/Single';
|
|
45
|
+
|
|
46
|
+
// 单个
|
|
47
|
+
const AboutTsf = () => AliveTransfer(About, '/about');
|
|
48
|
+
|
|
49
|
+
// 当Blog 下有子组件时, 第三个参数一定要, 与子组件的
|
|
50
|
+
const BlogTsf = (props: any) =>
|
|
51
|
+
AliveTransfer(Blog.bind(null, props), '/blog', ['single', '/team']);
|
|
52
|
+
// blog 下的子组件
|
|
53
|
+
const SingleTsf = () => AliveTransfer(Single, 'single');
|
|
54
|
+
const TeamTsf = () => AliveTransfer(Team, '/team');
|
|
55
|
+
|
|
56
|
+
export default function App() {
|
|
57
|
+
return (
|
|
58
|
+
<Router>
|
|
59
|
+
{/* 这个Client 只是用于标签加跳转的,使用时可不用 */}
|
|
60
|
+
<Route component={Client}>
|
|
61
|
+
<Route component={Home} path={'/'} />
|
|
62
|
+
{/* 单个缓存 single */}
|
|
63
|
+
<Route component={AboutTsf} path={'/about'} />
|
|
64
|
+
{/* 父子 缓存 Parent Child Nesting */}
|
|
65
|
+
<Route component={BlogTsf} path={'/blog'}>
|
|
66
|
+
<Route component={TeamTsf} path={'team'} />
|
|
67
|
+
<Route component={SingleTsf} path={'single'} />
|
|
68
|
+
</Route>
|
|
69
|
+
</Route>
|
|
70
|
+
</Router>
|
|
71
|
+
);
|
|
72
|
+
}
|
|
73
|
+
```
|
|
74
|
+
3 父 /views/Blog/index.tsx
|
|
75
|
+
```jsx
|
|
76
|
+
export default function Blog(props: any) {
|
|
77
|
+
return (
|
|
78
|
+
<div>
|
|
79
|
+
<h2>Blog</h2>
|
|
80
|
+
<div>{props.children}</div>
|
|
81
|
+
</div>
|
|
82
|
+
);
|
|
83
|
+
}
|
|
84
|
+
```
|
|
85
|
+
|
|
86
|
+
- 子 /views/Blog/Single/index.tsx 中
|
|
87
|
+
```jsx
|
|
88
|
+
import { useAlive } from "solid-alive"
|
|
89
|
+
|
|
90
|
+
export default function Single() {
|
|
91
|
+
const { onActivated,onDeactivated,removeAliveElement } = useAlive()
|
|
92
|
+
|
|
93
|
+
const click = () => {
|
|
94
|
+
removeAliveElement('/about') // delete '/about'; 删除 /about
|
|
95
|
+
// removeAliveElement() // delete all alive element; 会删除所有缓存的组件
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
// not call 这个不会被调用
|
|
99
|
+
onActivated(()=>{
|
|
100
|
+
console.log('Single-activeated-1')
|
|
101
|
+
})
|
|
102
|
+
|
|
103
|
+
// call 这个会被调用, 这个是最后一个才会被调用,缓存进入
|
|
104
|
+
onActivated(()=>{
|
|
105
|
+
console.log('Single-activeated-2')
|
|
106
|
+
})
|
|
107
|
+
// 缓存离开,
|
|
108
|
+
onDeactivated(()=>{
|
|
109
|
+
console.log('Single-deactivated')
|
|
110
|
+
})
|
|
111
|
+
return (
|
|
112
|
+
<div>
|
|
113
|
+
<h2>Single</h2>
|
|
114
|
+
<input type="text" style={{border:'2px solid red'}} />
|
|
115
|
+
</div>
|
|
116
|
+
)
|
|
117
|
+
}
|
|
118
|
+
```
|