@iuxs/solid-keep-alive 0.0.1 → 0.0.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/README.md +1 -1
- package/dist/index.js +1 -0
- package/dist/types/AliveProvider.d.ts +13 -0
- package/dist/types/aliveActive.d.ts +25 -0
- package/dist/types/aliveTransfer.d.ts +14 -0
- package/dist/types/context.d.ts +12 -0
- package/dist/types/hooks.d.ts +10 -0
- package/dist/types/index.d.ts +5 -0
- package/dist/types/types.d.ts +60 -0
- package/package.json +1 -1
package/README.md
CHANGED
package/dist/index.js
ADDED
|
@@ -0,0 +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,createMemo as s,runWithOwner as c,useContext as d,createComponent as r,createRoot as i,getOwner as l,createEffect as a,onCleanup as u}from"solid-js";const h=o(),v=o();function f(o){const[d,r]=n({}),i=s(e=>{if(!Array.isArray(o.include))return new Set([]);if(e&&e.size){for(const n of o.include)e.delete(n);e.size&&(n=e,r(t(e=>{(n=>{var t,o,s,c;for(const d of n)e[d]&&(e[d].parentId&&(null===(o=null===(t=e[e[d].parentId])||void 0===t?void 0:t.childIds)||void 0===o||o.delete(d)),e[d].hasEl=!1,e[d].init&&!e[d].noCache||(null===(c=(s=e[d]).dispose)||void 0===c||c.call(s),delete e[d]))})(n)})))}var n;return new Set(o.include)});return e(h.Provider,{value:{caches:d,include:i,currentIds:new Set,setCaches:r,setActive:(e,n,o,s)=>{"aOnceSet"===n&&"add"===s&&d[e].init&&d[e].hasEl?c(d[e].owner,o):r(t(t=>{t[e][n]?t[e][n][s](o):"add"===s&&(t[e][n]=new Set([o]))}))}},get children(){return o.children}})}let I=!1;const p=(n,o,s)=>((null==s?void 0:s.isolated)||(I=!0),function(f){var p;const S=d(h);if(!S||!o&&console.error(`[solid-keepalive]:id='${o}' error`))return r(n,f);const C=null===(p=d(v))||void 0===p?void 0:p.id,m=()=>{var e;return!C||(null===(e=S.caches[o])||void 0===e?void 0:e.parentId)===C},w=null==s?void 0:s.isolated;if(S.caches[o])w||S.currentIds.add(o);else{const s=w?null:[...S.currentIds].at(-1);s&&S.caches[s]&&S.setCaches(t(e=>{const n=e[s];n.childIds?n.childIds.add(o):n.childIds=new Set([o])})),w||S.currentIds.add(o);const c=!S.include().has(o);S.setCaches({[o]:Object.assign({id:o,parentId:s},c&&{noCache:c})}),i(s=>S.setCaches(t(t=>{t[o].dispose=s,t[o].owner=l(),t[o].component=e(v.Provider,{get value(){return Object.assign({id:o},c&&{noCache:c})},get children(){return r(n,f)}})})))}return a(()=>{var e;const n=S.caches[o];if(n&&m()&&(!I||w||(null===(e=n.childIds)||void 0===e?void 0:e.size)||(I=!1),!n.init&&(n.hasEl||(()=>{var e,n;if(null===(n=null===(e=S.caches[o])||void 0===e?void 0:e.component)||void 0===n?void 0:n.call(e))return S.setCaches(t(e=>{e[o].hasEl=!0,e[o].owner=l();for(const n of e[o].aOnceSet||[])n();delete e[o].aOnceSet})),!0})()))){S.setCaches(o,"init",!0);for(const e of n.aSet||[])e()}}),u(()=>{const e=S.caches[o];if(!w&&(I||!e))return;if(e.parentId){if(S.currentIds.has(o)){const e=n=>{var t;for(const o of n){S.currentIds.delete(o);const n=null===(t=S.caches[o])||void 0===t?void 0:t.childIds;(null==n?void 0:n.size)&&e(n)}};e([o])}}else S.currentIds.clear();if(!m())return;const n=!e.noCache&&e.hasEl;n&&S.setCaches(o,"init",!1);for(const n of e.dSet||[])n();n||S.setCaches(t(e=>{var n,t,s,c;e[o].parentId&&(null===(t=null===(n=e[e[o].parentId])||void 0===n?void 0:n.childIds)||void 0===t||t.delete(o)),null===(c=(s=e[o]).dispose)||void 0===c||c.call(s),delete e[o]}))}),m()&&c(S.caches[o].owner,()=>S.caches[o].component)}),S=(e,n)=>{if("function"!=typeof n)return;const{id:t,noCache:o}=d(v)||{};if(o&&"aOnceSet"!==e)return;const s=d(h);t&&s&&(s.setActive(t,e,n,"add"),"aOnceSet"!==e&&u(()=>{s.setActive(t,e,n,"delete")}))},C=e=>{S("aSet",e)},m=e=>{S("dSet",e)},w=e=>{const[t,o]=n();return S("aOnceSet",()=>{c(l(),()=>{o(d(e))})}),t};export{f as AliveProvider,p as aliveTransfer,C as onActivated,m as onDeactivated,w as useAliveContext};
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import type { AliveProviderProps } from "./types";
|
|
2
|
+
/**
|
|
3
|
+
* @description 容器
|
|
4
|
+
* @param {JSXElement} children
|
|
5
|
+
* @param {Array<string>} [include] ['home','about']
|
|
6
|
+
* @example
|
|
7
|
+
* ```jsx
|
|
8
|
+
* <AliveProvider include={['home','about']} >
|
|
9
|
+
<Router root={Contain} children={routes} />
|
|
10
|
+
</AliveProvider>
|
|
11
|
+
* ```
|
|
12
|
+
* */
|
|
13
|
+
export default function (props: AliveProviderProps): import("solid-js").JSX.Element;
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @description 进入缓存
|
|
3
|
+
* ```tsx
|
|
4
|
+
* import { onActivated } from 'solid-keepalive'
|
|
5
|
+
* //use
|
|
6
|
+
* onActivated(()=> console.log(234))
|
|
7
|
+
* ```
|
|
8
|
+
*/
|
|
9
|
+
export declare const onActivated: (cb: () => void) => void;
|
|
10
|
+
/**
|
|
11
|
+
* @description 离开缓存
|
|
12
|
+
* ```tsx
|
|
13
|
+
* import { onDeactivated } from 'solid-keepalive'
|
|
14
|
+
* onDeactivated(()=> console.log(234))
|
|
15
|
+
* ```
|
|
16
|
+
*/
|
|
17
|
+
export declare const onDeactivated: (cb: () => void) => void;
|
|
18
|
+
/**
|
|
19
|
+
* @description 进入缓存,只会执行一次
|
|
20
|
+
* ```tsx
|
|
21
|
+
* import { onActivatedOnce } from 'solid-keepalive'
|
|
22
|
+
* onDeactivated(()=> console.log(234))
|
|
23
|
+
* ```
|
|
24
|
+
*/
|
|
25
|
+
export declare const onActivatedOnce: (cb: () => void) => void;
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import { type JSXElement } from "solid-js";
|
|
2
|
+
/**
|
|
3
|
+
* @description 转换
|
|
4
|
+
* @example
|
|
5
|
+
* ```tsx
|
|
6
|
+
* import Home from 'xxx'
|
|
7
|
+
* const Home1 = aliveTransfer(Home, 'home')
|
|
8
|
+
* ```
|
|
9
|
+
* */
|
|
10
|
+
declare const aliveTransfer: <T extends Record<string, any>>(Component: (props: T) => JSXElement, id: string, params?: {
|
|
11
|
+
/** 成一个独立缓存组件 */
|
|
12
|
+
isolated?: boolean;
|
|
13
|
+
}) => (props: T) => import("solid-js").JSX.Element;
|
|
14
|
+
export default aliveTransfer;
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
declare const Context: import("solid-js").Context<{
|
|
2
|
+
currentIds: Set<string>;
|
|
3
|
+
include: import("solid-js").Accessor<Set<string>>;
|
|
4
|
+
caches: Record<string, import("./types").Cache>;
|
|
5
|
+
setCaches: import("solid-js/store").SetStoreFunction<import("./types").Caches>;
|
|
6
|
+
setActive: (id: string, t: import("./types").Activate, cb: () => void, t1: import("./types").SetType) => void;
|
|
7
|
+
} | undefined>;
|
|
8
|
+
declare const ChildContext: import("solid-js").Context<{
|
|
9
|
+
id: string;
|
|
10
|
+
noCache?: boolean;
|
|
11
|
+
} | undefined>;
|
|
12
|
+
export { ChildContext, Context };
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import { type Context } from "solid-js";
|
|
2
|
+
/**
|
|
3
|
+
* @description 子路由组件级获取父路由组件的 context
|
|
4
|
+
* @example
|
|
5
|
+
* ```
|
|
6
|
+
* import UserContext from 'xxx'
|
|
7
|
+
* const data = useAliveContext(UserContext)
|
|
8
|
+
* ```
|
|
9
|
+
*/
|
|
10
|
+
export declare const useAliveContext: <T>(context: Context<T>) => T;
|
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
import AliveProvider from "./AliveProvider";
|
|
2
|
+
import aliveTransfer from "./aliveTransfer";
|
|
3
|
+
import { onActivated, onDeactivated } from "./aliveActive";
|
|
4
|
+
import { useAliveContext } from "./hooks";
|
|
5
|
+
export { AliveProvider, onActivated, onDeactivated, useAliveContext, aliveTransfer, };
|
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
import type { JSXElement, Accessor, Owner } from "solid-js"
|
|
2
|
+
import type { SetStoreFunction } from "solid-js/store"
|
|
3
|
+
|
|
4
|
+
export type Expand<T> = T extends infer O ? { [K in keyof O]: O[K] } : never
|
|
5
|
+
|
|
6
|
+
export type AliveProviderProps = {
|
|
7
|
+
/** 子组件 */
|
|
8
|
+
children: JSXElement
|
|
9
|
+
/** 缓存的路由, 如 include:['home','about'] */
|
|
10
|
+
include?: Array<string>
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
/** 缓存的数据 */
|
|
14
|
+
export type Cache = Expand<{
|
|
15
|
+
/** 唯一值 */
|
|
16
|
+
id: string
|
|
17
|
+
/** 没有在 include 中的路由 */
|
|
18
|
+
noCache?: boolean
|
|
19
|
+
/** 表明dom加载过了 */
|
|
20
|
+
hasEl?: boolean
|
|
21
|
+
/** 是否有加载过, */
|
|
22
|
+
init?: boolean
|
|
23
|
+
/** 其子数据 */
|
|
24
|
+
childIds?: Set<string>
|
|
25
|
+
/** 父级 */
|
|
26
|
+
parentId?: string | null
|
|
27
|
+
/** JSX */
|
|
28
|
+
component?: JSXElement
|
|
29
|
+
/** 上下文 */
|
|
30
|
+
owner: Owner | null
|
|
31
|
+
/** 销毁函数 */
|
|
32
|
+
dispose?: () => void
|
|
33
|
+
/** 只执行一次的 active, 会在页面加载时执行, */
|
|
34
|
+
aOnceSet?: Set<() => void>
|
|
35
|
+
/** 激活 */
|
|
36
|
+
aSet?: Set<() => void>
|
|
37
|
+
/** 离开 */
|
|
38
|
+
dSet?: Set<() => void>
|
|
39
|
+
}>
|
|
40
|
+
|
|
41
|
+
/** 所有缓存数据 */
|
|
42
|
+
export type Caches = Record<string, Cache>
|
|
43
|
+
|
|
44
|
+
/** 共享数据 */
|
|
45
|
+
export type ContextProps = Expand<{
|
|
46
|
+
/** 当前正在展示的 所有id */
|
|
47
|
+
currentIds: Set<string>
|
|
48
|
+
/** 需要缓存的组件 id */
|
|
49
|
+
include: Accessor<Set<string>>
|
|
50
|
+
/** 数据 */
|
|
51
|
+
caches: Record<string, Cache>
|
|
52
|
+
/** 加 缓存 */
|
|
53
|
+
setCaches: SetStoreFunction<Caches>
|
|
54
|
+
/** 缓存回调函数 */
|
|
55
|
+
setActive: (id: string, t: Activate, cb: () => void, t1: SetType) => void
|
|
56
|
+
}>
|
|
57
|
+
|
|
58
|
+
export type Activate = "aSet" | "dSet" | "aOnceSet"
|
|
59
|
+
export type MapType = "set" | "delete"
|
|
60
|
+
export type SetType = "add" | "delete"
|