Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use @capacitor/App to detect foreground/background #261

Merged
merged 3 commits into from
Sep 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 7 additions & 10 deletions src/Components/Background.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import { toast } from "react-toastify";
import Toast from "./Toast";
import { useHistory } from "react-router";
import { RemoteBackup } from "./BackgroundJobs/RemoteBackup";
import { App } from "@capacitor/app";


export const Background = () => {
Expand Down Expand Up @@ -308,19 +309,15 @@ export const Background = () => {
}, [savedAssets, nodedUp]);

useEffect(() => {

const visiblityHandler = () => {
if (document.visibilityState === "visible") {
setRefresh(Math.random())
const listener = App.addListener("appStateChange", (state) => {
if (state.isActive) {
checkClipboard();
setRefresh(Math.random());
}
checkClipboard();
}
window.addEventListener("visibilitychange", visiblityHandler)
window.addEventListener("focus", checkClipboard);
})

return () => {
window.removeEventListener("visibilitychange", visiblityHandler);
window.removeEventListener("focus", checkClipboard);
listener.remove();
};
}, [checkClipboard])

Expand Down
255 changes: 141 additions & 114 deletions src/Components/BackgroundJobs/HealthCheck.tsx
Original file line number Diff line number Diff line change
@@ -1,132 +1,159 @@
import { useCallback, useEffect, useMemo } from "react"
import { getAllNostrClients, getNostrClient, nostrCallback, subToBeacons } from "../../Api/nostr"
import { useCallback, useEffect, useRef, useState } from "react"
import { getAllNostrClients, nostrCallback, subToBeacons } from "../../Api/nostr"
import { toast } from "react-toastify";
import Toast from "../Toast";
import { useDispatch, useSelector } from "../../State/store";
import { editPaySources } from "../../State/Slices/paySourcesSlice";
import { editSpendSources } from "../../State/Slices/spendSourcesSlice";
import { App } from "@capacitor/app";

const SubsCheckIntervalMs = 20 * 1000
const SubsThresholdMs = 10 * 1000
const BeaconMaxAgeSeconds = 2 * 60
export const HealthCheck = () => {
const paySource = useSelector(({ paySource }) => paySource)
const spendSource = useSelector(({ spendSource }) => spendSource)
const dispatch = useDispatch();
const updateSourceState = useCallback((source: string, state: { disconnected: boolean, name?: string }) => {
const payEntryId = paySource.order.find(s => s.startsWith(source))
if (payEntryId) {
const payEntry = paySource.sources[payEntryId]
let doUpdate = false
const update = { ...payEntry }
if (payEntry.disconnected !== state.disconnected) {
update.disconnected = state.disconnected
doUpdate = true
}
if (state.name && payEntry.label !== state.name) {
update.label = state.name
doUpdate = true
}
if (doUpdate) {
console.log("updating pay source", source)
dispatch({
type: editPaySources.type,
payload: update,
meta: { skipChangelog: true }
})
}
}
const spendEntryId = spendSource.order.find(s => s.startsWith(source))
if (spendEntryId) {
const spendEntry = spendSource.sources[spendEntryId]
let doUpdate = false
const update = { ...spendEntry }
if (spendEntry.disconnected !== state.disconnected) {
update.disconnected = state.disconnected
doUpdate = true
}
if (state.name && spendEntry.label !== state.name) {
update.label = state.name
doUpdate = true
}
if (doUpdate) {
console.log("updating spend source", source)
const paySource = useSelector(({ paySource }) => paySource)
const spendSource = useSelector(({ spendSource }) => spendSource)
const dispatch = useDispatch();

dispatch({
type: editSpendSources.type,
payload: update,
meta: { skipChangelog: true }
})
}
}
}, [paySource, spendSource])
const [isAppActive, setIsAppActive] = useState(true);
const intervalRef = useRef<NodeJS.Timeout | null>(null);


const checkHealth = useCallback(() => {
getAllNostrClients().forEach((wrapper) => {
const state = wrapper.getClientState()
let oldestSingleSub: nostrCallback<any> | undefined = undefined
wrapper.getSingleSubs().forEach(([_, cb]) => {
if (!oldestSingleSub || oldestSingleSub.startedAtMillis > cb.startedAtMillis) {
oldestSingleSub = cb
}
})
if (!oldestSingleSub) {
console.log("no active single subs")
return
}
const now = Date.now()
const startedAtMillis = (oldestSingleSub as nostrCallback<any>).startedAtMillis
if (now - startedAtMillis < SubsThresholdMs) {
console.log("oldest single sub is less than ", SubsThresholdMs, " seconds old")
return
}
console.log("oldest sub is", (startedAtMillis - now) / 1000, "seconds old!")
if (now - state.latestResponseAtMillis <= SubsThresholdMs) {
console.log("latest response is less than ", SubsThresholdMs, " seconds old")
return
}
console.log("latest response is more than ", SubsThresholdMs, " seconds old, checking beacon state")
wrapper.checkBeaconHealth(BeaconMaxAgeSeconds).then(beacon => {
if (!beacon) {
console.log("service is down, beacon is older than", BeaconMaxAgeSeconds, "seconds, disconnecting")
wrapper.disconnectCalls()
toast.error(<Toast title="Source Error" message={`Cannot connect to source: ${wrapper.getPubDst().slice(0, 10)}.`} />)
updateSourceState(wrapper.getPubDst(), { disconnected: true, })
return
}
updateSourceState(wrapper.getPubDst(), { disconnected: false, name: beacon.data.name })

})
})
const updateSourceState = useCallback((source: string, state: { disconnected: boolean, name?: string }) => {
const payEntryId = paySource.order.find(s => s.startsWith(source))
if (payEntryId) {
const payEntry = paySource.sources[payEntryId]
let doUpdate = false
const update = { ...payEntry }
if (payEntry.disconnected !== state.disconnected) {
update.disconnected = state.disconnected
doUpdate = true
}
if (state.name && payEntry.label !== state.name) {
update.label = state.name
doUpdate = true
}
if (doUpdate) {
console.log("updating pay source", source)
dispatch({
type: editPaySources.type,
payload: update,
meta: { skipChangelog: true }
})
}
}
const spendEntryId = spendSource.order.find(s => s.startsWith(source))
if (spendEntryId) {
const spendEntry = spendSource.sources[spendEntryId]
let doUpdate = false
const update = { ...spendEntry }
if (spendEntry.disconnected !== state.disconnected) {
update.disconnected = state.disconnected
doUpdate = true
}
if (state.name && spendEntry.label !== state.name) {
update.label = state.name
doUpdate = true
}
if (doUpdate) {
console.log("updating spend source", source)

}, [updateSourceState])
dispatch({
type: editSpendSources.type,
payload: update,
meta: { skipChangelog: true }
})
}
}
}, [paySource, spendSource, dispatch])

useEffect(() => {
const interval = setInterval(() => {
checkHealth()
}, SubsCheckIntervalMs)
return () => {
clearInterval(interval)
}
}, [checkHealth])

useEffect(() => {
return subToBeacons(up => {
updateSourceState(up.createdByPub, { disconnected: false, name: up.name })
})
}, [updateSourceState])
/*
const updateSourceState = (source: string, connected: boolean) => {
const payEntry = paySource.find(s => s.pasteField === source)
if (payEntry) {
dispatch(editPaySources({ ...payEntry, disconnected: !connected }))
}
const spendEntry = spendSource.find(s => s.pasteField === source)
if (spendEntry) {
dispatch(editSpendSources({ ...spendEntry, disconnected: !connected }))
}
}
*/
return null
const checkHealth = useCallback(() => {
if (!isAppActive) {
console.log("App is in the background, skipping health check");
return;
}

getAllNostrClients().forEach((wrapper) => {
const state = wrapper.getClientState()
let oldestSingleSub: nostrCallback<any> | undefined = undefined
wrapper.getSingleSubs().forEach(([_, cb]) => {
if (!oldestSingleSub || oldestSingleSub.startedAtMillis > cb.startedAtMillis) {
oldestSingleSub = cb
}
})
if (!oldestSingleSub) {
console.log("no active single subs")
return
}
const now = Date.now()
const startedAtMillis = (oldestSingleSub as nostrCallback<any>).startedAtMillis
if (now - startedAtMillis < SubsThresholdMs) {
console.log("oldest single sub is less than ", SubsThresholdMs, " seconds old")
return
}
console.log("oldest sub is", (startedAtMillis - now) / 1000, "seconds old!")
if (now - state.latestResponseAtMillis <= SubsThresholdMs) {
console.log("latest response is less than ", SubsThresholdMs, " seconds old")
return
}
console.log("latest response is more than ", SubsThresholdMs, " seconds old, checking beacon state")
wrapper.checkBeaconHealth(BeaconMaxAgeSeconds).then(beacon => {
if (!beacon) {
console.log("service is down, beacon is older than", BeaconMaxAgeSeconds, "seconds, disconnecting")
wrapper.disconnectCalls()
toast.error(<Toast title="Source Error" message={`Cannot connect to source: ${wrapper.getPubDst().slice(0, 10)}.`} />)
updateSourceState(wrapper.getPubDst(), { disconnected: true, })
return
}
updateSourceState(wrapper.getPubDst(), { disconnected: false, name: beacon.data.name })

})
})

}, [updateSourceState, isAppActive])

useEffect(() => {
if (isAppActive) {
intervalRef.current = setInterval(() => {
checkHealth();
}, SubsCheckIntervalMs);
} else if (intervalRef.current) {
clearInterval(intervalRef.current);
intervalRef.current = null;
}
return () => {
if (intervalRef.current) {
clearInterval(intervalRef.current);
}
}
}, [checkHealth, dispatch, isAppActive])

useEffect(() => {
const listener = App.addListener("appStateChange", (state) => setIsAppActive(state.isActive));

return () => {
listener.remove();
}
}, [])

useEffect(() => {
return subToBeacons(up => {
updateSourceState(up.createdByPub, { disconnected: false, name: up.name })
})
}, [updateSourceState])
/*
const updateSourceState = (source: string, connected: boolean) => {
const payEntry = paySource.find(s => s.pasteField === source)
if (payEntry) {
dispatch(editPaySources({ ...payEntry, disconnected: !connected }))
}
const spendEntry = spendSource.find(s => s.pasteField === source)
if (spendEntry) {
dispatch(editSpendSources({ ...spendEntry, disconnected: !connected }))
}
}
*/
return null
}