Skip to content

Commit

Permalink
Merge pull request #287 from caorushizi/feature
Browse files Browse the repository at this point in the history
feat: ✨  video sniffing logic optimization
  • Loading branch information
caorushizi authored Sep 26, 2024
2 parents 3a966ee + 3c6c296 commit a9728df
Show file tree
Hide file tree
Showing 6 changed files with 57 additions and 31 deletions.
1 change: 1 addition & 0 deletions packages/main/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@
"inversify": "^6.0.2",
"lint-staged": "^15.2.5",
"lodash": "^4.17.21",
"lru-cache": "^11.0.1",
"mime-types": "^2.1.35",
"nanoid": "^5.0.7",
"node-fetch": "^3.3.2",
Expand Down
10 changes: 10 additions & 0 deletions packages/main/src/helper/cache.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { LRUCache } from "lru-cache";

const options: LRUCache.OptionsMaxLimit<string, boolean, unknown> = {
max: 5000,

// how long to live in ms
ttl: 1000 * 60 * 5,
};

export const urlCache = new LRUCache(options);
1 change: 1 addition & 0 deletions packages/main/src/helper/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,3 +41,4 @@ export { convertToAudio } from "./ffmpeg.ts";
export { fetchWrapper as fetch };
export { fileExists } from "./file.ts";
export { videoPattern } from "./video.ts";
export { urlCache } from "./cache.ts";
63 changes: 33 additions & 30 deletions packages/main/src/services/SniffingHelperService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import {
PERSIST_WEBVIEW,
PRIVACY_WEBVIEW,
formatHeaders,
urlCache,
} from "../helper/index.ts";

export interface SourceParams {
Expand Down Expand Up @@ -48,8 +49,7 @@ const filterList: SourceFilter[] = [
@injectable()
export class SniffingHelper extends EventEmitter {
private pageInfo: PageInfo = { title: "", url: "" };
private ready = false;
private queue: SourceParams[] = [];
private readonly prepareDelay = 1000;

constructor(
@inject(TYPES.ElectronLogger)
Expand All @@ -59,38 +59,38 @@ export class SniffingHelper extends EventEmitter {
}

pluginReady() {
this.ready = true;
this.queue.forEach((item) => {
this.emit("source", item);
});
// empty
}

update(pageInfo: PageInfo) {
this.pageInfo = pageInfo;
}

reset(pageInfo: PageInfo) {
this.pageInfo = pageInfo;
this.ready = false;
this.queue = [];

listLoop: for (const filter of filterList) {
if (filter.hosts) {
for (const host of filter.hosts) {
if (!host.test(pageInfo.url)) {
continue;
checkPageInfo() {
// 发送 page 相关的信息
const sendPageInfo = () => {
listLoop: for (const filter of filterList) {
if (filter.hosts) {
for (const host of filter.hosts) {
if (!host.test(this.pageInfo.url)) {
continue;
}

this.send({
url: this.pageInfo.url,
documentURL: this.pageInfo.url,
name: this.pageInfo.title,
type: filter.type,
});
break listLoop;
}

this.send({
url: pageInfo.url,
documentURL: pageInfo.url,
name: pageInfo.title,
type: filter.type,
});
break listLoop;
}
}
}
};

setTimeout(() => {
sendPageInfo();
}, this.prepareDelay);
}

start(privacy: boolean = false) {
Expand All @@ -100,13 +100,16 @@ export class SniffingHelper extends EventEmitter {
}

send = (item: SourceParams) => {
const urlCacheKey = `${item.url}_${item.name}`;
const cacheUrl = urlCache.get(urlCacheKey);
if (cacheUrl) {
return;
}

this.logger.info(`[SniffingHelper] send: ${item.url}`);
// 等待 DOM 中浮窗加载完成
// if (this.ready) {
// } else {
// this.queue.push(item);
// }
this.emit("source", item);

urlCache.set(urlCacheKey, true);
};

private onSendHeaders = (details: OnSendHeadersListenerDetails): void => {
Expand Down
4 changes: 3 additions & 1 deletion packages/main/src/services/WebviewService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ export default class WebviewService {
onDomReady = () => {
if (!this.view) return;
const pageInfo = this.getPageInfo();
this.sniffingHelper.reset(pageInfo);
this.sniffingHelper.update(pageInfo);
this.window.webContents.send("webview-dom-ready", pageInfo);
};

Expand All @@ -107,6 +107,8 @@ export default class WebviewService {
} catch (err) {
// empty
}

this.sniffingHelper.checkPageInfo();
};

onDidFailLoad = (e: Event, code: number, desc: string) => {
Expand Down
9 changes: 9 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit a9728df

Please sign in to comment.