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 CHANGED
@@ -1 +1 @@
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 i,createRoot as l,createEffect as a,onCleanup as v}from"solid-js";var d=o({elements:{},info:{frozen:!1},setInfo:()=>{},insertElement:()=>{},onDeactivated:()=>{},onActivated:()=>{},removeAliveElement:()=>{},setCurrentComponentId:()=>{},insertCacheCb:()=>{}});function c(o){let[i,l]=n(),[a,v]=n({frozen:!0}),c="",s=new Map,f=new Map,u={onActivated:{},onDeactivated:{}};var m=e=>{var n;if(Reflect.has(i,e)){var t=null===(n=i[e])||void 0===n?void 0:n.children;null==t||t.forEach((n=>n!==e&&m(n))),l((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(l){var t=l.stack.split("\n"),o=t[0].includes("@")?5:6,r=t[o].trim(),i=r.includes("@")?r.split("@"):r.split(" ").slice(1);e=i[0],n=i[1]}return{caller:e,path:n}}(),o=u[e];return o[n]&&o[n]!==t?(console.warn(`[solid-alive]:检测到多个${e}函数 ${t}`),!1):(u[e][n]=t,!0)},h=(e,n)=>{if(!a.frozen&&n&&p(e)){var t={onActivated:s,onDeactivated:f}[e],o=t.get(c)||new Set;o.add(r([],n))&&t.set(c,o)}};return e(d.Provider,{value:{info:a,elements:i,setInfo:v,onActivated:e=>{h("onActivated",e)},onDeactivated:e=>{h("onDeactivated",e)},insertElement:e=>{let n=e.id;l([n],Object.assign(Object.assign({},i[n]),e))},removeAliveElement:e=>{if(null==e)for(const e of Object.values(i))m(e.id);else m(e)},setCurrentComponentId:e=>{c=e},insertCacheCb:e=>{let n=s.get(e),o=f.get(e);s.delete(e),f.delete(e),u.onActivated={},u.onDeactivated={},Reflect.has(i,e)&&l(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:c,setInfo:f,insertElement:u,setCurrentComponentId:m,insertCacheCb:p}=i(d);Reflect.has(c,t)||(f("frozen",!1),m(t),l((r=>{u({id:t,dispose:r,component:e(n,{}),children:Array.isArray(o)?new Set(o):null})})));var h=e=>{var n;if(c[e].isTop)return e;var t=null===(n=Object.values(c).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(c[t].loaded)s.add(t),f("frozen",!0),null===(e=c[t].onActivated)||void 0===e||e.forEach((e=>e())),f("frozen",!1);else{let e=null===(n=c[t])||void 0===n?void 0:n.component;for(;"function"==typeof e;)e=e();e instanceof HTMLElement&&p(t)}})),v((()=>{var e;r.frozen||(f("frozen",!0),null===(e=c[t].onDeactivated)||void 0===e||e.forEach((e=>e())),f("frozen",!1))})),c[t].component}function u(){var{onActivated:e,onDeactivated:n,removeAliveElement:t,setInfo:o}=i(d);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{c as AliveProvider,f as AliveTransfer,m as onActivated,p as onDeactivated,h as useAlive};
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,6 @@
1
+ import { JSX } from 'solid-js';
2
+ export declare function AliveComponent(props: {
3
+ children: JSX.Element;
4
+ id: string;
5
+ subIds?: Array<string>;
6
+ }): JSX.Element;
@@ -1,4 +1,4 @@
1
- import { ProveiderProps } from "./default";
1
+ import { ProveiderProps } from './default';
2
2
  /**
3
3
  * @description Alive
4
4
  * @param children jsx.element
@@ -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;
@@ -7,12 +7,15 @@ export interface ProveiderProps {
7
7
  export interface NodeInfo {
8
8
  id: string
9
9
  loaded?: boolean
10
- component?: JSX.Element | null
11
- children?: Set<string> | null
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
- isTop?: boolean | null
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
- removeAliveElement: (id?: string) => void
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
@@ -1,4 +1,5 @@
1
1
  import AliveProvider from './AliveProvider';
2
- import AliveTransfer from './AliveTransfer';
2
+ import aliveTransfer from './aliveTransfer';
3
+ import { AliveComponent } from './AliveComponent';
3
4
  import { onActivated, onDeactivated, useAlive } from './useAlive';
4
- export { AliveProvider, AliveTransfer, onActivated, onDeactivated, useAlive };
5
+ export { AliveComponent, AliveProvider, aliveTransfer, onActivated, onDeactivated, useAlive, };
@@ -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
- removeAliveElement: (id?: string) => void;
8
+ removeAliveElements: (ids?: Array<import("./default").IAliveElementIds>) => void;
9
9
  aliveFrozen: () => void;
10
10
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "solid-alive",
3
- "version": "0.2.0",
3
+ "version": "0.2.2",
4
4
  "author": "1iuxs",
5
5
  "description": "solid-alive",
6
6
  "type": "module",
@@ -5,14 +5,16 @@
5
5
  ### 描述(describe)
6
6
  - 用于 solid 组件缓存,只测试过2级路由缓存
7
7
  - AliveProvider
8
+ - AliveComponent 不要在 有缓存 的组件中使用
8
9
  - 在 useAlive
9
- - removeAliveElement: 函数, 可传一个参数, 不传就删除所有缓存 :
10
- removeAliveElement('/home')
10
+ - removeAliveElements: 函数, 可传一个参数, 不传就删除所有缓存 :
11
+ removeAliveElements(['/home'])
11
12
  - aliveForzen: 暂时不响应 路由数据变化, aliveForzen()
13
+
12
14
  - 子父 缓存/删除 问题
13
- - 如果某组件下有子组件,在父的 AliveTransfer中,
15
+ - 如果某组件下有子组件,在父的 aliveTransfer中,
14
16
  第三个参数,为对象 写上子组件的唯一id: {children:['/childrenId','asf',...]}
15
- - 使用见下图, 也可用 -removeAliveElement 删除
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 中 有 AliveTransfer, 参数: JSX , id:string, children?:[string..]
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 { AliveTransfer } from 'solid-alive';
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
- // 当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');
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 { removeAliveElement,aliveFrozen } = useAlive()
90
+ const { removeAliveElements,aliveFrozen } = useAlive()
92
91
 
93
- let divRef: Element | undefined = undefined
94
92
  const click = () => {
95
- removeAliveElement('/about') // delete '/about'; 删除 /about
96
- // removeAliveElement() // delete all alive element; 会删除所有缓存的组件
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
- /** 动态路由 App.tsx */
125
+ /** App.tsx */
124
126
  import { createEffect, lazy, type Component } from 'solid-js'
125
127
  import { Route, Router } from '@solidjs/router'
126
- import { useAlive,AliveTransfer } from "solid-alive"
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 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
- )
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, 'id', 'parentId'))}
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;