UNPKG

2.73 kBPlain TextView Raw
1// Copyright (c) .NET Foundation. All rights reserved.
2// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
3
4// This is an API that is similar to Observable, but we don't want users to confuse it for that so we rename things. Someone could
5// easily adapt it into the Rx interface if they wanted to. Unlike in C#, we can't just implement an "interface" and get extension
6// methods for free. The methods have to actually be added to the object (there are no extension methods in JS!). We don't want to
7// depend on RxJS in the core library, so instead we duplicate the minimum logic needed and then users can easily adapt these into
8// proper RxJS observables if they want.
9
10/** Defines the expected type for a receiver of results streamed by the server.
11 *
12 * @typeparam T The type of the items being sent by the server.
13 */
14export interface IStreamSubscriber<T> {
15 /** A boolean that will be set by the {@link @microsoft/signalr.IStreamResult} when the stream is closed. */
16 closed?: boolean;
17 /** Called by the framework when a new item is available. */
18 next(value: T): void;
19 /** Called by the framework when an error has occurred.
20 *
21 * After this method is called, no additional methods on the {@link @microsoft/signalr.IStreamSubscriber} will be called.
22 */
23 error(err: any): void;
24 /** Called by the framework when the end of the stream is reached.
25 *
26 * After this method is called, no additional methods on the {@link @microsoft/signalr.IStreamSubscriber} will be called.
27 */
28 complete(): void;
29}
30
31/** Defines the result of a streaming hub method.
32 *
33 * @typeparam T The type of the items being sent by the server.
34 */
35export interface IStreamResult<T> {
36 /** Attaches a {@link @microsoft/signalr.IStreamSubscriber}, which will be invoked when new items are available from the stream.
37 *
38 * @param {IStreamSubscriber<T>} observer The subscriber to attach.
39 * @returns {ISubscription<T>} A subscription that can be disposed to terminate the stream and stop calling methods on the {@link @microsoft/signalr.IStreamSubscriber}.
40 */
41 subscribe(subscriber: IStreamSubscriber<T>): ISubscription<T>;
42}
43
44/** An interface that allows an {@link @microsoft/signalr.IStreamSubscriber} to be disconnected from a stream.
45 *
46 * @typeparam T The type of the items being sent by the server.
47 */
48// @ts-ignore: We can't remove this, it's a breaking change, but it's not used.
49export interface ISubscription<T> {
50 /** Disconnects the {@link @microsoft/signalr.IStreamSubscriber} associated with this subscription from the stream. */
51 dispose(): void;
52}