Skip to content

Commit

Permalink
fix: when close db, use ref to prevent update to workspace.lastLocati…
Browse files Browse the repository at this point in the history
…onHash trigger this effect
  • Loading branch information
linonetwo committed Sep 10, 2023
1 parent b23306c commit 31eab41
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 6 deletions.
2 changes: 0 additions & 2 deletions src/pages/WikiWebView/WikiViewer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import { WebView } from 'react-native-webview';
import { styled } from 'styled-components/native';
import { useRequestNativePermissions } from '../../services/NativeService/hooks';
import { useRegisterService } from '../../services/registerServiceOnWebView';
import { useSQLiteService } from '../../services/SQLiteService/hooks';
import { useSetWebViewReferenceToService } from '../../services/WikiHookService/hooks';
import { useConfigStore } from '../../store/config';
import { IWikiWorkspace } from '../../store/wiki';
Expand Down Expand Up @@ -47,7 +46,6 @@ export const WikiViewer = ({ wikiWorkspace }: WikiViewerProps) => {
const triggerFullReload = () => {
setWebViewKeyToReloadAfterRecycleByOS(webViewKeyToReloadAfterRecycleByOS + 1);
};
useSQLiteService(wikiWorkspace);
servicesOfWorkspace.wikiHookService.setLatestOnReloadCallback(triggerFullReload);
useSetWebViewReferenceToService(servicesOfWorkspace.wikiHookService, webViewReference);
const [injectHtmlAndTiddlersStore, webviewSideReceiver] = useStreamChunksToWebView(webViewReference);
Expand Down
2 changes: 2 additions & 0 deletions src/pages/WikiWebView/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { StackScreenProps } from '@react-navigation/stack';
import React from 'react';
import { styled } from 'styled-components/native';
import { RootStackParameterList } from '../../App';
import { useCloseSQLite } from '../../services/SQLiteService/hooks';
import { useWikiStore } from '../../store/wiki';
import { WikiViewer } from './WikiViewer';

Expand All @@ -17,6 +18,7 @@ export interface WikiWebViewProps {
export const WikiWebView: React.FC<StackScreenProps<RootStackParameterList, 'WikiWebView'>> = ({ route }) => {
const { id } = route.params;
const activeWikiWorkspace = useWikiStore(state => state.wikis.find(wiki => wiki.id === id));
useCloseSQLite(activeWikiWorkspace);

return (
<Container>
Expand Down
13 changes: 9 additions & 4 deletions src/services/SQLiteService/hooks.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,22 @@
import { useEffect } from 'react';
import { useEffect, useRef } from 'react';
import { IWikiWorkspace } from '../../store/wiki';
import { sqliteServiceService } from '.';

export function useSQLiteService(workspace: IWikiWorkspace) {
export function useCloseSQLite(workspace: IWikiWorkspace | undefined) {
// use ref to prevent update to workspace.lastLocationHash trigger this effect
const databaseToCloseReference = useRef<IWikiWorkspace | undefined>(workspace);
useEffect(() => {
return (() => {
void (async () => {
try {
await sqliteServiceService.closeDatabase(workspace);
if (databaseToCloseReference.current === undefined) return;
console.log(`Closing sqlite database for ${databaseToCloseReference.current.id} in useSQLiteService`);
// eslint-disable-next-line react-hooks/exhaustive-deps
await sqliteServiceService.closeDatabase(databaseToCloseReference.current);
} catch (error) {
console.error(error);
}
})();
});
}, [workspace]);
}, []);
}

0 comments on commit 31eab41

Please sign in to comment.