From 8e7a45f6c91d90962afbce48bb2ed7ca6252df2b Mon Sep 17 00:00:00 2001 From: Markus Wolf Date: Fri, 25 Aug 2017 11:32:26 +0200 Subject: [PATCH] feat: use eager component pattern User eager component registration and component lifecycle listener to register actions automatically --- package-lock.json | 6 ++--- package.json | 2 +- src/command-manager.ts | 43 +++++++++++------------------------ src/command.ts | 1 + src/commands/browse.ts | 12 +++++++--- src/commands/pull-requests.ts | 24 ++++++++++++++----- src/commands/token.ts | 8 +++++-- src/commands/user.ts | 16 +++++++++---- src/main.ts | 4 ++++ 9 files changed, 67 insertions(+), 49 deletions(-) diff --git a/package-lock.json b/package-lock.json index c7c22d90..95d8e259 100644 --- a/package-lock.json +++ b/package-lock.json @@ -4151,9 +4151,9 @@ "dev": true }, "tsdi": { - "version": "0.11.1", - "resolved": "https://registry.npmjs.org/tsdi/-/tsdi-0.11.1.tgz", - "integrity": "sha1-DSrcl1FaGNjc8QeH4sBnUV3JG/I=", + "version": "0.12.1", + "resolved": "https://registry.npmjs.org/tsdi/-/tsdi-0.12.1.tgz", + "integrity": "sha512-htrmtkImPrTh6gQW8W0hdiLToRuyoFXtbZX/bOLSXK8kP9lBfCk7Sq5sO+wc1lDhYBC/t6p8Yg3Ujx0BMjgqxw==", "requires": { "reflect-metadata": "0.1.10" } diff --git a/package.json b/package.json index 781b3b68..c72f49cd 100644 --- a/package.json +++ b/package.json @@ -170,7 +170,7 @@ "lru-cache": "^4.0.2", "pretend": "^0.6.0", "sander": "^0.6.0", - "tsdi": "0.11.1" + "tsdi": "0.12.1" }, "repository": { "type": "git", diff --git a/src/command-manager.ts b/src/command-manager.ts index 40a61e8c..9ef548b8 100644 --- a/src/command-manager.ts +++ b/src/command-manager.ts @@ -1,21 +1,14 @@ -import { TSDI, component, inject, initialize } from 'tsdi'; +import { TSDI, LifecycleListener, component, inject, initialize } from 'tsdi'; import * as vscode from 'vscode'; import { Command } from './command'; -import { BrowseProject, BrowseOpenIssues, BrowseCurrentFile } from './commands/browse'; -import { - BrowsePullRequest, - BrowseSimpleRequest, - CheckoutPullRequest, - CreateSimplePullRequest, - CreatePullRequest, - MergePullRequest -} from './commands/pull-requests'; -import { SetGithubToken, SetGithubEnterpriseToken } from './commands/token'; -import { AddAssignee, RemoveAssignee, RequestReview, DeleteReviewRequest } from './commands/user'; +import './commands/browse'; +import './commands/pull-requests'; +import './commands/token'; +import './commands/user'; @component -export class CommandManager { +export class CommandManager implements LifecycleListener { @inject private tsdi: TSDI; @@ -25,25 +18,15 @@ export class CommandManager { @initialize protected init(): void { - this.register('vscode-github.setGitHubToken', this.tsdi.get(SetGithubToken)); - this.register('vscode-github.setGitHubEnterpriseToken', this.tsdi.get(SetGithubEnterpriseToken)); - this.register('vscode-github.browseProject', this.tsdi.get(BrowseProject)); - this.register('vscode-github.browseOpenIssue', this.tsdi.get(BrowseOpenIssues)); - this.register('vscode-github.browseCurrentFile', this.tsdi.get(BrowseCurrentFile)); - this.register('vscode-github.browserPullRequest', this.tsdi.get(BrowsePullRequest)); - this.register('vscode-github.checkoutPullRequests', this.tsdi.get(CheckoutPullRequest)); - this.register('vscode-github.browserSimplePullRequest', this.tsdi.get(BrowseSimpleRequest)); - this.register('vscode-github.createSimplePullRequest', this.tsdi.get(CreateSimplePullRequest)); - this.register('vscode-github.createPullRequest', this.tsdi.get(CreatePullRequest)); - this.register('vscode-github.mergePullRequest', this.tsdi.get(MergePullRequest)); - this.register('vscode-github.addAssignee', this.tsdi.get(AddAssignee)); - this.register('vscode-github.removeAssignee', this.tsdi.get(RemoveAssignee)); - this.register('vscode-github.requestReview', this.tsdi.get(RequestReview)); - this.register('vscode-github.deleteReviewRequest', this.tsdi.get(DeleteReviewRequest)); + this.tsdi.addLifecycleListener(this); } - private register(id: string, command: Command): void { - this.context.subscriptions.push(vscode.commands.registerCommand(id, () => command.run())); + public onCreate(component: any): void { + if (component instanceof Command) { + this.context.subscriptions.push( + vscode.commands.registerCommand(component.id, () => component.run()) + ); + } } } diff --git a/src/command.ts b/src/command.ts index 45f31408..b3466c39 100644 --- a/src/command.ts +++ b/src/command.ts @@ -5,6 +5,7 @@ import { GitHubError } from './github'; import { GitHubManager } from './github-manager'; export abstract class Command { + public abstract get id(): string; public abstract run(progress?: vscode.Progress<{ message?: string }>): void; } diff --git a/src/commands/browse.ts b/src/commands/browse.ts index f2d8e9b0..03cc8dfa 100644 --- a/src/commands/browse.ts +++ b/src/commands/browse.ts @@ -4,9 +4,11 @@ import * as vscode from 'vscode'; import { TokenCommand } from '../command'; import { showProgress } from '../helper'; -@component +@component({eager: true}) export class BrowseProject extends TokenCommand { + public id = 'vscode-github.browseProject'; + @showProgress protected async runWithToken(): Promise { const url = await this.githubManager.getGithubUrl(); @@ -15,9 +17,11 @@ export class BrowseProject extends TokenCommand { } -@component +@component({eager: true}) export class BrowseOpenIssues extends TokenCommand { + public id = 'vscode-github.browseOpenIssue'; + @showProgress protected async runWithToken(): Promise { const issues = await this.githubManager.issues(); @@ -37,9 +41,11 @@ export class BrowseOpenIssues extends TokenCommand { } -@component +@component({eager: true}) export class BrowseCurrentFile extends TokenCommand { + public id = 'vscode-github.browseCurrentFile'; + @showProgress protected async runWithToken(): Promise { const editor = vscode.window.activeTextEditor; diff --git a/src/commands/pull-requests.ts b/src/commands/pull-requests.ts index 11c60666..786179f8 100644 --- a/src/commands/pull-requests.ts +++ b/src/commands/pull-requests.ts @@ -47,9 +47,11 @@ abstract class PullRequestCommand extends TokenCommand { } -@component +@component({eager: true}) export class BrowsePullRequest extends PullRequestCommand { + public id = 'vscode-github.browserPullRequest'; + @showProgress protected async runWithToken(): Promise { const selected = await this.selectPullRequest(); @@ -60,9 +62,11 @@ export class BrowsePullRequest extends PullRequestCommand { } -@component +@component({eager: true}) export class BrowseSimpleRequest extends PullRequestCommand { + public id = 'vscode-github.browserSimplePullRequest'; + @showProgress protected async runWithToken(): Promise { const pullRequest = await this.githubManager.getPullRequestForCurrentBranch(); @@ -75,9 +79,11 @@ export class BrowseSimpleRequest extends PullRequestCommand { } -@component +@component({eager: true}) export class CheckoutPullRequest extends PullRequestCommand { + public id = 'vscode-github.checkoutPullRequests'; + @inject private statusBarManager: StatusBarManager; @@ -92,9 +98,11 @@ export class CheckoutPullRequest extends PullRequestCommand { } -@component +@component({eager: true}) export class CreateSimplePullRequest extends PullRequestCommand { + public id = 'vscode-github.createSimplePullRequest'; + @inject private statusBarManager: StatusBarManager; @@ -114,9 +122,11 @@ export class CreateSimplePullRequest extends PullRequestCommand { } -@component +@component({eager: true}) export class CreatePullRequest extends PullRequestCommand { + public id = 'vscode-github.createPullRequest'; + @inject private statusBarManager: StatusBarManager; @@ -170,9 +180,11 @@ export class CreatePullRequest extends PullRequestCommand { } -@component +@component({eager: true}) export class MergePullRequest extends PullRequestCommand { + public id = 'vscode-github.mergePullRequest'; + @inject private statusBarManager: StatusBarManager; diff --git a/src/commands/token.ts b/src/commands/token.ts index 67a8c7a0..2034c1fc 100644 --- a/src/commands/token.ts +++ b/src/commands/token.ts @@ -4,9 +4,11 @@ import * as vscode from 'vscode'; import { Command } from '../command'; import { GitHubManager, Tokens } from '../github-manager'; -@component +@component({eager: true}) export class SetGithubToken extends Command { + public id = 'vscode-github.setGitHubToken'; + @inject('vscode.ExtensionContext') private context: vscode.ExtensionContext; @@ -30,9 +32,11 @@ export class SetGithubToken extends Command { } -@component +@component({eager: true}) export class SetGithubEnterpriseToken extends Command { + public id = 'vscode-github.setGitHubEnterpriseToken'; + @inject('vscode.ExtensionContext') private context: vscode.ExtensionContext; diff --git a/src/commands/user.ts b/src/commands/user.ts index dc7c1e19..0e265d0c 100644 --- a/src/commands/user.ts +++ b/src/commands/user.ts @@ -13,9 +13,11 @@ abstract class UserCommand extends TokenCommand { } } -@component +@component({eager: true}) export class AddAssignee extends UserCommand { + public id = 'vscode-github.addAssignee'; + @showProgress protected async runWithToken(): Promise { const pullRequest = await this.githubManager.getPullRequestForCurrentBranch(); @@ -32,9 +34,11 @@ export class AddAssignee extends UserCommand { } -@component +@component({eager: true}) export class RemoveAssignee extends UserCommand { + public id = 'vscode-github.removeAssignee'; + @showProgress protected async runWithToken(): Promise { const pullRequest = await this.githubManager.getPullRequestForCurrentBranch(); @@ -51,9 +55,11 @@ export class RemoveAssignee extends UserCommand { } -@component +@component({eager: true}) export class RequestReview extends UserCommand { + public id = 'vscode-github.requestReview'; + @showProgress protected async runWithToken(): Promise { const pullRequest = await this.githubManager.getPullRequestForCurrentBranch(); @@ -70,9 +76,11 @@ export class RequestReview extends UserCommand { } -@component +@component({eager: true}) export class DeleteReviewRequest extends UserCommand { + public id = 'vscode-github.deleteReviewRequest'; + @showProgress protected async runWithToken(): Promise { const pullRequest = await this.githubManager.getPullRequestForCurrentBranch(); diff --git a/src/main.ts b/src/main.ts index ee442d66..97490f1d 100644 --- a/src/main.ts +++ b/src/main.ts @@ -1,6 +1,7 @@ import { TSDI, factory } from 'tsdi'; import * as vscode from 'vscode'; +import { CommandManager } from './command-manager'; import { Extension } from './extension'; let tsdi: TSDI; @@ -28,6 +29,9 @@ export function activate(context: vscode.ExtensionContext): void { tsdi = new TSDI(); tsdi.enableComponentScanner(); tsdi.register(ComponentFactory); + // note: trigger CommandManager creating for now + // this could be removed when tsdi is able to defer eager creation + tsdi.get(CommandManager); context.subscriptions.push(tsdi.get(Extension)); }