fix(mobile): preserve startup network path changes
Co-authored-by: codex <[email protected]>
This commit is contained in:
1 parent
68b85b9050
commit
eff1bf76aa
6 files changed
+143
-57
No files matched your search
@@ -0,0 +1,42 @@
|
||||
import { describe, expect, it } from "@effect/vitest";
|
||||
|
||||
import {
|
||||
observeNetworkPath,
|
||||
seedNetworkPathBaseline,
|
||||
UNKNOWN_NETWORK_PATH,
|
||||
} from "./network-path-change";
|
||||
|
||||
describe("network path change detection", () => {
|
||||
it("does not probe when the listener matches the seeded interface", () => {
|
||||
const seeded = seedNetworkPathBaseline(UNKNOWN_NETWORK_PATH, "WIFI");
|
||||
|
||||
expect(observeNetworkPath(seeded, "WIFI")).toEqual({
|
||||
baseline: { known: true, type: "WIFI" },
|
||||
shouldProbe: false,
|
||||
});
|
||||
});
|
||||
|
||||
it("probes when a known interface changes", () => {
|
||||
const seeded = seedNetworkPathBaseline(UNKNOWN_NETWORK_PATH, "WIFI");
|
||||
|
||||
expect(observeNetworkPath(seeded, "CELLULAR").shouldProbe).toBe(true);
|
||||
});
|
||||
|
||||
it("probes when the listener wins the race with the async seed", () => {
|
||||
const observed = observeNetworkPath(UNKNOWN_NETWORK_PATH, "CELLULAR");
|
||||
|
||||
expect(observed.shouldProbe).toBe(true);
|
||||
expect(seedNetworkPathBaseline(observed.baseline, "WIFI")).toEqual({
|
||||
known: true,
|
||||
type: "CELLULAR",
|
||||
});
|
||||
});
|
||||
|
||||
it("probes on the first known interface after an unknown observation", () => {
|
||||
const seeded = seedNetworkPathBaseline(UNKNOWN_NETWORK_PATH, "WIFI");
|
||||
const unknown = observeNetworkPath(seeded, null);
|
||||
|
||||
expect(unknown.shouldProbe).toBe(false);
|
||||
expect(observeNetworkPath(unknown.baseline, "CELLULAR").shouldProbe).toBe(true);
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,41 @@
|
||||
export interface NetworkPathBaseline {
|
||||
readonly known: boolean;
|
||||
readonly type: string | null;
|
||||
}
|
||||
|
||||
export const UNKNOWN_NETWORK_PATH: NetworkPathBaseline = {
|
||||
known: false,
|
||||
type: null,
|
||||
};
|
||||
|
||||
export function seedNetworkPathBaseline(
|
||||
baseline: NetworkPathBaseline,
|
||||
type: string | null,
|
||||
): NetworkPathBaseline {
|
||||
if (baseline.known || type === null) {
|
||||
return baseline;
|
||||
}
|
||||
return { known: true, type };
|
||||
}
|
||||
|
||||
export function observeNetworkPath(
|
||||
baseline: NetworkPathBaseline,
|
||||
type: string | null,
|
||||
): {
|
||||
readonly baseline: NetworkPathBaseline;
|
||||
readonly shouldProbe: boolean;
|
||||
} {
|
||||
if (type === null) {
|
||||
return {
|
||||
baseline: UNKNOWN_NETWORK_PATH,
|
||||
shouldProbe: false,
|
||||
};
|
||||
}
|
||||
return {
|
||||
baseline: { known: true, type },
|
||||
// If the initial async seed has not landed, the first listener event may
|
||||
// itself be the WiFi/cellular transition. A cheap advisory probe is safer
|
||||
// than losing that transition and waiting for the socket ping timeout.
|
||||
shouldProbe: !baseline.known || baseline.type !== type,
|
||||
};
|
||||
}
|
||||
@@ -31,6 +31,11 @@ import { appAtomRegistry } from "../state/atom-registry";
|
||||
import { clearThreadOutboxEnvironment } from "../state/thread-outbox";
|
||||
import { clearComposerDraftsEnvironment } from "../state/use-composer-drafts";
|
||||
import { mobileApplicationActiveWakeup } from "./app-state-wakeups";
|
||||
import {
|
||||
observeNetworkPath,
|
||||
seedNetworkPathBaseline,
|
||||
UNKNOWN_NETWORK_PATH,
|
||||
} from "./network-path-change";
|
||||
import { connectionStorageLayer } from "./storage";
|
||||
|
||||
function networkStatus(state: Network.NetworkState): "unknown" | "offline" | "online" {
|
||||
@@ -110,20 +115,17 @@ const wakeupsLayer = Wakeups.layer({
|
||||
// it, which keeps flapping paths cheap. Seed the current type so
|
||||
// the first flip after startup is detected; the listener only
|
||||
// reports changes.
|
||||
let networkType: string | null = null;
|
||||
let networkPath = UNKNOWN_NETWORK_PATH;
|
||||
void Network.getNetworkStateAsync()
|
||||
.then((current) => {
|
||||
networkType ??= current.type ?? null;
|
||||
networkPath = seedNetworkPathBaseline(networkPath, current.type ?? null);
|
||||
})
|
||||
.catch(() => undefined);
|
||||
const networkSubscription = Network.addNetworkStateListener((state) => {
|
||||
const nextType = state.type ?? null;
|
||||
const previousType = networkType;
|
||||
networkType = nextType;
|
||||
const observation = observeNetworkPath(networkPath, state.type ?? null);
|
||||
networkPath = observation.baseline;
|
||||
if (
|
||||
previousType !== null &&
|
||||
nextType !== null &&
|
||||
nextType !== previousType &&
|
||||
observation.shouldProbe &&
|
||||
state.isConnected === true &&
|
||||
AppState.currentState === "active"
|
||||
) {
|
||||
|
||||
@@ -1,7 +1,11 @@
|
||||
export const bunWebSocketOptions = {
|
||||
// Negotiate permessage-deflate with clients that offer it. A dedicated
|
||||
// compressor keeps a per-connection sliding window, while the shared
|
||||
// decompressor avoids uWebSockets' dedicated-decompressor aborts.
|
||||
// Negotiate permessage-deflate with clients that offer it; clients that
|
||||
// don't still get uncompressed frames on their connection. A dedicated
|
||||
// compressor keeps a per-connection sliding window (context takeover) so the
|
||||
// compression dictionary is shared across server-to-client frames.
|
||||
// Decompression uses the shared decompressor: uWebSockets' dedicated
|
||||
// decompressor path can abort connections (close 1006) on valid DEFLATE
|
||||
// input — see https://github.com/uNetworking/uWebSockets.js/issues/633.
|
||||
perMessageDeflate: {
|
||||
compress: "dedicated",
|
||||
decompress: "shared",
|
||||
|
||||
Reference in new issue
Block a user