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

In TS code, convert Shiny from interface to class #3990

Open
wch opened this issue Mar 8, 2024 · 0 comments
Open

In TS code, convert Shiny from interface to class #3990

wch opened this issue Mar 8, 2024 · 0 comments

Comments

@wch
Copy link
Collaborator

wch commented Mar 8, 2024

Some issues we currently have with the Shiny interface in our Typescript code:

  • Some function types end up being circular if you Cmd-click on them, like shinyUnbindAll.
  • Some of the methods on the Shiny object appear only after initialization, like setInputValue, bindAll, user, and so some authors might wait for those methods to be available, which is weird.
    type FullShinyDef = Required<
    Pick<
    Shiny,
    | "bindAll"
    | "forgetLastInputValue"
    | "initializeInputs"
    | "oncustommessage"
    | "setInputValue"
    | "shinyapp"
    | "unbindAll"
    | "user"
    >
  • It's just weird to have a Shiny interface and a FullShinyDef type.
  • It's strange that we initialize window.Shiny as an empty object, but we say that it is a Shiny object.
    function windowShiny(): Shiny {
    // Use `any` type as we know what we are doing is _dangerous_
    // Immediately init shiny on the window
    if (!(window as any)["Shiny"]) {
    (window as any)["Shiny"] = {};
    }
    return (window as any)["Shiny"];
    }

Proposed solution (with help from @schloerke):

  • Change Shiny from an interface to a class, and make sure it has all methods from the beginning.
  • Add an init() method to that class.
  • If some of the methods are called before init(), then throw an error, with information on how to fix the problem.
  • Add initPromise() method, which returns a promise that resolves at the end of init(). Then code that needs to wait for Shiny to be ready can just wait for that.
  • Add an onInit() method, which lets you register a callback to be invoked when the initPromise() resolves.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant