Skip to content

Commit

Permalink
Adds gating via @GATE to many node methods
Browse files Browse the repository at this point in the history
  • Loading branch information
eamodio committed Oct 21, 2018
1 parent ad0aae7 commit aceab81
Show file tree
Hide file tree
Showing 11 changed files with 79 additions and 19 deletions.
42 changes: 37 additions & 5 deletions src/system/decorators.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
'use strict';
import { Logger, LogLevel } from '../logger';
import { Functions } from './function';
import { Strings } from './string';

function decorate(decorator: (fn: Function, key: string) => Function): Function {
Expand Down Expand Up @@ -177,11 +178,7 @@ export function log<T>(
const start = options.timed ? process.hrtime() : undefined;
const result = fn.apply(this, args);

if (
result != null &&
(typeof result === 'object' || typeof result === 'function') &&
typeof result.then === 'function'
) {
if (result != null && Functions.isPromise(result)) {
const promise = result.then((r: any) => {
const timing =
start !== undefined ? ` \u2022 ${Strings.getDurationMilliseconds(start)} ms` : '';
Expand Down Expand Up @@ -224,6 +221,41 @@ export function log<T>(
};
}

export function gate() {
return (target: any, key: string, descriptor: PropertyDescriptor) => {
if (!(typeof descriptor.value === 'function')) throw new Error('not supported');

const gateKey = `$gate$${key}`;
const fn = descriptor.value;

descriptor.value = function(this: any, ...args: any[]) {
if (!this.hasOwnProperty(gateKey)) {
Object.defineProperty(this, gateKey, {
configurable: false,
enumerable: false,
writable: true,
value: undefined
});
}

let promise = this[gateKey];
if (promise === undefined) {
const result = fn.apply(this, args);
if (result == null || !Functions.isPromise(result)) {
return result;
}

this[gateKey] = promise = result.then((r: any) => {
this[gateKey] = undefined;
return r;
});
}

return promise;
};
};
}

function _memoize(fn: Function, key: string): Function {
const memoizeKey = `$memoize$${key}`;

Expand Down
4 changes: 4 additions & 0 deletions src/system/function.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,10 @@ export namespace Functions {
return tracked;
}

export function isPromise(o: any) {
return (typeof o === 'object' || typeof o === 'function') && typeof o.then === 'function';
}

export function once<T extends Function>(fn: T): T {
return _once(fn);
}
Expand Down
4 changes: 3 additions & 1 deletion src/views/nodes/fileHistoryTrackerNode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { Disposable, TextEditor, TreeItem, TreeItemCollapsibleState, Uri, window
import { UriComparer } from '../../comparers';
import { Container } from '../../container';
import { GitUri } from '../../git/gitService';
import { debug, Functions, log } from '../../system';
import { debug, Functions, gate, log } from '../../system';
import { FileHistoryView } from '../fileHistoryView';
import { MessageNode } from './common';
import { FileHistoryNode } from './fileHistoryNode';
Expand Down Expand Up @@ -52,6 +52,8 @@ export class FileHistoryTrackerNode extends SubscribeableViewNode<FileHistoryVie
return item;
}

@gate()
@debug()
async refresh() {
const editor = window.activeTextEditor;
if (editor == null || !Container.git.isTrackable(editor.document.uri)) {
Expand Down
4 changes: 3 additions & 1 deletion src/views/nodes/lineHistoryTrackerNode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { Disposable, Selection, TreeItem, TreeItemCollapsibleState, window } fro
import { UriComparer } from '../../comparers';
import { Container } from '../../container';
import { GitUri } from '../../git/gitService';
import { debug, Functions, log } from '../../system';
import { debug, Functions, gate, log } from '../../system';
import { LinesChangeEvent } from '../../trackers/gitLineTracker';
import { LineHistoryView } from '../lineHistoryView';
import { MessageNode } from './common';
Expand Down Expand Up @@ -53,6 +53,8 @@ export class LineHistoryTrackerNode extends SubscribeableViewNode<LineHistoryVie
return item;
}

@gate()
@debug()
async refresh() {
const editor = window.activeTextEditor;
if (editor == null || !Container.git.isTrackable(editor.document.uri)) {
Expand Down
6 changes: 5 additions & 1 deletion src/views/nodes/repositoriesNode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { Disposable, ProgressLocation, TextEditor, TreeItem, TreeItemCollapsible
import { Container } from '../../container';
import { GitUri } from '../../git/gitService';
import { Logger } from '../../logger';
import { debug, Functions, log } from '../../system';
import { debug, Functions, gate, log } from '../../system';
import { RepositoriesView } from '../repositoriesView';
import { RefreshReason } from '../viewBase';
import { MessageNode } from './common';
Expand Down Expand Up @@ -57,6 +57,7 @@ export class RepositoriesNode extends SubscribeableViewNode<RepositoriesView> {
return item;
}

@gate()
@log()
async fetchAll() {
if (this._children === undefined || this._children.length === 0) return;
Expand Down Expand Up @@ -86,6 +87,7 @@ export class RepositoriesNode extends SubscribeableViewNode<RepositoriesView> {
);
}

@gate()
@log()
async pullAll() {
if (this._children === undefined || this._children.length === 0) return;
Expand Down Expand Up @@ -115,6 +117,8 @@ export class RepositoriesNode extends SubscribeableViewNode<RepositoriesView> {
);
}

@gate()
@debug()
async refresh(reason?: RefreshReason) {
if (this._children === undefined) return;

Expand Down
11 changes: 8 additions & 3 deletions src/views/nodes/repositoryNode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import {
RepositoryChangeEvent,
RepositoryFileSystemChangeEvent
} from '../../git/gitService';
import { Dates, debug, Functions, log, Strings } from '../../system';
import { Dates, debug, Functions, gate, log, Strings } from '../../system';
import { RepositoriesView } from '../repositoriesView';
import { BranchesNode } from './branchesNode';
import { BranchNode } from './branchNode';
Expand Down Expand Up @@ -168,6 +168,7 @@ export class RepositoryNode extends SubscribeableViewNode<RepositoriesView> {
return item;
}

@gate()
@log()
async fetch(progress: boolean = true) {
if (!progress) return this.fetchCore();
Expand All @@ -189,6 +190,7 @@ export class RepositoryNode extends SubscribeableViewNode<RepositoriesView> {
this.view.triggerNodeChange(this);
}

@gate()
@log()
async pull(progress: boolean = true) {
if (!progress) return this.pullCore();
Expand All @@ -210,6 +212,7 @@ export class RepositoryNode extends SubscribeableViewNode<RepositoriesView> {
this.view.triggerNodeChange(this);
}

@gate()
@log()
async push(progress: boolean = true) {
if (!progress) return this.pushCore();
Expand All @@ -230,11 +233,13 @@ export class RepositoryNode extends SubscribeableViewNode<RepositoriesView> {
this.view.triggerNodeChange(this);
}

refresh() {
@gate()
@debug()
async refresh() {
this._status = this.repo.getStatus();

this._children = undefined;
void this.ensureSubscription();
await this.ensureSubscription();
}

@debug()
Expand Down
2 changes: 1 addition & 1 deletion src/views/nodes/resultsCommitsNode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ export class ResultsCommitsNode extends ViewNode implements PageableViewNode {
return item;
}

async refresh() {
refresh() {
this._commitsQueryResults = this._commitsQuery(this.maxCount);
}

Expand Down
2 changes: 1 addition & 1 deletion src/views/nodes/resultsFilesNode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ export class ResultsFilesNode extends ViewNode {
return item;
}

async refresh() {
refresh() {
this._filesQueryResults = this.getFilesQueryResultsCore();
}

Expand Down
10 changes: 8 additions & 2 deletions src/views/nodes/resultsNode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { TreeItem, TreeItemCollapsibleState } from 'vscode';
import { ShowCommitSearchCommandArgs } from '../../commands';
import { GlyphChars } from '../../constants';
import { GitRepoSearchBy } from '../../git/gitService';
import { Strings } from '../../system';
import { debug, Functions, gate, log, Strings } from '../../system';
import { ResultsView } from '../resultsView';
import { CommandMessageNode, MessageNode } from './common';
import { ResourceType, unknownGitUri, ViewNode } from './viewNode';
Expand Down Expand Up @@ -114,13 +114,17 @@ export class ResultsNode extends ViewNode {
this.view.triggerNodeChange();
}

@log()
clear() {
if (this._children.length === 0) return;

this._children.length = 0;
this.view.triggerNodeChange();
}

@log({
args: { 0: (n: ViewNode) => n.toString() }
})
dismiss(node: ViewNode) {
if (this._children.length === 0) return;

Expand All @@ -131,9 +135,11 @@ export class ResultsNode extends ViewNode {
this.view.triggerNodeChange();
}

@gate()
@debug()
async refresh() {
if (this._children.length === 0) return;

this._children.forEach(c => c.refresh());
await Promise.all(this._children.map(c => c.refresh()).filter(Functions.isPromise) as Promise<any>[]);
}
}
7 changes: 6 additions & 1 deletion src/views/nodes/viewNode.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
'use strict';
import { Command, Disposable, Event, TreeItem, TreeItemCollapsibleState, TreeViewVisibilityChangeEvent } from 'vscode';
import { GitUri } from '../../git/gitService';
import { debug, logName } from '../../system';
import { debug, gate, logName } from '../../system';
import { RefreshReason, TreeViewNodeStateChangeEvent, View } from '../viewBase';

export enum ResourceType {
Expand Down Expand Up @@ -65,6 +65,10 @@ export abstract class ViewNode {
this._uri = uri;
}

toString() {
return `${this.constructor.name}${this.id != null ? `(${this.id})` : ''}`;
}

protected _uri: GitUri;
get uri() {
return this._uri;
Expand All @@ -82,6 +86,7 @@ export abstract class ViewNode {
return undefined;
}

@gate()
@debug()
refresh(reason?: RefreshReason): void | boolean | Promise<void> | Promise<boolean> {}
}
Expand Down
6 changes: 3 additions & 3 deletions src/views/viewBase.ts
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ export abstract class ViewBase<TRoot extends ViewNode> implements TreeDataProvid
}

@debug({
args: { 0: (n: ViewNode) => `${n.constructor.name}${n.id != null ? `(${n.id})` : ''}` }
args: { 0: (n: ViewNode) => n.toString() }
})
async refreshNode(node: ViewNode, args?: RefreshNodeCommandArgs) {
if (args !== undefined) {
Expand All @@ -169,7 +169,7 @@ export abstract class ViewBase<TRoot extends ViewNode> implements TreeDataProvid
}

@log({
args: { 0: (n: ViewNode) => `${n.constructor.name}${n.id != null ? `(${n.id})` : ''}` }
args: { 0: (n: ViewNode) => n.toString() }
})
async reveal(
node: ViewNode,
Expand Down Expand Up @@ -198,7 +198,7 @@ export abstract class ViewBase<TRoot extends ViewNode> implements TreeDataProvid
}

@debug({
args: { 0: (n?: ViewNode) => (n != null ? `${n.constructor.name}${n.id != null ? `(${n.id})` : ''}` : '') }
args: { 0: (n: ViewNode) => (n != null ? n.toString() : '') }
})
triggerNodeChange(node?: ViewNode) {
// Since the root node won't actually refresh, force everything
Expand Down

0 comments on commit aceab81

Please sign in to comment.