Skip to content

Commit

Permalink
fix: only reload extension for ios 17.5+
Browse files Browse the repository at this point in the history
  • Loading branch information
BlueGreenMagick committed Jun 14, 2024
1 parent b042944 commit 314f215
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 30 deletions.
30 changes: 0 additions & 30 deletions main/src/extension/browserApi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ import Utils, {
type Satisfies,
type Thennable,
} from "lib/utils";
import { PLATFORM } from "consts";

/**
* Type map for messages between extension processes
Expand Down Expand Up @@ -111,8 +110,6 @@ export class BrowserApi {
if (opts.handleConnection) {
this.attachConnectionHandler();
}

this.setupIosPeriodicReload();
}

private attachRequestHandler() {
Expand Down Expand Up @@ -508,31 +505,4 @@ export class BrowserApi {
handleBrowserLoad(handler: () => void) {
chrome.runtime.onStartup.addListener(handler);
}

// workaround to ios 17.5 bug where background script freezes after ~30s of non-stop activity
// https://github.com/alexkates/content-script-non-responsive-bug/issues/1
private setupIosPeriodicReload() {
if (PLATFORM !== "ios" || this.context !== "background") return;
console.debug("Set up periodic ios reload");

let counter = 0;
let last = Date.now();

function checkReload() {
const curr = Date.now();
if (curr - last > 4500) {
counter = 0;
} else {
counter += 1;
}
last = curr;

if (counter > 25) {
console.debug("Reloading extension");
chrome.runtime.reload();
}
}

setInterval(checkReload, 1000);
}
}
40 changes: 40 additions & 0 deletions main/src/platform/ios/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import {
migrateConfigObject,
type StoredCompatConfiguration,
} from "lib/compat";
import { PLATFORM } from "consts";

export * from "../common";

Expand All @@ -32,11 +33,18 @@ export interface AppMessageMap {
search: [SearchRequest, RawTokenizeResult];
ttsVoices: [null, TTSVoice[]];
tts: [TTSRequest, null];
iosVersion: [null, IosVersion];
}

export type AppRequest<K extends keyof AppMessageMap> = AppMessageMap[K][0];
export type AppResponse<K extends keyof AppMessageMap> = AppMessageMap[K][1];

interface IosVersion {
major: number;
minor: number;
patch: number;
}

export class IosPlatform implements IPlatform {
static IS_DESKTOP = false;
static IS_IOS = true;
Expand All @@ -58,6 +66,8 @@ export class IosPlatform implements IPlatform {
browserApi.handleRequest("saveConfig", (config) => {
return this.saveConfig(config);
});

void this.setupIosPeriodicReload();
}
}

Expand Down Expand Up @@ -184,6 +194,36 @@ export class IosPlatform implements IPlatform {
await this.saveConfig(migrated);
return migrated;
}

// workaround to ios 17.5 bug where background script freezes after ~30s of non-stop activity
// https://github.com/alexkates/content-script-non-responsive-bug/issues/1
private async setupIosPeriodicReload() {
if (PLATFORM !== "ios" || this.browserApi.context !== "background") return;
console.debug("Set up periodic ios reload");

let wakeup = Date.now();
let last = Date.now();

function checkReload() {
const curr = Date.now();
if (curr - last > 4500) {
wakeup = curr;
}
last = curr;

if (curr - wakeup > 25 * 1000) {
console.debug("Reloading extension");
chrome.runtime.reload();
}
}

const iv = setInterval(checkReload, 1000);

const ver = await this.requestToApp("iosVersion", null);
if (!(ver.major > 18 || (ver.major === 17 && ver.minor >= 5))) {
clearInterval(iv);
}
}
}

IosPlatform satisfies IPlatformStatic;
Expand Down
10 changes: 10 additions & 0 deletions safari/iOS (Extension)/IOSWebExtensionHandler.swift
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,10 @@ class IOSWebExtensionHandler: NSObject, NSExtensionRequestHandling {
case "tts":
let req: TTSRequest = try jsonDeserialize(json: request)
try ttsSpeak(voice: req.voice, text: req.text)
case "iosVersion":
let ver = ProcessInfo.processInfo.operatingSystemVersion
let iosVersion = IosVersion(major: ver.majorVersion, minor: ver.minorVersion, patch: ver.patchVersion)
jsonResponse = try jsonSerialize(obj: iosVersion)
default:
return
}
Expand Down Expand Up @@ -74,3 +78,9 @@ private struct SearchRequest: Decodable {
var term: String
var charAt: UInt32?
}

private struct IosVersion: Codable {
var major: Int
var minor: Int
var patch: Int
}

0 comments on commit 314f215

Please sign in to comment.