Skip to content
This repository has been archived by the owner on Nov 27, 2023. It is now read-only.

Commit

Permalink
feat: use eager component pattern
Browse files Browse the repository at this point in the history
User eager component registration and component lifecycle listener to
register actions automatically
  • Loading branch information
KnisterPeter committed Aug 25, 2017
1 parent 5540eef commit 8e7a45f
Show file tree
Hide file tree
Showing 9 changed files with 67 additions and 49 deletions.
6 changes: 3 additions & 3 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
43 changes: 13 additions & 30 deletions src/command-manager.ts
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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())
);
}
}

}
1 change: 1 addition & 0 deletions src/command.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down
12 changes: 9 additions & 3 deletions src/commands/browse.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<void> {
const url = await this.githubManager.getGithubUrl();
Expand All @@ -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<void> {
const issues = await this.githubManager.issues();
Expand All @@ -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<void> {
const editor = vscode.window.activeTextEditor;
Expand Down
24 changes: 18 additions & 6 deletions src/commands/pull-requests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<void> {
const selected = await this.selectPullRequest();
Expand All @@ -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<void> {
const pullRequest = await this.githubManager.getPullRequestForCurrentBranch();
Expand All @@ -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;

Expand All @@ -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;

Expand All @@ -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;

Expand Down Expand Up @@ -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;

Expand Down
8 changes: 6 additions & 2 deletions src/commands/token.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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;

Expand Down
16 changes: 12 additions & 4 deletions src/commands/user.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<void> {
const pullRequest = await this.githubManager.getPullRequestForCurrentBranch();
Expand All @@ -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<void> {
const pullRequest = await this.githubManager.getPullRequestForCurrentBranch();
Expand All @@ -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<void> {
const pullRequest = await this.githubManager.getPullRequestForCurrentBranch();
Expand All @@ -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<void> {
const pullRequest = await this.githubManager.getPullRequestForCurrentBranch();
Expand Down
4 changes: 4 additions & 0 deletions src/main.ts
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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));
}

Expand Down

0 comments on commit 8e7a45f

Please sign in to comment.