Skip to content

An open-source, privacy-first, self-hosted knowledge workspace where humans and AI agents work together 开源、隐私优先、自托管的知识工作空间,让人与智能体在此协作

:bug: Synchronize marketplace package state across windows https://github.com/siyuan-note/siyuan/issues/20070

Daniel901d1bad39bb
diff --git a/app/src/config/bazaar.ts b/app/src/config/bazaar.ts
index e069a85b32..ef1ad7cd1a 100644
--- a/app/src/config/bazaar.ts
+++ b/app/src/config/bazaar.ts
@@ -128,6 +128,8 @@ export const bazaar = {
_bazaarRequestIDs: new Map<TBazaarType, number>(),
_updateState: "idle" as "idle" | "loading" | "loaded" | "error",
_updateRequestID: 0,
+ _downloadedRequest: undefined as Promise<void> | undefined,
+ _detailRequestIDs: new Map<string, number>(),
_localPackageUploading: false,
_pluginEnablePending: new Set<string>(),
_downloadedPluginsReady: false,
@@ -152,6 +154,8 @@ export const bazaar = {
});
this._mountGeneration++;
this._updateRequestID++;
+ this._downloadedRequest = undefined;
+ this._detailRequestIDs.clear();
this._pluginGlobalRequestID++;
this._downloadedPluginsReady = false;
this._pluginGlobalRequestPending = false;
@@ -191,6 +195,44 @@ export const bazaar = {
return this._isMountCurrent(mount) && (mount.requestID === undefined ||
this._bazaarRequestIDs.get(bazaarType) === mount.requestID);
},
+ async refreshPackages(types: TBazaarType[], app: App) {
+ if (!this.element?.isConnected || !window.siyuan.config.bazaar.trust) return;
+ const mount = this._captureMount();
+ const currentSide = this.element.querySelector("#configBazaarReadme.config__view--show .item__side");
+ const currentDetailKey = currentSide && this._getDetailKey(
+ currentSide.getAttribute("data-package-type") as TBazaarType, currentSide.getAttribute("data-name"));
+ // 清除隐藏详情的操作数据;当前详情保留到新请求成功,避免网络失败时丢失界面。
+ for (const key of this._data.details.keys()) {
+ if (key !== currentDetailKey && types.some(type => key.startsWith(type + ":"))) {
+ this._data.details.delete(key);
+ }
+ }
+ for (const [key, requestID] of this._detailRequestIDs) {
+ if (types.some(type => key.startsWith(type + ":"))) this._detailRequestIDs.set(key, requestID + 1);
+ }
+ // 等待当前列表请求结束,确保变更通知不会被加载标记吞掉。
+ while (this._downloadedRequest) {
+ await this._downloadedRequest;
+ if (!this._isMountCurrent(mount)) return;
+ }
+ const requests: Promise<void>[] = [];
+ types.forEach(type => {
+ const downloaded = this._genMyHTML(type, app, true, false);
+ if (downloaded) requests.push(downloaded);
+ const panel = this.element.querySelector(`#${BAZAAR_PACKAGE_CONFIG[type].panelID}`)?.parentElement;
+ if (panel?.getAttribute("data-init") === "true") {
+ requests.push(this._reloadBazaarType(type, true));
+ }
+ });
+ requests.push(this._checkUpdate(true, true));
+ await Promise.all(requests);
+ if (!this._isMountCurrent(mount) || !this.element.isConnected) return;
+ const side = this.element.querySelector("#configBazaarReadme.config__view--show .item__side");
+ const type = side?.getAttribute("data-package-type") as TBazaarType;
+ if (types.includes(type)) {
+ await this._refreshReadmeDetail(type, side.getAttribute("data-name"));
+ }
+ },
_syncPluginGlobalSwitch() {
const switchElement = this.element?.querySelector('[data-type="plugins-enable"]') as HTMLInputElement;
if (!switchElement) {
@@ -437,12 +479,15 @@ export const bazaar = {
},
_fetchPackageDetail(bazaarType: TBazaarType, packageName: string, callback: (detail: IBazaarPackageDetail) => void) {
const mount = bazaar._captureMount();
- fetchPost("/api/bazaar/getBazaarPackage", {
+ const key = this._getDetailKey(bazaarType, packageName);
+ const requestID = (this._detailRequestIDs.get(key) || 0) + 1;
+ this._detailRequestIDs.set(key, requestID);
+ return fetchPost("/api/bazaar/getBazaarPackage", {
packageType: bazaarType,
packageName,
frontend: getFrontend(),
}, response => {
- if (!bazaar._isMountCurrent(mount)) {
+ if (!bazaar._isMountCurrent(mount) || bazaar._detailRequestIDs.get(key) !== requestID) {
return;
}
if (response.code !== 0 || !response.data) {
@@ -452,7 +497,7 @@ export const bazaar = {
const detail = response.data;
bazaar._setPackageDetail(bazaarType, packageName, detail);
callback(detail);
- });
+ }, undefined, undefined, undefined, 30000);
},
_openBazaarAlternative(bazaarType: TBazaarType, packageName: string) {
const detail = bazaar._getPackageDetail(bazaarType, packageName);
@@ -645,18 +690,19 @@ ${primaryAction ? '<div class="fn__hr"></div>' : ""}
counterElement.classList.toggle("fn__none", count === 0);
counterElement.textContent = count.toString();
},
- _checkUpdate(force = false) {
+ _checkUpdate(force = false, preserveView = false): Promise<void> | undefined {
if (!force && ["loading", "loaded"].includes(this._updateState)) {
return;
}
- this._updateState = "loading";
+ const keepResults = preserveView && this._updateState === "loaded";
+ if (!keepResults) this._updateState = "loading";
this._syncUpdateTabCounter();
const requestID = ++this._updateRequestID;
const mount = this._captureMount();
- if (this._isUpdatePanelActive()) {
+ if (!keepResults && this._isUpdatePanelActive()) {
this._renderUpdatePanel();
}
- fetchPost("/api/bazaar/getUpdatedPackage", {frontend: getFrontend()}, (response) => {
+ return fetchPost("/api/bazaar/getUpdatedPackage", {frontend: getFrontend()}, (response) => {
if (requestID !== this._updateRequestID || !this._isMountCurrent(mount) || !this.element?.isConnected) {
return;
}
@@ -673,9 +719,12 @@ ${primaryAction ? '<div class="fn__hr"></div>' : ""}
this._syncUpdateTabCounter();
this._syncDownloadedUpdateButtons();
if (this._isUpdatePanelActive()) {
+ const content = this.element.querySelector("#configBazaarDownloaded");
+ const scrollTop = content.scrollTop;
this._renderUpdatePanel();
+ if (preserveView) content.scrollTop = scrollTop;
}
- });
+ }, undefined, undefined, undefined, 30000);
},
_renderUpdatePanel() {
const contentElement = bazaar.element.querySelector("#configBazaarDownloaded");
@@ -776,7 +825,7 @@ ${primaryAction ? '<div class="fn__hr"></div>' : ""}
}
});
},
- _genMyHTML(bazaarType: TBazaarType, app: App, preserveOrder = false) {
+ _genMyHTML(bazaarType: TBazaarType, app: App, preserveOrder = false, refreshDetail = true): Promise<void> | false {
const contentElement = bazaar.element.querySelector("#configBazaarDownloaded");
const config = BAZAAR_PACKAGE_CONFIG[bazaarType];
const myType = config.myType;
@@ -794,7 +843,7 @@ ${primaryAction ? '<div class="fn__hr"></div>' : ""}
bazaar._updateDownloadedSortSelect(bazaarType);
const initialSortValue = bazaar._getDownloadedSortValue(bazaarType);
const mount = bazaar._captureMount();
- fetchPost(config.api.installed, {
+ const request = fetchPost(config.api.installed, {
frontend: getFrontend(),
keyword: (contentElement.previousElementSibling.querySelector(".b3-text-field") as HTMLInputElement)?.value || "",
}, response => {
@@ -887,7 +936,9 @@ type="checkbox">
bazaar._data.downloadedDefault = packageItems;
bazaar._data.downloaded = packages;
bazaar._data.downloadedType = bazaarType;
+ const scrollTop = contentElement.scrollTop;
contentElement.innerHTML = html ? html : `<ul class="b3-list b3-list--background"><li class="b3-list--empty">${window.siyuan.languages.emptyContent}</li></ul>`;
+ if (preserveOrder) contentElement.scrollTop = scrollTop;
if (bazaarType === "plugins") {
bazaar._downloadedPluginsReady = true;
bazaar._syncPluginGlobalSwitch();
@@ -897,7 +948,7 @@ type="checkbox">
bazaar._loadDownloadedDeprecations(bazaarType, app);
const sideElement = bazaar.element.querySelector("#configBazaarReadme.config__view--show .item__side");
// 仅刷新「已下载」详情,避免通过 URI 打开的在线详情被本地数据覆盖
- if (sideElement?.getAttribute("data-from") === "downloaded" &&
+ if (refreshDetail && sideElement?.getAttribute("data-from") === "downloaded" &&
sideElement.getAttribute("data-package-type") === bazaarType) {
const packageName = sideElement.getAttribute("data-name");
const downloadedItem = bazaar._data.downloaded.find((item) => item.name === packageName);
@@ -911,9 +962,13 @@ type="checkbox">
}
}
}, undefined, undefined, undefined, 30000).finally(() => {
- contentElement.removeAttribute("data-loading");
+ if (bazaar._downloadedRequest === request) {
+ contentElement.removeAttribute("data-loading");
+ bazaar._downloadedRequest = undefined;
+ }
});
- return true;
+ bazaar._downloadedRequest = request;
+ return request;
},
_data: {
themes: [] as IBazaarItem[],
@@ -966,14 +1021,15 @@ type="checkbox">
upsert(bazaar._data[bazaarType]);
}
},
- _renderReadme(bazaarType: TBazaarType, from: "downloaded" | "updated" | "bazaar", data: IBazaarItem, detail?: IBazaarPackageDetail) {
+ _renderReadme(bazaarType: TBazaarType, from: "downloaded" | "updated" | "bazaar", data: IBazaarItem, detail?: IBazaarPackageDetail, preserveView = false) {
const mount = bazaar._captureMount();
const readmeElement = bazaar.element.querySelector("#configBazaarReadme") as HTMLElement;
+ const previousScrollTop = preserveView ? readmeElement.querySelector(".item__main")?.scrollTop || 0 : 0;
const navTitle = window.siyuan.languages[BAZAAR_PACKAGE_CONFIG[bazaarType].languageKey];
bazaar._upsertReadmeData(bazaarType, from, data);
const updatedDetail = from === "updated" ? bazaar._getUpdatedItem(bazaarType, data.name) : undefined;
- const installed = detail?.installed || updatedDetail?.installed || (from === "downloaded" ? data : undefined);
- const available = detail?.available || updatedDetail?.available ||
+ const installed = detail ? detail.installed : updatedDetail?.installed || (from === "downloaded" ? data : undefined);
+ const available = detail ? detail.available : updatedDetail?.available ||
(from === "downloaded" ? bazaar._getUpdatedItem(bazaarType, data.name)?.available : data);
const displayData = from === "downloaded" ? installed || data : available || data;
const ratingKey = getRatingKey(bazaarType, displayData.name);
@@ -1124,17 +1180,19 @@ type="checkbox">
previewImage.src = displayData.previewURL;
}
const isInstalledReadme = from === "downloaded";
+ const mainElement = readmeElement.querySelector(".item__main");
if (isInstalledReadme) {
const mdElement = readmeElement.querySelector(".item__readme");
mdElement.innerHTML = window.DOMPurify.sanitize(displayData.preferredReadme || "", BAZAAR_README_SANITIZE_OPTIONS);
highlightRender(mdElement);
+ if (preserveView) mainElement.scrollTop = previousScrollTop;
} else {
fetchPost("/api/bazaar/getBazaarPackageREADME", {
repoURL: displayData.repoURL,
repoHash: displayData.repoHash,
packageType: bazaarType
}, response => {
- if (!bazaar._isMountCurrent(mount)) {
+ if (!bazaar._isMountCurrent(mount) || readmeElement.querySelector(".item__main") !== mainElement) {
return;
}
const sideElement = readmeElement.querySelector(".item__side");
@@ -1146,6 +1204,7 @@ type="checkbox">
const mdElement = readmeElement.querySelector(".item__readme");
mdElement.innerHTML = window.DOMPurify.sanitize(response.data.html, BAZAAR_README_SANITIZE_OPTIONS);
highlightRender(mdElement);
+ if (preserveView) mainElement.scrollTop = previousScrollTop;
});
}
const needsPackageDetail = !detail && (from === "downloaded" || (from === "bazaar" && data.installed));
@@ -1300,30 +1359,37 @@ type="checkbox">
if (!fallback) {
return;
}
- bazaar._fetchPackageDetail(bazaarType, packageName, (detail) => {
+ return bazaar._fetchPackageDetail(bazaarType, packageName, (detail) => {
const currentSideElement = bazaar.element.querySelector("#configBazaarReadme.config__view--show .item__side");
if (currentSideElement?.getAttribute("data-from") !== from ||
currentSideElement.getAttribute("data-package-type") !== bazaarType ||
currentSideElement.getAttribute("data-name") !== packageName) {
return;
}
- const refreshedData = from === "downloaded" ? detail.installed || fallback : detail.available || fallback;
- bazaar._renderReadme(bazaarType, from, refreshedData, detail);
+ const readme = currentSideElement.closest("#configBazaarReadme");
+ if (!detail.installed && !detail.available) {
+ readme.classList.remove("config__view--show");
+ return;
+ }
+ const scrollTop = readme.scrollTop;
+ const refreshedData = from === "downloaded" ? detail.installed || detail.available : detail.available || detail.installed;
+ bazaar._renderReadme(bazaarType, !detail.installed ? "bazaar" : from, refreshedData, detail, true);
+ readme.scrollTop = scrollTop;
});
},
- _reloadBazaarType(bazaarType: TBazaarType) {
+ _reloadBazaarType(bazaarType: TBazaarType, preserveView = false) {
const config = BAZAAR_PACKAGE_CONFIG[bazaarType];
const mount = bazaar._beginBazaarRequest(bazaarType);
const keyword = (mount.element.querySelector(
`.config-bazaar__panel[data-type="${config.tabType}"] .b3-text-field`) as HTMLInputElement)?.value.trim() || "";
- fetchPost(config.api.bazaar, {
+ return fetchPost(config.api.bazaar, {
...(config.bazaarRequestUsesFrontend ? {frontend: getFrontend()} : {}),
keyword,
}, response => {
if (response.code === 0) {
- bazaar._onBazaar(response, bazaarType, mount);
+ bazaar._onBazaar(response, bazaarType, mount, preserveView);
}
- });
+ }, undefined, undefined, undefined, 30000);
},
_refreshPackageUI(bazaarType: TBazaarType, packageName: string, app: App) {
const sideElement = bazaar.element.querySelector("#configBazaarReadme.config__view--show .item__side");
@@ -1843,7 +1909,7 @@ type="checkbox">
this._bazaarCardRenderStates.set(container, state);
this._watchBazaarCardBatch(state);
},
- _onBazaar(response: IWebSocketData, bazaarType: TBazaarType, mount: IBazaarMountSnapshot) {
+ _onBazaar(response: IWebSocketData, bazaarType: TBazaarType, mount: IBazaarMountSnapshot, preserveView = false) {
if (!bazaar._isBazaarRequestCurrent(bazaarType, mount)) {
return;
}
@@ -1863,7 +1929,9 @@ type="checkbox">
bazaar._cacheBazaarDeprecations(bazaarType, response.data.packages);
const sortValue = window.siyuan.storage[Constants.LOCAL_BAZAAR][BAZAAR_PACKAGE_CONFIG[bazaarType].tabType];
const packages = sortValue && sortValue !== "0" ? sortBazaarPackages(response.data.packages, sortValue) : response.data.packages;
+ const scrollTop = element.scrollTop;
bazaar._renderBazaarCards(element, packages, bazaarType,
bazaarType === "themes" ? (bazaar.element.querySelector("#bazaarSelect") as HTMLSelectElement)?.value : undefined);
+ if (preserveView) element.scrollTop = scrollTop;
}
};
diff --git a/app/src/config/bazaarTab.ts b/app/src/config/bazaarTab.ts
index cdb70b4bbc..fe20de6469 100644
--- a/app/src/config/bazaarTab.ts
+++ b/app/src/config/bazaarTab.ts
@@ -1,10 +1,39 @@
import type {App} from "../index";
+import {BAZAAR_PACKAGE_TYPES, isBazaarPackageType} from "./bazaar/packageConfig";
type BazaarModule = typeof import("./bazaar");
let bazaarModulePromise: Promise<BazaarModule> | undefined;
-let activeMountState: {root: HTMLElement; token: symbol} | undefined;
+let activeMountState: {root: HTMLElement; token: symbol; app?: App} | undefined;
let activeMountPromise: Promise<BazaarModule> | undefined;
+const pendingPackageTypes = new Set<TBazaarType>();
+let refreshingPackages: Promise<void> | undefined;
+
+/** 合并变更通知,读取期间的新通知在下一轮处理,未打开集市时不加载模块。 */
+export const refreshMountedBazaar = (types: readonly TBazaarType[] = BAZAAR_PACKAGE_TYPES): Promise<void> => {
+ if (!activeMountState) return Promise.resolve();
+ types.filter(isBazaarPackageType).forEach(type => pendingPackageTypes.add(type));
+ if (refreshingPackages) return refreshingPackages;
+ refreshingPackages = Promise.resolve().then(async () => {
+ while (pendingPackageTypes.size) {
+ const types = [...pendingPackageTypes];
+ pendingPackageTypes.clear();
+ const state = activeMountState;
+ if (!state) continue;
+ const module = await activeMountPromise;
+ if (activeMountState !== state) {
+ if (activeMountState) types.forEach(type => pendingPackageTypes.add(type));
+ continue;
+ }
+ if (module.bazaar.element !== state.root || !state.root.isConnected) continue;
+ await module.bazaar.refreshPackages(types, state.app);
+ }
+ }).catch(error => console.error("Could not refresh marketplace packages", error)).finally(() => {
+ refreshingPackages = undefined;
+ if (pendingPackageTypes.size) void refreshMountedBazaar([]);
+ });
+ return refreshingPackages;
+};
const loadBazaarModule = () => {
if (!bazaarModulePromise) {
@@ -31,7 +60,7 @@ export const mountBazaarTab = (
keywords?: string,
app?: App,
): Promise<void> => {
- const state = {root, token: Symbol()};
+ const state = {root, token: Symbol(), app};
activeMountState = state;
const mountPromise = loadBazaarModule().then((module) => {
if (activeMountState === state) {
diff --git a/app/src/config/setting/sync.test.ts b/app/src/config/setting/sync.test.ts
index 85c0364cb2..83b72ade27 100644
--- a/app/src/config/setting/sync.test.ts
+++ b/app/src/config/setting/sync.test.ts
@@ -26,6 +26,30 @@ test("settings notifications update runtime configuration and refresh the regist
});
for (const mobile of [false, true]) {
+ test(`marketplace configuration and reconnection refresh mounted packages (mobile=${mobile})`, async () => {
+ const {parse} = require("ifdef-loader/preprocessor");
+ const compiled = transpileModule(parse(readFileSync("src/config/setting/sync.ts", "utf8"),
+ {MOBILE: mobile, BROWSER: mobile}, false, true), {
+ compilerOptions: {module: ModuleKind.CommonJS, target: ScriptTarget.ES2021},
+ }).outputText;
+ const config = {bazaar: {trust: true}, editor: {}, appearance: {}, keymap: {}};
+ let refreshed = 0;
+ const exports = {} as {refreshSettingConfig: (namespace?: string) => Promise<void>};
+ runInNewContext(compiled, {exports, window: {siyuan: {config}}, console, require: () => ({
+ fetchSyncPost: async () => ({code: 0, data: {conf: config}}),
+ systemConfig: (value: unknown) => value,
+ objEquals: (left: unknown, right: unknown) => JSON.stringify(left) === JSON.stringify(right),
+ syncSettingTasks() {}, getSettingTabDefs: (): {id: string}[] => [],
+ refreshMountedBazaar: () => { refreshed++; },
+ })});
+ await exports.refreshSettingConfig("editor");
+ assert.equal(refreshed, 0);
+ await exports.refreshSettingConfig("bazaar");
+ assert.equal(refreshed, 1);
+ await exports.refreshSettingConfig();
+ assert.equal(refreshed, 2);
+ });
+
test(`OCR notifications preserve the settings page and unrelated runtime configuration (mobile=${mobile})`, async () => {
const {parse} = require("ifdef-loader/preprocessor");
const compiled = transpileModule(parse(readFileSync("src/config/setting/sync.ts", "utf8"),
@@ -110,7 +134,7 @@ for (const mobile of [false, true]) {
fetchSyncPost: async () => ({code: 0, data: {conf: JSON.parse(JSON.stringify({...config, ai: next}))}}),
systemConfig: (value: unknown) => value,
objEquals: (left: unknown, right: unknown) => JSON.stringify(left) === JSON.stringify(right),
- ...aiExports, syncSettingTasks() {}, remountOpenSettingTab: async () => {}, getSettingTabDefs: () => [{id: "ai"}],
+ ...aiExports, syncSettingTasks() {}, refreshMountedBazaar() {}, remountOpenSettingTab: async () => {}, getSettingTabDefs: () => [{id: "ai"}],
};
const exports = {} as {refreshSettingConfig: (namespace?: string) => Promise<void>};
runInNewContext(compile("src/config/setting/sync.ts"), {exports, require: () => dependencies, window: windowContext, console});
@@ -148,7 +172,7 @@ for (const mobile of [false, true]) {
fetchSyncPost: async () => failed ? {code: -1} : {code: 0, data: {conf: JSON.parse(JSON.stringify(next))}},
systemConfig: (value: unknown) => value,
objEquals: (left: unknown, right: unknown) => JSON.stringify(left) === JSON.stringify(right),
- syncSettingTasks() {}, processSync() {}, getSettingTabDefs: () => [{id: "app"}, {id: "access"}],
+ syncSettingTasks() {}, refreshMountedBazaar() {}, processSync() {}, getSettingTabDefs: () => [{id: "app"}, {id: "access"}],
remountOpenSettingTab: async (tab: string) => { remounted.push(tab); },
})});
await exports.refreshSettingConfig("system");
diff --git a/app/src/config/setting/sync.ts b/app/src/config/setting/sync.ts
index 8dee6688d5..02f5ad3ee4 100644
--- a/app/src/config/setting/sync.ts
+++ b/app/src/config/setting/sync.ts
@@ -7,6 +7,7 @@ import {objEquals} from "../../util/functions";
import {syncSettingTasks} from "./taskBlocker";
import {processSync} from "../../dialog/processSystem";
import {notifyOCRChanged} from "../ocrRuntime";
+import {refreshMountedBazaar} from "../bazaarTab";
/// #if !MOBILE
import {applyKeymap} from "../tabs/keymapRuntime";
import {remountOpenSettingTab} from "./mount";
@@ -72,6 +73,9 @@ export const refreshSettingConfig = (namespace = "*"): Promise<void> => {
if (includes("ocr") && next.ocr) {
notifyOCRChanged();
}
+ if (includes("bazaar")) {
+ void refreshMountedBazaar();
+ }
/// #if !MOBILE
const tabs: Record<string, TSettingTab> = {fileTree: "file", secrets: "secretsVariables", variables: "secretsVariables", system: "app", publish: "access"};
const remountTabs = new Set<TSettingTab>();
diff --git a/app/src/layout/Model.ts b/app/src/layout/Model.ts
index 06475a4f53..c2afa66ed5 100644
--- a/app/src/layout/Model.ts
+++ b/app/src/layout/Model.ts
@@ -40,7 +40,12 @@ export class Model {
private processWebSocketMessage(data: string, callback: (data: IWebSocketData) => void) {
// 消息处理依赖面板子类,调用时加载以避免基类初始化期间形成循环依赖。
const {processMessage}: typeof import("../util/processMessage") = require("../util/processMessage");
- callback.call(this, processMessage(JSON.parse(data)));
+ const message = processMessage(JSON.parse(data));
+ if (message && message.cmd === "bazaarChanged") {
+ const {refreshMountedBazaar}: typeof import("../config/bazaarTab") = require("../config/bazaarTab");
+ void refreshMountedBazaar(message.data);
+ }
+ callback.call(this, message);
}
public flushMainMessages() {
diff --git a/app/tests/bazaarSync.test.js b/app/tests/bazaarSync.test.js
new file mode 100644
index 0000000000..61f64849f4
--- /dev/null
+++ b/app/tests/bazaarSync.test.js
@@ -0,0 +1,241 @@
+const assert = require("node:assert/strict");
+const {readFileSync} = require("node:fs");
+const {test} = require("node:test");
+const {runInNewContext} = require("node:vm");
+const ts = require("typescript");
+const config = require("../src/config/bazaar/packageConfig.ts");
+
+const compile = source => ts.transpileModule(source, {
+ compilerOptions: {module: ts.ModuleKind.CommonJS, target: ts.ScriptTarget.ES2021},
+}).outputText;
+const deferred = () => {
+ let resolve;
+ const promise = new Promise(done => { resolve = done; });
+ return {promise, resolve};
+};
+const loadTab = (refreshPackages = async () => {}) => {
+ let loads = 0;
+ const module = {
+ bazaar: {refreshPackages},
+ mountBazaarTab(root) { this.bazaar.element = root; },
+ unmountBazaarTab() {},
+ };
+ const exports = {};
+ runInNewContext(compile(readFileSync("src/config/bazaarTab.ts", "utf8")), {
+ exports, console, require: path => {
+ if (path === "./bazaar") { loads++; return module; }
+ return config;
+ },
+ });
+ return {api: exports, loads: () => loads};
+};
+
+test("marketplace notifications do not load an unopened marketplace", async () => {
+ const {api, loads} = loadTab();
+ await api.refreshMountedBazaar(["widgets"]);
+ assert.equal(loads(), 0);
+});
+
+test("marketplace refresh merges notifications and drains changes received during a request", async () => {
+ const started = deferred();
+ const blocked = deferred();
+ const calls = [];
+ const app = {};
+ const {api} = loadTab(async (types, owner) => {
+ assert.equal(owner, app);
+ calls.push(Array.from(types));
+ if (calls.length === 1) { started.resolve(); await blocked.promise; }
+ });
+ await api.mountBazaarTab({isConnected: true}, undefined, app);
+ const pending = api.refreshMountedBazaar(["widgets"]);
+ api.refreshMountedBazaar(["widgets", "templates"]);
+ await started.promise;
+ api.refreshMountedBazaar(["icons"]);
+ api.refreshMountedBazaar(["icons", "plugins"]);
+ assert.equal(calls.length, 1);
+ blocked.resolve();
+ await pending;
+ assert.deepEqual(calls, [["widgets", "templates"], ["icons", "plugins"]]);
+});
+
+test("queued marketplace refresh ignores an unmounted or detached page", async () => {
+ let refreshed = 0;
+ const {api} = loadTab(async () => { refreshed++; });
+ const root = {isConnected: true};
+ await api.mountBazaarTab(root);
+ const pending = api.refreshMountedBazaar(["themes"]);
+ api.unmountBazaarTab(root);
+ await pending;
+ assert.equal(refreshed, 0);
+ await api.mountBazaarTab({isConnected: false});
+ await api.refreshMountedBazaar();
+ assert.equal(refreshed, 0);
+});
+
+test("a settings remount during notification delivery does not lose the refresh", async () => {
+ const calls = [];
+ const {api} = loadTab(async types => { calls.push(Array.from(types)); });
+ const root = {isConnected: true};
+ await api.mountBazaarTab(root);
+ const pending = api.refreshMountedBazaar(["themes"]);
+ await Promise.resolve();
+ await api.mountBazaarTab(root);
+ await pending;
+ assert.deepEqual(calls, [["themes"]]);
+});
+
+const source = ts.createSourceFile("bazaar.ts", readFileSync("src/config/bazaar.ts", "utf8"), ts.ScriptTarget.ES2021, true);
+const declaration = source.statements.find(statement => ts.isVariableStatement(statement) &&
+ statement.declarationList.declarations.some(item => item.name.getText(source) === "bazaar"));
+const object = declaration.declarationList.declarations[0].initializer;
+const loadMethods = (names, dependencies = {}) => {
+ const methods = names.map(name => object.properties.find(property => property.name?.getText(source) === name).getText(source));
+ const exports = {};
+ runInNewContext(compile(`export const bazaar = {${methods.join(",")}};`), {
+ exports, BAZAAR_PACKAGE_CONFIG: config.BAZAAR_PACKAGE_CONFIG,
+ window: {siyuan: {config: {bazaar: {trust: true}}}}, ...dependencies,
+ });
+ return exports.bazaar;
+};
+
+test("package refresh waits for an installed-list request and preserves the selected view", async () => {
+ const blocked = deferred();
+ const calls = [];
+ const bazaar = loadMethods(["refreshPackages"]);
+ Object.assign(bazaar, {
+ element: {isConnected: true, querySelector: selector => selector === "#configBazaarWidget" ?
+ {parentElement: {getAttribute: () => "true"}} : null},
+ _captureMount: () => 1, _isMountCurrent: () => true,
+ _data: {details: new Map([["widgets:old", {}], ["themes:unrelated", {}]])},
+ _detailRequestIDs: new Map([["widgets:old", 1]]),
+ _downloadedRequest: blocked.promise,
+ _genMyHTML: (type, app, order, detail) => { calls.push(["installed", type, order, detail]); return Promise.resolve(); },
+ _reloadBazaarType: (type, preserve) => { calls.push(["online", type, preserve]); return Promise.resolve(); },
+ _checkUpdate: (force, preserve) => { calls.push(["updates", force, preserve]); return Promise.resolve(); },
+ });
+ const pending = bazaar.refreshPackages(["widgets"], {});
+ assert.deepEqual(calls, []);
+ assert.equal(bazaar._data.details.has("widgets:old"), false);
+ assert.equal(bazaar._data.details.has("themes:unrelated"), true);
+ assert.equal(bazaar._detailRequestIDs.get("widgets:old"), 2);
+ bazaar._downloadedRequest = undefined;
+ blocked.resolve();
+ await pending;
+ assert.deepEqual(calls, [["installed", "widgets", true, false], ["online", "widgets", true], ["updates", true, true]]);
+});
+
+test("package refresh stops when the page is replaced while awaiting an older request", async () => {
+ const blocked = deferred();
+ const bazaar = loadMethods(["refreshPackages"]);
+ Object.assign(bazaar, {
+ element: {isConnected: true, querySelector: () => null}, _captureMount: () => 1, _isMountCurrent: () => false,
+ _data: {details: new Map()}, _detailRequestIDs: new Map(),
+ _downloadedRequest: blocked.promise, _genMyHTML: () => assert.fail("stale page refreshed"),
+ });
+ const pending = bazaar.refreshPackages(["widgets"], {});
+ blocked.resolve();
+ await pending;
+});
+
+test("installed refresh preserves search and scroll, and an old request cannot clear a new loading flag", async () => {
+ const requests = [];
+ const bazaar = loadMethods(["_genMyHTML"], {
+ getFrontend: () => "desktop",
+ fetchPost: (_url, data, callback) => {
+ const pending = deferred();
+ requests.push({data, callback, ...pending});
+ return pending.promise;
+ },
+ loadDownloadedRatings() {}, loadDownloadedUserRatings() {},
+ window: {siyuan: {languages: {emptyContent: "Empty"}}},
+ });
+ let generation = 1;
+ let loading = false;
+ const button = {classList: {contains: () => false}, getAttribute: () => "myWidget"};
+ const content = {
+ scrollTop: 160,
+ set innerHTML(_value) { this.scrollTop = 0; },
+ getAttribute: () => loading ? "true" : null,
+ setAttribute: () => { loading = true; }, removeAttribute: () => { loading = false; },
+ previousElementSibling: {querySelector: selector => selector === ".b3-text-field" ? {value: "search term"} :
+ selector === ".counter" ? {classList: {add() {}}} : button},
+ };
+ Object.assign(bazaar, {
+ element: {querySelector: selector => selector === "#configBazaarDownloaded" ? content : null},
+ _data: {}, _captureMount: () => generation, _isMountCurrent: value => value === generation,
+ _updateDownloadedToolbar() {}, _updateDownloadedSortSelect() {}, _getDownloadedSortValue: () => "2",
+ _applyDownloadedDeprecations() {}, _preserveDownloadedOrder: data => data, _loadDownloadedDeprecations() {},
+ });
+ const old = bazaar._genMyHTML("widgets", {}, true);
+ generation++;
+ loading = false;
+ const current = bazaar._genMyHTML("widgets", {}, true);
+ requests[0].callback({data: {packages: []}});
+ requests[0].resolve();
+ await old;
+ assert.equal(loading, true);
+ assert.equal(requests[1].data.keyword, "search term");
+ requests[1].callback({data: {packages: []}});
+ requests[1].resolve();
+ await current;
+ assert.equal(loading, false);
+ assert.equal(content.scrollTop, 160);
+});
+
+test("package detail ignores responses superseded by a cross-window refresh", async () => {
+ const callbacks = [];
+ const bazaar = loadMethods(["_fetchPackageDetail"], {
+ getFrontend: () => "desktop",
+ fetchPost: (_url, _data, callback) => { callbacks.push(callback); return Promise.resolve(); },
+ });
+ let saved;
+ Object.assign(bazaar, {
+ _captureMount: () => 1, _isMountCurrent: () => true, _getDetailKey: () => "widgets:sample",
+ _detailRequestIDs: new Map(), _setPackageDetail: (_type, _name, detail) => { saved = detail; },
+ });
+ const received = [];
+ await bazaar._fetchPackageDetail("widgets", "sample", detail => received.push(detail));
+ await bazaar._fetchPackageDetail("widgets", "sample", detail => received.push(detail));
+ const latest = {installed: null, available: {name: "sample"}};
+ callbacks[1]({code: 0, data: latest});
+ callbacks[0]({code: 0, data: {installed: {name: "sample"}}});
+ assert.equal(saved, latest);
+ assert.deepEqual(received, [latest]);
+});
+
+for (const available of [undefined, {name: "sample"}]) {
+ test(`uninstalled package detail ${available ? "switches to online actions" : "closes for a local-only package"}`, async () => {
+ let closed = false;
+ let rendered;
+ const readme = {scrollTop: 125, classList: {remove: () => { closed = true; }}};
+ const side = {getAttribute: name => ({"data-from": "downloaded", "data-package-type": "widgets", "data-name": "sample"})[name],
+ closest: () => readme};
+ const bazaar = loadMethods(["_refreshReadmeDetail"]);
+ Object.assign(bazaar, {
+ element: {querySelector: () => side}, _getPackageDetail: () => ({installed: {name: "sample"}}),
+ _fetchPackageDetail: async (_type, _name, callback) => callback({installed: null, available}),
+ _renderReadme: (...args) => { rendered = args; readme.scrollTop = 0; },
+ });
+ await bazaar._refreshReadmeDetail("widgets", "sample");
+ assert.equal(closed, !available);
+ if (available) {
+ assert.equal(rendered[1], "bazaar");
+ assert.equal(rendered[3].installed, null);
+ assert.equal(rendered[4], true);
+ assert.equal(readme.scrollTop, 125);
+ }
+ });
+}
+
+test("shared WebSocket dispatcher refreshes marketplace state before forwarding the notification", () => {
+ const model = ts.createSourceFile("Model.ts", readFileSync("src/layout/Model.ts", "utf8"), ts.ScriptTarget.ES2021, true);
+ const declaration = model.statements.find(ts.isClassDeclaration);
+ const method = declaration.members.find(member => member.name?.getText(model) === "processWebSocketMessage");
+ const calls = [];
+ const exports = {};
+ runInNewContext(compile(`export class Model {${method.getText(model)}}`), {exports, require: path =>
+ path.includes("processMessage") ? {processMessage: data => data} :
+ {refreshMountedBazaar: types => { calls.push(Array.from(types)); return Promise.resolve(); }}});
+ new exports.Model().processWebSocketMessage(JSON.stringify({cmd: "bazaarChanged", data: ["templates"]}), message => calls.push(message.cmd));
+ assert.deepEqual(calls, [["templates"], "bazaarChanged"]);
+});
diff --git a/kernel/model/bazaar.go b/kernel/model/bazaar.go
index afe90a55a2..34758b6688 100644
--- a/kernel/model/bazaar.go
+++ b/kernel/model/bazaar.go
@@ -628,6 +628,12 @@ func finishInstall(pkgType string, items []batchInstallItem, themeOptions *Theme
}
refreshAppearancePackages(nil, names)
}
+ pushBazaarChanged(pkgType)
+}
+
+// 集市包落盘并完成运行时处理后,通知所有前端更新本地安装状态。
+var pushBazaarChanged = func(pkgType string) {
+ util.BroadcastByType("main", "bazaarChanged", 0, "", []string{pkgType})
}
// InstallBazaarPackage 安装集市包,themeOptions 仅在 pkgType 为 "themes" 时生效
@@ -769,6 +775,7 @@ func UninstallPackage(pkgType, packageName string) error {
case "icons":
refreshAppearancePackages(nil, []string{packageName})
}
+ pushBazaarChanged(pkgType)
return nil
}
diff --git a/kernel/model/bazaar_sync_test.go b/kernel/model/bazaar_sync_test.go
new file mode 100644
index 0000000000..96eea68674
--- /dev/null
+++ b/kernel/model/bazaar_sync_test.go
@@ -0,0 +1,73 @@
+package model
+
+import (
+ "os"
+ "path/filepath"
+ "testing"
+
+ "github.com/siyuan-note/siyuan/kernel/util"
+)
+
+func captureBazaarChanges(t *testing.T) *[]string {
+ t.Helper()
+ previous := pushBazaarChanged
+ var changes []string
+ pushBazaarChanged = func(pkgType string) { changes = append(changes, pkgType) }
+ t.Cleanup(func() { pushBazaarChanged = previous })
+ return &changes
+}
+
+func TestBazaarChangedAfterInstall(t *testing.T) {
+ setupSyncMutationTest(t)
+ setupAppearancePackagesTest(t)
+ t.Cleanup(CloseWatchThemes)
+ changes := captureBazaarChanges(t)
+ for _, pkgType := range []string{"plugins", "themes", "icons", "widgets", "templates"} {
+ t.Run(pkgType, func(t *testing.T) {
+ *changes = nil
+ finishInstall(pkgType, nil, nil, false)
+ if len(*changes) != 0 {
+ t.Fatal("empty or entirely failed batch must not notify")
+ }
+ finishInstall(pkgType, []batchInstallItem{{name: "sample"}}, nil, false)
+ if len(*changes) != 1 || (*changes)[0] != pkgType {
+ t.Fatalf("new installation notification = %v", *changes)
+ }
+ *changes = nil
+ finishInstall(pkgType, []batchInstallItem{
+ {name: "sample", meta: installMeta{update: true}},
+ {name: "other", meta: installMeta{update: true}},
+ }, nil, false)
+ if len(*changes) != 1 || (*changes)[0] != pkgType {
+ t.Fatalf("batch update notification = %v", *changes)
+ }
+ })
+ }
+}
+
+func TestBazaarChangedAfterUninstall(t *testing.T) {
+ setupSyncMutationTest(t)
+ changes := captureBazaarChanges(t)
+ packagePath := filepath.Join(util.DataDir, "widgets", "sample")
+ if err := os.MkdirAll(packagePath, 0755); err != nil {
+ t.Fatal(err)
+ }
+ if err := os.WriteFile(filepath.Join(packagePath, "widget.json"), []byte(`{"name":"sample","version":"1.0.0"}`), 0644); err != nil {
+ t.Fatal(err)
+ }
+ if err := UninstallPackage("widgets", "sample"); err != nil {
+ t.Fatal(err)
+ }
+ if len(*changes) != 1 || (*changes)[0] != "widgets" {
+ t.Fatalf("uninstall notification = %v", *changes)
+ }
+ if _, err := os.Stat(packagePath); !os.IsNotExist(err) {
+ t.Fatalf("package still exists after uninstall: %v", err)
+ }
+ if err := UninstallPackage("widgets", "sample"); err == nil {
+ t.Fatal("expected missing package error")
+ }
+ if len(*changes) != 1 {
+ t.Fatal("failed uninstall must not notify")
+ }
+}
diff --git a/kernel/model/bazaar_test.go b/kernel/model/bazaar_test.go
index 33b7a572e9..349cf7fe6e 100644
--- a/kernel/model/bazaar_test.go
+++ b/kernel/model/bazaar_test.go
@@ -295,6 +295,7 @@ func TestGetPackageUninstallPathUsesInvalidPackageDirectory(t *testing.T) {
func TestInstallLocalBazaarPackageReplacesEmptyDirectoryTree(t *testing.T) {
setupSyncMutationTest(t)
+ changes := captureBazaarChanges(t)
oldDataDir, oldTempDir := util.DataDir, util.TempDir
root := t.TempDir()
util.DataDir = filepath.Join(root, "data")
@@ -342,6 +343,21 @@ func TestInstallLocalBazaarPackageReplacesEmptyDirectoryTree(t *testing.T) {
if _, err = os.Stat(filepath.Join(installPath, "plugin.json")); err != nil {
t.Fatalf("installed package manifest is missing: %s", err)
}
+ if len(*changes) != 1 || (*changes)[0] != "plugins" {
+ t.Fatalf("local installation notification = %v", *changes)
+ }
+ if _, err = InstallLocalBazaarPackage(archivePath, "", false); err == nil {
+ t.Fatal("expected existing package error")
+ }
+ if len(*changes) != 1 {
+ t.Fatal("rejected installation must not notify")
+ }
+ if _, err = InstallLocalBazaarPackage(archivePath, "", true); err != nil {
+ t.Fatal(err)
+ }
+ if len(*changes) != 2 {
+ t.Fatal("local overwrite must notify even when the plugin is disabled")
+ }
}
func TestFilterUpdatableBazaarPackages(t *testing.T) {