Skip to content
This repository has been archived by the owner on Jan 23, 2024. It is now read-only.

Commit

Permalink
feat: random port when default port is used
Browse files Browse the repository at this point in the history
  • Loading branch information
aidenlx committed Jul 26, 2021
1 parent 4cd60ba commit 74ed1d6
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 30 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
"eslint-plugin-prettier": "^3.4.0",
"eslint-plugin-simple-import-sort": "^7.0.0",
"express": "^4.17.1",
"get-port": "^5.1.1",
"http-proxy-middleware": "^2.0.1",
"json": "^11.0.0",
"obsidian": "^0.12.11",
Expand Down
80 changes: 50 additions & 30 deletions src/mxbili-main.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import getPort from "get-port";
import { debounce, Notice, Plugin, Setting } from "obsidian";

import fetchBiliPoster from "./fake-bili/fetch-poster";
Expand All @@ -10,36 +11,6 @@ export default class MxBili extends Plugin {

fetchPoster = fetchBiliPoster;

portSetting = (containerEl: HTMLElement) =>
new Setting(containerEl)
.setName("代理端口号")
.setDesc("若与现有端口冲突请手动指定其他端口")
.addText((text) => {
const save = debounce(
async (value: string) => {
this.setupProxy(+value);
this.settings.port = +value;
await this.saveSettings();
},
500,
true,
);
text
.setValue(this.settings.port.toString())
.onChange(async (value: string) => {
text.inputEl.toggleClass("incorrect", !isVaildPort(value));
if (isVaildPort(value)) save(value);
});
});

async onload() {
console.log("loading MxBili");

await this.loadSettings();

this.setupProxy(this.settings.port);
}

setupProxy = (port: number): void => {
if (this.server) this.server.close().listen(port);
else {
Expand All @@ -52,6 +23,32 @@ export default class MxBili extends Plugin {
}
};

/**
* detect if port being used, and save free port
* @param port desire port
* @returns free port
*/
setupPort = async (port: number): Promise<number> => {
const newPort = await getPort({ port });
if (newPort !== port) {
new Notice(`${port}端口已被占用,切换至${newPort}`);
}
if (this.settings.port !== newPort) {
this.settings.port = newPort;
await this.saveSettings();
}
return newPort;
};

async onload() {
console.log("loading MxBili");

await this.loadSettings();

const newPort = await this.setupPort(this.settings.port);
this.setupProxy(newPort);
}

onunload() {
console.log("unloading MxBili");

Expand All @@ -65,6 +62,29 @@ export default class MxBili extends Plugin {
async saveSettings() {
await this.saveData(this.settings);
}

portSetting = (containerEl: HTMLElement) =>
new Setting(containerEl)
.setName("代理端口号")
.setDesc("若与现有端口冲突请手动指定其他端口")
.addText((text) => {
const save = debounce(
async (value: string) => {
const newPort = await this.setupPort(+value);
if (newPort !== +value) text.setValue(newPort.toString());
this.setupProxy(newPort);
},
500,
true,
);
text
.setValue(this.settings.port.toString())
.onChange(async (value: string) => {
text.inputEl.toggleClass("incorrect", !isVaildPort(value));
if (isVaildPort(value) && this.settings.port !== +value)
save(value);
});
});
}

interface MxBiliSettings {
Expand Down

0 comments on commit 74ed1d6

Please sign in to comment.