observable.d.ts 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. /** 消费者接口 */
  2. export interface IObserver<T, E, C> {
  3. /** 用来接收 Observable 中的 next 类型通知 */
  4. next: (value: T) => void;
  5. /** 用来接收 Observable 中的 error 类型通知 */
  6. error: (err: E) => void;
  7. /** 用来接收 Observable 中的 complete 类型通知 */
  8. complete: (res: C) => void;
  9. }
  10. export interface NextObserver<T, E, C> {
  11. next: (value: T) => void;
  12. error?: (err: E) => void;
  13. complete?: (res: C) => void;
  14. }
  15. export interface ErrorObserver<T, E, C> {
  16. next?: (value: T) => void;
  17. error: (err: E) => void;
  18. complete?: (res: C) => void;
  19. }
  20. export interface CompletionObserver<T, E, C> {
  21. next?: (value: T) => void;
  22. error?: (err: E) => void;
  23. complete: (res: C) => void;
  24. }
  25. export declare type PartialObserver<T, E, C> = NextObserver<T, E, C> | ErrorObserver<T, E, C> | CompletionObserver<T, E, C>;
  26. export interface IUnsubscribable {
  27. /** 取消 observer 的订阅 */
  28. unsubscribe(): void;
  29. }
  30. /** Subscription 的接口 */
  31. export interface ISubscriptionLike extends IUnsubscribable {
  32. readonly closed: boolean;
  33. }
  34. export declare type TeardownLogic = () => void;
  35. export interface ISubscribable<T, E, C> {
  36. subscribe(observer?: PartialObserver<T, E, C> | ((value: T) => void), error?: (error: any) => void, complete?: () => void): IUnsubscribable;
  37. }
  38. /** 表示可清理的资源,比如 Observable 的执行 */
  39. declare class Subscription implements ISubscriptionLike {
  40. /** 用来标示该 Subscription 是否被取消订阅的标示位 */
  41. closed: boolean;
  42. /** 清理 subscription 持有的资源 */
  43. private _unsubscribe;
  44. /** 取消 observer 的订阅 */
  45. unsubscribe(): void;
  46. /** 添加一个 tear down 在该 Subscription 的 unsubscribe() 期间调用 */
  47. add(teardown: TeardownLogic): void;
  48. }
  49. /**
  50. * 实现 Observer 接口并且继承 Subscription 类,Observer 是消费 Observable 值的公有 API
  51. * 所有 Observers 都转化成了 Subscriber,以便提供类似 Subscription 的能力,比如 unsubscribe
  52. */
  53. export declare class Subscriber<T, E, C> extends Subscription implements IObserver<T, E, C> {
  54. protected isStopped: boolean;
  55. protected destination: Partial<IObserver<T, E, C>>;
  56. constructor(observerOrNext?: PartialObserver<T, E, C> | ((value: T) => void) | null, error?: ((err: E) => void) | null, complete?: ((res: C) => void) | null);
  57. unsubscribe(): void;
  58. next(value: T): void;
  59. error(err: E): void;
  60. complete(result: C): void;
  61. }
  62. /** 可观察对象,当前的上传事件的集合 */
  63. export declare class Observable<T, E, C> implements ISubscribable<T, E, C> {
  64. private _subscribe;
  65. constructor(_subscribe: (subscriber: Subscriber<T, E, C>) => TeardownLogic);
  66. subscribe(observer: PartialObserver<T, E, C>): Subscription;
  67. subscribe(next: null | undefined, error: null | undefined, complete: (res: C) => void): Subscription;
  68. subscribe(next: null | undefined, error: (error: E) => void, complete?: (res: C) => void): Subscription;
  69. subscribe(next: (value: T) => void, error: null | undefined, complete: (res: C) => void): Subscription;
  70. }
  71. export {};