delayedChannel.ts 632 B

1234567891011121314151617
  1. import { type CancellationToken, Event, Relay } from "./foundation.js";
  2. import type { IChannel } from "./channels.shared.js";
  3. export function getDelayedChannel<T extends IChannel>(promise: Promise<T>): T {
  4. return {
  5. call(command: string, arg?: any, cancellationToken?: CancellationToken): Promise<any> {
  6. return promise.then((channel) => channel.call(command, arg, cancellationToken));
  7. },
  8. listen(event: string, arg?: any): Event<any> {
  9. const relay = new Relay<any>();
  10. promise.then((channel) => {
  11. relay.input = channel.listen(event, arg);
  12. });
  13. return relay.event;
  14. },
  15. } as T;
  16. }