-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* fix: create extension instance only when enabled Signed-off-by: Roman <ixrock@gmail.com> * mark extension.isEnabled with private modifier Signed-off-by: Roman <ixrock@gmail.com> * try-catch errors for extension.disable() Signed-off-by: Roman <ixrock@gmail.com> * fixes & refactoring Signed-off-by: Roman <ixrock@gmail.com> * make ext.isBundled non optional Signed-off-by: Roman <ixrock@gmail.com>
- Loading branch information
Showing
8 changed files
with
170 additions
and
81 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
import type { LensExtensionId } from "./lens-extension"; | ||
import type { ExtensionLoader } from "./extension-loader"; | ||
import { BaseStore } from "../common/base-store" | ||
import { action, observable, reaction, toJS } from "mobx"; | ||
|
||
export interface LensExtensionsStoreModel { | ||
extensions: Record<LensExtensionId, LensExtensionState>; | ||
} | ||
|
||
export interface LensExtensionState { | ||
enabled?: boolean; | ||
} | ||
|
||
export class ExtensionsStore extends BaseStore<LensExtensionsStoreModel> { | ||
constructor() { | ||
super({ | ||
configName: "lens-extensions", | ||
}); | ||
} | ||
|
||
protected state = observable.map<LensExtensionId, LensExtensionState>(); | ||
|
||
protected getState(extensionLoader: ExtensionLoader) { | ||
const state: Record<LensExtensionId, LensExtensionState> = {}; | ||
return Array.from(extensionLoader.userExtensions).reduce((state, [extId, ext]) => { | ||
state[extId] = { | ||
enabled: ext.isEnabled, | ||
} | ||
return state; | ||
}, state) | ||
} | ||
|
||
async manageState(extensionLoader: ExtensionLoader) { | ||
await extensionLoader.whenLoaded; | ||
await this.whenLoaded; | ||
|
||
// activate user-extensions when state is ready | ||
extensionLoader.userExtensions.forEach((ext, extId) => { | ||
ext.isEnabled = this.isEnabled(extId); | ||
}); | ||
|
||
// apply state on changes from store | ||
reaction(() => this.state.toJS(), extensionsState => { | ||
extensionsState.forEach((state, extId) => { | ||
const ext = extensionLoader.getExtension(extId); | ||
if (ext && !ext.isBundled) { | ||
ext.isEnabled = state.enabled; | ||
} | ||
}) | ||
}) | ||
|
||
// save state on change `extension.isEnabled` | ||
reaction(() => this.getState(extensionLoader), extensionsState => { | ||
this.state.merge(extensionsState) | ||
}) | ||
} | ||
|
||
isEnabled(extId: LensExtensionId) { | ||
const state = this.state.get(extId); | ||
return !state /* enabled by default */ || state.enabled; | ||
} | ||
|
||
@action | ||
protected fromStore({ extensions }: LensExtensionsStoreModel) { | ||
this.state.merge(extensions); | ||
} | ||
|
||
toJSON(): LensExtensionsStoreModel { | ||
return toJS({ | ||
extensions: this.state.toJSON(), | ||
}, { | ||
recurseEverything: true | ||
}) | ||
} | ||
} | ||
|
||
export const extensionsStore = new ExtensionsStore(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.