Przeglądaj źródła

Adds Promises to system lib

main
Eric Amodio 5 lat temu
rodzic
commit
c076be088b
8 zmienionych plików z 42 dodań i 22 usunięć
  1. +2
    -2
      src/quickpicks/referencesQuickPick.ts
  2. +1
    -0
      src/system.ts
  3. +2
    -2
      src/system/decorators/gate.ts
  4. +2
    -1
      src/system/decorators/log.ts
  5. +1
    -13
      src/system/function.ts
  6. +30
    -0
      src/system/promise.ts
  7. +2
    -2
      src/views/nodes/compareNode.ts
  8. +2
    -2
      src/views/nodes/searchNode.ts

+ 2
- 2
src/quickpicks/referencesQuickPick.ts Wyświetl plik

@ -3,7 +3,7 @@ import { CancellationToken, CancellationTokenSource, QuickPickItem, window } fro
import { GlyphChars } from '../constants';
import { Container } from '../container';
import { GitBranch, GitTag } from '../git/gitService';
import { Functions } from '../system';
import { Promises } from '../system';
import { CommandQuickPickItem, getQuickPickIgnoreFocusOut } from './commonQuickPicks';
import { BranchQuickPickItem, RefQuickPickItem, TagQuickPickItem } from './gitQuickPicks';
@ -131,7 +131,7 @@ export class ReferencesQuickPick {
): Promise<(BranchQuickPickItem | TagQuickPickItem | CommandQuickPickItem)[]> {
include = include || 'all';
const results = await Functions.cancellable(
const results = await Promises.cancellable(
Promise.all<GitBranch[] | undefined, GitTag[] | undefined>([
include === 'all' || include === 'branches'
? Container.git.getBranches(this.repoPath, {

+ 1
- 0
src/system.ts Wyświetl plik

@ -8,6 +8,7 @@ export * from './system/decorators/memoize';
export * from './system/function';
export * from './system/iterable';
export * from './system/object';
export * from './system/promise';
export * from './system/searchTree';
export * from './system/string';
export * from './system/version';

+ 2
- 2
src/system/decorators/gate.ts Wyświetl plik

@ -1,5 +1,5 @@
'use strict';
import { Functions } from '../function';
import { Promises } from '../promise';
export function gate() {
return (target: any, key: string, descriptor: PropertyDescriptor) => {
@ -27,7 +27,7 @@ export function gate() {
let promise = this[gateKey];
if (promise === undefined) {
const result = fn!.apply(this, args);
if (result == null || !Functions.isPromise(result)) {
if (result == null || !Promises.isPromise(result)) {
return result;
}

+ 2
- 1
src/system/decorators/log.ts Wyświetl plik

@ -1,6 +1,7 @@
'use strict';
import { LogCorrelationContext, Logger, TraceLevel } from '../../logger';
import { Functions } from '../function';
import { Promises } from '../promise';
import { Strings } from '../string';
const emptyStr = '';
@ -287,7 +288,7 @@ export function log any>(
}
};
if (result != null && Functions.isPromise(result)) {
if (result != null && Promises.isPromise(result)) {
const promise = result.then(logResult);
promise.catch(logError);
}

+ 1
- 13
src/system/function.ts Wyświetl plik

@ -1,6 +1,6 @@
'use strict';
import { debounce as _debounce, once as _once } from 'lodash-es';
import { CancellationToken, Disposable } from 'vscode';
import { Disposable } from 'vscode';
export interface Deferrable {
cancel(): void;
@ -27,14 +27,6 @@ export namespace Functions {
};
}
export function cancellable<T>(promise: Promise<T>, token: CancellationToken): Promise<T | undefined> {
return new Promise<T | undefined>((resolve, reject) => {
token.onCancellationRequested(() => resolve(undefined));
promise.then(resolve, reject);
});
}
export interface DebounceOptions {
leading?: boolean;
maxWait?: number;
@ -133,10 +125,6 @@ export namespace Functions {
return value === undefined ? (o as any)[propOrMatcher] !== undefined : (o as any)[propOrMatcher] === value;
}
export function isPromise<T>(obj: T | Promise<T>): obj is Promise<T> {
return obj && typeof (obj as Promise<T>).then === 'function';
}
export function once<T extends (...args: any[]) => any>(fn: T): T {
return _once(fn);
}

+ 30
- 0
src/system/promise.ts Wyświetl plik

@ -0,0 +1,30 @@
'use strict';
import { CancellationToken } from 'vscode';
export namespace Promises {
export function cancellable<T>(promise: Promise<T>, token: CancellationToken): Promise<T | undefined> {
return new Promise<T | undefined>((resolve, reject) => {
token.onCancellationRequested(() => resolve(undefined));
promise.then(resolve, reject);
});
}
export function isPromise<T>(obj: T | Promise<T>): obj is Promise<T> {
return obj && typeof (obj as Promise<T>).then === 'function';
}
export class TimeoutError<T> extends Error {
constructor(public readonly promise: T) {
super('Promise timed out');
}
}
export function timeout<T>(promise: Promise<T>, ms: number): Promise<T> {
return new Promise<T>((resolve, reject) => {
setTimeout(() => reject(new TimeoutError(promise)), ms);
promise.then(resolve, reject);
});
}
}

+ 2
- 2
src/views/nodes/compareNode.ts Wyświetl plik

@ -4,7 +4,7 @@ import { getRepoPathOrPrompt } from '../../commands';
import { CommandContext, GlyphChars, NamedRef, setCommandContext } from '../../constants';
import { GitService } from '../../git/gitService';
import { CommandQuickPickItem, ReferencesQuickPick } from '../../quickpicks';
import { debug, Functions, gate, Iterables, log } from '../../system';
import { debug, gate, Iterables, log, Promises } from '../../system';
import { CompareView } from '../compareView';
import { MessageNode } from './common';
import { ComparePickerNode } from './comparePickerNode';
@ -120,7 +120,7 @@ export class CompareNode extends ViewNode {
const promises: Promise<any>[] = [
...Iterables.filterMap(this._children, c => {
const result = c.refresh === undefined ? false : c.refresh();
return Functions.isPromise<boolean | void>(result) ? result : undefined;
return Promises.isPromise<boolean | void>(result) ? result : undefined;
})
];
await Promise.all(promises);

+ 2
- 2
src/views/nodes/searchNode.ts Wyświetl plik

@ -3,7 +3,7 @@ import { TreeItem, TreeItemCollapsibleState } from 'vscode';
import { SearchCommitsCommandArgs } from '../../commands';
import { GlyphChars } from '../../constants';
import { GitRepoSearchBy } from '../../git/gitService';
import { debug, Functions, gate, Iterables, log } from '../../system';
import { debug, gate, Iterables, log, Promises } from '../../system';
import { View } from '../viewBase';
import { CommandMessageNode, MessageNode } from './common';
import { ResourceType, unknownGitUri, ViewNode } from './viewNode';
@ -139,7 +139,7 @@ export class SearchNode extends ViewNode {
const promises: Promise<any>[] = [
...Iterables.filterMap(this._children, c => {
const result = c.refresh === undefined ? false : c.refresh();
return Functions.isPromise<boolean | void>(result) ? result : undefined;
return Promises.isPromise<boolean | void>(result) ? result : undefined;
})
];
await Promise.all(promises);

Ładowanie…
Anuluj
Zapisz