2023-10-27 03:59:05 +08:00
|
|
|
import React, { useEffect, useState } from 'react';
|
2024-01-31 05:38:09 +08:00
|
|
|
import { createChannelIdentifier } from 'bigbluebutton-html-plugin-sdk/dist/cjs/data-channel/utils';
|
2023-11-29 02:31:28 +08:00
|
|
|
import { DataChannelArguments } from 'bigbluebutton-html-plugin-sdk/dist/cjs/data-channel/types';
|
|
|
|
import {
|
|
|
|
HookEventWrapper, UnsubscribedEventDetails, SubscribedEventDetails,
|
|
|
|
} from 'bigbluebutton-html-plugin-sdk/dist/cjs/core/types';
|
|
|
|
import {
|
2024-01-16 01:12:39 +08:00
|
|
|
HookEvents,
|
2023-11-29 02:31:28 +08:00
|
|
|
} from 'bigbluebutton-html-plugin-sdk/dist/cjs/core/enum';
|
2024-04-24 05:17:32 +08:00
|
|
|
import { DataChannelHooks, DataChannelTypes } from 'bigbluebutton-html-plugin-sdk/dist/cjs/data-channel/enums';
|
2023-11-29 02:31:28 +08:00
|
|
|
|
2024-04-24 05:17:32 +08:00
|
|
|
import { MapInformation, PluginDataChannelManagerProps } from './types';
|
2023-11-29 02:31:28 +08:00
|
|
|
import { DataChannelItemManager } from './channel-manager/manager';
|
2023-10-27 03:59:05 +08:00
|
|
|
|
2024-01-16 01:12:39 +08:00
|
|
|
const PluginDataChannelManager: React.ElementType<PluginDataChannelManagerProps> = ((
|
|
|
|
props: PluginDataChannelManagerProps,
|
2023-10-27 03:59:05 +08:00
|
|
|
) => {
|
|
|
|
const {
|
|
|
|
pluginApi,
|
|
|
|
} = props;
|
|
|
|
|
|
|
|
const { pluginName: pluginNameInUse } = pluginApi;
|
|
|
|
if (!pluginNameInUse) return (<></>);
|
|
|
|
const [
|
|
|
|
mapOfDataChannelInformation,
|
|
|
|
setMapOfDataChannelInformation,
|
2024-04-24 05:17:32 +08:00
|
|
|
] = useState(new Map<string, MapInformation>());
|
2023-10-27 03:59:05 +08:00
|
|
|
|
2024-04-24 05:17:32 +08:00
|
|
|
const updateHookUsage = (channelName: string,
|
|
|
|
pluginName: string, deltaSubscribe: number,
|
|
|
|
dataChannelType: DataChannelTypes, subChannelName: string) => {
|
2023-10-27 03:59:05 +08:00
|
|
|
setMapOfDataChannelInformation((
|
2024-04-24 05:17:32 +08:00
|
|
|
previousMap,
|
2023-10-27 03:59:05 +08:00
|
|
|
) => {
|
|
|
|
if (pluginName === pluginNameInUse) {
|
2024-04-24 05:17:32 +08:00
|
|
|
const uniqueId = createChannelIdentifier(channelName, subChannelName, pluginName);
|
|
|
|
const newMap = new Map<string, MapInformation>(previousMap.entries());
|
|
|
|
let newArrayTypes: DataChannelTypes[] = previousMap.get(uniqueId)?.types || [];
|
|
|
|
if (deltaSubscribe < 0) {
|
|
|
|
const index = newArrayTypes.indexOf(dataChannelType);
|
|
|
|
if (index > -1) {
|
|
|
|
newArrayTypes.splice(index, 1);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
newArrayTypes = newArrayTypes.concat([dataChannelType]);
|
|
|
|
}
|
|
|
|
newMap.set(uniqueId, {
|
|
|
|
totalUses: (previousMap.get(uniqueId)?.totalUses || 0) + deltaSubscribe,
|
|
|
|
subChannelName,
|
|
|
|
channelName,
|
|
|
|
types: newArrayTypes,
|
|
|
|
});
|
2023-10-27 03:59:05 +08:00
|
|
|
return newMap;
|
|
|
|
}
|
|
|
|
return previousMap;
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
const subscribeHandler: EventListener = (
|
2023-11-29 02:31:28 +08:00
|
|
|
(event: HookEventWrapper<void>) => {
|
2024-01-31 05:38:09 +08:00
|
|
|
if (event.detail.hook === DataChannelHooks.DATA_CHANNEL_BUILDER) {
|
2023-11-29 02:31:28 +08:00
|
|
|
const eventDetails = event.detail as SubscribedEventDetails;
|
2024-04-24 05:17:32 +08:00
|
|
|
const hookArguments = eventDetails?.hookArguments as DataChannelArguments;
|
|
|
|
const dataChannelTypeFromEvent = hookArguments.dataChannelType!;
|
|
|
|
const { subChannelName } = hookArguments;
|
2023-11-29 02:31:28 +08:00
|
|
|
if (hookArguments?.channelName && hookArguments?.pluginName) {
|
2024-04-24 05:17:32 +08:00
|
|
|
updateHookUsage(hookArguments.channelName,
|
|
|
|
hookArguments.pluginName, 1, dataChannelTypeFromEvent, subChannelName);
|
2023-10-27 03:59:05 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}) as EventListener;
|
|
|
|
const unsubscribeHandler: EventListener = (
|
2023-11-29 02:31:28 +08:00
|
|
|
(event: HookEventWrapper<void>) => {
|
2024-01-31 05:38:09 +08:00
|
|
|
if (event.detail.hook === DataChannelHooks.DATA_CHANNEL_BUILDER) {
|
2023-11-29 02:31:28 +08:00
|
|
|
const eventDetails = event.detail as UnsubscribedEventDetails;
|
2024-04-24 05:17:32 +08:00
|
|
|
const hookArguments = eventDetails?.hookArguments as DataChannelArguments;
|
|
|
|
const dataChannelTypeFromEvent = hookArguments.dataChannelType!;
|
|
|
|
const { subChannelName } = hookArguments;
|
2023-11-29 02:31:28 +08:00
|
|
|
if (hookArguments?.channelName && hookArguments?.pluginName) {
|
2024-04-24 05:17:32 +08:00
|
|
|
updateHookUsage(hookArguments.channelName,
|
|
|
|
hookArguments.pluginName, -1, dataChannelTypeFromEvent, subChannelName);
|
2023-10-27 03:59:05 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}) as EventListener;
|
|
|
|
|
2023-11-29 02:31:28 +08:00
|
|
|
window.addEventListener(HookEvents.SUBSCRIBED, subscribeHandler);
|
|
|
|
window.addEventListener(HookEvents.UNSUBSCRIBED, unsubscribeHandler);
|
2023-10-27 03:59:05 +08:00
|
|
|
return () => {
|
2023-11-29 02:31:28 +08:00
|
|
|
window.removeEventListener(HookEvents.SUBSCRIBED, subscribeHandler);
|
|
|
|
window.removeEventListener(HookEvents.UNSUBSCRIBED, unsubscribeHandler);
|
2023-10-27 03:59:05 +08:00
|
|
|
};
|
|
|
|
}, []);
|
|
|
|
|
|
|
|
return (
|
|
|
|
<>
|
|
|
|
{
|
2024-04-24 05:17:32 +08:00
|
|
|
Array.from(mapOfDataChannelInformation.keys()).filter(
|
|
|
|
(keyIdentifier: string) => mapOfDataChannelInformation.get(keyIdentifier) !== undefined
|
|
|
|
&& mapOfDataChannelInformation.get(keyIdentifier)!.totalUses > 0,
|
|
|
|
).map((keyIdentifier: string) => {
|
|
|
|
const { subChannelName, channelName } = mapOfDataChannelInformation.get(keyIdentifier)!;
|
2024-05-02 03:57:08 +08:00
|
|
|
const identifier = createChannelIdentifier(
|
|
|
|
channelName, subChannelName, pluginNameInUse,
|
|
|
|
);
|
2024-04-24 05:17:32 +08:00
|
|
|
return (
|
2023-10-27 03:59:05 +08:00
|
|
|
<DataChannelItemManager
|
|
|
|
{...{
|
2024-05-02 03:57:08 +08:00
|
|
|
key: identifier,
|
|
|
|
identifier,
|
2023-10-27 03:59:05 +08:00
|
|
|
pluginName: pluginNameInUse,
|
2024-04-24 05:17:32 +08:00
|
|
|
channelName,
|
|
|
|
subChannelName,
|
|
|
|
dataChannelTypes: mapOfDataChannelInformation.get(keyIdentifier)!.types,
|
2023-10-28 00:54:00 +08:00
|
|
|
pluginApi,
|
2023-10-27 03:59:05 +08:00
|
|
|
}}
|
|
|
|
/>
|
2024-04-24 05:17:32 +08:00
|
|
|
);
|
|
|
|
})
|
2023-10-27 03:59:05 +08:00
|
|
|
}
|
|
|
|
</>
|
|
|
|
);
|
2024-01-16 01:12:39 +08:00
|
|
|
}) as React.ElementType<PluginDataChannelManagerProps>;
|
2023-10-27 03:59:05 +08:00
|
|
|
|
2024-01-16 01:12:39 +08:00
|
|
|
export default PluginDataChannelManager;
|