Skip to content

Commit 456308c

Browse files
committed
🎨 Unregister plugin event bus on unload #16910 ; expose registerCustomEventBus on window.siyuan
1 parent f3390e3 commit 456308c

File tree

27 files changed

+204
-188
lines changed

27 files changed

+204
-188
lines changed

app/src/asset/index.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import {setStorageVal, updateHotkeyTip} from "../protyle/util/compatibility";
1616
import {App} from "../index";
1717
import {clearOBG} from "../layout/dock/util";
1818
import {getDisplayName} from "../util/pathName";
19+
import {emitToEventBus} from "../plugin/EventBus";
1920

2021
export class Asset extends Model {
2122
public path: string;
@@ -35,9 +36,7 @@ export class Asset extends Model {
3536
this.element.addEventListener("click", (event) => {
3637
clearOBG();
3738
setPanelFocus(this.element.parentElement.parentElement);
38-
this.app.plugins.forEach(item => {
39-
item.eventBus.emit("click-pdf", {event});
40-
});
39+
emitToEventBus("click-pdf", {event});
4140
});
4241
if (typeof this.pdfId === "string") {
4342
this.getPdfId(() => {

app/src/boot/globalEvent/command/global.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -432,7 +432,7 @@ export const globalCommand = (command: string, app: App) => {
432432
setReadOnly(!window.siyuan.config.editor.readOnly);
433433
return true;
434434
case "lockScreen":
435-
lockScreen(app);
435+
lockScreen();
436436
return true;
437437
case "newFile":
438438
newFile({

app/src/boot/globalEvent/keydown.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1340,7 +1340,7 @@ export const windowKeyDown = (app: App, event: KeyboardEvent) => {
13401340
return;
13411341
}
13421342
if (matchHotKey(window.siyuan.config.keymap.general.lockScreen.custom, event)) {
1343-
lockScreen(app);
1343+
lockScreen();
13441344
event.preventDefault();
13451345
return;
13461346
}

app/src/card/openCard.ts

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ import {updateCardHV} from "./util";
2626
import {showMessage} from "../dialog/message";
2727
import {Menu} from "../plugin/Menu";
2828
import {transaction} from "../protyle/wysiwyg/transaction";
29+
import {emitToEventBus} from "../plugin/EventBus";
2930

3031
const genCardCount = (cardsData: ICardData, allIndex = 0) => {
3132
let newIndex = 0;
@@ -695,7 +696,7 @@ export const bindCardEvent = async (options: {
695696
element.previousElementSibling.textContent = currentCard.nextDues[btnIndex - 1];
696697
});
697698
actionElements[1].classList.remove("fn__none");
698-
emitEvent(options.app, currentCard, type);
699+
emitEvent(currentCard, type);
699700
return;
700701
}
701702
} else if (type === "-2") { // 上一步
@@ -708,7 +709,7 @@ export const bindCardEvent = async (options: {
708709
index,
709710
cardsData: options.cardsData
710711
});
711-
emitEvent(options.app, options.cardsData.cards[index + 1], type);
712+
emitEvent(options.cardsData.cards[index + 1], type);
712713
}
713714
return;
714715
}
@@ -737,7 +738,7 @@ export const bindCardEvent = async (options: {
737738
notebook: docId,
738739
reviewedCards: options.cardsData.cards
739740
}, async (result) => {
740-
emitEvent(options.app, options.cardsData.cards[index - 1], type);
741+
emitEvent(options.cardsData.cards[index - 1], type);
741742
index = 0;
742743
options.cardsData = result.data;
743744
for (let i = 0; i < options.app.plugins.length; i++) {
@@ -768,19 +769,17 @@ export const bindCardEvent = async (options: {
768769
index,
769770
cardsData: options.cardsData
770771
});
771-
emitEvent(options.app, options.cardsData.cards[index - 1], type);
772+
emitEvent(options.cardsData.cards[index - 1], type);
772773
});
773774
}
774775
});
775776
return editor;
776777
};
777778

778-
const emitEvent = (app: App, card: ICard, type: string) => {
779-
app.plugins.forEach(item => {
780-
item.eventBus.emit("click-flashcard-action", {
781-
type,
782-
card
783-
});
779+
const emitEvent = (card: ICard, type: string) => {
780+
emitToEventBus("click-flashcard-action", {
781+
type,
782+
card
784783
});
785784
};
786785

app/src/dialog/processSystem.ts

Lines changed: 12 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ import {hideAllElements, hideElements} from "../protyle/ui/hideElements";
2424
import {App} from "../index";
2525
import {saveScroll} from "../protyle/scroll/saveScroll";
2626
import {isInAndroid, isInHarmony, isInIOS, setStorageVal} from "../protyle/util/compatibility";
27-
import {Plugin} from "../plugin";
27+
import {emitToEventBus} from "../plugin/EventBus";
2828

2929
const updateTitle = (rootID: string, tab: Tab, protyle?: IProtyle) => {
3030
fetchPost("/api/block/getDocInfo", {
@@ -233,13 +233,11 @@ export const setDefRefCount = (data: {
233233
}
234234
};
235235

236-
export const lockScreen = (app: App) => {
236+
export const lockScreen = () => {
237237
if (window.siyuan.config.readonly) {
238238
return;
239239
}
240-
app.plugins.forEach(item => {
241-
item.eventBus.emit("lock-screen");
242-
});
240+
emitToEventBus("lock-screen");
243241
/// #if BROWSER
244242
fetchPost("/api/system/logoutAuth", {}, () => {
245243
redirectToCheckAuth();
@@ -551,7 +549,7 @@ export const downloadProgress = (data: { id: string, percent: number }) => {
551549
}
552550
};
553551

554-
export const processSync = (data?: IWebSocketData, plugins?: Plugin[]) => {
552+
export const processSync = (data?: IWebSocketData) => {
555553
/// #if MOBILE
556554
const menuSyncUseElement = document.querySelector("#menuSyncNow use");
557555
const barSyncUseElement = document.querySelector("#toolbarSync use");
@@ -578,6 +576,7 @@ export const processSync = (data?: IWebSocketData, plugins?: Plugin[]) => {
578576
} else if (data.code === 1) { // success
579577
menuSyncUseElement?.setAttribute("xlink:href", "#iconCloudSucc");
580578
barSyncUseElement.setAttribute("xlink:href", "#iconCloudSucc");
579+
document.getElementById("toolbarSync").classList.add("fn__none");
581580
}
582581
/// #else
583582
const iconElement = document.querySelector("#barSync");
@@ -607,13 +606,11 @@ export const processSync = (data?: IWebSocketData, plugins?: Plugin[]) => {
607606
useElement.setAttribute("xlink:href", "#iconCloudSucc");
608607
}
609608
/// #endif
610-
plugins.forEach((item) => {
611-
if (data.code === 0) {
612-
item.eventBus.emit("sync-start", data);
613-
} else if (data.code === 1) {
614-
item.eventBus.emit("sync-end", data);
615-
} else if (data.code === 2) {
616-
item.eventBus.emit("sync-fail", data);
617-
}
618-
});
609+
if (data.code === 0) {
610+
emitToEventBus("sync-start", data);
611+
} else if (data.code === 1) {
612+
emitToEventBus("sync-end", data);
613+
} else if (data.code === 2) {
614+
emitToEventBus("sync-fail", data);
615+
}
619616
};

app/src/editor/openLink.ts

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import {App} from "../index";
1313
import {fetchPost} from "../util/fetch";
1414
import {checkFold} from "../util/noRelyPCFunction";
1515
import {openMobileFileById} from "../mobile/editor";
16+
import {emitToEventBus} from "../plugin/EventBus";
1617

1718
export const processSYLink = (app: App, url: string) => {
1819
let urlObj: URL;
@@ -82,13 +83,11 @@ export const processSYLink = (app: App, url: string) => {
8283
ipcRenderer.send(Constants.SIYUAN_CMD, "show");
8384
/// #endif
8485
}
85-
app.plugins.forEach(plugin => {
86-
plugin.eventBus.emit("open-siyuan-url-block", {
87-
url,
88-
id,
89-
focus,
90-
exist: existResponse.data,
91-
});
86+
emitToEventBus("open-siyuan-url-block", {
87+
url,
88+
id,
89+
focus,
90+
exist: existResponse.data,
9291
});
9392
});
9493
return true;

app/src/editor/util.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ import {preventScroll} from "../protyle/scroll/preventScroll";
3636
import {clearOBG} from "../layout/dock/util";
3737
import {Model} from "../layout/Model";
3838
import {hideElements} from "../protyle/ui/hideElements";
39+
import {emitToEventBus} from "../plugin/EventBus";
3940

4041
export const openFileById = async (options: {
4142
app: App,
@@ -597,9 +598,7 @@ export const updatePanelByEditor = (options: {
597598
}
598599
}
599600
}
600-
options.protyle.app.plugins.forEach(item => {
601-
item.eventBus.emit("switch-protyle", {protyle: options.protyle});
602-
});
601+
emitToEventBus("switch-protyle", {protyle: options.protyle});
603602
}
604603
// 切换页签或关闭所有页签时,需更新对应的面板
605604
const models = getAllModels();

app/src/index.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ import {Tag} from "./layout/dock/Tag";
4141
import {updateControlAlt} from "./protyle/util/hotKey";
4242
import {updateAppearance} from "./config/util/updateAppearance";
4343
import {renderSnippet} from "./config/util/snippets";
44+
import {registerCustomEventBus, emitToEventBus} from "./plugin/EventBus";
4445

4546
export class App {
4647
public plugins: import("./plugin").Plugin[] = [];
@@ -65,14 +66,13 @@ export class App {
6566
closedTabs: [],
6667
ctrlIsPressed: false,
6768
altIsPressed: false,
69+
registerCustomEventBus,
6870
ws: new Model({
6971
app: this,
7072
id: genUUID(),
7173
type: "main",
7274
msgCallback: (data) => {
73-
this.plugins.forEach((plugin) => {
74-
plugin.eventBus.emit("ws-main", data);
75-
});
75+
emitToEventBus("ws-main", data);
7676
if (data) {
7777
switch (data.cmd) {
7878
case "setAppearance":
@@ -173,7 +173,7 @@ export class App {
173173
transactionError();
174174
break;
175175
case "syncing":
176-
processSync(data, this.plugins);
176+
processSync(data);
177177
break;
178178
case "backgroundtask":
179179
progressBackgroundTask(data.data.tasks);

app/src/layout/dock/Files.ts

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ import {ipcRenderer} from "electron";
2828
/// #endif
2929
import {hideTooltip, showTooltip} from "../../dialog/tooltip";
3030
import {selectOpenTab} from "./util";
31+
import {emitToEventBus} from "../../plugin/EventBus";
3132

3233
export class Files extends Model {
3334
public element: HTMLElement;
@@ -57,9 +58,7 @@ export class Files extends Model {
5758
break;
5859
case "mount":
5960
this.onMount(data);
60-
options.app.plugins.forEach((item) => {
61-
item.eventBus.emit("opened-notebook", data);
62-
});
61+
emitToEventBus("opened-notebook", data);
6362
break;
6463
case "createnotebook":
6564
setNoteBook((notebooks) => {
@@ -81,9 +80,7 @@ export class Files extends Model {
8180
break;
8281
case "unmount":
8382
this.onRemove(data);
84-
options.app.plugins.forEach((item) => {
85-
item.eventBus.emit("closed-notebook", data);
86-
});
83+
emitToEventBus("closed-notebook", data);
8784
break;
8885
case "removeDoc":
8986
this.onRemove(data);

app/src/menus/workspace.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -520,7 +520,7 @@ export const workspaceMenu = (app: App, rect: DOMRect) => {
520520
icon: "iconLock",
521521
accelerator: window.siyuan.config.keymap.general.lockScreen.custom,
522522
click: () => {
523-
lockScreen(app);
523+
lockScreen();
524524
}
525525
}).element);
526526
window.siyuan.menus.menu.append(new MenuItem({

0 commit comments

Comments
 (0)