/
renderingSessionPoller.ts
166 lines (147 loc) · 4.51 KB
/
renderingSessionPoller.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT license.
import { PollOperationState, Poller, PollOperation } from "@azure/core-lro";
import { KnownRenderingSessionStatus } from "../generated/models/index";
import { getSessionInternal, endSessionInternal } from "../internal/commonQueries";
import { AbortSignalLike } from "@azure/abort-controller";
import { RemoteRendering } from "../generated/operationsInterfaces";
import { delay } from "@azure/core-util";
import { RenderingSession } from "../internal/renderingSession";
/**
* Options to configure the poller for the beginSession operation.
*/
export interface RenderingSessionPollerOptions {
/** The interval between calls to poll the service for the status of a session. */
intervalInMs?: number;
}
/**
* The state carried by the poller for the beginSession operation.
*/
export interface RenderingSessionOperationState extends PollOperationState<RenderingSession> {
/**
* The latest response when querying the service. The session may or may not be ready.
*/
latestResponse: RenderingSession;
}
/**
* @internal
*/
export class RenderingSessionOperationStateImpl implements RenderingSessionOperationState {
latestResponse: RenderingSession;
constructor(conversionState: RenderingSession) {
this.latestResponse = conversionState;
}
get isStarted(): boolean {
return true;
}
get isCompleted(): boolean {
return this.latestResponse.status !== KnownRenderingSessionStatus.Starting;
}
get isCancelled(): boolean {
return this.latestResponse.status === KnownRenderingSessionStatus.Stopped;
}
get error(): Error | undefined {
if (this.latestResponse.status === "Error") {
return this.latestResponse.error;
}
return undefined;
}
get result(): RenderingSession {
return this.latestResponse;
}
}
/**
* @internal
*/
class RenderingSessionOperation
implements PollOperation<RenderingSessionOperationState, RenderingSession> {
private accountId: string;
private operations: RemoteRendering;
state: RenderingSessionOperationState;
constructor(
accountId: string,
operations: RemoteRendering,
state: RenderingSessionOperationState
) {
this.accountId = accountId;
this.operations = operations;
this.state = state;
}
async update(_options?: {
abortSignal?: AbortSignalLike;
fireProgress?: (state: RenderingSessionOperationStateImpl) => void;
}): Promise<RenderingSessionOperation> {
this.state.latestResponse = await getSessionInternal(
this.accountId,
this.operations,
this.state.latestResponse.sessionId,
"RenderingSessionOperation-Update"
);
return this;
}
/**
* Attempts to cancel the underlying operation.
*
* It only optionally receives an object with an abortSignal property, from \@azure/abort-controller's AbortSignalLike.
*
* It returns a promise that should be resolved with an updated version of the poller's operation.
*
* @param options - Optional properties passed to the operation's update method.
*/
async cancel(options?: { abortSignal?: AbortSignalLike }): Promise<RenderingSessionOperation> {
await endSessionInternal(
this.accountId,
this.operations,
this.state.latestResponse.sessionId,
"RenderingSessionOperation-Cancel",
options
);
return this;
}
/**
* Serializes the operation.
* Useful when wanting to create a poller that monitors an existing operation.
*/
toString(): string {
return this.state.latestResponse.sessionId;
}
}
/**
* @internal
*/
export class RenderingSessionPoller extends Poller<
RenderingSessionOperationState,
RenderingSession
> {
/**
* Defines how much time the poller is going to wait before making a new request to the service.
*/
public intervalInMs: number = 10000;
constructor(
accountId: string,
operations: RemoteRendering,
renderingSession: RenderingSession,
options: RenderingSessionPollerOptions
) {
super(
new RenderingSessionOperation(
accountId,
operations,
new RenderingSessionOperationStateImpl(renderingSession)
)
);
this.intervalInMs = options.intervalInMs ? options.intervalInMs : 10000;
}
/**
* The method used by the poller to wait before attempting to update its operation.
*/
async delay(): Promise<void> {
return delay(this.intervalInMs);
}
/**
* Gets the public state of the polling operation
*/
public getOperationState(): RenderingSessionOperationState {
return this.operation.state;
}
}