Skip to content

Commit

Permalink
[notification-hubs] Moving to core-lro v3 (#31002)
Browse files Browse the repository at this point in the history
### Packages impacted by this PR

- @azure/notification-hubs

### Issues associated with this PR


### Describe the problem that is addressed by this PR

Moves @azure/notification-hubs to @azure/core-lro v3

### What are the possible designs available to address the problem? If
there are more than one possible design, why was the one in this PR
chosen?


### Are there test cases added in this PR? _(If not, why?)_


### Provide a list of related PRs _(if any)_


### Command used to generate this PR:**_(Applicable only to SDK release
request PRs)_

### Checklists
- [ ] Added impacted package name to the issue description
- [ ] Does this PR needs any fixes in the SDK Generator?** _(If so,
create an Issue in the
[Autorest/typescript](https://github.com/Azure/autorest.typescript)
repository and link it here)_
- [ ] Added a changelog (if necessary)
  • Loading branch information
mpodwysocki authored Sep 5, 2024
1 parent c34e215 commit f169f9e
Show file tree
Hide file tree
Showing 7 changed files with 51 additions and 29 deletions.
6 changes: 5 additions & 1 deletion sdk/notificationhubs/notification-hubs/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
# Release History

## 1.2.4 (Unreleased)
## 2.0.0 (Unreleased)

### Features Added

- Moved to `@azure/core-lro` v3 for long running operations.

### Breaking Changes

- Moving from `@azure/core-lro` v2 to v3 for long running operations. For APIs such as `beginSubmitNotificationHubJob`, this is a breaking change as it uses the new `PollerLike` interface instead of the old `SimplePollerLike` interface. See the [Polling API](https://github.com/Azure/azure-sdk-for-js/blob/main/sdk/core/core-lro/docs/MIGRATION.md#poller-api) migration guide for more information.

### Bugs Fixed

- Added `pushChannelExpired` and `pushVariables` to the `Installation` type.
Expand Down
4 changes: 2 additions & 2 deletions sdk/notificationhubs/notification-hubs/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@azure/notification-hubs",
"version": "1.2.4",
"version": "2.0.0",
"description": "Azure Notification Hubs SDK for JavaScript",
"sdk-type": "client",
"main": "./dist/commonjs/index.js",
Expand Down Expand Up @@ -80,7 +80,7 @@
"dependencies": {
"@azure/core-auth": "^1.7.2",
"@azure/abort-controller": "^2.1.2",
"@azure/core-lro": "^2.7.2",
"@azure/core-lro": "^3.0.0",
"@azure/core-paging": "^1.6.2",
"@azure/core-rest-pipeline": "^1.15.2",
"@azure/core-tracing": "^1.1.2",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import { OperationState } from '@azure/core-lro';
import { PagedAsyncIterableIterator } from '@azure/core-paging';
import { PipelineRequest } from '@azure/core-rest-pipeline';
import { PipelineResponse } from '@azure/core-rest-pipeline';
import { SimplePollerLike } from '@azure/core-lro';
import { PollerLike } from '@azure/core-lro';

// @public
export function beginSubmitNotificationHubJob(context: NotificationHubsClientContext, notificationHubJob: NotificationHubJob, polledOperationOptions?: PolledOperationOptions): Promise<NotificationHubJobPoller>;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
import { ClientOptions } from '@azure-rest/core-client';
import { OperationOptions } from '@azure-rest/core-client';
import { OperationState } from '@azure/core-lro';
import { SimplePollerLike } from '@azure/core-lro';
import { PollerLike } from '@azure/core-lro';

// @public
export interface AdmInstallation extends DeviceTokenInstallation {
Expand Down Expand Up @@ -835,7 +835,7 @@ export interface NotificationHubJob {
}

// @public
export type NotificationHubJobPoller = SimplePollerLike<OperationState<NotificationHubJob>, NotificationHubJob>;
export type NotificationHubJobPoller = PollerLike<OperationState<NotificationHubJob>, NotificationHubJob>;

// @public
export type NotificationHubJobStatus =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import { ClientOptions } from '@azure-rest/core-client';
import { OperationOptions } from '@azure-rest/core-client';
import { OperationState } from '@azure/core-lro';
import { PagedAsyncIterableIterator } from '@azure/core-paging';
import { SimplePollerLike } from '@azure/core-lro';
import { PollerLike } from '@azure/core-lro';

// @public
export interface AdmInstallation extends DeviceTokenInstallation {
Expand Down Expand Up @@ -836,7 +836,7 @@ export interface NotificationHubJob {
}

// @public
export type NotificationHubJobPoller = SimplePollerLike<OperationState<NotificationHubJob>, NotificationHubJob>;
export type NotificationHubJobPoller = PollerLike<OperationState<NotificationHubJob>, NotificationHubJob>;

// @public
export type NotificationHubJobStatus =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
// Licensed under the MIT License.

import { AbortSignalLike } from "@azure/abort-controller";
import { CancelOnProgress, OperationState, SimplePollerLike } from "@azure/core-lro";
import { CancelOnProgress, OperationState, PollerLike } from "@azure/core-lro";
import { NotificationHubJob, NotificationHubJobPoller } from "../models/notificationHubJob.js";
import { NotificationHubsClientContext } from "./index.js";
import { PolledOperationOptions } from "../models/options.js";
Expand Down Expand Up @@ -38,12 +38,13 @@ export async function beginSubmitNotificationHubJob(
const processProgressCallbacks = async (): Promise<void> =>
progressCallbacks.forEach((h) => h(state));
let resultPromise: Promise<NotificationHubJob> | undefined;
let cancelJob: (() => void) | undefined;
const abortController = new AbortController();
const currentPollIntervalInMs = polledOperationOptions.updateIntervalInMs ?? 2000;

const poller: SimplePollerLike<OperationState<NotificationHubJob>, NotificationHubJob> = {
async poll(options?: { abortSignal?: AbortSignalLike }): Promise<void> {
const poller: PollerLike<OperationState<NotificationHubJob>, NotificationHubJob> = {
async poll(options?: {
abortSignal?: AbortSignalLike;
}): Promise<OperationState<NotificationHubJob>> {
submittedJob = await getNotificationHubJob(context, submittedJob.jobId!, options);
if (submittedJob.status === "Running" || submittedJob.status === "Started") {
state.status = "running";
Expand All @@ -67,6 +68,8 @@ export async function beginSubmitNotificationHubJob(
if (state.status === "failed") {
throw state.error;
}

return state;
},

pollUntilDone(pollOptions?: { abortSignal?: AbortSignalLike }): Promise<NotificationHubJob> {
Expand All @@ -84,9 +87,9 @@ export async function beginSubmitNotificationHubJob(
}

try {
if (!poller.isDone()) {
if (!poller.isDone) {
await poller.poll({ abortSignal });
while (!poller.isDone()) {
while (!poller.isDone) {
await delay(currentPollIntervalInMs, { abortSignal });
await poller.poll({ abortSignal });
}
Expand All @@ -96,7 +99,7 @@ export async function beginSubmitNotificationHubJob(
}
switch (state.status) {
case "succeeded":
return poller.getResult() as NotificationHubJob;
return poller.result as NotificationHubJob;
case "canceled":
throw new Error("Operation was canceled");
case "failed":
Expand All @@ -117,30 +120,45 @@ export async function beginSubmitNotificationHubJob(
return () => progressCallbacks.delete(s);
},

isDone(): boolean {
get isDone(): boolean {
return ["succeeded", "failed", "canceled"].includes(state.status);
},

stopPolling(): void {
abortController.abort();
cancelJob?.();
get operationState(): OperationState<NotificationHubJob> | undefined {
return state;
},

isStopped(): boolean {
return resultPromise === undefined;
get result(): NotificationHubJob | undefined {
return state.result;
},

getOperationState(): OperationState<NotificationHubJob> {
return state;
async serialize(): Promise<string> {
return JSON.stringify({ state });
},

getResult(): NotificationHubJob | undefined {
return state.result;
async submitted() {
// No-op
return;
},

toString() {
return JSON.stringify({ state });
then<TResult1 = NotificationHubJob, TResult2 = never>(
onfulfilled?:
| ((value: NotificationHubJob) => TResult1 | PromiseLike<TResult1>)
| undefined
| null,
onrejected?: ((reason: any) => TResult2 | PromiseLike<TResult2>) | undefined | null,
): Promise<TResult1 | TResult2> {
return poller.pollUntilDone().then(onfulfilled, onrejected);
},
catch<TResult2 = never>(
onrejected?: ((reason: any) => TResult2 | PromiseLike<TResult2>) | undefined | null,
): Promise<NotificationHubJob | TResult2> {
return poller.pollUntilDone().catch(onrejected);
},
finally(onfinally?: (() => void) | undefined | null): Promise<NotificationHubJob> {
return poller.pollUntilDone().finally(onfinally);
},
[Symbol.toStringTag]: "Poller",
};

return poller;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.

import { OperationState, SimplePollerLike } from "@azure/core-lro";
import { OperationState, PollerLike } from "@azure/core-lro";

/**
* Describes the types of notification hub jobs.
Expand Down Expand Up @@ -131,7 +131,7 @@ export interface NotificationHubJob {
/**
* Describes a poller for NotificationHubJob types.
*/
export type NotificationHubJobPoller = SimplePollerLike<
export type NotificationHubJobPoller = PollerLike<
OperationState<NotificationHubJob>,
NotificationHubJob
>;

0 comments on commit f169f9e

Please sign in to comment.