Skip to content

Commit

Permalink
🐛 [desktop-dev] 修复deeplinks第二次打开失败异常
Browse files Browse the repository at this point in the history
  • Loading branch information
kingsword09 committed Oct 26, 2023
1 parent 4ad0ffe commit 4ecbad6
Show file tree
Hide file tree
Showing 5 changed files with 109 additions and 55 deletions.
20 changes: 19 additions & 1 deletion desktop-dev/src/browser/jmm/assets/src/components/Header.vue
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import type { $InstallProgressInfo } from "&/jmm.api.serve.ts";
import type { $JmmAppInstallManifest } from "&/types.ts";
import { toJsonlinesStream } from "helper/stream/jsonlinesStreamHelper.ts";
import { streamRead } from "helper/stream/readableStreamHelper";
import { ref } from "vue";
import { ref, watchEffect } from "vue";
const enum DOWNLOAD_STATUS {
INIT = -1, // 初始状态
PAUSE = 0, // 暂停
Expand All @@ -23,6 +23,23 @@ const props = defineProps({
},
});
watchEffect(async () => {
if (props.appInfo.id && downloadState.value !== DOWNLOAD_STATUS.DONE) {
await querySelf();
}
});
async function querySelf() {
const mmid = props.appInfo.id;
const url = `${getApiOrigin()}/app/query?mmid=${mmid}`;
const res = await fetch(url);
const result: Boolean = await res.json();
if (res.ok && result) {
downloadState.value = DOWNLOAD_STATUS.DONE;
}
}
/**
* 关闭当前APP
*/
Expand Down Expand Up @@ -75,6 +92,7 @@ const openApp = async () => {
// 然后打开指定的应用
const mmid = props.appInfo.id;
const res = await fetch(`${url}/app/open?mmid=${mmid}`);
if (res.ok) {
closeSelf();
}
Expand Down
90 changes: 48 additions & 42 deletions desktop-dev/src/browser/jmm/jmm.api.serve.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import { Store } from "../../helper/electronStore.ts";
import { simpleEncoder } from "../../helper/encoding.ts";
import { headersGetTotalLength } from "../../helper/httpHelper.ts";
import { locks } from "../../helper/locksManager.ts";
import { fetchMatch } from "../../helper/patternHelper.ts";
import { ReadableStreamOut } from "../../helper/stream/readableStreamHelper.ts";
import { createHttpDwebServer } from "../../std/http/helper/$createHttpDwebServer.ts";
import type { JmmNMM } from "./jmm.ts";
Expand Down Expand Up @@ -65,37 +66,37 @@ export async function createApiServer(this: JmmNMM) {
port: 6363,
});
const serverIpc = await this.apiServer.listen();
serverIpc
.onFetch(
async (event) => {
if (event.pathname === "/app/install") {
const appInfo = await event.json<$JmmAppInstallManifest>();
const res = await locks.request(`jmm-install:${appInfo.id}`, () => _appInstall.call(this, event, appInfo));
/// 上锁
return res
}
},
async (event) => {
if (event.pathname === "/close/self") {
return this.jmmServer?.close();
}
},
async (event) => {
if (event.pathname === "/app/open") {
const id = event.searchParams.get("mmid") as $MMID;
// 打开之前需要先关闭 否者更新后无法实现 更新打开 ??
await this.context?.dns.close(id);
const connectResult = this.context?.dns.connect(id);
if (connectResult === undefined) {
throw new Error(`${id} not found!`);
}
// opendAppIpc.postMessage(IpcEvent.fromText("activity", ""));
return Response.json(true);
}

const onFetchMatcher = fetchMatch()
.post("/app/install", async (event) => {
const appInfo = await event.json<$JmmAppInstallManifest>();
const res = await locks.request(`jmm-install:${appInfo.id}`, () => _appInstall.call(this, event, appInfo));
/// 上锁
return res;
})
.get("/close/self", async (event) => {
return this.jmmServer?.close();
})
.get("/app/open", async (event) => {
const id = event.searchParams.get("mmid") as $MMID;
// 打开之前需要先关闭 否者更新后无法实现 更新打开 ??
await this.context?.dns.close(id);
const connectResult = this.context?.dns.connect(id);
if (connectResult === undefined) {
throw new Error(`${id} not found!`);
}
// opendAppIpc.postMessage(IpcEvent.fromText("activity", ""));
return Response.json(true);
})
.get("/app/query", async (event) => {
const id = event.searchParams.get("mmid") as $MMID;
if(await this.context?.dns.query(id)) {
return Response.json(true);
} else {
return Response.json(false);
}
)
.internalServerError()
.cors();
});
serverIpc.onFetch(onFetchMatcher.run).internalServerError().cors();
}
/**
* 应用程序安装的核心逻辑
Expand Down Expand Up @@ -254,15 +255,20 @@ async function _appInstall(this: JmmNMM, event: FetchEvent, appInfo: $JmmAppInst
return enqueueInstallProgress("install", 0, true, `invalid bundle-mime-type: ${bundleMime}`);
}
// createSession
const sessionFilePath = path.join(installDir,"/usr/sys/session.json")
const sessionFilePath = path.join(installDir, "/usr/sys/session.json");
const nowDate = Date.now();
if(fs.existsSync(sessionFilePath)) {
const sessionInfo = fs.readFileSync(sessionFilePath).toJSON() as unknown as $SessionInfo
sessionInfo.upgradeTime = nowDate
fs.writeFileSync(sessionFilePath,JSON.stringify(sessionInfo))
} {
fs.mkdirSync(path.join(installDir,"/usr/sys"), { recursive: true });
fs.writeFileSync(sessionFilePath,`{"installTime":${nowDate},"upgradeTime":${nowDate},"installUrl":"${appInfo.bundle_url}"}`,{})
if (fs.existsSync(sessionFilePath)) {
const sessionInfo = fs.readFileSync(sessionFilePath).toJSON() as unknown as $SessionInfo;
sessionInfo.upgradeTime = nowDate;
fs.writeFileSync(sessionFilePath, JSON.stringify(sessionInfo));
}
{
fs.mkdirSync(path.join(installDir, "/usr/sys"), { recursive: true });
fs.writeFileSync(
sessionFilePath,
`{"installTime":${nowDate},"upgradeTime":${nowDate},"installUrl":"${appInfo.bundle_url}"}`,
{}
);
}
fs.unlinkSync(tempFilePath);
fs.unlinkSync(hashFilePath);
Expand All @@ -282,10 +288,10 @@ async function _appInstall(this: JmmNMM, event: FetchEvent, appInfo: $JmmAppInst
}

export type $SessionInfo = {
installTime:number,
upgradeTime:number,
installUrl:string
}
installTime: number;
upgradeTime: number;
installUrl: string;
};

export type $InstallProgressInfo = {
state: "download" | "install";
Expand Down
34 changes: 24 additions & 10 deletions desktop-dev/src/browser/jmm/jmm.www.serve.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { $MMID } from "../../core/types.ts";
import { tryDevUrl } from "../../helper/electronIsDev.ts";
import { createNativeWindow } from "../../helper/openNativeWindow.ts";
import { buildUrl } from "../../helper/urlHelper.ts";
Expand All @@ -6,17 +7,30 @@ import type { JmmNMM } from "./jmm.ts";

export class JmmServer {
constructor(
private win: Electron.BrowserWindow,
private mm: JmmNMM,
private jmmUrl: string,
private jmmServer: HttpDwebServer
) {}

private win?: Electron.BrowserWindow = undefined;

close() {
this.win.close();
this.win?.close();
}

show(metadataUrl: string) {
async show(metadataUrl: string) {
if(!this.win) {
this.win = await createNativeWindow(this.mm.mmid, {
width: 350,
height: 750,
show: false,
});
this.win.setVisibleOnAllWorkspaces(true);
this.win.addListener("close", _ => {
this.win = undefined;
});
}

this.win.loadURL(
buildUrl(this.jmmUrl, {
search: {
Expand All @@ -34,12 +48,12 @@ export class JmmServer {
}).href;
const jmmUrl = await tryDevUrl(jmmProdUrl, `http://localhost:3601/index.html`);

const jmmWin = await createNativeWindow(mm.mmid, {
width: 350,
height: 750,
show: false,
});
jmmWin.setVisibleOnAllWorkspaces(true);
return new JmmServer(jmmWin, mm, jmmUrl, jmmServer);
// const jmmWin = await createNativeWindow(mm.mmid, {
// width: 350,
// height: 750,
// show: false,
// });
// jmmWin.setVisibleOnAllWorkspaces(true);
return new JmmServer(mm, jmmUrl, jmmServer);
}
}
17 changes: 15 additions & 2 deletions desktop-dev/src/browser/web/server.www.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { buildRequestX } from "../../core/helper/ipcRequestHelper.ts";
import { $MicroModule } from "../../core/types.ts";
import { createHttpDwebServer } from "../../std/http/helper/$createHttpDwebServer.ts";

Expand All @@ -8,11 +9,23 @@ export async function createWWWServer(this: $MicroModule) {
port: 433,
});

const API_PREFIX = "/api/";
const serverIpc = await wwwServer.listen();
serverIpc.onFetch(async (event) => {
const { pathname, search } = event.url;
const url = `file:///sys/browser/desk${pathname}?mode=stream`;
return await this.nativeFetch(url);
let url: string;
if (pathname.startsWith(API_PREFIX)) {
url = `file://${pathname.slice(API_PREFIX.length)}${search}`;
} else {
url = `file:///sys/browser/desk${pathname}?mode=stream`;
}
const request = buildRequestX(url, {
method: event.method,
headers: event.headers,
body: event.ipcRequest.body.raw,
});

return await this.nativeFetch(request);
});
return wwwServer;
}
3 changes: 3 additions & 0 deletions desktop-dev/src/browser/web/web.nmm.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,9 @@ export class WebBrowserNMM extends NativeMicroModule {
// 激活的逻辑
this.onActivity(async (event, ipc) => {
this.win = await this.openBrowserWindow(browserUrl);
this.win?.addListener("close", (_) => {
this.win = undefined;
});
void this.win?.loadURL(
buildUrl(browserUrl, {
search: {
Expand Down

0 comments on commit 4ecbad6

Please sign in to comment.