Skip to content

Commit ff18fe8

Browse files
committed
Renames decorators/function for better clarity
- 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
1 parent 266192b commit ff18fe8

File tree

6 files changed

+29
-13
lines changed

6 files changed

+29
-13
lines changed

src/commands/commandBase.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import type { TextEditor, TextEditorEdit } from 'vscode';
22
import { commands, Disposable } from 'vscode';
33
import type { GlCommands } from '../constants.commands';
44
import { registerCommand } from '../system/-webview/command';
5-
import { sequentialize } from '../system/function';
5+
import { runSequentially } from '../system/function';
66
import type { CommandContext } from './commandContext';
77
import type { CommandContextParsingOptions } from './commandContext.utils';
88
import { parseCommandContext } from './commandContext.utils';
@@ -41,7 +41,7 @@ export abstract class GlCommandBase implements Disposable {
4141

4242
// If there an array of contexts, then we want to execute the command for each
4343
if (Array.isArray(context)) {
44-
return sequentialize(
44+
return runSequentially(
4545
this.preExecute,
4646
context.map<[CommandContext, ...any[]]>((c: CommandContext) => [c, ...rest]),
4747
this,

src/plus/integrations/authentication/integrationAuthentication.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import type { Sources } from '../../../constants.telemetry';
88
import type { Container } from '../../../container';
99
import { gate } from '../../../system/decorators/-webview/gate';
1010
import { debug, log } from '../../../system/decorators/log';
11-
import { serialize } from '../../../system/decorators/serialize';
11+
import { sequentialize } from '../../../system/decorators/serialize';
1212
import type { DeferredEventExecutor } from '../../../system/event';
1313
import {
1414
isCloudSelfHostedIntegrationId,
@@ -480,7 +480,7 @@ class BuiltInAuthenticationProvider extends LocalIntegrationAuthenticationProvid
480480
}
481481

482482
@debug()
483-
@serialize()
483+
@sequentialize()
484484
override async getSession(
485485
descriptor?: IntegrationAuthenticationSessionDescriptor,
486486
options?: { createIfNeeded?: boolean; forceNewSession?: boolean },

src/system/decorators/serialize.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
export function serialize(): (target: any, key: string, descriptor: PropertyDescriptor) => void {
1+
export function sequentialize(): (target: any, key: string, descriptor: PropertyDescriptor) => void {
22
return (_target: any, key: string, descriptor: PropertyDescriptor) => {
33
// eslint-disable-next-line @typescript-eslint/no-unsafe-function-type
44
let fn: Function | undefined;
@@ -9,7 +9,7 @@ export function serialize(): (target: any, key: string, descriptor: PropertyDesc
99
}
1010
if (fn === undefined) throw new Error('Not supported');
1111

12-
const serializeKey = `$serialize$${key}`;
12+
const serializeKey = `$sequentialize$${key}`;
1313

1414
descriptor.value = function (this: any, ...args: any[]) {
1515
if (!Object.prototype.hasOwnProperty.call(this, serializeKey)) {

src/system/function.ts

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -213,18 +213,34 @@ export function disposableInterval(fn: (...args: any[]) => void, ms: number): Di
213213
return disposable;
214214
}
215215

216-
export async function sequentialize<T extends (...args: any[]) => unknown>(
216+
export async function runSequentially<T extends (...args: any[]) => unknown>(
217217
fn: T,
218-
argArray: Parameters<T>[],
218+
arrayOfArgs: Parameters<T>[],
219219
thisArg?: unknown,
220220
): Promise<any> {
221-
for (const args of argArray) {
221+
for (const args of arrayOfArgs) {
222222
try {
223223
void (await fn.apply(thisArg, args));
224224
} catch {}
225225
}
226226
}
227227

228+
export function sequentialize<T extends (...args: any[]) => Promise<any>>(fn: T): T {
229+
let promise: Promise<unknown> | undefined;
230+
231+
return function (...args: any[]): Promise<any> {
232+
// eslint-disable-next-line no-return-await, @typescript-eslint/no-unsafe-return
233+
const run = async () => await fn(...args);
234+
if (promise == null) {
235+
promise = run();
236+
} else {
237+
promise = promise.then(run, run);
238+
}
239+
240+
return promise;
241+
} as T;
242+
}
243+
228244
/**
229245
* Szudzik elegant pairing function
230246
* http://szudzik.com/ElegantPairing.pdf

src/views/viewCommands.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ import type { OpenWorkspaceLocation } from '../system/-webview/utils';
5151
import { openUrl, openWorkspace, revealInFileExplorer } from '../system/-webview/utils';
5252
import { filterMap } from '../system/array';
5353
import { log } from '../system/decorators/log';
54-
import { partial, sequentialize } from '../system/function';
54+
import { partial, runSequentially } from '../system/function';
5555
import { join, map } from '../system/iterable';
5656
import { DeepLinkActionType } from '../uris/deepLinks/deepLink';
5757
import type { LaunchpadItemNode } from './launchpadView';
@@ -122,7 +122,7 @@ export function registerViewCommand(
122122
}
123123

124124
// Execute the command for each node sequentially
125-
return sequentialize(
125+
return runSequentially(
126126
callback,
127127
nodes.map<[ViewNode, ...any[]]>(n => [n, ...rest]),
128128
thisArg,

src/webviews/webviewController.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import { executeCommand, executeCoreCommand } from '../system/-webview/command';
99
import { setContext } from '../system/-webview/context';
1010
import { getScopedCounter } from '../system/counter';
1111
import { debug, logName } from '../system/decorators/log';
12-
import { serialize } from '../system/decorators/serialize';
12+
import { sequentialize } from '../system/decorators/serialize';
1313
import { getLoggableName, Logger } from '../system/logger';
1414
import { getLogScope, getNewLogScope, setLogScopeExit } from '../system/logger.scope';
1515
import { pauseOnCancelOrTimeout } from '../system/promise';
@@ -689,7 +689,7 @@ export class WebviewController<
689689
}
690690
}
691691

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

0 commit comments

Comments
 (0)