bigbluebutton / bigbluebutton-html-plugin-sdk

BigBlueButton Plugin SDK
GNU Lesser General Public License v3.0
6 stars 5 forks source link

Change return type of useDataChannel function #89

Closed GuiLeme closed 2 months ago

GuiLeme commented 2 months ago

What is the idea?

Basically I would like to change the return type of the useDataChannelFunction, because we changed the philosophy of that hook. At the beginning of its development, we intended to mimic the behavior of React.useState, so we had something like [state, setState] = useDataChannel<type>(...).

But since then, we added the delete function, and now an update one, and therfore we have a 4-item array, as demonstrated below:

const [
  data: GraphqlResponseWrapper<DataChannelEntryResponseType<T>[]>,
  pushEntry: PushEntryFunction<T>,
  deleteEntry: DeleteEntryFunction,
  replaceEntry: ReplaceEntryFunction<T>,
] = useDataChannel(...)

This is a bit difficult to manipulate, mainly because if we want to get all the items except for one in the middle, for instance, the delete function, we would still have to declare it, and have lint errors in typescript.

So the idea is that now, instead of having an array as the return, we could simply have an object, this way, we can select only the properties that we want. As an example, I am going to show a before/after of a code using the data-channel:

Before:

const [
  urlToGenericLinkResponse,
  pushUrlToGenericLink
] = pluginApi.useDataChannel<DataToGenericLink>('urlToGenericLink');

After:

const {
  data: urlToGenericLinkResponse,
  pushEntry: pushUrlToGenericLink,
} = pluginApi.useDataChannel<DataToGenericLink>('urlToGenericLink');

// The rest of the code remains the same since the variables it depends on did not change.
TiagoJacobs commented 2 months ago

Hello @GuiLeme - I think it's a great idea, because with arrays, in order to access the third or fourth element, you need to specify the second one, which is painful.

It will require a rewrite of the current plugins, but it's very minor (just change the way it destructure the hook return), and as we didn't released the BBB 3.0 yet, it seems a good moment to do this refactor.