Skip to content

Commit

Permalink
Improve check for whether a plugin is enabled to work better with oth…
Browse files Browse the repository at this point in the history
…er plugins like lazy-plugin that persist a disabled status and then enable the plugin without saving that status (#136)
  • Loading branch information
swar8080 authored Feb 1, 2025
1 parent c2603c5 commit d07c2a6
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 2 deletions.
4 changes: 4 additions & 0 deletions src/main.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,9 @@ export default class PluginUpdateCheckerPlugin extends Plugin {
if (!this.app.workspace.getActiveViewOfType(PluginUpdateManagerView)) {
this.app.workspace.detachLeavesOfType(PLUGIN_UPDATES_MANAGER_VIEW_TYPE);

// Get the latest plugin state which may have recently changed (ex: after lazy-plugin finishes lazy enablement)
await store.dispatch(syncApp(this.app));

//Desktop opens in new tab, mobile replaces the current note/tab
const newLeafPaneType = Platform.isMobile ? false : 'tab';
await this.app.workspace.getLeaf(newLeafPaneType).setViewState({
Expand All @@ -190,6 +193,7 @@ export default class PluginUpdateCheckerPlugin extends Plugin {
const pluginLeaf = this.app.workspace.getLeavesOfType(
PLUGIN_UPDATES_MANAGER_VIEW_TYPE
)[0];

if (pluginLeaf) {
this.app.workspace.revealLeaf(pluginLeaf);
}
Expand Down
17 changes: 16 additions & 1 deletion src/state/actionProducers/syncApp.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,26 @@ export const syncApp = (app: App) => {
const manifests = values(obsidianApp.plugins?.manifests) || [];

let enabledPlugins: Record<string, boolean> | undefined = undefined;
if (obsidianApp.plugins?.enabledPlugins instanceof Set) {

if (typeof obsidianApp.plugins?.isEnabled == 'function') {
// The isEnabled() function checks if the plugin is currently enabled, regardless of whether enablement is persisted.
// This is needed to work with plugins like lazy-plugin which seems to persist a disabled status and temporarily enable without persisting.
enabledPlugins = {};
const pluginIds = manifests.map((manifest) => manifest.id);
for (let pluginId of pluginIds) {
enabledPlugins[pluginId] = obsidianApp.plugins.isEnabled(pluginId);
}
} else if (obsidianApp.plugins?.enabledPlugins instanceof Set) {
// Old way of checking enabled plugins, which may be needed for compatibility with older versions of obsidian.
// This checks plugins saved as enabled, as in their enablement persists between reloads.
enabledPlugins = {};
for (let pluginId of obsidianApp.plugins.enabledPlugins) {
enabledPlugins[pluginId] = true;
}
} else {
console.warn(
'Unable to determine enabled plugins, obsidian APIs may have changed! Please file a bug report at https://github.com/swar8080/obsidian-plugin-update-tracker.'
);
}

return syncPluginManifests({ manifests, enabledPlugins });
Expand Down
5 changes: 4 additions & 1 deletion src/state/actionProducers/updatePlugins.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,13 @@ type Params = {
export const updatePlugins = createAsyncThunk(
'obsidian/updatePlugins',
async (params: Params, thunkAPI) => {
const state = thunkAPI.getState() as State;
const dispatch = thunkAPI.dispatch;
const app = window.app as ObsidianApp;

// Update cached values that may be stale before retrieving state
await dispatch(syncApp(app));
const state = thunkAPI.getState() as State;

const allPlugins = InstalledPluginReleases.create(
state.obsidian.pluginManifests,
state.releases.releases
Expand Down
1 change: 1 addition & 0 deletions src/state/obsidianReducer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ export type ObsidianApp = App & {
enabledPlugins?: Set<string>;
disablePlugin?: (pluginId: string) => Promise<any>;
enablePlugin?: (pluginId: string) => Promise<any>;
isEnabled?: (pluginId: string) => boolean;
loadManifests?: () => Promise<any>;
};
};
Expand Down

0 comments on commit d07c2a6

Please sign in to comment.