Stream.ts 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  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. // 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
  4. // easily adapt it into the Rx interface if they wanted to. Unlike in C#, we can't just implement an "interface" and get extension
  5. // 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
  6. // depend on RxJS in the core library, so instead we duplicate the minimum logic needed and then users can easily adapt these into
  7. // proper RxJS observables if they want.
  8. /** Defines the expected type for a receiver of results streamed by the server.
  9. *
  10. * @typeparam T The type of the items being sent by the server.
  11. */
  12. export interface IStreamSubscriber<T> {
  13. /** A boolean that will be set by the {@link @aspnet/signalr.IStreamResult} when the stream is closed. */
  14. closed?: boolean;
  15. /** Called by the framework when a new item is available. */
  16. next(value: T): void;
  17. /** Called by the framework when an error has occurred.
  18. *
  19. * After this method is called, no additional methods on the {@link @aspnet/signalr.IStreamSubscriber} will be called.
  20. */
  21. error(err: any): void;
  22. /** Called by the framework when the end of the stream is reached.
  23. *
  24. * After this method is called, no additional methods on the {@link @aspnet/signalr.IStreamSubscriber} will be called.
  25. */
  26. complete(): void;
  27. }
  28. /** Defines the result of a streaming hub method.
  29. *
  30. * @typeparam T The type of the items being sent by the server.
  31. */
  32. export interface IStreamResult<T> {
  33. /** Attaches a {@link @aspnet/signalr.IStreamSubscriber}, which will be invoked when new items are available from the stream.
  34. *
  35. * @param {IStreamSubscriber<T>} observer The subscriber to attach.
  36. * @returns {ISubscription<T>} A subscription that can be disposed to terminate the stream and stop calling methods on the {@link @aspnet/signalr.IStreamSubscriber}.
  37. */
  38. subscribe(subscriber: IStreamSubscriber<T>): ISubscription<T>;
  39. }
  40. /** An interface that allows an {@link @aspnet/signalr.IStreamSubscriber} to be disconnected from a stream.
  41. *
  42. * @typeparam T The type of the items being sent by the server.
  43. */
  44. // @ts-ignore: We can't remove this, it's a breaking change, but it's not used.
  45. export interface ISubscription<T> {
  46. /** Disconnects the {@link @aspnet/signalr.IStreamSubscriber} associated with this subscription from the stream. */
  47. dispose(): void;
  48. }