Skip to content
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

[notification-hubs] Moving to core-lro v3 #31002

Merged
merged 2 commits into from
Sep 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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> = {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

just curious why not leverage the buildCreatePoller from core-lro directly

export function buildCreatePoller<TResponse, TResult, TState extends OperationState<TResult>>(

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's not exported at the top level of core-lro, hence why it cannot be used.

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
>;
Loading