mirror of
https://github.com/lensapp/lens.git
synced 2025-05-20 05:10:56 +00:00
extensions-api: store, part 1
Signed-off-by: Roman <ixrock@gmail.com>
This commit is contained in:
parent
505a5c7d9f
commit
0ff9c655d1
@ -12,9 +12,9 @@
|
|||||||
},
|
},
|
||||||
"scripts": {
|
"scripts": {
|
||||||
"dev": "concurrently -k \"yarn dev-run -C\" \"yarn dev:main\" \"yarn dev:renderer\"",
|
"dev": "concurrently -k \"yarn dev-run -C\" \"yarn dev:main\" \"yarn dev:renderer\"",
|
||||||
"dev-run": "cross-env DEBUG=true nodemon --watch static/build/main.js --exec \"electron --inspect .\"",
|
"dev-run": "nodemon --watch static/build/main.js --exec \"electron --inspect .\"",
|
||||||
"dev:main": "cross-env DEBUG=true yarn compile:main --watch",
|
"dev:main": "yarn compile:main --watch",
|
||||||
"dev:renderer": "cross-env DEBUG=true yarn compile:renderer --watch",
|
"dev:renderer": "yarn compile:renderer --watch",
|
||||||
"compile": "env NODE_ENV=production concurrently yarn:compile:*",
|
"compile": "env NODE_ENV=production concurrently yarn:compile:*",
|
||||||
"compile:main": "webpack --config webpack.main.ts",
|
"compile:main": "webpack --config webpack.main.ts",
|
||||||
"compile:renderer": "webpack --config webpack.renderer.ts",
|
"compile:renderer": "webpack --config webpack.renderer.ts",
|
||||||
|
|||||||
77
src/extensions/extension-store.ts
Normal file
77
src/extensions/extension-store.ts
Normal file
@ -0,0 +1,77 @@
|
|||||||
|
import { action, observable, toJS } from "mobx";
|
||||||
|
import { BaseStore } from "../common/base-store";
|
||||||
|
import { LensExtension } from "./extension";
|
||||||
|
|
||||||
|
export type ExtensionId = string;
|
||||||
|
export type ExtensionVersion = string | number;
|
||||||
|
|
||||||
|
export interface ExtensionStoreModel {
|
||||||
|
version: ExtensionVersion;
|
||||||
|
extensions: Record<ExtensionId, ExtensionModel>
|
||||||
|
}
|
||||||
|
|
||||||
|
export interface ExtensionModel {
|
||||||
|
id: ExtensionId;
|
||||||
|
version: ExtensionVersion;
|
||||||
|
name: string;
|
||||||
|
description?: string;
|
||||||
|
enabled?: boolean;
|
||||||
|
}
|
||||||
|
|
||||||
|
export class ExtensionStore extends BaseStore<ExtensionStoreModel> {
|
||||||
|
private constructor() {
|
||||||
|
super({
|
||||||
|
configName: "lens-extension-store",
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
@observable version: ExtensionVersion = "0.0.0";
|
||||||
|
@observable extensions = observable.map<ExtensionId, LensExtension>();
|
||||||
|
@observable removed = observable.map<ExtensionId, LensExtension>();
|
||||||
|
|
||||||
|
getById(id: ExtensionId): LensExtension {
|
||||||
|
return this.extensions.get(id);
|
||||||
|
}
|
||||||
|
|
||||||
|
async removeById(id: ExtensionId) {
|
||||||
|
const extension = this.getById(id);
|
||||||
|
if (extension) {
|
||||||
|
this.extensions.delete(id);
|
||||||
|
return extension.uninstall();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@action
|
||||||
|
protected fromStore({ extensions, version }: ExtensionStoreModel) {
|
||||||
|
if (version) {
|
||||||
|
this.version = version;
|
||||||
|
}
|
||||||
|
if (extensions) {
|
||||||
|
const currentExtensions = new Map(Object.entries(extensions));
|
||||||
|
this.extensions.forEach(extension => {
|
||||||
|
if (!currentExtensions.has(extension.id)) {
|
||||||
|
this.removed.set(extension.id, extension);
|
||||||
|
}
|
||||||
|
})
|
||||||
|
currentExtensions.forEach(model => {
|
||||||
|
const extension = this.getById(model.id)
|
||||||
|
if (!extension) {
|
||||||
|
this.extensions.set(model.id, new LensExtension(model));
|
||||||
|
} else {
|
||||||
|
extension.importModel(model);
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
toJSON(): ExtensionStoreModel {
|
||||||
|
return toJS({
|
||||||
|
version: this.version,
|
||||||
|
extensions: this.extensions.toJSON(),
|
||||||
|
}, {
|
||||||
|
recurseEverything: true,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
export const extensionStore = ExtensionStore.getInstance<ExtensionStore>()
|
||||||
38
src/extensions/extension.ts
Normal file
38
src/extensions/extension.ts
Normal file
@ -0,0 +1,38 @@
|
|||||||
|
import { observable } from "mobx";
|
||||||
|
import { ExtensionModel } from "./extension-store";
|
||||||
|
|
||||||
|
export type ExtensionId = string;
|
||||||
|
export type ExtensionVersion = string | number;
|
||||||
|
|
||||||
|
export class LensExtension implements ExtensionModel {
|
||||||
|
public id: ExtensionId;
|
||||||
|
public version: string | number;
|
||||||
|
|
||||||
|
@observable name = "";
|
||||||
|
@observable description = "";
|
||||||
|
@observable isEnabled = false;
|
||||||
|
|
||||||
|
constructor(model: ExtensionModel) {
|
||||||
|
this.importModel(model);
|
||||||
|
}
|
||||||
|
|
||||||
|
importModel(model: ExtensionModel) {
|
||||||
|
Object.assign(this, model);
|
||||||
|
}
|
||||||
|
|
||||||
|
async install() {
|
||||||
|
// todo
|
||||||
|
}
|
||||||
|
|
||||||
|
async uninstall() {
|
||||||
|
// todo
|
||||||
|
}
|
||||||
|
|
||||||
|
async enable() {
|
||||||
|
// todo
|
||||||
|
}
|
||||||
|
|
||||||
|
async disable() {
|
||||||
|
// todo
|
||||||
|
}
|
||||||
|
}
|
||||||
0
src/extensions/index.ts
Normal file
0
src/extensions/index.ts
Normal file
Loading…
Reference in New Issue
Block a user