Repository navigation
fix: resolve all waiters for plugin loads #2955
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
569fb98
184a04c
798f2f2
84587fa
2174db4
05fcc35
6bea2a5
d772c6e
e08e971
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -60,13 +60,20 @@ import fonts from "lib/fonts"; | |
| import fullscreen from "lib/fullscreen"; | ||
| import { | ||
| BROKEN_PLUGINS, | ||
| isInitialPluginLoadComplete, | ||
| isPluginLoading, | ||
| LOADED_PLUGINS, | ||
| onPluginLoadCallback, | ||
| onPluginLoadFailedCallback, | ||
| onPluginsLoadCompleteCallback, | ||
| } from "lib/loadPlugins"; | ||
| import notificationManager from "lib/notificationManager"; | ||
| import openFolder, { addedFolder } from "lib/openFolder"; | ||
| import orientation from "lib/orientation"; | ||
| import PluginWaiters, { | ||
| pluginLoadError, | ||
| waitForPlugin as waitForPluginResult, | ||
| } from "lib/pluginWaiters"; | ||
| import projects from "lib/projects"; | ||
| import selectionMenu from "lib/selectionMenu"; | ||
| import appSettings from "lib/settings"; | ||
|
|
@@ -97,7 +104,7 @@ class Acode { | |
| #pluginUnmount = {}; | ||
| // Registered formatter implementations (populated by plugins) | ||
| #formatter = []; | ||
| #pluginWatchers = {}; | ||
| #pluginWatchers = new PluginWaiters(); | ||
|
|
||
| /** | ||
| * Clear a plugin's broken mark (so it can be retried) | ||
|
|
@@ -702,31 +709,22 @@ class Acode { | |
| } | ||
|
|
||
| [onPluginLoadCallback](pluginId) { | ||
| if (this.#pluginWatchers[pluginId]) { | ||
| this.#pluginWatchers[pluginId].resolve(); | ||
| delete this.#pluginWatchers[pluginId]; | ||
| } | ||
| this.#pluginWatchers.resolve(pluginId, true); | ||
| } | ||
|
|
||
| [onPluginsLoadCompleteCallback]() { | ||
| for (const pluginId in this.#pluginWatchers) { | ||
| this.#pluginWatchers[pluginId].reject( | ||
| new Error(`Plugin '${pluginId}' failed to load.`), | ||
| ); | ||
| } | ||
| this.#pluginWatchers = {}; | ||
| [onPluginLoadFailedCallback](pluginId, error) { | ||
| this.#pluginWatchers.reject(pluginId, error || pluginLoadError(pluginId)); | ||
| } | ||
|
|
||
| waitForPlugin(pluginId) { | ||
| return new Promise((resolve, reject) => { | ||
| if (LOADED_PLUGINS.has(pluginId)) { | ||
| return resolve(true); | ||
| } | ||
| [onPluginsLoadCompleteCallback](filter) { | ||
| this.#pluginWatchers.rejectAll(pluginLoadError, filter); | ||
| } | ||
|
|
||
| this.#pluginWatchers[pluginId] = { | ||
| resolve, | ||
| reject, | ||
| }; | ||
| waitForPlugin(pluginId) { | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If the initial load has already completed ( Since this function is being rewritten anyway, it'd be good to reject immediately in that case. Related: with the new |
||
| return waitForPluginResult(this.#pluginWatchers, pluginId, { | ||
| isLoaded: (id) => LOADED_PLUGINS.has(id), | ||
| isInitialPluginLoadComplete, | ||
| isPluginLoading, | ||
| }); | ||
| } | ||
|
|
||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,60 @@ | ||
| export function pluginLoadError(pluginId) { | ||
| return new Error(`Plugin '${pluginId}' failed to load.`); | ||
| } | ||
|
|
||
| export function waitForPlugin(waiters, pluginId, state) { | ||
| if (state.isLoaded(pluginId)) return Promise.resolve(true); | ||
| if (state.isInitialPluginLoadComplete() && !state.isPluginLoading(pluginId)) { | ||
| return Promise.reject(pluginLoadError(pluginId)); | ||
| } | ||
| return waiters.waitFor(pluginId); | ||
| } | ||
|
|
||
| export default class PluginWaiters { | ||
| #waiters = new Map(); | ||
|
|
||
| waitFor(pluginId) { | ||
| let deferred = this.#waiters.get(pluginId); | ||
| if (!deferred) { | ||
| let resolve; | ||
| let reject; | ||
| const promise = new Promise((res, rej) => { | ||
| resolve = res; | ||
| reject = rej; | ||
| }); | ||
| deferred = { promise, resolve, reject }; | ||
| this.#waiters.set(pluginId, deferred); | ||
| } | ||
| return deferred.promise; | ||
| } | ||
|
|
||
| resolve(pluginId, value = true) { | ||
| const deferred = this.#waiters.get(pluginId); | ||
| if (!deferred) return; | ||
|
|
||
| this.#waiters.delete(pluginId); | ||
| deferred.resolve(value); | ||
| } | ||
|
|
||
| reject(pluginId, error) { | ||
| const deferred = this.#waiters.get(pluginId); | ||
| if (!deferred) return; | ||
|
|
||
| this.#waiters.delete(pluginId); | ||
| deferred.reject(error); | ||
| } | ||
|
|
||
| rejectAll(getError, filter) { | ||
| for (const [pluginId, deferred] of this.#waiters) { | ||
| if (filter && !filter(pluginId)) continue; | ||
| this.#waiters.delete(pluginId); | ||
| let error; | ||
| try { | ||
| error = getError(pluginId); | ||
| } catch (cause) { | ||
| error = cause instanceof Error ? cause : new Error(String(cause)); | ||
| } | ||
| deferred.reject(error); | ||
| } | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -11,7 +11,7 @@ import actionStack from "lib/actionStack"; | |
| import auth from "lib/auth"; | ||
| import config from "lib/config"; | ||
| import installPlugin from "lib/installPlugin"; | ||
| import loadPlugin from "lib/loadPlugin"; | ||
| import { loadPluginWithTimeout } from "lib/loadPlugins"; | ||
| import settings from "lib/settings"; | ||
| import FileBrowser from "pages/fileBrowser"; | ||
| import Plugin from "pages/plugin"; | ||
|
|
@@ -809,7 +809,7 @@ export default function PluginsInclude(updates) { | |
| } else { | ||
| delete disabledMap[id]; | ||
| settings.update({ pluginsDisabled: disabledMap }, false); | ||
| await loadPlugin(id); | ||
| await loadPluginWithTimeout(id); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Check the result before announcing success or updating the lists. The sidebar enable handler also ignores this result. Knowledge Base Used: Plugin lifecycle and extension management |
||
| window.toast(strings["plugin_enabled"] || "Plugin enabled"); | ||
| } | ||
|
|
||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,141 @@ | ||
| import { beforeEach, describe, expect, it, vi } from "vitest"; | ||
|
|
||
| const mocks = vi.hoisted(() => ({ | ||
| fsOperation: vi.fn(), | ||
| loadPlugin: vi.fn(), | ||
| settings: { value: { appTheme: "", pluginsDisabled: {} } }, | ||
| })); | ||
|
|
||
| vi.mock("fileSystem", () => ({ default: mocks.fsOperation })); | ||
| vi.mock("lib/loadPlugin", () => ({ default: mocks.loadPlugin })); | ||
| vi.mock("lib/settings", () => ({ default: mocks.settings })); | ||
| vi.mock("utils/Url", () => ({ | ||
| default: { basename: (url) => String(url).split("/").at(-1) }, | ||
| })); | ||
|
|
||
| import { | ||
| isInitialPluginLoadComplete, | ||
| isPluginLoading, | ||
| LOADED_PLUGINS, | ||
| default as loadPlugins, | ||
| onPluginLoadCallback, | ||
| onPluginLoadFailedCallback, | ||
| onPluginsLoadCompleteCallback, | ||
| } from "lib/loadPlugins"; | ||
| import PluginWaiters, { | ||
| pluginLoadError, | ||
| waitForPlugin, | ||
| } from "lib/pluginWaiters"; | ||
|
|
||
| describe("plugin-load waiter integration", () => { | ||
| beforeEach(() => { | ||
| mocks.fsOperation.mockReset(); | ||
| mocks.loadPlugin.mockReset(); | ||
| globalThis.PLUGIN_DIR = "plugins/"; | ||
| }); | ||
|
|
||
| function useWaiters(waiters) { | ||
| globalThis.acode = { | ||
| waitForPlugin(pluginId) { | ||
| return waitForPlugin(waiters, pluginId, { | ||
| isLoaded: (id) => LOADED_PLUGINS.has(id), | ||
| isInitialPluginLoadComplete, | ||
| isPluginLoading, | ||
| }); | ||
| }, | ||
| [onPluginLoadCallback](pluginId) { | ||
| waiters.resolve(pluginId, true); | ||
| }, | ||
| [onPluginLoadFailedCallback](pluginId, error) { | ||
| waiters.reject(pluginId, error); | ||
| }, | ||
| [onPluginsLoadCompleteCallback](filter) { | ||
| waiters.rejectAll(pluginLoadError, filter); | ||
| }, | ||
| }; | ||
| } | ||
|
|
||
| it("does not reject regular plugin waiters after a theme-only pass", async () => { | ||
| globalThis.strings = { "loading plugins": "Loading plugins" }; | ||
| globalThis.toast = vi.fn(); | ||
| const waiters = new PluginWaiters(); | ||
| useWaiters(waiters); | ||
| let settled = false; | ||
| const pending = globalThis.acode.waitForPlugin("regular-example").then( | ||
| (value) => { | ||
| settled = true; | ||
| return value; | ||
| }, | ||
| (error) => { | ||
| settled = true; | ||
| return error; | ||
| }, | ||
| ); | ||
| const lsDir = vi | ||
| .fn() | ||
| .mockResolvedValue([ | ||
| { url: "plugins/theme-example" }, | ||
| { url: "plugins/regular-example" }, | ||
| ]); | ||
| mocks.fsOperation.mockReturnValue({ lsDir }); | ||
| mocks.loadPlugin.mockResolvedValue(undefined); | ||
|
|
||
| await expect(loadPlugins(true)).resolves.toBe(1); | ||
| expect(isInitialPluginLoadComplete()).toBe(false); | ||
| expect(settled).toBe(false); | ||
| await loadPlugins(); | ||
| expect(isInitialPluginLoadComplete()).toBe(true); | ||
| await expect(pending).resolves.toBe(true); | ||
| expect(mocks.loadPlugin).toHaveBeenCalledTimes(2); | ||
| expect(lsDir).toHaveBeenCalledTimes(2); | ||
| }); | ||
|
|
||
| it("settles existing waiters when plugin discovery fails", async () => { | ||
| const waiters = new PluginWaiters(); | ||
| const pending = waiters.waitFor("example"); | ||
| useWaiters(waiters); | ||
| mocks.fsOperation.mockReturnValue({ | ||
| lsDir: vi | ||
| .fn() | ||
| .mockRejectedValue(new Error("plugin directory unavailable")), | ||
| }); | ||
|
|
||
| await expect(loadPlugins()).rejects.toThrow("plugin directory unavailable"); | ||
| expect(isInitialPluginLoadComplete()).toBe(true); | ||
| await expect(pending).rejects.toThrow("Plugin 'example' failed to load."); | ||
| await expect( | ||
| globalThis.acode.waitForPlugin("not-installed"), | ||
| ).rejects.toThrow("Plugin 'not-installed' failed to load."); | ||
| }); | ||
|
|
||
| it("keeps waiters pending through a timed-out background load", async () => { | ||
| vi.useFakeTimers(); | ||
| globalThis.strings = { "loading plugins": "Loading plugins" }; | ||
| globalThis.toast = vi.fn(); | ||
| const waiters = new PluginWaiters(); | ||
| useWaiters(waiters); | ||
| let finishLoading; | ||
| mocks.loadPlugin.mockImplementation( | ||
| () => new Promise((resolve) => (finishLoading = resolve)), | ||
| ); | ||
| mocks.fsOperation.mockReturnValue({ | ||
| lsDir: vi.fn().mockResolvedValue([{ url: "plugins/slow-example" }]), | ||
| }); | ||
| let waiterSettled = false; | ||
| const loading = loadPlugins(); | ||
| const pending = globalThis.acode | ||
| .waitForPlugin("slow-example") | ||
| .then((value) => { | ||
| waiterSettled = true; | ||
| return value; | ||
| }); | ||
| await vi.advanceTimersByTimeAsync(15_000); | ||
| await expect(loading).resolves.toBe(0); | ||
| expect(waiterSettled).toBe(false); | ||
|
|
||
| finishLoading(); | ||
| await expect(pending).resolves.toBe(true); | ||
| vi.clearAllTimers(); | ||
| vi.useRealTimers(); | ||
| }); | ||
| }); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
When a plugin throws during loading,
onPluginLoadFailedCallbackrejects its waiters with the original error instead of the existing plugin-specific error promised by this PR. An error such as “Syntax error” no longer identifies which plugin failed. Reject withpluginLoadError(pluginId)and keep the original error as its cause.