solid-alive 0.3.2 → 0.3.4

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 CHANGED
@@ -1 +1 @@
1
- import{createComponent as e}from"solid-js/web";import{createContext as t,useContext as n,createRoot as r,getOwner as i,onCleanup as l,runWithOwner as o,createEffect as s,untrack as d}from"solid-js";import{produce as v,createStore as a}from"solid-js/store";const u=Symbol("currentId"),c=Symbol("setActiveCb");var m=t({elements:{},setElements:()=>{},setActiveCb:()=>{},aliveIds:()=>{}});const f=t({[u]:void 0,[c]:()=>{}});var A=(t,n,r)=>e(f.Provider,{value:{[u]:r,[c]:n},get children(){return t()}});function y(e,t,s){return function(d){var a,u,c=n(m);return(null===(a=c.aliveIds())||void 0===a?void 0:a.includes(t))?(c.elements[t]?null===(u=c.elements[t].onActivated)||void 0===u||u.forEach((e=>e())):(c.setElements({[t]:{id:t,subsets:Array.isArray(s)?s:null}}),r((n=>{c.setElements(v((r=>{r[t].dispose=n,r[t].id=t,r[t].owner=i(),r[t].element=A((()=>e(d)),c.setActiveCb,t)})))}))),l((()=>{var e;null===(e=c.elements[t].onDeactivated)||void 0===e||e.forEach((e=>e()))})),c.elements[t].owner&&o(c.elements[t].owner,(()=>c.elements[t].element))):A((()=>e(d)))}}function h(t){const[n,r]=a(),i=e=>{var t,l,o;if(Array.isArray(e))for(var s of e)i(null===(t=n[s])||void 0===t?void 0:t.subsets),null===(o=null===(l=n[s])||void 0===l?void 0:l.dispose)||void 0===o||o.call(l),r({[s]:void 0})};return s((e=>{var n=Array.isArray(t.include)?t.include:[];if(e.length>n.length){var r=new Set(n);i(e.filter((e=>!r.has(e))))}return n}),Array.isArray(t.include)?t.include:[]),e(m.Provider,{value:{elements:n,setElements:r,setActiveCb:(e,t,n,i)=>{r(v((r=>{r[e][t]?r[e][t][i](n):"add"===i&&(r[e][t]=new Set([n]))})))},aliveIds:()=>t.include},get children(){return t.children}})}function p(e,t){d((()=>{var r,i=n(f),o=i[u];o&&(null===(r=i[c])||void 0===r||r.call(i,o,e,t,"add"),"onActivated"===e&&s((()=>{!function(e){"undefined"!=typeof Promise&&"function"==typeof Promise.prototype.then?Promise.resolve().then(e):setTimeout(e)}(t)})),l((()=>{var n;null===(n=i[c])||void 0===n||n.call(i,o,e,t,"delete"),t=null})))}))}function b(e){p("onActivated",e)}function w(e){p("onDeactivated",e)}export{h as AliveProvider,y as aliveTransfer,b as onActivated,w as onDeactivated};
1
+ import{createComponent as e}from"solid-js/web";import{createContext as n,useContext as t,createRoot as r,createEffect as i,onCleanup as o,untrack as l}from"solid-js";import{produce as s,createStore as a}from"solid-js/store";const d=Symbol("currentId"),v=Symbol("setActiveCb"),u=Symbol("parentId");var c=n({elements:{},setElements:()=>{},setActiveCb:()=>{},aliveIds:()=>{},info:{frozen:!1},transitionEnterName:()=>{}});const f=n({[d]:void 0,[v]:()=>{}});var m=(n,t,r,i)=>e(f.Provider,{value:{[d]:i,[v]:r,[u]:t},get children(){return n()}}),p=e=>{for(;"function"==typeof e;)e=e();return e},h=!1;function y(e,n,l){return function(a){var v,u,y,A,b=t(c),E=t(f)[d],I=b.transitionEnterName();if(!(null===(v=b.aliveIds())||void 0===v?void 0:v.includes(n)))return m((()=>e(a)));if(E&&(null===(u=b.elements[n])||void 0===u?void 0:u.parentId)&&b.elements[n].parentId!==E)return null;b.elements[n]?b.info.frozen?(null===(y=b.elements[n].subsets)||void 0===y?void 0:y.some((e=>b.elements[e])))||(b.info.frozen=!1):null===(A=b.elements[n].onActivated)||void 0===A||A.forEach((e=>e())):(b.setElements({[n]:{id:n,subsets:Array.isArray(l)?l:null}}),r((t=>{b.setElements(s((r=>{var i;r[n].dispose=t,r[n].parentId=null===(i=Object.values(b.elements).find((e=>{var t;return null===(t=e.subsets)||void 0===t?void 0:t.includes(n)})))||void 0===i?void 0:i.id,r[n].element=m((()=>e(a)),r[n].parentId,b.setActiveCb,n)})))})));var g=e=>{if(e instanceof HTMLElement&&!h){h=!0,e.classList.add(I);var n=()=>{e.removeEventListener("animationend",n),e.classList.remove(I),h=!1};e.addEventListener("animationend",n)}};return I&&!b.info.frozen&&i((()=>{if(!h){var e=n;do{b.elements[e].parentId||g(p(b.elements[e].element)),e=b.elements[e].parentId}while(e)}})),o((()=>{var e,t;b.info.frozen||null===(t=null===(e=b.elements[n])||void 0===e?void 0:e.onDeactivated)||void 0===t||t.forEach((e=>e()))})),b.elements[n].element}}function A(n){const[t,r]=a();return i((e=>{var i=Array.isArray(n.include)?n.include:[];if(e.length>i.length){var o=new Set(i);(e=>{var n,i;if(Array.isArray(e))for(var o of e)null===(i=null===(n=t[o])||void 0===n?void 0:n.dispose)||void 0===i||i.call(n),r({[o]:void 0})})(e.filter((e=>!o.has(e))))}return i}),Array.isArray(n.include)?n.include:[]),e(c.Provider,{value:{info:{frozen:!1},elements:t,setElements:r,setActiveCb:(e,n,t,i)=>{r(s((r=>{r[e][n]?r[e][n][i](t):"add"===i&&(r[e][n]=new Set([t]))})))},aliveIds:()=>n.include,transitionEnterName:()=>n.transitionEnterName},get children(){return n.children}})}const b=(()=>{let e=[],n=!1,t=()=>{},r=/iPad|iPhone|iPod/.test(navigator.userAgent)&&!window.MSStream,i=()=>{n=!1;var t=e.slice(0);e.length=0;for(let e=0;e<t.length;e++)t[e]()};if("undefined"!=typeof Promise&&"function"==typeof Promise.prototype.then){const e=Promise.resolve();t=()=>{e.then(i),r&&setTimeout((()=>{}))}}else if("undefined"!=typeof MutationObserver){let e=1;const n=new MutationObserver(i),r=document.createTextNode(String(e));n.observe(r,{characterData:!0}),t=()=>{e=(e+1)%2,r.data=String(e)}}else t=()=>{setTimeout(i,0)};return r=>{e.push(r),n||(n=!0,t())}})();function E(e,n){l((()=>{var r,i=t(f),l=i[d];l&&(null===(r=i[v])||void 0===r||r.call(i,l,e,n,"add"),"onActivated"===e&&b(n),o((()=>{var t;null===(t=i[v])||void 0===t||t.call(i,l,e,n,"delete"),n=null})))}))}function I(e){E("onActivated",e)}function g(e){E("onDeactivated",e)}function S(){var e=t(c).info;return()=>e.frozen=!0}export{A as AliveProvider,y as aliveTransfer,b as nextTick,I as onActivated,g as onDeactivated,S as useAliveFrozen};
@@ -1,2 +1,8 @@
1
1
  import { ProveiderProps } from "./types";
2
+ /**
3
+ * @description Alive
4
+ * @param children jsx.element
5
+ * @param {Arrya<string> | null} [include] 哪些路由要缓存, 默认不缓存, ['/','/about',...]
6
+ * @param {string} [transitionEnterName] 动画名称 transitionEnterName="appear"
7
+ */
2
8
  export default function AliveProvider(props: ProveiderProps): import("solid-js").JSX.Element;
@@ -1,2 +1,3 @@
1
1
  export declare function onActivated(cb: () => void): void;
2
2
  export declare function onDeactivated(cb: () => void): void;
3
+ export declare function useAliveFrozen(): () => boolean;
@@ -1,2 +1,8 @@
1
1
  import { JSX } from "solid-js";
2
- export default function aliveTransfer(Component: <T>(props: T) => JSX.Element, id: string, subsets?: Array<string>): <T>(props: T) => JSX.Element;
2
+ /**
3
+ * @description Alive 组件用的 转换函数; aliveTransfer(Comp, ‘/home’)
4
+ * @param { ()=> JSX.Element } Component () => JSX.Element
5
+ * @param { string } id string,自己的id 值,一定要唯一
6
+ * @param { Array<string> } [subsets] [string,...], 子组件的 id值 可不传
7
+ */
8
+ export default function aliveTransfer(Component: <T>(props: T) => JSX.Element, id: string, subsets?: Array<string> | null): <T>(props: T) => JSX.Element;
@@ -1,9 +1,11 @@
1
1
  import { Context } from "./types";
2
2
  export declare const CURRENTID: unique symbol;
3
3
  export declare const SETACTIVECB: unique symbol;
4
+ export declare const PARENTID: unique symbol;
4
5
  declare const _default: import("solid-js").Context<Context>;
5
6
  export default _default;
6
7
  export declare const ChildContext: import("solid-js").Context<{
7
8
  [CURRENTID]: string | undefined;
8
9
  [SETACTIVECB]?: Context["setActiveCb"];
10
+ [PARENTID]?: string;
9
11
  }>;
@@ -1,4 +1,5 @@
1
1
  import aliveTransfer from "./aliveTransfer";
2
2
  import AliveProvider from "./AliveProvider";
3
- import { onActivated, onDeactivated } from "./active";
4
- export { aliveTransfer, AliveProvider, onActivated, onDeactivated };
3
+ import { onActivated, onDeactivated, useAliveFrozen } from "./active";
4
+ import nextTick from "./nextTick";
5
+ export { aliveTransfer, AliveProvider, onActivated, onDeactivated, useAliveFrozen, nextTick };
@@ -1 +1,2 @@
1
- export declare function nextTick(cb: () => void): void;
1
+ declare const nextTick: (cb: () => void) => void;
2
+ export default nextTick;
@@ -1,4 +1,4 @@
1
- import { Owner, JSX } from "solid-js"
1
+ import { Owner, JSX, Accessor } from "solid-js"
2
2
  import { SetStoreFunction } from "solid-js/store"
3
3
 
4
4
  export type EmitType = keyof Emit
@@ -14,21 +14,34 @@ export interface Emit {
14
14
 
15
15
  export interface ProveiderProps {
16
16
  children: JSX.Element
17
- include?: Array<string>
17
+ include?: Array<string>
18
+ transitionEnterName?: string
19
+ }
20
+
21
+ export interface Info {
22
+ frozen: boolean
18
23
  }
19
24
 
20
25
  export interface Element {
21
26
  id: string
22
27
  element: JSX.Element
28
+ subsets?:Array<string> | null
23
29
  dispose?: () => void
24
30
  onActivated?: Set<() => void>
25
31
  onDeactivated?: Set<() => void>
26
- subsets?: Array<string>
32
+ parentId?: string
27
33
  owner?: Owner | null
28
34
  }
29
35
  export interface Context {
30
36
  elements: Record<string, Element>
31
37
  setElements: SetStoreFunction<{}>
32
- aliveIds: ()=> Array<string> | undefined
33
- setActiveCb: (id:string,t: Activate, cb: () => void, t1: 'add'| 'delete') => void
38
+ aliveIds: () => Array<string> | undefined
39
+ setActiveCb: (
40
+ id: string,
41
+ t: Activate,
42
+ cb: () => void,
43
+ t1: "add" | "delete"
44
+ ) => void
45
+ info: { frozen: boolean }
46
+ transitionEnterName: () => string | void
34
47
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "solid-alive",
3
- "version": "0.3.2",
3
+ "version": "0.3.4",
4
4
  "author": "1iuxs",
5
5
  "description": "solid-alive",
6
6
  "type": "module",
@@ -12,9 +12,7 @@
12
12
  "test": "echo \"Error: no test specified\" && exit 1"
13
13
  },
14
14
  "keywords": [
15
- "keep-alive",
16
15
  "solid-js",
17
- "keepalive",
18
16
  "cache",
19
17
  "solid-alive",
20
18
  "solid-keep-alive",
@@ -32,7 +30,7 @@
32
30
  },
33
31
  "license": "ISC",
34
32
  "dependencies": {
35
- "solid-js": "^1.9.3"
33
+ "solid-js": "^1.9.4"
36
34
  },
37
35
  "devDependencies": {
38
36
  "@rollup/plugin-json": "^6.1.0",
@@ -40,13 +38,13 @@
40
38
  "@rollup/plugin-typescript": "^11.1.6",
41
39
  "@types/css-modules": "^1.0.5",
42
40
  "autoprefixer": "^10.4.20",
43
- "rollup": "^4.29.2",
41
+ "rollup": "^4.30.1",
44
42
  "rollup-plugin-cleanup": "^3.2.1",
45
43
  "rollup-plugin-postcss": "^4.0.2",
46
44
  "rollup-plugin-serve": "^1.1.1",
47
45
  "rollup-plugin-terser": "^7.0.2",
48
46
  "rollup-plugin-typescript2": "^0.36.0",
49
47
  "rollup-preset-solid": "^2.0.1",
50
- "typescript": "^5.7.2"
48
+ "typescript": "^5.7.3"
51
49
  }
52
50
  }
package/readme.md CHANGED
@@ -1,155 +1,176 @@
1
1
  ## solid-alive
2
2
 
3
3
  ### 安装(install)
4
- - pnpm add solid-alive / npm i solid-alive / yarn add solid-alive
4
+
5
+ - pnpm add solid-alive / npm i solid-alive / yarn add solid-alive
6
+
5
7
  ### 描述(describe)
6
- - 用于 solid 组件缓存,只测试过2级路由缓存
7
- - AliveProvider
8
- - include : 数组, ['/','/about'], 当数据变少时, 会自动去删除少的数据缓存
9
- - 在 useAlive
10
- - onActivated
11
- - onDeactivated
12
8
 
13
- - 子父 缓存/删除 问题
14
- - 如果某组件下有子组件,在父的 aliveTransfer中,
15
- 第三个参数,为对象 写上子组件的唯一id: {children:['/childrenId','asf',...]}
9
+ - 用于 solid 组件缓存,只测试过 2 级路由缓存
10
+ - AliveProvider
11
+ - include : 数组, 默认不缓存, ['/','/about'], 当数据变少时, 会自动去删除少的数据缓存
12
+ - transitionEnterName 动画, class 名称, 案例下面
13
+ - onActivated
14
+ - onDeactivated
15
+ - useAliveFrozen: hooks 在新增路由时 ,暂时不响应 路由数据变化
16
+ 使用: var aliveFrozen = useAliveFrozen()
17
+ - nextTick
16
18
 
19
+ ### 使用(use)
17
20
 
21
+ 1 /index.tsx,AliveProvider 将 app 包裹
18
22
 
19
- ### 使用(use)
20
- 1 /index.tsx,AliveProvider将app 包裹
21
- ```jsx
23
+ ```jsx
22
24
  import { render } from 'solid-js/web'
23
25
  import App from './App'
24
26
  import { AliveProvider } from 'solid-alive'
27
+ // import styles from "css.path"
25
28
 
26
29
  const root = document.getElementById('root')
27
30
 
28
- render(() =>
29
- <AliveProvider include={[]}>
30
- <App />
31
- </AliveProvider>
31
+ render(() =>
32
+ // cache id = '/' ;
33
+ // transitionEnterName={styles.appear}
34
+ <AliveProvider include={['/']} transitionEnterName="xxx">
35
+ <App />
36
+ </AliveProvider>
32
37
  , root!)
33
- ```
38
+ ```
34
39
 
35
- 2 /App.tsx ,在 solid-alive 中 有 aliveTransfer, 参数: JSX , id:string, children?:[string..]
36
- ```jsx
37
- import { Route, Router } from '@solidjs/router';
38
- import { aliveTransfer } from 'solid-alive';
40
+ transitionEnterName 动画 css
39
41
 
40
- import Home from './views/Home';
41
- import Client from './views/Client';
42
- import About from './views/About';
43
- import Team from './views/Blog/Team';
44
- import Blog from './views/Blog';
45
- import Single from './views/Blog/Single';
42
+ ```css
43
+ .appear {
44
+ animation: _appear 0.3s ease-in;
45
+ -webkit-animation: _appear 0.3s ease-in;
46
+ }
47
+
48
+ @keyframes _appear {
49
+ 0% {
50
+ opacity: 0;
51
+ }
52
+ 100% {
53
+ opacity: 1;
54
+ }
55
+ }
56
+ ```
46
57
 
58
+ 2 /App.tsx ,在 solid-alive 中 有 aliveTransfer, 参数: JSX , id:string, children?:[string..]
47
59
 
48
- const HomeTransfer = aliveTransfer(Home, '/'),
49
- AboutTsf = aliveTransfer(About, '/about'),
50
- BlogTsf = aliveTransfer(Blog, '/blog', ['/contact', 'single']),
51
- SingleTsf = aliveTransfer(Single,'single')
60
+ ```jsx
61
+ import { Route, Router } from "@solidjs/router"
62
+ import { aliveTransfer } from "solid-alive"
63
+
64
+ import Home from "./views/Home"
65
+ import Client from "./views/Client"
66
+ import About from "./views/About"
67
+ import Team from "./views/Blog/Team"
68
+ import Blog from "./views/Blog"
69
+ import Single from "./views/Blog/Single"
70
+ import Contact from "./views/Contact"
71
+
72
+ const HomeTransfer = aliveTransfer(Home, "/"),
73
+ AboutTsf = aliveTransfer(About, "/about"),
74
+ BlogTsf = aliveTransfer(Blog, "/blog", ["single", "/contact"]),
75
+ SingleTsf = aliveTransfer(Single, "single"),
76
+ ContactTsf = aliveFransfer(Contact, "/contact")
52
77
 
53
78
  export default function App() {
54
79
  return (
55
80
  <Router>
56
- {/* 这个Client 只是用于标签加跳转的,使用时可不用 */}
57
- <Route component={Client}>
58
- <Route component={Home} path={'/'} />
59
- {/* 单个缓存 single */}
60
- <Route component={AboutTsf} path={'/about'} />
61
- {/* 父子 缓存 Parent Child Nesting */}
62
- <Route component={BlogTsf} path={'/blog'}>
63
- <Route component={SingleTsf} path={'single'} />
64
- </Route>
81
+ <Route component={Home} path={"/"} />
82
+ {/* 单个缓存 single */}
83
+ <Route component={AboutTsf} path={"/about"} />
84
+ {/* 父子 缓存 Parent Child Nesting */}
85
+ <Route component={BlogTsf} path={"/blog"}>
86
+ <Route component={SingleTsf} path={"single"} />
87
+ <Route component={ContactTsf} path={"contact"} />
65
88
  </Route>
66
89
  </Router>
67
- );
68
- }
69
- ```
70
- 3 父 /views/Blog/index.tsx
71
- ```jsx
72
- export default function Blog(props: any) {
73
- return (
74
- <div>
75
- <h2>Blog</h2>
76
- <div>{props.children}</div>
77
- </div>
78
- );
90
+ )
79
91
  }
80
92
  ```
81
93
 
82
- - 子 /views/Blog/Single/index.tsx
83
- ```tsx
84
- import { onActivated,onDeactivated,useAlive, AliveComponent } from "solid-alive"
85
-
86
- export default function Single() {
87
- const { removeAliveElements,aliveFrozen } = useAlive()
88
-
94
+ 3 父 /views/Blog/index.tsx
89
95
 
96
+ ```jsx
97
+ import { onActivated, onDeactivated, useAliveFrozen } from "@/alives"
98
+ export default function Blog(props: any) {
99
+ // 新增路由时用
100
+ const aliveFrozen = useAliveFrozen()
101
+ const addRouter = () => {
102
+ aliveFrozen()
103
+ // add route....
104
+ }
90
105
  //todo call 这个会被调用
91
- onActivated(()=>{
92
- console.log('Single-activeated-1')
106
+ onActivated(() => {
107
+ console.log("Blog-activeated-1")
93
108
  })
94
-
109
+
95
110
  //todo call 也会被调用
96
- onActivated(()=>{
97
- console.log('Single-activeated-2')
111
+ onActivated(() => {
112
+ console.log("Blog-activeated-2")
98
113
  })
99
114
  // 缓存离开,
100
- onDeactivated(()=>{
101
- console.log('Single-deactivated')
115
+ onDeactivated(() => {
116
+ console.log("Blog-deactivated")
102
117
  })
118
+
119
+ // createEffect(()=>{
120
+ // console.log('blog-createEffect')
121
+ // })
122
+
103
123
  return (
104
124
  <div>
105
- <h2>Single</h2>
106
- <input type="text" style={{ border: '2px solid red' }} />
125
+ <h2>Blog</h2>
126
+ {props.children}
107
127
  </div>
108
128
  )
109
129
  }
110
130
  ```
111
131
 
112
- 3 动态生成 路由
132
+ 2 动态生成 路由
133
+
113
134
  ```tsx
114
135
  /** App.tsx */
115
- import { createEffect, lazy, type Component } from 'solid-js'
116
- import { Route, Router } from '@solidjs/router'
117
- import { useAlive, aliveTransfer } from "solid-alive"
136
+ import { createEffect, lazy, type Component } from "solid-js"
137
+ import { Route, Router } from "@solidjs/router"
138
+ import { aliveTransfer } from "solid-alive"
118
139
 
119
140
  const modules = import.meta.glob<{ default: Component<any> }>([
120
- './views/**/**.tsx',
121
- './views/**.tsx'
141
+ "./views/**/**.tsx",
142
+ "./views/**.tsx",
122
143
  ])
123
144
 
124
- const transferRouter = (data: MenuData[]) =>
125
- data.flatMap(item => {
126
- let module = modules[`./views${item.realPath}`]
127
- if (!module) return []
128
- const Transfer = aliveTransfer(
129
- lazy(module),
130
- item.path,
131
- item.children?.map(item => item.path)
132
- )
133
- return (
134
- <Route component={Transfer} path={item.path.split('/').at(-1)}>
135
- {item.children ? transferRouter(item.children) : null}
136
- </Route>
137
- )
138
- })
145
+ const transferRouter = (data: IMenuData[]) => (
146
+ <For each={data}>
147
+ {(item) => {
148
+ var module = modules[`./views${item.realPath.replace(/\.\./g, "")}`]
149
+ if (!module) return []
150
+ return (
151
+ <Route
152
+ component={aliveTransfer(
153
+ lazy(module),
154
+ item.path,
155
+ item.children?.map((item) => item.path)
156
+ )}
157
+ path={item.path.split("/").at(-1)}
158
+ >
159
+ {item.children ? transferRouter(item.children) : null}
160
+ </Route>
161
+ )
162
+ }}
163
+ </For>
164
+ )
139
165
 
140
166
  const App: Component = () => {
141
- const { aliveFrozen } = useAlive()
142
-
143
- const [data, setData] = createStore<MenuData[]>([])
144
167
  return (
145
168
  <Router>
146
- <Route component={Client}>
147
- {/* treeDatadata变成 树结构数据 */}
148
- {transferRouter(treeData(data))}
149
- </Route>
169
+ {/* treeData 将 data变成 树结构数据 */}
170
+ {transferRouter(treeData(data))}
150
171
  </Router>
151
172
  )
152
173
  }
153
174
 
154
175
  export default App
155
- ```
176
+ ```