finos / FDC3

An open standard for the financial desktop.
https://fdc3.finos.org
Other
187 stars 109 forks source link

Refactors ContextTypes&Intent to union type instead of enum #1139

Closed andreifloricel closed 2 months ago

andreifloricel commented 6 months ago

closes #1138

linux-foundation-easycla[bot] commented 6 months ago

CLA Signed

The committers listed above are authorized under a signed CLA.

netlify[bot] commented 6 months ago

Deploy Preview for fdc3 ready!

Name Link
Latest commit 17cf9e62ad1a87d7faadc993f0cfe4ea67d852ac
Latest deploy log https://app.netlify.com/sites/fdc3/deploys/65f00cb0d36cdf0008cc230b
Deploy Preview https://deploy-preview-1139--fdc3.netlify.app
Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify site configuration.

andreifloricel commented 4 months ago

Sorry, this PR has slipped under my radar over the last few weeks. I'll update it with suggested improvements in the coming days.

bingenito commented 4 months ago

This change LGTM - although I think we should consider whether to add an array or use an array instead to simplify implementation of a runtime check as to whether a context is a standard type.

@kriswest Do you have a use case in mind where you would need to know if it is a standard type?

kriswest commented 4 months ago

@andreifloricel could outline the use case for this type and confirm or deny whether you see a use case for a util function to check if a context is a standard type at runtime? If we can confirm that I can probably document it and add in a util fn to do it (which may need an array or the type names).

Without a use case, one of the other maintainers suggested dropping the enum/union entirely... I sort of see a use case - but I'd much rather confirmation from someone using it for real!

andreifloricel commented 4 months ago

@andreifloricel could outline the use case for this type and confirm or deny whether you see a use case for a util function to check if a context is a standard type at runtime?

I think it all comes down to a type-safe API and implicitly to a superior DX. E.g. it's quite nice to get have autocompletion for Context/Intents on all the DesktopAgent methods. Also checking for standard Context/Intent is quite useful when your implementation has to support dynamic subscribing/emitting of FDC3 payloads (e.g. raiseIntentForContext() without knowing upfront which Context will the user provide, etc)

@kriswest I updated the PR:

bingenito commented 4 months ago

Isn't this a semver major breaking api change to anyone using the old style constants like below?

  import { ContextTypes, Intents } from "@finos/fdc3";
  window.fdc3.addContextListener(ContextTypes.Instrument, () => {});
  window.fdc3.addIntentListener(Intents.ViewInstrument, () => {});
bingenito commented 4 months ago
  • added utility functions: isStandardContextType(contextType: string), `isStandardIntent(intent: string)

I don't understand the use case for these. Do you have a local implementation dependent on these?

getPossibleContextsForIntent(intent: StandardIntent)

Isn't this opinionated?

andreifloricel commented 4 months ago

Isn't this a semver major breaking api change to anyone using the old style constants like below?

  import { ContextTypes, Intents } from "@finos/fdc3";
  window.fdc3.addContextListener(ContextTypes.Instrument, () => {});
  window.fdc3.addIntentListener(Intents.ViewInstrument, () => {});

No, all changes in this PR are backward compatible:

strict_typing_backwards_compatibility

andreifloricel commented 4 months ago
  • added utility functions: isStandardContextType(contextType: string), `isStandardIntent(intent: string)

I don't understand the use case for these. Do you have a local implementation dependent on these?

A real use case for us is that we need to dynamically build UI controls for our users (e.g., right-click a grid cell and generate an intent for the context referenced in that specific cell). When we do this, it's very useful to know if that specific intent and/or context is a standard FDC3 one, because in that case we can make a lot of educated guesses. Such as, for example, deriving possible contexts for a standard intention (see the next section of the commentary). @kriswest what use case did you have in mind?

getPossibleContextsForIntent(intent: StandardIntent)

Isn't this opinionated?

Well, it is, but it's FDC3's opinion :) As I understand the FDC3 specification, each standard intent (Verb) has an associated list of possible contexts (Nouns), e.g. https://fdc3.finos.org/docs/intents/ref/ViewChart#possible-contexts

bingenito commented 4 months ago

Isn't this a semver major breaking api change to anyone using the old style constants like below?

  import { ContextTypes, Intents } from "@finos/fdc3";
  window.fdc3.addContextListener(ContextTypes.Instrument, () => {});
  window.fdc3.addIntentListener(Intents.ViewInstrument, () => {});

No, all changes in this PR are backwards compatible:

  • ContextType and Intent types still accept any string value ( as it must support custom Contexts/Intent)
  • also, changes in the DesktopAgent Api don't affect existing implementations, see https://tsplay.dev/wX0KLw

I agree with the change and type guard, but it is a breaking change on the caller side as ContextTypes and Intents is no longer exported. Put the above in a new file in the project and it is fine. Switch to this branch and it has errors for missing exports.

andreifloricel commented 4 months ago

I agree with the change and type guard, but it is a breaking change on the caller side as ContextTypes and Intents is no longer exported.

Good spot! I reintroduced the deprecated enum types but marked them as deprecated.

kriswest commented 4 months ago

Shall we pick this up briefly at tomorrow's Standards Working Group meeting? Happy to add it to the agenda - I haven't yet caught up on the conversation.

andreifloricel commented 4 months ago

Hi @kriswest,

since I can't attend today's Standards Working Group meeting, I'll try to to summarize all the changes in this task:

Note:

andreifloricel commented 3 months ago

I removed the getPossibleContextsForIntent() method.