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

feat: add fdc3Ready helper function and getInfo to npm package exports #360

Merged
merged 3 commits into from
Apr 13, 2021

Conversation

rikoe
Copy link
Contributor

@rikoe rikoe commented Apr 12, 2021

To add support to the FDC3 NPM package for the new fdc3Ready event, this adds an asynchronous fdc3Ready() function that has the following properties:

  • it will resolve immediately if window.fdc3 is already defined
  • it has a default timeout of 5 seconds (as recommend by @kriswest), but can be called with a smaller wait time as well, e.g. await fdc3Ready(1000)
  • after the timeout expires, or the fdc3Ready event is fired, it will again check if window.fdc3 is defined, and resolve/reject as appropriate

Also adds the getInfo method as an exported function, which was missing.

Tests for all of the above are included, as is documentation on the FDC3 website.

Example usage:

import { fdc3Ready, broadcast } from '@finos/fdc3'

try {
  await fdc3Ready(1000) // wait 1 second before timing out
  fdc3.broadcast({ ... })
} catch (error) {
  // handle
}

- add fdc3Ready function with 5 seconds default timeout
- add tests for fdc3Ready function
- use jest-mock-extended to mock DesktopAgent interface
- change prettier line width to support longer lines
- describe function in docs with usage examples
@thorsent
Copy link
Contributor

I think that there's not really much point in trying to auto-magically check fdc3Ready because the API isn't entirely async (promisified). broadcast() and addContextListener() are by far the most likely calls that a developer will make in the global scope, and those will both throw throwIfNoGlobal(), forcing the developer to manually implement the clumsy fdc3Ready logic, and negating the value of the auto-magic.

I see two possibilities to remedy this:

One is to simply require that desktop agents provide a global fdc3 and handle it all. (e.g. Cosaic's fdc3 implementation has implemented simple queuing logic so that developers don't need to worry about fdc3Ready.)

The second possibility is to promisify the remaining calls (e.g. broadcast(), addContextListener(), addIntentListener()) so that the hidden await pattern can be implemented across the board.

@rikoe
Copy link
Contributor Author

rikoe commented Apr 12, 2021

Thanks @thorsent.

I do agree that making the three synchronous calls is counter-productive, I think in a future release, everything should be promisified. Doing this work has made clear to me how problematic those three functions that are synchronous are.

I basically see two options:

  1. Just keep the current code with rejects or throws if fdc3 isn't available, and recommend users to manually call fdc3Ready() function, once only (which has the added benefit of not running code which isn't necessary).

    The function still makes it slightly easier to handle the window event listener pattern, by wrapping it as a promise with a timeout.

  2. Automatically call fdc3Ready() in all imported functions for safety of users who just do import { raiseIntent } from '@finos/fdc3' without understanding the nuances of the global object or ready event.

    This is with the understanding that it won't be possible for synchronous operations like broadcast until we promisify them, and people will have to look at the docs, or discover by trial and error that it is best to wait on fdc3Ready() beforehand.

@thorsent @kriswest @mattjamieson any thoughts on the above two options? I don't really mind either way.

Note that either of the two options above are just for ES6 import convenience in the npm package, and not suggested API spec changes - window.fdc3 and window.addEventListener('fdc3Ready') are still the underlying specification pieces.

@kriswest
Copy link
Contributor

kriswest commented Apr 12, 2021 via email

@rikoe
Copy link
Contributor Author

rikoe commented Apr 12, 2021

@kriswest agreed on both counts. Given that we have voted on the FDC3 1.2 release specification already, I am not sure we can change the signature now.

We should either reopen the voting, or wait until 2.0 and change, along with the other sync operators (I prefer the latter).

This does remind me that getInfo isn't currently exposed in the npm package though, will add it.

I will update PR to remove the fdc3Ready call inside exposed operators.

- don't call fdc3Ready from methods, revert to old behaviour that jut throws an error
- change docs to match
- add getInfo to exported methods
@rikoe rikoe changed the title feat: add fdc3Ready function to npm package to wrap fdc3Ready event feat: add fdc3Ready helper function and getInfo to npm package exports Apr 12, 2021
Copy link
Contributor

@kriswest kriswest left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM
One minor suggestion on a comment.
Tests all pass, however, there is a complaint about an unhandled promise rejection that could be cleaned up/caught (not sure which test):
image
Running with --trace-warnings did not make this any clearer (npx cross-env NODE_OPTIONS="--trace-warnings" tsdx test --verbose)
Thank you for increasing the printWidth

docs/api/ref/Globals.md Outdated Show resolved Hide resolved
- remove helper methods and use jest error checking primitives directly
- add suggested parenthesis to globals doc
@rikoe rikoe merged commit 7ffe04b into finos:master Apr 13, 2021
@rikoe rikoe deleted the add-fdc3ready-function branch April 13, 2021 17:11
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants