solid-alive 0.2.6 → 0.2.7

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,createComputed as a,createEffect as d,onCleanup as s}from"solid-js";var v=o({elements:{},symbolClose:Symbol("close"),info:{frozen:!1,cbOnOff:"off",currComponentId:""},setInfo:()=>{},insertElement:()=>{},onDeactivated:()=>{},onActivated:()=>{},removeAliveElements:()=>{},insertCacheCb:()=>{}});function c(o){let[i,l]=n(),a={frozen:!1,cbOnOff:"off",currComponentId:""},d=Symbol("close"),s=new Map,c=new Map,f={onActivated:{},onDeactivated:{}};var u=e=>{var n,t,o;if(Reflect.has(i,e)){var r=null===(n=i[e])||void 0===n?void 0:n.subIds;null==r||r.forEach((n=>n!==e&&u(n))),null===(o=null===(t=i[e])||void 0===t?void 0:t.dispose)||void 0===o||o.call(t),l({[e]:void 0})}},m=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=f[e];return(!o[n]||o[n]===t)&&(f[e][n]=t,!0)},b=(e,n)=>{var{cbOnOff:t,currComponentId:o}=a;if("on"===t&&o!==d&&n&&m(e)){var i={onActivated:s,onDeactivated:c}[e],l=i.get(o)||new Set;l.add(r([],n))&&i.set(o,l)}};return e(v.Provider,{value:{info:a,elements:i,symbolClose:d,setInfo:(e,n)=>{a[e]=n},onActivated:e=>{b("onActivated",e)},onDeactivated:e=>{b("onDeactivated",e)},insertElement:e=>{let n=e.id,t=Object.values(i).find((e=>{var t;return null===(t=e.subIds)||void 0===t?void 0:t.has(n)}));l([n],Object.assign(Object.assign(Object.assign({},i[n]),e),{fatherId:null==t?void 0:t.id}))},removeAliveElements:e=>{if(Array.isArray(e))for(const n of e)u(n);else if(!e)for(const e of Object.values(i))u(e.id)},insertCacheCb:e=>{let n=s.get(e),o=c.get(e);s.delete(e),c.delete(e),f.onActivated={},f.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 f=new Set([]);function u(e,n,t){return function(o){var{info:r,elements:c,symbolClose:u,setInfo:m,insertElement:b,insertCacheCb:A}=i(v);Reflect.has(c,n)||l((r=>{m("currComponentId",n),m("cbOnOff","on"),b({id:n,dispose:r,owner:null,element:e(o),subIds:Array.isArray(t)?new Set(t):null})}));var O=e=>{var n=c[e].fatherId;return n?O(n):e};return f.size&&!f.has(O(n))&&f.clear(),a((()=>{var e;!c[n].loaded&&(e=>{var n;let t=null===(n=c[e])||void 0===n?void 0:n.element;for(;"function"==typeof t;)t=t();return!!(t instanceof HTMLElement||Array.isArray(t))&&t})(n)&&(A(n),!(null===(e=c[n].subIds)||void 0===e?void 0:e.size)&&m("currComponentId",u))})),d((()=>{var e,t,o;r.frozen?!(null===(e=c[n].subIds)||void 0===e?void 0:e.size)&&m("frozen",!1):(null===(t=c[n])||void 0===t?void 0:t.loaded)&&(f.add(n),m("cbOnOff","off"),null===(o=c[n].onActivated)||void 0===o||o.forEach((e=>e())),m("cbOnOff","on"))})),s((()=>{var e,t;r.frozen||(m("cbOnOff","off"),null===(t=null===(e=c[n])||void 0===e?void 0:e.onDeactivated)||void 0===t||t.forEach((e=>e())),m("cbOnOff","on"))})),c[n].element}}function m(n){var t=u((()=>n.children),n.id,n.subIds);return e(t,{})}function b(){var{onActivated:e,onDeactivated:n,removeAliveElements:t,setInfo:o}=i(v);return{onActivated:e,onDeactivated:n,removeAliveElements:t,setInfo:o}}function A(e){var{onActivated:n}=b();n(e)}function O(e){var{onDeactivated:n}=b();n(e)}function h(){var{removeAliveElements:e,setInfo:n}=b();return{removeAliveElements:e,aliveFrozen:()=>n("frozen",!0)}}export{m as AliveComponent,c as AliveProvider,u as aliveTransfer,A as onActivated,O as onDeactivated,h as useAlive};
1
+ import{createComponent as e}from"solid-js/web";import{createStore as n,produce as o}from"solid-js/store";import{createContext as t,createComputed as r,on as i,useContext as l,createRoot as s,getOwner as d,createEffect as a,onCleanup as f,runWithOwner as v}from"solid-js";var c=t({elements:{},symbolClose:Symbol("close"),info:{frozen:!1,cbOnOff:"off",currComponentId:""},setInfo:()=>{},insertElement:()=>{},removeAliveElements:()=>{},insertCacheCb:()=>{},setCb:()=>{}});function u(t){let[l,s]=n(),d={frozen:!1,cbOnOff:"off",currComponentId:""},a=Symbol("close"),f=new Map,v=new Map;var u=e=>{let n=e.id,o=Object.values(l).find((e=>{var o;return null===(o=e.subIds)||void 0===o?void 0:o.has(n)}));s([n],Object.assign(Object.assign(Object.assign({},l[n]),e),{fatherId:null==o?void 0:o.id}))},m=e=>{let n=f.get(e),t=v.get(e);f.delete(e),v.delete(e),Reflect.has(l,e)&&s(o((o=>{o[e].onActivated=n,o[e].onDeactivated=t,o[e].loaded=!0})))},b=e=>{var n,o,t;if(Reflect.has(l,e)){var r=null===(n=l[e])||void 0===n?void 0:n.subIds;null==r||r.forEach((n=>n!==e&&b(n))),null===(t=(o=l[e]).dispose)||void 0===t||t.call(o),s({[e]:void 0})}},O=e=>{if(Array.isArray(e))for(const n of e)b(n);else if(!e)for(const e of Object.values(l))b(e.id)},I=(e,n)=>{var{cbOnOff:o,currComponentId:t}=d;if("on"===o&&t!==a&&n){var r={onActivated:f,onDeactivated:v}[e],l=r.get(t)||new Set;l.add(i([],n))&&r.set(t,l)}},h=(e,n)=>{d[e]=n};return r(((e=[])=>{var n=t.includes||[];if(e.length>n.length){var o=new Set(n);O(e.filter((e=>!o.has(e))))}return n}),t.includes||[]),e(c.Provider,{get value(){return{info:d,elements:l,symbolClose:a,setInfo:h,setCb:I,insertElement:u,removeAliveElements:O,insertCacheCb:m,aliveIds:t.includes}},get children(){return t.children}})}function m(e,n,o){return function(t){var{info:i,elements:u,symbolClose:m,setInfo:b,insertElement:O,insertCacheCb:I,aliveIds:h}=l(c);if(!h||!h.includes(n))return e(t);!Reflect.has(u,n)&&s((r=>{b("currComponentId",n),b("cbOnOff","on"),O({id:n,dispose:r,owner:d(),element:e(t),subIds:Array.isArray(o)?new Set(o):null})}));return r((()=>{var e;!u[n].loaded&&(e=>{for(var n,o=null===(n=u[e])||void 0===n?void 0:n.element;"function"==typeof o;)o=o();return o})(n)&&(I(n),!(null===(e=u[n].subIds)||void 0===e?void 0:e.size)&&b("currComponentId",m))})),a((()=>{var e,o,t;i.frozen?!(null===(e=u[n].subIds)||void 0===e?void 0:e.size)&&b("frozen",!1):(null===(o=u[n])||void 0===o?void 0:o.loaded)&&(b("cbOnOff","off"),null===(t=u[n].onActivated)||void 0===t||t.forEach((e=>e())),b("cbOnOff","on"))})),f((()=>{var e,o;i.frozen||(b("cbOnOff","off"),null===(o=null===(e=u[n])||void 0===e?void 0:e.onDeactivated)||void 0===o||o.forEach((e=>e())),b("cbOnOff","on"))})),u[n].owner&&v(u[n].owner,(()=>u[n].element))}}function b(n){var o=m((()=>n.children),n.id,n.subIds);return e(o,{})}function O(){var{setCb:e,removeAliveElements:n,setInfo:o}=l(c);return{onActivated:n=>e("onActivated",n),onDeactivated:n=>e("onDeactivated",n),removeAliveElements:n,setInfo:o}}function I(e){var{onActivated:n}=O();n(e)}function h(e){var{onDeactivated:n}=O();n(e)}function A(){var{removeAliveElements:e,setInfo:n}=O();return{onActivated:I,onDeactivated:h,removeAliveElements:e,aliveFrozen:()=>n("frozen",!0)}}export{b as AliveComponent,u as AliveProvider,m as aliveTransfer,I as onActivated,h as onDeactivated,A as useAlive};
@@ -1,6 +1,7 @@
1
- import { ProveiderProps } from "./default";
1
+ import { ProveiderProps } from './default';
2
2
  /**
3
3
  * @description Alive
4
4
  * @param children jsx.element
5
+ * @param {Arrya<string> | null} [includes] 哪些路由要缓存, 不写默认缓存所有
5
6
  */
6
7
  export default function AliveProvider(props: ProveiderProps): import("solid-js").JSX.Element;
@@ -5,4 +5,4 @@ import { JSX } from 'solid-js';
5
5
  * @param { string } id string,自己的id 值,一定要唯一
6
6
  * @param { Array<string> } [subIds] [string,...], 子组件的 id值 可不传,这样默认销毁时不会去干掉子组件,
7
7
  */
8
- export default function aliveTransfer(Component: <T>(props: T) => JSX.Element, id: string, subIds?: Array<string>): <T>(props: T) => JSX.Element;
8
+ export default function aliveTransfer(Component: <T>(props: T) => JSX.Element, id: string, subIds?: Array<string>): <T>(props: T) => any;
@@ -1,14 +1,20 @@
1
1
  import { JSX } from 'solid-js'
2
2
 
3
- export interface ProveiderProps {
3
+ interface pub{
4
+ aliveIds?: Array<string> | null
5
+ }
6
+
7
+ export interface ProveiderProps {
4
8
  children: JSX.Element
9
+ includes?:pub['aliveIds']
5
10
  }
6
11
 
12
+ export type tActivated = "onActivated" | "onDeactivated"
13
+
7
14
  export interface NodeInfo {
8
15
  id: string
9
16
  loaded?: boolean
10
17
  owner: any
11
- props?: any
12
18
  component?: ((props: any) => JSX.Element) | null
13
19
  element?: JSX.Element | null
14
20
  subIds?: Set<string> | null
@@ -34,16 +40,15 @@ export interface IInfo {
34
40
 
35
41
  export type TSetInfo = <T extends keyof IInfo>(key: T, value: IInfo[T]) => void
36
42
 
37
- export interface ContextProps {
43
+ export interface ContextProps extends pub {
38
44
  elements: StoreProps
39
45
  info: IInfo
40
46
  symbolClose: symbol
41
47
  setInfo: TSetInfo
42
48
  insertElement: (d: NodeInfo) => void
43
- onActivated: (cb: () => void) => void
44
- onDeactivated: (cb: () => void) => void
45
49
  removeAliveElements: (ids?: Array<IAliveElementIds>) => void
46
50
  insertCacheCb: (id: string) => void
51
+ setCb:(t: tActivated, cb: () => void)=>void
47
52
  }
48
53
 
49
54
  interface IActive {
@@ -3,8 +3,12 @@ export declare function onDeactivated(fn: () => void): void;
3
3
  /**
4
4
  * @returns removeAliveElement 删除缓存组件,
5
5
  * @returns aliveFrozen 让 alive 暂时失去响应, 一般在加新增路由数据时使用
6
+ * @returns onActivated 进入
7
+ * @returns onDeactivated 离开
6
8
  */
7
9
  export declare function useAlive(): {
10
+ onActivated: typeof onActivated;
11
+ onDeactivated: typeof onDeactivated;
8
12
  removeAliveElements: (ids?: Array<import("./default").IAliveElementIds>) => void;
9
13
  aliveFrozen: () => void;
10
14
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "solid-alive",
3
- "version": "0.2.6",
3
+ "version": "0.2.7",
4
4
  "author": "1iuxs",
5
5
  "description": "solid-alive",
6
6
  "type": "module",
package/readme.md CHANGED
@@ -5,10 +5,13 @@
5
5
  ### 描述(describe)
6
6
  - 用于 solid 组件缓存,只测试过2级路由缓存
7
7
  - AliveProvider
8
+ - includes : 数组, 不传默认缓存所有, ['/','/about']
8
9
  - AliveComponent 不要在 有缓存 的组件中使用
9
10
  - 在 useAlive
10
11
  - removeAliveElements: 函数, 可传一个参数, 不传就删除所有缓存 :
11
12
  removeAliveElements(['/home'])
13
+ - onActivated
14
+ - onDeactivated
12
15
  - aliveForzen: 暂时不响应 路由数据变化, aliveForzen()
13
16
 
14
17
  - 子父 缓存/删除 问题
@@ -27,6 +30,7 @@ import { AliveProvider } from 'solid-alive'
27
30
 
28
31
  const root = document.getElementById('root')
29
32
 
33
+ // includes, 默认缓存所有
30
34
  render(() =>
31
35
  <AliveProvider>
32
36
  <App />
@@ -47,11 +51,10 @@ import Blog from './views/Blog';
47
51
  import Single from './views/Blog/Single';
48
52
 
49
53
 
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'])
54
+ const HomeTransfer = aliveTransfer(Home, '/'),
55
+ AboutTsf = aliveTransfer(About, '/about'),
56
+ BlogTsf = aliveTransfer(Blog, '/blog', ['/contact', 'single']),
57
+ SingleTsf = aliveTransfer(Single,'single')
55
58
 
56
59
  export default function App() {
57
60
  return (
@@ -99,7 +102,7 @@ export default function Single() {
99
102
  console.log('Single-activeated-1')
100
103
  })
101
104
 
102
- //todo no call 这个不会被调用
105
+ //todo call 也会被调用
103
106
  onActivated(()=>{
104
107
  console.log('Single-activeated-2')
105
108
  })
@@ -1,4 +0,0 @@
1
- export declare function getCallerFunctionName(): {
2
- caller: any;
3
- path: any;
4
- };