Skip to content

Commit

Permalink
Renames decorators/function for better clarity
Browse files Browse the repository at this point in the history
 - Renames `serialize` decorator to `sequentialize`
 - Renames `sequentialize` function to `runSequentially`
 - Adds a new `sequentialize` which returns a new function which waits for previous calls to finish before executing the current
  • Loading branch information
eamodio committed Jan 22, 2025
1 parent 266192b commit ff18fe8
Show file tree
Hide file tree
Showing 6 changed files with 29 additions and 13 deletions.
4 changes: 2 additions & 2 deletions src/commands/commandBase.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import type { TextEditor, TextEditorEdit } from 'vscode';
import { commands, Disposable } from 'vscode';
import type { GlCommands } from '../constants.commands';
import { registerCommand } from '../system/-webview/command';
import { sequentialize } from '../system/function';
import { runSequentially } from '../system/function';
import type { CommandContext } from './commandContext';
import type { CommandContextParsingOptions } from './commandContext.utils';
import { parseCommandContext } from './commandContext.utils';
Expand Down Expand Up @@ -41,7 +41,7 @@ export abstract class GlCommandBase implements Disposable {

// If there an array of contexts, then we want to execute the command for each
if (Array.isArray(context)) {
return sequentialize(
return runSequentially(
this.preExecute,
context.map<[CommandContext, ...any[]]>((c: CommandContext) => [c, ...rest]),
this,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import type { Sources } from '../../../constants.telemetry';
import type { Container } from '../../../container';
import { gate } from '../../../system/decorators/-webview/gate';
import { debug, log } from '../../../system/decorators/log';
import { serialize } from '../../../system/decorators/serialize';
import { sequentialize } from '../../../system/decorators/serialize';
import type { DeferredEventExecutor } from '../../../system/event';
import {
isCloudSelfHostedIntegrationId,
Expand Down Expand Up @@ -480,7 +480,7 @@ class BuiltInAuthenticationProvider extends LocalIntegrationAuthenticationProvid
}

@debug()
@serialize()
@sequentialize()
override async getSession(
descriptor?: IntegrationAuthenticationSessionDescriptor,
options?: { createIfNeeded?: boolean; forceNewSession?: boolean },
Expand Down
4 changes: 2 additions & 2 deletions src/system/decorators/serialize.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
export function serialize(): (target: any, key: string, descriptor: PropertyDescriptor) => void {
export function sequentialize(): (target: any, key: string, descriptor: PropertyDescriptor) => void {
return (_target: any, key: string, descriptor: PropertyDescriptor) => {
// eslint-disable-next-line @typescript-eslint/no-unsafe-function-type
let fn: Function | undefined;
Expand All @@ -9,7 +9,7 @@ export function serialize(): (target: any, key: string, descriptor: PropertyDesc
}
if (fn === undefined) throw new Error('Not supported');

const serializeKey = `$serialize$${key}`;
const serializeKey = `$sequentialize$${key}`;

descriptor.value = function (this: any, ...args: any[]) {
if (!Object.prototype.hasOwnProperty.call(this, serializeKey)) {
Expand Down
22 changes: 19 additions & 3 deletions src/system/function.ts
Original file line number Diff line number Diff line change
Expand Up @@ -213,18 +213,34 @@ export function disposableInterval(fn: (...args: any[]) => void, ms: number): Di
return disposable;
}

export async function sequentialize<T extends (...args: any[]) => unknown>(
export async function runSequentially<T extends (...args: any[]) => unknown>(
fn: T,
argArray: Parameters<T>[],
arrayOfArgs: Parameters<T>[],
thisArg?: unknown,
): Promise<any> {
for (const args of argArray) {
for (const args of arrayOfArgs) {
try {
void (await fn.apply(thisArg, args));
} catch {}
}
}

export function sequentialize<T extends (...args: any[]) => Promise<any>>(fn: T): T {
let promise: Promise<unknown> | undefined;

return function (...args: any[]): Promise<any> {
// eslint-disable-next-line no-return-await, @typescript-eslint/no-unsafe-return
const run = async () => await fn(...args);
if (promise == null) {
promise = run();
} else {
promise = promise.then(run, run);
}

return promise;
} as T;
}

/**
* Szudzik elegant pairing function
* http://szudzik.com/ElegantPairing.pdf
Expand Down
4 changes: 2 additions & 2 deletions src/views/viewCommands.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ import type { OpenWorkspaceLocation } from '../system/-webview/utils';
import { openUrl, openWorkspace, revealInFileExplorer } from '../system/-webview/utils';
import { filterMap } from '../system/array';
import { log } from '../system/decorators/log';
import { partial, sequentialize } from '../system/function';
import { partial, runSequentially } from '../system/function';
import { join, map } from '../system/iterable';
import { DeepLinkActionType } from '../uris/deepLinks/deepLink';
import type { LaunchpadItemNode } from './launchpadView';
Expand Down Expand Up @@ -122,7 +122,7 @@ export function registerViewCommand(
}

// Execute the command for each node sequentially
return sequentialize(
return runSequentially(
callback,
nodes.map<[ViewNode, ...any[]]>(n => [n, ...rest]),
thisArg,
Expand Down
4 changes: 2 additions & 2 deletions src/webviews/webviewController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import { executeCommand, executeCoreCommand } from '../system/-webview/command';
import { setContext } from '../system/-webview/context';
import { getScopedCounter } from '../system/counter';
import { debug, logName } from '../system/decorators/log';
import { serialize } from '../system/decorators/serialize';
import { sequentialize } from '../system/decorators/serialize';
import { getLoggableName, Logger } from '../system/logger';
import { getLogScope, getNewLogScope, setLogScopeExit } from '../system/logger.scope';
import { pauseOnCancelOrTimeout } from '../system/promise';
Expand Down Expand Up @@ -689,7 +689,7 @@ export class WebviewController<
}
}

@serialize()
@sequentialize()
@debug<WebviewController<ID, State>['postMessage']>({
args: false,
enter: m => `(${m.id}|${m.method}${m.completionId ? `+${m.completionId}` : ''})`,
Expand Down

0 comments on commit ff18fe8

Please sign in to comment.