tracing.d.ts 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. // disable automatic export
  2. export {};
  3. /**
  4. * This type is only interesting if you're only using this module for a specifc build environment.
  5. *
  6. * With module augmentation you can declare what build of scheduler you are using by
  7. * augmenting this interface with e.g. `interface Build { type: 'development'; }`
  8. * Depending on the build some exported members have different types.
  9. * Possible values are `production`, `profiling` and `development`.
  10. * The default behavior for the types is to use a union of all possible types.
  11. */
  12. // tslint:disable-next-line: no-empty-interface
  13. export interface Build {}
  14. export type EnableSchedulerTracing = Build extends { type: infer BuildType }
  15. ? BuildType extends "production" | "profiling"
  16. ? false
  17. : BuildType extends "development"
  18. ? true
  19. : undefined
  20. : undefined;
  21. type TypeByBuildFlag<
  22. Flag extends boolean | undefined,
  23. WhenTrue,
  24. WhenFalse
  25. > = Flag extends undefined
  26. ? (WhenTrue | WhenFalse)
  27. : Flag extends true
  28. ? WhenTrue
  29. : WhenFalse;
  30. type IfSchedulerTracing<WhenTrue, WhenFalse> = TypeByBuildFlag<
  31. EnableSchedulerTracing,
  32. WhenTrue,
  33. WhenFalse
  34. >;
  35. export interface Interaction {
  36. __count: number;
  37. id: number;
  38. name: string;
  39. timestamp: number;
  40. }
  41. export interface Subscriber {
  42. /**
  43. * A new interaction has been created via the trace() method.
  44. */
  45. onInteractionTraced: (interaction: Interaction) => void;
  46. /**
  47. * All scheduled async work for an interaction has finished.
  48. */
  49. onInteractionScheduledWorkCompleted: (interaction: Interaction) => void;
  50. /**
  51. * New async work has been scheduled for a set of interactions.
  52. * When this work is later run, onWorkStarted/onWorkStopped will be called.
  53. * A batch of async/yieldy work may be scheduled multiple times before completing.
  54. * In that case, onWorkScheduled may be called more than once before onWorkStopped.
  55. * Work is scheduled by a "thread" which is identified by a unique ID.
  56. */
  57. onWorkScheduled: (interactions: Set<Interaction>, threadID: number) => void;
  58. /**
  59. * A batch of scheduled work has been canceled.
  60. * Work is done by a "thread" which is identified by a unique ID.
  61. */
  62. onWorkCanceled: (interactions: Set<Interaction>, threadID: number) => void;
  63. /**
  64. * A batch of work has started for a set of interactions.
  65. * When this work is complete, onWorkStopped will be called.
  66. * Work is not always completed synchronously; yielding may occur in between.
  67. * A batch of async/yieldy work may also be re-started before completing.
  68. * In that case, onWorkStarted may be called more than once before onWorkStopped.
  69. * Work is done by a "thread" which is identified by a unique ID.
  70. */
  71. onWorkStarted: (interactions: Set<Interaction>, threadID: number) => void;
  72. /**
  73. * A batch of work has completed for a set of interactions.
  74. * Work is done by a "thread" which is identified by a unique ID.
  75. */
  76. onWorkStopped: (interactions: Set<Interaction>, threadID: number) => void;
  77. }
  78. export interface InteractionsRef {
  79. current: Set<Interaction>;
  80. }
  81. export interface SubscriberRef {
  82. current: Subscriber | null;
  83. }
  84. export const __interactionsRef: IfSchedulerTracing<InteractionsRef, null>;
  85. export const __subscriberRef: IfSchedulerTracing<SubscriberRef, null>;
  86. export function unstable_clear<T>(callback: () => T): T;
  87. export function unstable_getCurrent(): Set<Interaction> | null;
  88. export function unstable_getThreadID(): number;
  89. export function unstable_trace<T>(
  90. name: string,
  91. timestamp: number,
  92. callback: () => T,
  93. threadID?: number
  94. ): T;
  95. export type WrappedFunction<T extends (...args: any[]) => any> = T & {
  96. cancel: () => void;
  97. };
  98. /**
  99. * The callback is immediately returned if the enableSchedulerTracing is disabled.
  100. * It is unclear for which bundles this is the case.
  101. *
  102. * @param callback
  103. * @param threadID
  104. */
  105. export function unstable_wrap<T extends (...args: any[]) => any>(
  106. callback: T,
  107. threadID?: number
  108. ): IfSchedulerTracing<WrappedFunction<T>, T>;
  109. export function unstable_subscribe(subscriber: Subscriber): void;
  110. export function unstable_unsubscribe(subscriber: Subscriber): void;