Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Inconsistent Use of Promises #1294

Closed
robmoffat opened this issue Jul 26, 2024 · 6 comments · Fixed by #1305
Closed

Inconsistent Use of Promises #1294

robmoffat opened this issue Jul 26, 2024 · 6 comments · Fixed by #1305
Labels
api FDC3 API Working Group bug Something isn't working formal specification

Comments

@robmoffat
Copy link
Member

robmoffat commented Jul 26, 2024

Channel contains this, promised API call:

public addContextListener(contextType: string | null, handler: ContextHandler): Promise<Listener>;

PrivateChannel however, has unpromised API calls, which also return listeners:

interface  PrivateChannel extends Channel {
  // methods
  onAddContextListener(handler: (contextType?: string) => void): Listener;
  onUnsubscribe(handler: (contextType?: string) => void): Listener;
  onDisconnect(handler: () => void): Listener;
  disconnect(): void;
}

... which lead to race conditions in my tests, sadly. I doubt we'll ever fix this but raising nonetheless.

@robmoffat robmoffat added the bug Something isn't working label Jul 26, 2024
@kriswest
Copy link
Contributor

kriswest commented Jul 26, 2024

Those were supposed to be async. I think they were introduced at the same time as we made the rest of the API async and this probably went in at around the same time or just before and no one noticed the discrepency - I'm suprised nobody's brought it up befor enow. We should fix it for consistency, but it'd require a vote. Alternatively we could deprecate these and introduce some new async versions? Perhaps use the addEventListener pattern instead so we only need 2 functions (addEventListener and disconnect)?

@kriswest kriswest added api FDC3 API Working Group formal specification labels Jul 26, 2024
@kriswest
Copy link
Contributor

So to make that a concrete proposal:

we could change:

interface  PrivateChannel extends Channel {
  // methods
  onAddContextListener(handler: (contextType?: string) => void): Listener;
  onUnsubscribe(handler: (contextType?: string) => void): Listener;
  onDisconnect(handler: () => void): Listener;
  disconnect(): void;
}

to:

interface  PrivateChannel extends Channel {
  addEventListener(type: PrivateChannelEventType  | null, handler: EventHandler): Promise<Listener>;
  disconnect(): Promise<void>;
  
  // deprecated methods
  /** @deprecated use `addEventListener("addContextListener", handler)` instead. */
  onAddContextListener(handler: (contextType?: string) => void): Listener;
  /** @deprecated use `addEventListener("unsubscribe", handler)` instead. */
  onUnsubscribe(handler: (contextType?: string) => void): Listener;
  /** @deprecated use `addEventListener("disconnect", handler)` instead. */
  onDisconnect(handler: () => void): Listener;
}

export enum PrivateChannelEventType {
  ADD_CONTEXT_LISTENER = "addContextListener"
  UNSUBSCRIBE = "unsubscribe"
  DISCONNECT = "disconnect"
}

2.2 would actually be good time to do that as we're introducing addEventListener to the DesktopAgent API, fixing a second consistency issue!

@Roaders
Copy link
Contributor

Roaders commented Aug 9, 2024

PrivateChannel.disconnect() is probably another candidate for this. It's an async process with a PrivateChannelDisconnectRequest and PrivateChannelDisconnectResponse so we should be returning a promise IMHO

@robmoffat
Copy link
Member Author

@Roaders Agreed but I think @kriswest already included that, no?

@kriswest
Copy link
Contributor

@Roaders @robmoffat I did include it yes - the return type just changes from void to Promise<void>.

@Roaders
Copy link
Contributor

Roaders commented Aug 19, 2024

ok, apologies. I must have missed that one.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
api FDC3 API Working Group bug Something isn't working formal specification
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants