Skip to content

Commit

Permalink
Merge pull request #708 from Hi-Windom/v0.33
Browse files Browse the repository at this point in the history
V0.33
  • Loading branch information
Soltus authored May 9, 2024
2 parents 62b5f84 + b970694 commit 150622d
Show file tree
Hide file tree
Showing 4 changed files with 121 additions and 26 deletions.
4 changes: 2 additions & 2 deletions .vscode/extensions/Sillot/package.json
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
{
"name": "sillot",
"displayName": "汐洛 Sillot",
"description": "汐洛(Sillot)孵化自思源笔记(siyuan-note),致力于服务智慧新彖乄。此插件为汐洛官方插件,提供多功能一体化集成。",
"version": "0.33.1500",
"description": "汐洛彖夲肜矩阵(Sillot T☳Converbenk Matrix)为智慧新彖务服务。此插件为汐洛官方插件,提供多功能一体化集成。",
"version": "0.33.1510",
"preview": true,
"repository": "https://github.com/Hi-Windom/Sillot",
"publisher": "Hi-Windom",
Expand Down
2 changes: 1 addition & 1 deletion app/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "sillot",
"version": "0.33.1500",
"version": "0.33.1510",
"syv": "3.0.14",
"sypv": "[]",
"description": "Build Your Eternal Digital Garden",
Expand Down
139 changes: 117 additions & 22 deletions app/src/layout/Model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,108 @@ import type {Tab} from "./Tab";
/// #endif
import {processMessage} from "../util/processMessage";
import {kernelError, reloadSync} from "../dialog/processSystem";
import type {App} from "../index";
import {App} from "../index";

// export class Model {
// public ws: WebSocket;
// public reqId: number;
// /// #if !MOBILE
// public parent: Tab;
// /// #else
// // @ts-ignore
// // biome-ignore lint/suspicious/noDuplicateClassMembers: <explanation>
// public parent: any;
// /// #endif
// public app: App;

// constructor(options: {
// app: App,
// id: string,
// type?: TWS,
// callback?: () => void,
// msgCallback?: (data: IWebSocketData) => void
// }) {
// this.app = options.app;
// if (options.msgCallback) {
// this.connect(options);
// }
// }

// private connect(options: {
// id: string,
// type?: TWS,
// callback?: () => void,
// msgCallback?: (data: IWebSocketData) => void
// }) {
// const websocketURL = `${window.location.protocol === "https:" ? "wss" : "ws"}://${window.location.host}/ws`;
// const ws = new WebSocket(`${websocketURL}?app=${Constants.SIYUAN_APPID}&id=${options.id}${options.type ? "&type=" + options.type : ""}`);
// ws.onopen = () => {
// if (options.callback) {
// options.callback.call(this);
// }
// const logElement = document.getElementById("errorLog");
// if (logElement) {
// // 内核中断后无法 catch fetch 请求错误,重连会导致无法执行 transactionsTimeout
// reloadSync(this.app, {upsertRootIDs: [], removeRootIDs: []});
// window.siyuan.dialogs.find(item => {
// if (item.element.id === "errorLog") {
// item.destroy();
// return true;
// }
// });
// }
// };
// ws.onmessage = (event) => {
// if (options.msgCallback) {
// const data = processMessage(JSON.parse(event.data));
// options.msgCallback.call(this, data);
// }
// };
// ws.onclose = (ev) => {
// if (0 <= ev.reason.indexOf("unauthenticated")) {
// return;
// }

// if (0 > ev.reason.indexOf("close websocket")) {
// console.warn("WebSocket is closed. Reconnect will be attempted in 3 second.", ev);
// setTimeout(() => {
// this.connect({
// id: options.id,
// type: options.type,
// msgCallback: options.msgCallback
// });
// }, 3000);
// }
// };
// ws.onerror = (err: Event & { target: { url: string, readyState: number } }) => {
// if (err.target.url.endsWith("&type=main") && err.target.readyState === 3) {
// kernelError();
// }
// };
// this.ws = ws;
// }

// public send(cmd: string, param: Record<string, unknown>, process = false) {
// if (!this.ws) { // Inbox 无 ws
// return;
// }
// this.reqId = process ? 0 : new Date().getTime();
// this.ws.send(JSON.stringify({
// cmd,
// reqId: this.reqId,
// param,
// // pushMode
// // 0: 所有应用所有会话广播
// // 1:自我应用会话单播
// // 2:非自我会话广播
// // 4:非自我应用所有会话广播
// // 5:单个应用内所有会话广播
// // 6:非自我应用主会话广播
// }));
// }
// }
export class Model {
public ws: WebSocket;
public ws: WebSocket | null = null;
public reqId: number;
/// #if !MOBILE
public parent: Tab;
Expand All @@ -18,28 +116,17 @@ export class Model {
/// #endif
public app: App;

constructor(options: {
app: App,
id: string,
type?: TWS,
callback?: () => void,
msgCallback?: (data: IWebSocketData) => void
}) {
constructor(options: ModelOptions) {
this.app = options.app;
if (options.msgCallback) {
this.connect(options);
}
}

private connect(options: {
id: string,
type?: TWS,
callback?: () => void,
msgCallback?: (data: IWebSocketData) => void
}) {
private connect(options: ModelOptions) {
const websocketURL = `${window.location.protocol === "https:" ? "wss" : "ws"}://${window.location.host}/ws`;
const ws = new WebSocket(`${websocketURL}?app=${Constants.SIYUAN_APPID}&id=${options.id}${options.type ? "&type=" + options.type : ""}`);
ws.onopen = () => {
this.ws = new WebSocket(`${websocketURL}?app=${Constants.SIYUAN_APPID}&id=${options.id}${options.type ? "&type=" + options.type : ""}`);
this.ws.onopen = () => {
if (options.callback) {
options.callback.call(this);
}
Expand All @@ -55,13 +142,13 @@ export class Model {
});
}
};
ws.onmessage = (event) => {
this.ws.onmessage = (event) => {
if (options.msgCallback) {
const data = processMessage(JSON.parse(event.data));
options.msgCallback.call(this, data);
}
};
ws.onclose = (ev) => {
this.ws.onclose = (ev) => {
if (0 <= ev.reason.indexOf("unauthenticated")) {
return;
}
Expand All @@ -72,17 +159,17 @@ export class Model {
this.connect({
id: options.id,
type: options.type,
msgCallback: options.msgCallback
msgCallback: options.msgCallback,
app: new App
});
}, 3000);
}
};
ws.onerror = (err: Event & { target: { url: string, readyState: number } }) => {
this.ws.onerror = (err: Event & { target: { url: string, readyState: number } }) => {
if (err.target.url.endsWith("&type=main") && err.target.readyState === 3) {
kernelError();
}
};
this.ws = ws;
}

public send(cmd: string, param: Record<string, unknown>, process = false) {
Expand All @@ -104,3 +191,11 @@ export class Model {
}));
}
}

interface ModelOptions {
app: App;
id: string;
type?: TWS;
callback?: () => void;
msgCallback?: (data: IWebSocketData) => void;
}
2 changes: 1 addition & 1 deletion docs/starlight/package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "starlight",
"type": "module",
"version": "0.33.1500",
"version": "0.33.1510",
"scripts": {
"test": "echo 'Test your sister day by day'",
"dev": "astro dev",
Expand Down

0 comments on commit 150622d

Please sign in to comment.