generated from xhofe/solid-lib
-
Notifications
You must be signed in to change notification settings - Fork 511
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
perf(history): cache history objs to improve performance (#226)
Co-authored-by: Andy Hsu <[email protected]>
- Loading branch information
Showing
8 changed files
with
86 additions
and
45 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
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,51 @@ | ||
import { ObjStore, objStore, State } from "~/store/obj" | ||
import { getGlobalPage, setGlobalPage } from "~/hooks" | ||
|
||
interface History { | ||
obj: object | ||
page: number | ||
scroll: number | ||
} | ||
|
||
export const HistoryMap = new Map<string, History>() | ||
|
||
const waitForNextFrame = () => { | ||
return new Promise((resolve) => setTimeout(resolve)) | ||
} | ||
|
||
export const recordHistory = (path: string) => { | ||
const obj = JSON.parse(JSON.stringify(objStore)) | ||
if ( | ||
![State.FetchingMore, State.Folder, State.File].includes(objStore.state) | ||
) { | ||
return | ||
} | ||
if (objStore.state === State.FetchingMore) { | ||
obj.state = State.Folder | ||
} | ||
const history = { | ||
obj, | ||
page: getGlobalPage(), | ||
scroll: window.scrollY, | ||
} | ||
HistoryMap.set(path, history) | ||
} | ||
|
||
export const recoverHistory = async (path: string) => { | ||
if (!HistoryMap.has(path)) return | ||
const history = HistoryMap.get(path)! | ||
setGlobalPage(history.page) | ||
ObjStore.setState(State.Initial) | ||
await waitForNextFrame() | ||
ObjStore.set(history.obj) | ||
await waitForNextFrame() | ||
window.scroll({ top: history.scroll, behavior: "smooth" }) | ||
} | ||
|
||
export const hasHistory = (path: string) => { | ||
return HistoryMap.has(path) | ||
} | ||
|
||
export const clearHistory = (path: string) => { | ||
HistoryMap.delete(path) | ||
} |
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 was deleted.
Oops, something went wrong.