Просмотр исходного кода

Renames promise members and removes "namespace"

main
Eric Amodio 2 лет назад
Родитель
Сommit
b85b135fdc
19 измененных файлов: 115 добавлений и 105 удалений
  1. +7
    -10
      src/annotations/autolinks.ts
  2. +6
    -5
      src/annotations/lineAnnotationController.ts
  3. +4
    -3
      src/commands/gitCommands.ts
  4. +6
    -5
      src/git/formatters/commitFormatter.ts
  5. +25
    -22
      src/git/gitProviderService.ts
  6. +6
    -3
      src/git/remotes/provider.ts
  7. +6
    -5
      src/hovers/hovers.ts
  8. +7
    -8
      src/quickpicks/commitPicker.ts
  9. +7
    -6
      src/statusbar/statusBarController.ts
  10. +0
    -1
      src/system.ts
  11. +1
    -1
      src/system/decorators/gate.ts
  12. +1
    -1
      src/system/decorators/log.ts
  13. +1
    -1
      src/system/decorators/timeout.ts
  14. +13
    -13
      src/system/promise.ts
  15. +3
    -2
      src/views/nodes/autolinkedItemsNode.ts
  16. +4
    -3
      src/views/nodes/resultsCommitsNode.ts
  17. +4
    -3
      src/views/nodes/resultsFilesNode.ts
  18. +6
    -3
      src/views/searchAndCompareView.ts
  19. +8
    -10
      src/views/viewBase.ts

+ 7
- 10
src/annotations/autolinks.ts Просмотреть файл

@ -5,7 +5,8 @@ import { GlyphChars } from '../constants';
import { Container } from '../container';
import { GitRemote, IssueOrPullRequest } from '../git/models';
import { Logger } from '../logger';
import { Dates, debug, Encoding, Iterables, Promises, Strings } from '../system';
import { Dates, debug, Encoding, Iterables, Strings } from '../system';
import { PromiseCancelledError, raceAll } from '../system/promise';
const numRegex = /<num>/g;
@ -89,11 +90,7 @@ export class Autolinks implements Disposable {
if (ids.size === 0) return undefined;
const issuesOrPullRequests = await Promises.raceAll(
ids.values(),
id => provider.getIssueOrPullRequest(id),
timeout,
);
const issuesOrPullRequests = await raceAll(ids.values(), id => provider.getIssueOrPullRequest(id), timeout);
if (issuesOrPullRequests.size === 0 || Iterables.every(issuesOrPullRequests.values(), pr => pr === undefined)) {
return undefined;
}
@ -113,7 +110,7 @@ export class Autolinks implements Disposable {
text: string,
markdown: boolean,
remotes?: GitRemote[],
issuesOrPullRequests?: Map<string, IssueOrPullRequest | Promises.CancellationError | undefined>,
issuesOrPullRequests?: Map<string, IssueOrPullRequest | PromiseCancelledError | undefined>,
footnotes?: Map<number, string>,
) {
for (const ref of this._references) {
@ -143,7 +140,7 @@ export class Autolinks implements Disposable {
private ensureAutolinkCached(
ref: CacheableAutolinkReference | DynamicAutolinkReference,
issuesOrPullRequests?: Map<string, IssueOrPullRequest | Promises.CancellationError | undefined>,
issuesOrPullRequests?: Map<string, IssueOrPullRequest | PromiseCancelledError | undefined>,
): ref is CacheableAutolinkReference | DynamicAutolinkReference {
if (isDynamic(ref)) return true;
@ -182,7 +179,7 @@ export class Autolinks implements Disposable {
title = ` "${ref.title.replace(numRegex, num)}`;
if (issue != null) {
if (issue instanceof Promises.CancellationError) {
if (issue instanceof PromiseCancelledError) {
title += `\n${GlyphChars.Dash.repeat(2)}\nDetails timed out`;
} else {
const issueTitle = issue.title.replace(/([")\\])/g, '\\$1').trim();
@ -225,7 +222,7 @@ export class Autolinks implements Disposable {
footnotes.set(
index,
`${linkText}: ${
issue instanceof Promises.CancellationError
issue instanceof PromiseCancelledError
? 'Details timed out'
: `${issue.title} ${GlyphChars.Dot} ${
issue.closed ? 'Closed' : 'Opened'

+ 6
- 5
src/annotations/lineAnnotationController.ts Просмотреть файл

@ -18,7 +18,8 @@ import { CommitFormatter } from '../git/formatters';
import { GitBlameCommit, PullRequest } from '../git/models';
import { Authentication } from '../git/remotes/provider';
import { LogCorrelationContext, Logger } from '../logger';
import { debug, Iterables, log, Promises } from '../system';
import { debug, Iterables, log } from '../system';
import { PromiseCancelledError, PromiseCancelledErrorWithId, raceAll } from '../system/promise';
import { LinesChangeEvent, LineSelection } from '../trackers/gitLineTracker';
import { Annotations } from './annotations';
@ -170,7 +171,7 @@ export class LineAnnotationController implements Disposable {
if (refs.size === 0) return undefined;
const { provider } = remote;
const prs = await Promises.raceAll(
const prs = await raceAll(
refs.values(),
ref => this.container.git.getPullRequestForCommit(ref, provider),
timeout,
@ -335,21 +336,21 @@ export class LineAnnotationController implements Disposable {
editor: TextEditor,
prs: Map<
string,
PullRequest | Promises.CancellationErrorWithId<string, Promise<PullRequest | undefined>> | undefined
PullRequest | PromiseCancelledErrorWithId<string, Promise<PullRequest | undefined>> | undefined
>,
cancellationToken: CancellationToken,
timeout: number,
cc: LogCorrelationContext | undefined,
) {
// If there are any PRs that timed out, refresh the annotation(s) once they complete
const count = Iterables.count(prs.values(), pr => pr instanceof Promises.CancellationError);
const count = Iterables.count(prs.values(), pr => pr instanceof PromiseCancelledError);
if (cancellationToken.isCancellationRequested || count === 0) return;
Logger.debug(cc, `${GlyphChars.Dot} ${count} pull request queries took too long (over ${timeout} ms)`);
const resolved = new Map<string, PullRequest | undefined>();
for (const [key, value] of prs) {
resolved.set(key, value instanceof Promises.CancellationError ? await value.promise : value);
resolved.set(key, value instanceof PromiseCancelledError ? await value.promise : value);
}
if (cancellationToken.isCancellationRequested || editor !== this._editor) return;

+ 4
- 3
src/commands/gitCommands.ts Просмотреть файл

@ -5,7 +5,8 @@ import { Usage, WorkspaceState } from '../constants';
import type { Container } from '../container';
import { KeyMapping } from '../keyboard';
import { Directive, DirectiveQuickPickItem } from '../quickpicks';
import { log, Promises } from '../system';
import { log } from '../system/decorators/log';
import { isPromise } from '../system/promise';
import { command, Command, CommandContext, Commands } from './common';
import { BranchGitCommand, BranchGitCommandArgs } from './git/branch';
import { CherryPickGitCommand, CherryPickGitCommandArgs } from './git/cherry-pick';
@ -334,7 +335,7 @@ export class GitCommandsCommand extends Command {
return;
}
if (Promises.is(result)) {
if (isPromise(result)) {
input.buttons = this.getButtons(step, commandsStep.command);
}
@ -506,7 +507,7 @@ export class GitCommandsCommand extends Command {
return;
}
if (Promises.is(result)) {
if (isPromise(result)) {
quickpick.buttons = this.getButtons(
activeCommand != null ? activeCommand.value : step,
activeCommand ?? commandsStep.command,

+ 6
- 5
src/git/formatters/commitFormatter.ts Просмотреть файл

@ -16,7 +16,8 @@ import { DateStyle, FileAnnotationType } from '../../configuration';
import { GlyphChars } from '../../constants';
import { Container } from '../../container';
import { emojify } from '../../emojis';
import { Iterables, Promises, Strings } from '../../system';
import { Iterables, Strings } from '../../system';
import { PromiseCancelledError } from '../../system/promise';
import { ContactPresence } from '../../vsls/vsls';
import { GitUri } from '../gitUri';
import { GitCommit, GitLogCommit, GitRemote, GitRevision, IssueOrPullRequest, PullRequest } from '../models';
@ -26,7 +27,7 @@ import { FormatOptions, Formatter } from './formatter';
const emptyStr = '';
export interface CommitFormatOptions extends FormatOptions {
autolinkedIssuesOrPullRequests?: Map<string, IssueOrPullRequest | Promises.CancellationError | undefined>;
autolinkedIssuesOrPullRequests?: Map<string, IssueOrPullRequest | PromiseCancelledError | undefined>;
avatarSize?: number;
dateStyle?: DateStyle;
editor?: { line: number; uri: Uri };
@ -36,7 +37,7 @@ export interface CommitFormatOptions extends FormatOptions {
messageAutolinks?: boolean;
messageIndent?: number;
messageTruncateAtNewLine?: boolean;
pullRequestOrRemote?: PullRequest | Promises.CancellationError | GitRemote;
pullRequestOrRemote?: PullRequest | PromiseCancelledError | GitRemote;
pullRequestPendingMessage?: string;
presence?: ContactPresence;
previousLineDiffUris?: { current: GitUri; previous: GitUri | undefined };
@ -358,7 +359,7 @@ export class CommitFormatter extends Formatter {
}\n${GlyphChars.Dash.repeat(2)}\n${Strings.escapeMarkdown(pr.title).replace(/"/g, '\\"')}\n${
pr.state
}, ${pr.formatDateFromNow()}")`;
} else if (pr instanceof Promises.CancellationError) {
} else if (pr instanceof PromiseCancelledError) {
commands += `${separator}[$(git-pull-request) PR $(loading~spin)](command:${Commands.RefreshHover} "Searching for a Pull Request (if any) that introduced this commit...")`;
} else if (pr.provider != null && Container.instance.config.integrations.enabled) {
commands += `${separator}[$(plug) Connect to ${pr.provider.name}${
@ -540,7 +541,7 @@ export class CommitFormatter extends Formatter {
} else {
text = `PR #${pr.id}`;
}
} else if (pr instanceof Promises.CancellationError) {
} else if (pr instanceof PromiseCancelledError) {
text = this._options.markdown
? `[PR $(loading~spin)](command:${Commands.RefreshHover} "Searching for a Pull Request (if any) that introduced this commit...")`
: this._options?.pullRequestPendingMessage ?? emptyStr;

+ 25
- 22
src/git/gitProviderService.ts Просмотреть файл

@ -29,9 +29,12 @@ import {
import type { Container } from '../container';
import { ProviderNotFoundError } from '../errors';
import { Logger } from '../logger';
import { Arrays, debug, gate, Iterables, log, Promises } from '../system';
import { groupByFilterMap, groupByMap } from '../system/array';
import { gate } from '../system/decorators/gate';
import { debug, log } from '../system/decorators/log';
import { count, filter, flatMap, map } from '../system/iterable';
import { isDescendent, normalizePath } from '../system/path';
import { PromiseOrValue } from '../system/promise';
import { cancellable, isPromise, PromiseCancelledError, PromiseOrValue } from '../system/promise';
import { CharCode } from '../system/string';
import { vslsUriPrefixRegex } from '../vsls/vsls';
import { GitProvider, GitProviderDescriptor, GitProviderId, PagedResult, ScmRepository } from './gitProvider';
@ -117,7 +120,7 @@ export class GitProviderService implements Disposable {
} else if (added?.length) {
// If a provider was added, only preserve paths with a resolved repoPath
for (const [key, value] of this._pathToRepoPathCache) {
if (value === null || Promises.is(value)) {
if (value === null || isPromise(value)) {
this._pathToRepoPathCache.delete(key);
}
}
@ -141,7 +144,7 @@ export class GitProviderService implements Disposable {
} else if (added?.length) {
// If a repository was added, only preserve paths with a resolved repoPath
for (const [key, value] of this._pathToRepoPathCache) {
if (value === null || Promises.is(value)) {
if (value === null || isPromise(value)) {
this._pathToRepoPathCache.delete(key);
}
}
@ -263,18 +266,18 @@ export class GitProviderService implements Disposable {
}
get registeredProviders(): GitProviderDescriptor[] {
return [...Iterables.map(this._providers.values(), p => ({ ...p.descriptor }))];
return [...map(this._providers.values(), p => ({ ...p.descriptor }))];
}
get openRepositories(): Repository[] {
const repositories = [...Iterables.filter(this.repositories, r => !r.closed)];
const repositories = [...filter(this.repositories, r => !r.closed)];
if (repositories.length === 0) return repositories;
return Repository.sort(repositories);
}
get openRepositoryCount(): number {
return Iterables.count(this.repositories, r => !r.closed);
return count(this.repositories, r => !r.closed);
}
get repositories(): Iterable<Repository> {
@ -423,14 +426,14 @@ export class GitProviderService implements Disposable {
}
getOpenRepositories(id: GitProviderId): Iterable<Repository> {
return Iterables.filter(this.repositories, r => !r.closed && (id == null || id === r.provider.id));
return filter(this.repositories, r => !r.closed && (id == null || id === r.provider.id));
}
getOpenRepositoriesByProvider(): Map<GitProviderId, Repository[]> {
const repositories = [...Iterables.filter(this.repositories, r => !r.closed)];
const repositories = [...filter(this.repositories, r => !r.closed)];
if (repositories.length === 0) return new Map();
return Arrays.groupByMap(repositories, r => r.provider.id);
return groupByMap(repositories, r => r.provider.id);
}
private _discoveredWorkspaceFolders = new Map<WorkspaceFolder, Promise<Repository[]>>();
@ -451,8 +454,8 @@ export class GitProviderService implements Disposable {
const results = await Promise.allSettled(promises);
const repositories = Iterables.flatMap<PromiseFulfilledResult<Repository[]>, Repository>(
Iterables.filter<PromiseSettledResult<Repository[]>, PromiseFulfilledResult<Repository[]>>(
const repositories = flatMap<PromiseFulfilledResult<Repository[]>, Repository>(
filter<PromiseSettledResult<Repository[]>, PromiseFulfilledResult<Repository[]>>(
results,
(r): r is PromiseFulfilledResult<Repository[]> => r.status === 'fulfilled',
),
@ -927,7 +930,7 @@ export class GitProviderService implements Disposable {
this.getTags(repoPath),
]);
const branchesAndTagsBySha = Arrays.groupByFilterMap(
const branchesAndTagsBySha = groupByFilterMap(
(branches as (GitBranch | GitTag)[]).concat(tags as (GitBranch | GitTag)[]),
bt => bt.sha,
bt => {
@ -1314,18 +1317,18 @@ export class GitProviderService implements Disposable {
}
let promiseOrPR = provider.getPullRequestForBranch(branch, options);
if (promiseOrPR == null || !Promises.is(promiseOrPR)) {
if (promiseOrPR == null || !isPromise(promiseOrPR)) {
return promiseOrPR;
}
if (timeout != null && timeout > 0) {
promiseOrPR = Promises.cancellable(promiseOrPR, timeout);
promiseOrPR = cancellable(promiseOrPR, timeout);
}
try {
return await promiseOrPR;
} catch (ex) {
if (ex instanceof Promises.CancellationError) {
if (ex instanceof PromiseCancelledError) {
throw ex;
}
@ -1366,18 +1369,18 @@ export class GitProviderService implements Disposable {
}
let promiseOrPR = provider.getPullRequestForCommit(ref);
if (promiseOrPR == null || !Promises.is(promiseOrPR)) {
if (promiseOrPR == null || !isPromise(promiseOrPR)) {
return promiseOrPR;
}
if (options?.timeout != null && options.timeout > 0) {
promiseOrPR = Promises.cancellable(promiseOrPR, options.timeout);
promiseOrPR = cancellable(promiseOrPR, options.timeout);
}
try {
return await promiseOrPR;
} catch (ex) {
if (ex instanceof Promises.CancellationError) {
if (ex instanceof PromiseCancelledError) {
throw ex;
}
@ -1522,7 +1525,7 @@ export class GitProviderService implements Disposable {
let repoPathOrPromise = this._pathToRepoPathCache.get(filePath);
if (repoPathOrPromise !== undefined) {
rp = Promises.is(repoPathOrPromise) ? await repoPathOrPromise : repoPathOrPromise;
rp = isPromise(repoPathOrPromise) ? await repoPathOrPromise : repoPathOrPromise;
// If the repoPath is explicitly null, then we know no repo exists
if (rp === null) return undefined;
@ -1878,8 +1881,8 @@ export class GitProviderService implements Disposable {
@log()
async getOpenScmRepositories(): Promise<ScmRepository[]> {
const results = await Promise.allSettled([...this._providers.values()].map(p => p.getOpenScmRepositories()));
const repositories = Iterables.flatMap<PromiseFulfilledResult<ScmRepository[]>, ScmRepository>(
Iterables.filter<PromiseSettledResult<ScmRepository[]>, PromiseFulfilledResult<ScmRepository[]>>(
const repositories = flatMap<PromiseFulfilledResult<ScmRepository[]>, ScmRepository>(
filter<PromiseSettledResult<ScmRepository[]>, PromiseFulfilledResult<ScmRepository[]>>(
results,
(r): r is PromiseFulfilledResult<ScmRepository[]> => r.status === 'fulfilled',
),

+ 6
- 3
src/git/remotes/provider.ts Просмотреть файл

@ -15,7 +15,10 @@ import { WorkspaceState } from '../../constants';
import { Container } from '../../container';
import { AuthenticationError, ProviderRequestClientError } from '../../errors';
import { Logger } from '../../logger';
import { debug, Encoding, gate, log, Promises } from '../../system';
import { gate } from '../../system/decorators/gate';
import { debug, log } from '../../system/decorators/log';
import { encodeUrl } from '../../system/encoding';
import { isPromise } from '../../system/promise';
import {
Account,
DefaultBranch,
@ -235,7 +238,7 @@ export abstract class RemoteProvider implements RemoteProviderReference {
protected encodeUrl(url: string): string;
protected encodeUrl(url: string | undefined): string | undefined;
protected encodeUrl(url: string | undefined): string | undefined {
return Encoding.encodeUrl(url)?.replace(/#/g, '%23');
return encodeUrl(url)?.replace(/#/g, '%23');
}
}
@ -530,7 +533,7 @@ export abstract class RichRemoteProvider extends RemoteProvider {
pr = this.getPullRequestForCommitCore(ref);
this._prsByCommit.set(ref, pr);
}
if (pr == null || !Promises.is(pr)) return pr ?? undefined;
if (pr == null || !isPromise(pr)) return pr ?? undefined;
return pr.then(pr => pr ?? undefined);
}

+ 6
- 5
src/hovers/hovers.ts Просмотреть файл

@ -17,7 +17,8 @@ import {
PullRequest,
} from '../git/models';
import { Logger, LogLevel } from '../logger';
import { Iterables, Promises, Strings } from '../system';
import { Iterables, Strings } from '../system';
import { PromiseCancelledError } from '../system/promise';
export namespace Hovers {
export async function changesMessage(
@ -190,7 +191,7 @@ export namespace Hovers {
cancellationToken?: CancellationToken;
pullRequests?: {
enabled: boolean;
pr?: PullRequest | Promises.CancellationError<Promise<PullRequest | undefined>>;
pr?: PullRequest | PromiseCancelledError<Promise<PullRequest | undefined>>;
};
getBranchAndTagTips?: (
sha: string,
@ -296,7 +297,7 @@ export namespace Hovers {
if (autolinks != null && Logger.enabled(LogLevel.Debug)) {
// If there are any issues/PRs that timed out, log it
const count = Iterables.count(autolinks.values(), pr => pr instanceof Promises.CancellationError);
const count = Iterables.count(autolinks.values(), pr => pr instanceof PromiseCancelledError);
if (count !== 0) {
Logger.debug(
cc,
@ -309,7 +310,7 @@ export namespace Hovers {
// const pending = [
// ...Iterables.map(autolinks.values(), issueOrPullRequest =>
// issueOrPullRequest instanceof Promises.CancellationError
// issueOrPullRequest instanceof CancelledPromiseError
// ? issueOrPullRequest.promise
// : undefined,
// ),
@ -376,7 +377,7 @@ export namespace Hovers {
return pr;
} catch (ex) {
if (ex instanceof Promises.CancellationError) {
if (ex instanceof PromiseCancelledError) {
Logger.debug(cc, `timed out ${GlyphChars.Dot} ${Strings.getDurationMilliseconds(start)} ms`);
return ex;

+ 7
- 8
src/quickpicks/commitPicker.ts Просмотреть файл

@ -11,7 +11,8 @@ import {
DirectiveQuickPickItem,
getQuickPickIgnoreFocusOut,
} from '../quickpicks';
import { Iterables, Promises } from '../system';
import { filter, map } from '../system/iterable';
import { isPromise } from '../system/promise';
export namespace CommitPicker {
export async function show(
@ -33,7 +34,7 @@ export namespace CommitPicker {
quickpick.matchOnDescription = true;
quickpick.matchOnDetail = true;
if (Promises.is(log)) {
if (isPromise(log)) {
quickpick.busy = true;
quickpick.enabled = false;
quickpick.show();
@ -56,7 +57,7 @@ export namespace CommitPicker {
? [DirectiveQuickPickItem.create(Directive.Cancel)]
: [
...(options?.showOtherReferences ?? []),
...Iterables.map(log.commits.values(), commit =>
...map(log.commits.values(), commit =>
CommitQuickPickItem.create(commit, options?.picked === commit.ref, {
compact: true,
icon: true,
@ -206,7 +207,7 @@ export namespace StashPicker {
quickpick.matchOnDescription = true;
quickpick.matchOnDetail = true;
if (Promises.is(stash)) {
if (isPromise(stash)) {
quickpick.busy = true;
quickpick.enabled = false;
quickpick.show();
@ -217,10 +218,8 @@ export namespace StashPicker {
if (stash != null) {
quickpick.items = [
...(options?.showOtherReferences ?? []),
...Iterables.map(
options?.filter != null
? Iterables.filter(stash.commits.values(), options.filter)
: stash.commits.values(),
...map(
options?.filter != null ? filter(stash.commits.values(), options.filter) : stash.commits.values(),
commit =>
CommitQuickPickItem.create(commit, options?.picked === commit.ref, {
compact: true,

+ 7
- 6
src/statusbar/statusBarController.ts Просмотреть файл

@ -19,7 +19,8 @@ import { CommitFormatter } from '../git/formatters';
import { GitBlameCommit, PullRequest } from '../git/models';
import { Hovers } from '../hovers/hovers';
import { LogCorrelationContext, Logger } from '../logger';
import { debug, Promises } from '../system';
import { debug } from '../system/decorators/log';
import { PromiseCancelledError } from '../system/promise';
import { LinesChangeEvent } from '../trackers/gitLineTracker';
export class StatusBarController implements Disposable {
@ -333,7 +334,7 @@ export class StatusBarController implements Disposable {
private async getPullRequest(
commit: GitBlameCommit,
{ timeout }: { timeout?: number } = {},
): Promise<PullRequest | Promises.CancellationError<Promise<PullRequest | undefined>> | undefined> {
): Promise<PullRequest | PromiseCancelledError<Promise<PullRequest | undefined>> | undefined> {
const remote = await this.container.git.getRichRemoteProvider(commit.repoPath);
if (remote?.provider == null) return undefined;
@ -341,7 +342,7 @@ export class StatusBarController implements Disposable {
try {
return await this.container.git.getPullRequestForCommit(commit.ref, provider, { timeout: timeout });
} catch (ex) {
return ex instanceof Promises.CancellationError ? ex : undefined;
return ex instanceof PromiseCancelledError ? ex : undefined;
}
}
@ -357,7 +358,7 @@ export class StatusBarController implements Disposable {
| undefined,
pullRequests: {
enabled: boolean;
pr: PullRequest | Promises.CancellationError<Promise<PullRequest | undefined>> | undefined | undefined;
pr: PullRequest | PromiseCancelledError<Promise<PullRequest | undefined>> | undefined | undefined;
},
cancellationToken: CancellationToken,
) {
@ -386,12 +387,12 @@ export class StatusBarController implements Disposable {
private async waitForPendingPullRequest(
editor: TextEditor,
commit: GitBlameCommit,
pr: PullRequest | Promises.CancellationError<Promise<PullRequest | undefined>> | undefined,
pr: PullRequest | PromiseCancelledError<Promise<PullRequest | undefined>> | undefined,
cancellationToken: CancellationToken,
timeout: number,
cc: LogCorrelationContext | undefined,
) {
if (cancellationToken.isCancellationRequested || !(pr instanceof Promises.CancellationError)) return;
if (cancellationToken.isCancellationRequested || !(pr instanceof PromiseCancelledError)) return;
// If the PR timed out, refresh the status bar once it completes
Logger.debug(cc, `${GlyphChars.Dot} pull request query took too long (over ${timeout} ms)`);

+ 0
- 1
src/system.ts Просмотреть файл

@ -29,7 +29,6 @@ export * as Encoding from './system/encoding';
export * as Functions from './system/function';
export * as Iterables from './system/iterable';
export * as Objects from './system/object';
export * as Promises from './system/promise';
export * from './system/searchTree';
export * from './system/stopwatch';
export * as Strings from './system/string';

+ 1
- 1
src/system/decorators/gate.ts Просмотреть файл

@ -1,5 +1,5 @@
'use strict';
import { is as isPromise } from '../promise';
import { isPromise } from '../promise';
import { resolveProp } from './resolver';
export function gate<T extends (...arg: any) => any>(resolver?: (...args: Parameters<T>) => string) {

+ 1
- 1
src/system/decorators/log.ts Просмотреть файл

@ -3,7 +3,7 @@ import { hrtime } from '@env/hrtime';
import { LogCorrelationContext, Logger, LogLevel } from '../../logger';
import { filterMap } from '../array';
import { getParameters } from '../function';
import { is as isPromise } from '../promise';
import { isPromise } from '../promise';
import { getDurationMilliseconds } from '../string';
const emptyStr = '';

+ 1
- 1
src/system/decorators/timeout.ts Просмотреть файл

@ -1,5 +1,5 @@
'use strict';
import { cancellable, is as isPromise } from '../promise';
import { cancellable, isPromise } from '../promise';
export function timeout(timeout: number): any;
export function timeout(timeoutFromLastArg: true, defaultTimeout?: number): any;

+ 13
- 13
src/system/promise.ts Просмотреть файл

@ -33,13 +33,13 @@ export function any(...promises: Promise[]): Promise {
});
}
export class CancellationError<T extends Promise<any> = Promise<any>> extends Error {
export class PromiseCancelledError<T extends Promise<any> = Promise<any>> extends Error {
constructor(public readonly promise: T, message: string) {
super(message);
}
}
export class CancellationErrorWithId<TKey, T extends Promise<any> = Promise<any>> extends CancellationError<T> {
export class PromiseCancelledErrorWithId<TKey, T extends Promise<any> = Promise<any>> extends PromiseCancelledError<T> {
constructor(public readonly id: TKey, promise: T, message: string) {
super(promise, message);
}
@ -65,7 +65,7 @@ export function cancellable(
if (typeof options.onDidCancel === 'function') {
options.onDidCancel(resolve, reject);
} else {
reject(new CancellationError(promise, options.cancelMessage ?? 'TIMED OUT'));
reject(new PromiseCancelledError(promise, options.cancelMessage ?? 'TIMED OUT'));
}
}, timeoutOrToken);
} else {
@ -76,7 +76,7 @@ export function cancellable(
if (typeof options.onDidCancel === 'function') {
options.onDidCancel(resolve, reject);
} else {
reject(new CancellationError(promise, options.cancelMessage ?? 'CANCELLED'));
reject(new PromiseCancelledError(promise, options.cancelMessage ?? 'CANCELLED'));
}
});
}
@ -102,23 +102,23 @@ export function cancellable(
});
}
export function is<T>(obj: PromiseLike<T> | T): obj is Promise<T> {
export function isPromise<T>(obj: PromiseLike<T> | T): obj is Promise<T> {
return obj instanceof Promise || typeof (obj as PromiseLike<T>)?.then === 'function';
}
export function raceAll<TPromise>(
promises: Promise<TPromise>[],
timeout?: number,
): Promise<(TPromise | CancellationError<Promise<TPromise>>)[]>;
): Promise<(TPromise | PromiseCancelledError<Promise<TPromise>>)[]>;
export function raceAll<TPromise, T>(
promises: Map<T, Promise<TPromise>>,
timeout?: number,
): Promise<Map<T, TPromise | CancellationErrorWithId<T, Promise<TPromise>>>>;
): Promise<Map<T, TPromise | PromiseCancelledErrorWithId<T, Promise<TPromise>>>>;
export function raceAll<TPromise, T>(
ids: Iterable<T>,
fn: (id: T) => Promise<TPromise>,
timeout?: number,
): Promise<Map<T, TPromise | CancellationErrorWithId<T, Promise<TPromise>>>>;
): Promise<Map<T, TPromise | PromiseCancelledErrorWithId<T, Promise<TPromise>>>>;
export async function raceAll<TPromise, T>(
promisesOrIds: Promise<TPromise>[] | Map<T, Promise<TPromise>> | Iterable<T>,
timeoutOrFn?: number | ((id: T) => Promise<TPromise>),
@ -135,7 +135,7 @@ export async function raceAll(
if (promises instanceof Map) {
return new Map(
await Promise.all(
map<[T, Promise<TPromise>], Promise<[T, TPromise | CancellationErrorWithId<T, Promise<TPromise>>]>>(
map<[T, Promise<TPromise>], Promise<[T, TPromise | PromiseCancelledErrorWithId<T, Promise<TPromise>>]>>(
promises.entries(),
timeout == null
? ([id, promise]) => promise.then(p => [id, p])
@ -143,9 +143,9 @@ export async function raceAll(
Promise.race([
promise,
new Promise<CancellationErrorWithId<T, Promise<TPromise>>>(resolve =>
new Promise<PromiseCancelledErrorWithId<T, Promise<TPromise>>>(resolve =>
setTimeout(
() => resolve(new CancellationErrorWithId(id, promise, 'TIMED OUT')),
() => resolve(new PromiseCancelledErrorWithId(id, promise, 'TIMED OUT')),
timeout,
),
),
@ -161,8 +161,8 @@ export async function raceAll(
: promises.map(p =>
Promise.race([
p,
new Promise<CancellationError<Promise<TPromise>>>(resolve =>
setTimeout(() => resolve(new CancellationError(p, 'TIMED OUT')), timeout),
new Promise<PromiseCancelledError<Promise<TPromise>>>(resolve =>
setTimeout(() => resolve(new PromiseCancelledError(p, 'TIMED OUT')), timeout),
),
]),
),

+ 3
- 2
src/views/nodes/autolinkedItemsNode.ts Просмотреть файл

@ -3,7 +3,8 @@ import { TreeItem, TreeItemCollapsibleState } from 'vscode';
import { GitUri } from '../../git/gitUri';
import { GitFile, GitLog, GitRemote, IssueOrPullRequest, PullRequest } from '../../git/models';
import { RichRemoteProvider } from '../../git/remotes/provider';
import { debug, gate, Promises } from '../../system';
import { debug, gate } from '../../system';
import { PromiseCancelledErrorWithId } from '../../system/promise';
import { ViewsWithCommits } from '../viewBase';
import { AutolinkedItemNode } from './autolinkedItemNode';
import { MessageNode } from './common';
@ -53,7 +54,7 @@ export class AutolinkedItemsNode extends ViewNode {
if (autolinkedMapResult.status === 'fulfilled' && autolinkedMapResult.value != null) {
for (const [id, issue] of autolinkedMapResult.value) {
if (issue == null || issue instanceof Promises.CancellationErrorWithId) continue;
if (issue == null || issue instanceof PromiseCancelledErrorWithId) continue;
items.set(id, issue);
}

+ 4
- 3
src/views/nodes/resultsCommitsNode.ts Просмотреть файл

@ -2,7 +2,8 @@
import { TreeItem, TreeItemCollapsibleState } from 'vscode';
import { GitUri } from '../../git/gitUri';
import { GitLog } from '../../git/models';
import { debug, gate, Iterables, Promises } from '../../system';
import { debug, gate, Iterables } from '../../system';
import { cancellable, PromiseCancelledError } from '../../system/promise';
import { ViewsWithCommits } from '../viewBase';
import { AutolinkedItemsNode } from './autolinkedItemsNode';
import { CommitNode } from './commitNode';
@ -126,7 +127,7 @@ export class ResultsCommitsNode
} else {
try {
let log;
({ label, log } = await Promises.cancellable(this.getCommitsQueryResults(), 100));
({ label, log } = await cancellable(this.getCommitsQueryResults(), 100));
state =
log == null || log.count === 0
? TreeItemCollapsibleState.None
@ -134,7 +135,7 @@ export class ResultsCommitsNode
? TreeItemCollapsibleState.Expanded
: TreeItemCollapsibleState.Collapsed;
} catch (ex) {
if (ex instanceof Promises.CancellationError) {
if (ex instanceof PromiseCancelledError) {
ex.promise.then(() => this.triggerChange(false));
}

+ 4
- 3
src/views/nodes/resultsFilesNode.ts Просмотреть файл

@ -3,8 +3,9 @@ import { ThemeIcon, TreeItem, TreeItemCollapsibleState } from 'vscode';
import { ViewFilesLayout } from '../../configuration';
import { GitUri } from '../../git/gitUri';
import { GitFile } from '../../git/models';
import { Arrays, debug, gate, Iterables, Promises, Strings } from '../../system';
import { Arrays, debug, gate, Iterables, Strings } from '../../system';
import { joinPaths, normalizePath } from '../../system/path';
import { cancellable, PromiseCancelledError } from '../../system/promise';
import { ViewsWithCommits } from '../viewBase';
import { FileNode, FolderNode } from './folderNode';
import { ResultsFileNode } from './resultsFileNode';
@ -98,7 +99,7 @@ export class ResultsFilesNode extends ViewNode {
let state;
try {
const results = await Promises.cancellable(this.getFilesQueryResults(), 100);
const results = await cancellable(this.getFilesQueryResults(), 100);
label = results.label;
files = (this.filtered ? results.filtered?.files : undefined) ?? results.files;
@ -114,7 +115,7 @@ export class ResultsFilesNode extends ViewNode {
? TreeItemCollapsibleState.Expanded
: TreeItemCollapsibleState.Collapsed;
} catch (ex) {
if (ex instanceof Promises.CancellationError) {
if (ex instanceof PromiseCancelledError) {
ex.promise.then(() => queueMicrotask(() => this.triggerChange(false)));
}

+ 6
- 3
src/views/searchAndCompareView.ts Просмотреть файл

@ -8,7 +8,10 @@ import { GitUri } from '../git/gitUri';
import { GitLog, GitRevision } from '../git/models';
import { SearchPattern } from '../git/search';
import { ReferencePicker, ReferencesQuickPickIncludes } from '../quickpicks';
import { debug, gate, Iterables, log, Promises } from '../system';
import { filterMap } from '../system/array';
import { gate } from '../system/decorators/gate';
import { debug, log } from '../system/decorators/log';
import { isPromise } from '../system/promise';
import {
CompareResultsNode,
ContextValues,
@ -121,9 +124,9 @@ export class SearchAndCompareViewNode extends ViewNode {
if (this.children.length === 0) return;
const promises: Promise<any>[] = [
...Iterables.filterMap(this.children, c => {
...filterMap(this.children, c => {
const result = c.refresh === undefined ? false : c.refresh();
return Promises.is<boolean | void>(result) ? result : undefined;
return isPromise<boolean | void>(result) ? result : undefined;
}),
];
await Promise.all(promises);

+ 8
- 10
src/views/viewBase.ts Просмотреть файл

@ -34,7 +34,9 @@ import {
} from '../configuration';
import { Container } from '../container';
import { Logger } from '../logger';
import { debug, Functions, log, Promises } from '../system';
import { debug, log } from '../system/decorators/log';
import { debounce } from '../system/function';
import { cancellable, isPromise } from '../system/promise';
import { BranchesView } from './branchesView';
import { CommitsView } from './commitsView';
import { ContributorsView } from './contributorsView';
@ -240,7 +242,7 @@ export abstract class ViewBase<
this.onConfigurationChanged(e);
}, this),
this.tree,
this.tree.onDidChangeVisibility(Functions.debounce(this.onVisibilityChanged, 250), this),
this.tree.onDidChangeVisibility(debounce(this.onVisibilityChanged, 250), this),
this.tree.onDidCollapseElement(this.onElementCollapsed, this),
this.tree.onDidExpandElement(this.onElementExpanded, this),
);
@ -394,7 +396,7 @@ export abstract class ViewBase<
if (predicate(node)) return node;
if (canTraverse != null) {
const traversable = canTraverse(node);
if (Promises.is(traversable)) {
if (isPromise(traversable)) {
if (!(await traversable)) continue;
} else if (!traversable) {
continue;
@ -418,13 +420,9 @@ export abstract class ViewBase<
await this.loadMoreNodeChildren(node, defaultPageSize);
pagedChildren = await Promises.cancellable(
Promise.resolve(node.getChildren()),
token ?? 60000,
{
onDidCancel: resolve => resolve([]),
},
);
pagedChildren = await cancellable(Promise.resolve(node.getChildren()), token ?? 60000, {
onDidCancel: resolve => resolve([]),
});
child = pagedChildren.find(predicate);
if (child != null) return child;

Загрузка…
Отмена
Сохранить