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 CHANGED
@@ -5,14 +5,15 @@
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()
12
13
  - 子父 缓存/删除 问题
13
14
  - 如果某组件下有子组件,在父的 AliveTransfer中,
14
15
  第三个参数,为对象 写上子组件的唯一id: {children:['/childrenId','asf',...]}
15
- - 使用见下图, 也可用 -removeAliveElement 删除
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 { AliveProvider } from 'solid-alive'
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 { AliveTransfer } from 'solid-alive';
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
- // 当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');
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 { removeAliveElement,aliveFrozen } = useAlive()
90
+ const { removeAliveElements,aliveFrozen } = useAlive()
92
91
 
93
92
  let divRef: Element | undefined = undefined
94
93
  const click = () => {
95
- removeAliveElement('/about') // delete '/about'; 删除 /about
96
- // removeAliveElement() // delete all alive element; 会删除所有缓存的组件
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
- /** 动态路由 App.tsx */
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 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
- )
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 n,produce as t}from"solid-js/store";import{createContext as o,on as r,useContext as l,createRoot as i,createEffect as a,onCleanup as c}from"solid-js";var v=o({elements:{},info:{frozen:!1},setInfo:()=>{},insertElement:()=>{},onDeactivated:()=>{},onActivated:()=>{},removeAliveElement:()=>{},setCurrentComponentId:()=>{},insertCacheCb:()=>{}});function d(o){let[l,i]=n(),[a,c]=n({frozen:!0}),d="",s=new Map,f=new Map,u={cbType:"",caller:"",path:""};var m=e=>{var n;if(Reflect.has(l,e)){var t=null===(n=l[e])||void 0===n?void 0:n.children;null==t||t.forEach((n=>n!==e&&m(n))),i((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(i){var t=i.stack.split("\n"),o=t[0].includes("@")?5:6,r=t[o].trim(),l=r.includes("@")?r.split("@"):r.split(" ").slice(1);e=l[0],n=l[1]}return{caller:e,path:n}}();return u.caller&&u.cbType===e&&u.caller===n&&u.path!==t?(console.warn(`[solid-alive]:检测到多个${e}函数 ${t}`),!1):(u={caller:n,path:t,cbType:e},!0)},h=(e,n)=>{if(!a.frozen&&n&&p(e)){var t={onActivated:s,onDeactivated:f}[e],o=t.get(d)||new Set;o.add(r([],n))&&t.set(d,o)}};return e(v.Provider,{value:{info:a,elements:l,setInfo:(e,n)=>{c(e,n)},onActivated:e=>{h("onActivated",e)},onDeactivated:e=>{h("onDeactivated",e)},insertElement:e=>{let n=e.id;i([n],Object.assign(Object.assign({},l[n]),e))},removeAliveElement:e=>{if(null==e)for(const e of Object.values(l))m(e.id);else m(e)},setCurrentComponentId:e=>{d=e},insertCacheCb:e=>{let n=s.get(e),o=f.get(e);s.delete(e),f.delete(e),Reflect.has(l,e)&&i(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:d,setInfo:f,insertElement:u,setCurrentComponentId:m,insertCacheCb:p}=l(v);Reflect.has(d,t)||(f("frozen",!1),m(t),i((r=>{u({id:t,dispose:r,component:e(n,{}),children:Array.isArray(o)?new Set(o):null})})));var h=e=>{var n;if(d[e].isTop)return e;var t=null===(n=Object.values(d).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(d[t].loaded)s.add(t),f("frozen",!0),null===(e=d[t].onActivated)||void 0===e||e.forEach((e=>e())),f("frozen",!1);else{let e=null===(n=d[t])||void 0===n?void 0:n.component;for(;"function"==typeof e;)e=e();e instanceof HTMLElement&&p(t)}})),c((()=>{var e;r.frozen||(f("frozen",!0),null===(e=d[t].onDeactivated)||void 0===e||e.forEach((e=>e())),f("frozen",!1))})),d[t].component}function u(){var{onActivated:e,onDeactivated:n,removeAliveElement:t,setInfo:o}=l(v);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{d 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 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,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>, saveComponent?: boolean): <T>(props: T) => JSX.Element;
@@ -7,8 +7,10 @@ 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
+ 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
- removeAliveElement: (id?: string) => void
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
@@ -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.1.9",
3
+ "version": "0.2.1",
4
4
  "author": "1iuxs",
5
5
  "description": "solid-alive",
6
6
  "type": "module",
@@ -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;