-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Add invoke support for web pubsub client #36750
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
vicancy
wants to merge
1
commit into
Azure:main
Choose a base branch
from
vicancy:invoke
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
161 changes: 161 additions & 0 deletions
161
sdk/web-pubsub/web-pubsub-client/src/invocationManager.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,161 @@ | ||
| // Copyright (c) Microsoft Corporation. | ||
| // Licensed under the MIT License. | ||
|
|
||
| import type { AbortSignalLike } from "@azure/abort-controller"; | ||
| import type { InvokeResponseMessage } from "./models/messages.js"; | ||
| import { InvocationError } from "./errors/index.js"; | ||
|
|
||
| export interface InvocationWaitOptions { | ||
| abortSignal?: AbortSignalLike; | ||
| } | ||
|
|
||
| export interface InvocationRegistration { | ||
| invocationId: string; | ||
| wait(options?: InvocationWaitOptions): Promise<InvokeResponseMessage>; | ||
| } | ||
|
|
||
| /** | ||
| * Manages pending invocations awaiting invokeResponse frames. | ||
| */ | ||
| export class InvocationManager { | ||
| private readonly _entries = new Map<string, InvocationEntity>(); | ||
| private _nextId = 0; | ||
|
|
||
| public registerInvocation(invocationId?: string): InvocationRegistration { | ||
| const resolvedId = invocationId ?? this._generateInvocationId(); | ||
| if (this._entries.has(resolvedId)) { | ||
| throw new InvocationError("Invocation id is already registered.", { | ||
| invocationId: resolvedId, | ||
| }); | ||
| } | ||
|
|
||
| const entity = new InvocationEntity(resolvedId); | ||
| this._entries.set(resolvedId, entity); | ||
| return { | ||
| invocationId: resolvedId, | ||
| wait: (options?: InvocationWaitOptions) => this._waitForEntry(entity, options), | ||
| }; | ||
| } | ||
|
|
||
| public resolveInvocation(message: InvokeResponseMessage): boolean { | ||
| const entry = this._entries.get(message.invocationId); | ||
| if (!entry) { | ||
| return false; | ||
| } | ||
| this._entries.delete(message.invocationId); | ||
| entry.resolve(message); | ||
| return true; | ||
| } | ||
|
|
||
| public rejectInvocation(invocationId: string, reason: unknown): boolean { | ||
| const entry = this._entries.get(invocationId); | ||
| if (!entry) { | ||
| return false; | ||
| } | ||
| this._entries.delete(invocationId); | ||
| entry.reject(reason); | ||
| return true; | ||
| } | ||
|
|
||
| public discard(invocationId: string): void { | ||
| this._entries.delete(invocationId); | ||
| } | ||
|
|
||
| public rejectAll(createReason: (invocationId: string) => unknown): void { | ||
| this._entries.forEach((entry, invocationId) => { | ||
| if (this._entries.delete(invocationId)) { | ||
| entry.reject(createReason(invocationId)); | ||
| } | ||
| }); | ||
| } | ||
|
|
||
| private _waitForEntry( | ||
| entry: InvocationEntity, | ||
| options?: InvocationWaitOptions, | ||
| ): Promise<InvokeResponseMessage> { | ||
| const waitPromise = entry.promise(); | ||
| const abortSignal = options?.abortSignal; | ||
|
|
||
| if (!abortSignal) { | ||
| return waitPromise; | ||
| } | ||
|
|
||
| if (abortSignal.aborted) { | ||
| if (this._entries.delete(entry.invocationId)) { | ||
| entry.reject(this._createAbortError(entry.invocationId)); | ||
| } | ||
| return waitPromise; | ||
| } | ||
|
|
||
| return new Promise<InvokeResponseMessage>((resolve, reject) => { | ||
| const onAbort = (): void => { | ||
| abortSignal.removeEventListener("abort", onAbort); | ||
| if (this._entries.delete(entry.invocationId)) { | ||
| entry.reject(this._createAbortError(entry.invocationId)); | ||
| } | ||
| }; | ||
|
|
||
| abortSignal.addEventListener("abort", onAbort); | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. these listeners may leak if |
||
|
|
||
| waitPromise | ||
| .then((result) => { | ||
| abortSignal.removeEventListener("abort", onAbort); | ||
| resolve(result); | ||
| }) | ||
| .catch((err) => { | ||
| abortSignal.removeEventListener("abort", onAbort); | ||
| reject(err); | ||
| }); | ||
| }); | ||
| } | ||
|
|
||
| private _generateInvocationId(): string { | ||
| this._nextId += 1; | ||
| return this._nextId.toString(); | ||
| } | ||
|
|
||
| private _createAbortError(invocationId: string): InvocationError { | ||
| return new InvocationError("Invocation cancelled by abortSignal.", { | ||
| invocationId, | ||
| }); | ||
| } | ||
| } | ||
|
|
||
| class InvocationEntity { | ||
| private readonly _promise: Promise<InvokeResponseMessage>; | ||
| private _resolve: | ||
| | ((value: InvokeResponseMessage | PromiseLike<InvokeResponseMessage>) => void) | ||
| | undefined; | ||
| private _reject: ((reason?: unknown) => void) | undefined; | ||
|
|
||
| constructor(public readonly invocationId: string) { | ||
| this._promise = new Promise<InvokeResponseMessage>((resolve, reject) => { | ||
| this._resolve = resolve; | ||
| this._reject = reject; | ||
| }); | ||
| } | ||
|
|
||
| public promise(): Promise<InvokeResponseMessage> { | ||
| return this._promise; | ||
| } | ||
|
|
||
| public resolve(value: InvokeResponseMessage | PromiseLike<InvokeResponseMessage>): void { | ||
| const callback = this._resolve; | ||
| if (!callback) { | ||
| return; | ||
| } | ||
| this._resolve = undefined; | ||
| this._reject = undefined; | ||
| callback(value); | ||
| } | ||
|
|
||
| public reject(reason?: unknown): void { | ||
| const callback = this._reject; | ||
| if (!callback) { | ||
| return; | ||
| } | ||
| this._resolve = undefined; | ||
| this._reject = undefined; | ||
| callback(reason); | ||
| } | ||
| } | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Add JSDoc comments to the
InvocationErrorOptionsinterface properties to be consistent with the pattern used forSendMessageErrorOptionsabove it.