-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
bf2ee64
commit ac4c6f0
Showing
14 changed files
with
262 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
import React, { FC, useCallback, useReducer } from 'react'; | ||
import { IKeepAliveContext, KeepAliveContext } from './KeepAliveContext'; | ||
import { KeepAliveActions, keepAliveReducer, KeepAliveState } from './KeepAliveReducer'; | ||
import * as actionTypes from './actionTypes'; | ||
|
||
interface IKeepAliveProps { | ||
children: React.ReactNode; | ||
} | ||
|
||
export type ISetKeepAliveState = (arg: KeepAliveActions['payload']) => void; | ||
|
||
/** | ||
* { | ||
* keepAliveId: 'class' | ||
* reactElement: ReactElement | ||
* nodes: Node[] | ||
* status: 'CREATING' | 'CREATED' | ||
* } | ||
*/ | ||
|
||
const KeepAlive: FC<IKeepAliveProps> = (props) => { | ||
const [keepAliveStates, dispatch] = useReducer(keepAliveReducer, {}); | ||
|
||
const setKeepAliveState: ISetKeepAliveState = useCallback( | ||
({ reactElement, keepAliveId }) => { | ||
if (!keepAliveStates[keepAliveId]) { | ||
dispatch({ | ||
type: actionTypes.CREATING, | ||
payload: { | ||
keepAliveId, | ||
reactElement, | ||
}, | ||
}); | ||
} | ||
}, | ||
[keepAliveStates], | ||
); | ||
|
||
return ( | ||
<KeepAliveContext.Provider | ||
value={{ | ||
keepAliveStates, | ||
setKeepAliveState, | ||
dispatch, | ||
}} | ||
> | ||
{props.children} | ||
{Object?.values(keepAliveStates).map((state) => { | ||
const { keepAliveId, reactElement } = state; | ||
return ( | ||
<div | ||
key={keepAliveId} | ||
ref={(node) => { | ||
if (node && !keepAliveStates[keepAliveId]?.nodes) { | ||
dispatch({ | ||
type: actionTypes.CREATED, | ||
payload: { | ||
keepAliveId, | ||
nodes: Array.from(node.childNodes) as any[], | ||
}, | ||
}); | ||
} | ||
}} | ||
> | ||
{reactElement} | ||
</div> | ||
); | ||
})} | ||
</KeepAliveContext.Provider> | ||
); | ||
}; | ||
|
||
export default KeepAlive; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
import { createContext } from 'react'; | ||
import { ISetKeepAliveState } from './KeepAlive'; | ||
import { KeepAliveActions, KeepAliveState } from './KeepAliveReducer'; | ||
|
||
export interface IKeepAliveContext { | ||
keepAliveStates: KeepAliveState; | ||
setKeepAliveState: ISetKeepAliveState; | ||
dispatch: React.Dispatch<KeepAliveActions>; | ||
} | ||
|
||
export const KeepAliveContext = createContext<IKeepAliveContext>({} as IKeepAliveContext); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
import * as actionTypes from './actionTypes'; | ||
|
||
export interface KeepAliveState { | ||
[key: string]: { | ||
keepAliveId: string; | ||
reactElement?: React.ReactElement; | ||
status: string; | ||
nodes?: Array<React.ReactElement>; | ||
}; | ||
} | ||
|
||
const initialState = {}; | ||
|
||
export type KeepAliveActions = | ||
| { | ||
type: 'CREATING'; | ||
payload: { | ||
keepAliveId: string; | ||
reactElement?: React.ReactElement; | ||
nodes?: Array<HTMLDivElement>; | ||
}; | ||
} | ||
| { | ||
type: 'CREATED'; | ||
payload: { | ||
keepAliveId: string; | ||
reactElement?: React.ReactElement; | ||
nodes?: Array<HTMLDivElement>; | ||
}; | ||
}; | ||
|
||
export function keepAliveReducer(state: KeepAliveState | any, action: KeepAliveActions): KeepAliveState { | ||
const { type, payload } = action; | ||
const { keepAliveId, reactElement, nodes } = payload; | ||
switch (action.type) { | ||
case actionTypes.CREATING: { | ||
return { | ||
...state, | ||
[keepAliveId]: { | ||
keepAliveId, | ||
reactElement, | ||
status: type, | ||
nodes: null, | ||
}, | ||
}; | ||
} | ||
case actionTypes.CREATED: { | ||
return { | ||
...state, | ||
[keepAliveId]: { | ||
...state[keepAliveId], | ||
status: type, | ||
nodes, | ||
}, | ||
}; | ||
} | ||
default: { | ||
return state; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
import React, { useContext, useEffect } from 'react'; | ||
import { KeepAliveContext } from './KeepAliveContext'; | ||
|
||
interface IKeepAliveTransferProps { | ||
KeepAliveComponent: React.ComponentType; | ||
keepAliveId: string; | ||
} | ||
|
||
const KeepAliveTransfer = <Props extends IKeepAliveTransferProps>( | ||
KeepAliveComponent: React.ComponentType<Props> | any, | ||
keepAliveId?: string, | ||
) => { | ||
const displayName = KeepAliveComponent.displayName || KeepAliveComponent.name || keepAliveId; | ||
|
||
return (props: Omit<Props, keyof IKeepAliveTransferProps>) => { | ||
const _ref = React.useRef<HTMLDivElement>(null!); | ||
const { keepAliveStates, setKeepAliveState } = useContext(KeepAliveContext); | ||
|
||
useEffect(() => { | ||
const state = keepAliveStates[displayName]; | ||
if (state && state?.nodes) { | ||
state.nodes.forEach((node: any) => _ref.current.appendChild(node)); | ||
} else { | ||
setKeepAliveState({ | ||
keepAliveId: displayName, | ||
reactElement: <KeepAliveComponent {...(props as Props)} />, | ||
}); | ||
} | ||
}, [keepAliveStates, setKeepAliveState, props]); | ||
|
||
return <div ref={_ref} />; | ||
}; | ||
}; | ||
|
||
export default KeepAliveTransfer; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
## Context | ||
|
||
- 将组件 dom 缓存保存在一个对象中 | ||
- id: {nodes,ReactElement,status} | ||
|
||
## 结构 | ||
|
||
- Provider => 方法 属性 | ||
- 组件 => nodes => 设置缓存的方法 组件的缓存 | ||
|
||
## use | ||
|
||
```tsx | pure | ||
import { KeepAlive } from '@/core/base/KeepAlive'; | ||
|
||
<KeepAlive></KeepAlive>; | ||
|
||
export default compose<typeof Activity>( | ||
withRoutePage, | ||
withRouter, | ||
connect(({ global, login }: ConnectState) => ({ token: login.token })), | ||
KeepAliveTransfer, | ||
)(Activity); | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
export const CREATING = 'CREATING'; | ||
export const CREATED = 'CREATED'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
import KeepAlive from './KeepAlive'; | ||
import KeepAliveTransfer from './KeepAliveTransfer'; | ||
|
||
export { KeepAlive, KeepAliveTransfer }; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,11 @@ | ||
import FileView from '@/components/FileViewer/demo/demo'; | ||
export default FileView; | ||
import { KeepAliveTransfer } from '@/core/base/KeepAlive'; | ||
import { withRoutePage } from '@/core/Enhance/withRoutePage'; | ||
import { withRouter } from '@umijs/max'; | ||
import { compose } from 'redux'; | ||
|
||
const IndexPage = () => { | ||
return <FileView />; | ||
}; | ||
|
||
export default compose(withRoutePage, withRouter, KeepAliveTransfer)(IndexPage); |