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 the useDataChannel hook #56

Closed GuiLeme closed 8 months ago

GuiLeme commented 8 months ago

Brief description

When using the hook useDataChannel the function oblige the developer to use a generic, so that the data it will receive will come in certain pattern. But it seems that all the returns of the hook already come within a very structured pattern, like so:

DataThatWeReceive <T> {
    pluginDataChannelMessage: <T> {
    createdAt: string;
    dataChannel: string;
    fromUserId: string;
    messageId: string;
    payloadJson: T;
    pluginName: string;
    toRoles: string[];
  }[];
}

Now, this is the structure that we receive inside the data property of the GraphqlResponseWrapper, and so it would be easier for the developer to already have this in the palm of their hands than trying to guess what the return will be.

Changes:

So, the way we have now, we need to construct the pluginDataChannelMessage interface and also the wrapping structure, and within that, we can pass the data we are interested in. For instance, if we want to pass a PickedUser in the dataChannel, now we would need to do the following:

What we have:

interface PickedUser{
  userId: string;
  role: string;
// Rest of properties
}

interface DataThatWeReceive {
    pluginDataChannelMessage {
    createdAt: string;
    dataChannel: string;
    fromUserId: string;
    messageId: string;
    payloadJson: PickedUser;
    pluginName: string;
    toRoles: string[];
  }[];
}

const [data, dispatcherFunction] = useDataChannel<DataThatWeReceive>('pickRandomUser');

And than if we want another dataChannel, we would have to either recreate the DataThatWeReceive interface with the new payload we want or make it generic (because the overall structure is precisely the same).

What we want:

PickedUser{
  userId: string;
  role: string;
// Rest of properties
}

const [data, dispatcherFunction] = useDataChannel<PickedUser>('pickRandomUser');