提交学习笔记专用
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

125 lines
5.1 KiB

  1. type ArgumentsType<T> = T extends (...args: infer A) => any ? A : never;
  2. type ReturnType<T> = T extends (...args: any) => infer R ? R : never;
  3. type PromisifyFn<T> = ReturnType<T> extends Promise<any> ? T : (...args: ArgumentsType<T>) => Promise<Awaited<ReturnType<T>>>;
  4. type Thenable<T> = T | PromiseLike<T>;
  5. type BirpcResolver = (name: string, resolved: (...args: unknown[]) => unknown) => Thenable<((...args: unknown[]) => unknown) | undefined>;
  6. interface ChannelOptions {
  7. /**
  8. * Function to post raw message
  9. */
  10. post: (data: any, ...extras: any[]) => any | Promise<any>;
  11. /**
  12. * Listener to receive raw message
  13. */
  14. on: (fn: (data: any, ...extras: any[]) => void) => any | Promise<any>;
  15. /**
  16. * Clear the listener when `$close` is called
  17. */
  18. off?: (fn: (data: any, ...extras: any[]) => void) => any | Promise<any>;
  19. /**
  20. * Custom function to serialize data
  21. *
  22. * by default it passes the data as-is
  23. */
  24. serialize?: (data: any) => any;
  25. /**
  26. * Custom function to deserialize data
  27. *
  28. * by default it passes the data as-is
  29. */
  30. deserialize?: (data: any) => any;
  31. /**
  32. * Call the methods with the RPC context or the original functions object
  33. */
  34. bind?: 'rpc' | 'functions';
  35. }
  36. interface EventOptions<Remote> {
  37. /**
  38. * Names of remote functions that do not need response.
  39. */
  40. eventNames?: (keyof Remote)[];
  41. /**
  42. * Maximum timeout for waiting for response, in milliseconds.
  43. *
  44. * @default 60_000
  45. */
  46. timeout?: number;
  47. /**
  48. * Custom resolver to resolve function to be called
  49. *
  50. * For advanced use cases only
  51. */
  52. resolver?: BirpcResolver;
  53. /**
  54. * Custom error handler
  55. *
  56. * @deprecated use `onFunctionError` and `onGeneralError` instead
  57. */
  58. onError?: (error: Error, functionName: string, args: any[]) => boolean | void;
  59. /**
  60. * Custom error handler for errors occurred in local functions being called
  61. *
  62. * @returns `true` to prevent the error from being thrown
  63. */
  64. onFunctionError?: (error: Error, functionName: string, args: any[]) => boolean | void;
  65. /**
  66. * Custom error handler for errors occurred during serialization or messsaging
  67. *
  68. * @returns `true` to prevent the error from being thrown
  69. */
  70. onGeneralError?: (error: Error, functionName?: string, args?: any[]) => boolean | void;
  71. /**
  72. * Custom error handler for timeouts
  73. *
  74. * @returns `true` to prevent the error from being thrown
  75. */
  76. onTimeoutError?: (functionName: string, args: any[]) => boolean | void;
  77. }
  78. type BirpcOptions<Remote> = EventOptions<Remote> & ChannelOptions;
  79. type BirpcFn<T> = PromisifyFn<T> & {
  80. /**
  81. * Send event without asking for response
  82. */
  83. asEvent: (...args: ArgumentsType<T>) => void;
  84. };
  85. interface BirpcGroupFn<T> {
  86. /**
  87. * Call the remote function and wait for the result.
  88. */
  89. (...args: ArgumentsType<T>): Promise<Awaited<ReturnType<T>>[]>;
  90. /**
  91. * Send event without asking for response
  92. */
  93. asEvent: (...args: ArgumentsType<T>) => void;
  94. }
  95. type BirpcReturn<RemoteFunctions, LocalFunctions = Record<string, never>> = {
  96. [K in keyof RemoteFunctions]: BirpcFn<RemoteFunctions[K]>;
  97. } & {
  98. $functions: LocalFunctions;
  99. $close: (error?: Error) => void;
  100. $closed: boolean;
  101. $rejectPendingCalls: (handler?: PendingCallHandler) => Promise<void>[];
  102. };
  103. type PendingCallHandler = (options: Pick<PromiseEntry, 'method' | 'reject'>) => void | Promise<void>;
  104. type BirpcGroupReturn<RemoteFunctions> = {
  105. [K in keyof RemoteFunctions]: BirpcGroupFn<RemoteFunctions[K]>;
  106. };
  107. interface BirpcGroup<RemoteFunctions, LocalFunctions = Record<string, never>> {
  108. readonly clients: BirpcReturn<RemoteFunctions, LocalFunctions>[];
  109. readonly functions: LocalFunctions;
  110. readonly broadcast: BirpcGroupReturn<RemoteFunctions>;
  111. updateChannels: (fn?: ((channels: ChannelOptions[]) => void)) => BirpcReturn<RemoteFunctions, LocalFunctions>[];
  112. }
  113. interface PromiseEntry {
  114. resolve: (arg: any) => void;
  115. reject: (error: any) => void;
  116. method: string;
  117. timeoutId?: ReturnType<typeof setTimeout>;
  118. }
  119. declare const DEFAULT_TIMEOUT = 60000;
  120. declare const setTimeout: typeof globalThis.setTimeout;
  121. declare function createBirpc<RemoteFunctions = Record<string, never>, LocalFunctions extends object = Record<string, never>>(functions: LocalFunctions, options: BirpcOptions<RemoteFunctions>): BirpcReturn<RemoteFunctions, LocalFunctions>;
  122. declare function cachedMap<T, R>(items: T[], fn: ((i: T) => R)): R[];
  123. declare function createBirpcGroup<RemoteFunctions = Record<string, never>, LocalFunctions extends object = Record<string, never>>(functions: LocalFunctions, channels: ChannelOptions[] | (() => ChannelOptions[]), options?: EventOptions<RemoteFunctions>): BirpcGroup<RemoteFunctions, LocalFunctions>;
  124. export { type ArgumentsType, type BirpcFn, type BirpcGroup, type BirpcGroupFn, type BirpcGroupReturn, type BirpcOptions, type BirpcResolver, type BirpcReturn, type ChannelOptions, DEFAULT_TIMEOUT, type EventOptions, type PromisifyFn, type ReturnType, type Thenable, cachedMap, createBirpc, createBirpcGroup };