* -------------------------------------------------------------------------
* This file is part of the MindStudio project.
* Copyright (c) 2025 Huawei Technologies Co.,Ltd.
*
* MindStudio is licensed under Mulan PSL v2.
* You can use this software according to the terms and conditions of the Mulan PSL v2.
* You may obtain a copy of Mulan PSL v2 at:
*
* http://license.coscl.org.cn/MulanPSL2
*
* THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
* EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
* MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
* See the Mulan PSL v2 for more details.
* -------------------------------------------------------------------------
*/
import { runInAction } from 'mobx';
import { store } from '@/store';
import type { CardInfo, Session } from '@/entity/session';
import connector from '@/connection';
import { firstLetterUpper } from '@/utils';
import {
sendMap,
sendLanguage,
sendStatus,
sendTheme,
} from './sendNotification';
import { NotificationMessage } from './notification';
import { SessionAction } from '@/utils/enum';
import { customConsole as console } from '@insight/lib/utils';
export const updateSessionHandler = (e: NotificationMessage): void => {
const session = store.sessionStore.activeSession;
const receiver = e.data.body;
if (receiver === undefined) {
console.warn('data.body is undefined, please check your params');
}
const updateState = updateSession(receiver);
if (updateState.unitcount) {
session.timelinePageInfo.unitCount = updateState.unitcount;
}
setTimeout(() => {
const isSend =
(updateState.parseCompleted !== undefined ||
updateState.unitcount !== undefined ||
updateState.isBinary === true ||
updateState.durationFileCompleted === true ||
updateState.isIpynb === true ||
updateState.ipynbUrl !== '') &&
receiver.broadcast !== false;
if (isSend) {
connector.send({
event: 'updateSession',
body: {
parseCompleted: session.parseCompleted,
unitcount: session.unitcount,
instrVersion: session.instrVersion,
...updateState,
},
});
}
});
};
export const updateSession = (receiver: Record<string, any>): Record<string, any> => {
if (receiver === undefined || receiver == null) {
return {};
}
const session = store.sessionStore.activeSession;
const receiverPropKeys = Object.keys(receiver);
const sessionPropKeys = Object.keys(session);
const updateState: Record<string, any> = {};
for (const key of receiverPropKeys) {
const isSameType = Object.prototype.toString.call(receiver[key]) === Object.prototype.toString.call(session[key as keyof Session]);
const valid = sessionPropKeys.includes(key) && (isSameType || session[key as keyof Session] === null);
if (valid) {
Object.assign(updateState, { [key]: receiver[key] });
} else {
console.warn(`you just send a invalid data: {${key}: ${receiver[key]}} to update session, please check it`);
}
}
runInAction(() => {
Object.entries(updateState).forEach(([key, value]) => {
(session as any)[key] = value;
});
});
return updateState;
};
export const getParseStatusHandler = (e: NotificationMessage): void => {
const session = store.sessionStore.activeSession;
const receiver = e.data.body;
const requestList = receiver?.requests as string[];
if (requestList?.length > 0) {
requestList.forEach(key => {
sendMap[key]?.(e.data.from);
});
return;
}
const requestKey = receiver?.request as string;
if (requestKey !== undefined && requestKey !== null && (session as any)[requestKey] !== undefined) {
connector.send({
event: 'updateSession',
body: { [requestKey]: (session as any)[requestKey] },
to: receiver?.from === undefined ? undefined : firstLetterUpper(receiver?.from),
});
return;
}
sendStatus();
sendTheme();
};
export const getThemeHandler = (): void => {
sendTheme();
};
export const deleteCardHandler = (e: NotificationMessage): void => {
const receiver = e.data.body;
if (receiver === null || receiver === undefined) {
console.warn('data.body is undefined, please check your params');
return;
}
connector.send({ event: 'deleteCard', body: receiver });
const removeCardInfos = receiver.info as CardInfo[];
const removeCardIds: Set<string> = new Set(removeCardInfos.map(({ cardId }) => cardId));
if (removeCardIds.size > 0) {
const session = store.sessionStore.activeSession;
const memoryCardInfos = session.memoryCardInfos.filter((item) => !removeCardIds.has(item.rankInfo.rankId));
const operatorCardInfos = session.operatorCardInfos.filter((item) => !removeCardIds.has(item.rankInfo.rankId));
const iERankIds = session.iERankIds.filter((item) => !removeCardIds.has(item));
updateSession({ memoryCardInfos, operatorCardInfos, iERankIds });
}
};
export const switchModuleHandler = (e: NotificationMessage): void => {
const session = store.sessionStore.activeSession;
const body = e.data.body;
if (body?.switchTo === null || body?.switchTo === undefined) {
return;
}
const moduleName = firstLetterUpper(body.switchTo);
runInAction(() => {
session.actionListener = { type: SessionAction.SWITCH_ACTIVE_MODULE, value: moduleName };
});
if (body.toModuleEvent !== undefined) {
connector.send({
event: body.toModuleEvent,
to: body.broadcast ? undefined : moduleName,
body: body.params,
});
}
};
export const getLanguageHandler = (e: NotificationMessage): void => {
sendLanguage(e.data.from);
};
export const openImportDialogHandler = (): void => {
const session = store.sessionStore.activeSession;
session.actionListener = { type: SessionAction.IMPORT_MOE_LOAD_DATA, value: '' };
};
export const clusterCompletedHandler = (e: any): void => {
const data = e.data.body;
const session = store.sessionStore.activeSession;
runInAction(() => {
session.setClusterParsed(data.clusterPath);
connector.send({
event: 'updateClusterPageInfo',
body: session.clusterPageInfo,
});
});
};
export const clusterDurationCompletedHandler = (e: any): void => {
const data = e.data.body;
const session = store.sessionStore.activeSession;
runInAction(() => {
session.setClusterDurationParsed(data.clusterPath);
if (data.isCluster !== undefined) {
session.isCluster = data.isCluster;
}
connector.send({
event: 'updateClusterPageInfo',
body: session.clusterPageInfo,
});
});
};