refactor: 重连功能迁移重构,画布数据更新迁移
parent
3396cf3d62
commit
05ed81fa5a
@ -0,0 +1,59 @@
|
||||
const hasObjectValues = (value: any) => {
|
||||
return Boolean(value && typeof value === 'object' && Object.keys(value).length > 0);
|
||||
};
|
||||
|
||||
const hasInitialCanvasData = (initialData: any, useDefault: boolean) => {
|
||||
if (!initialData) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (Array.isArray(initialData)) {
|
||||
return initialData.length > 0;
|
||||
}
|
||||
|
||||
if (useDefault) {
|
||||
return (
|
||||
hasObjectValues(initialData?.main?.components) ||
|
||||
hasObjectValues(initialData?.components)
|
||||
);
|
||||
}
|
||||
|
||||
return hasObjectValues(initialData);
|
||||
};
|
||||
|
||||
export const hasCanvasContent = (canvas: any) => {
|
||||
return Boolean(
|
||||
canvas &&
|
||||
((Array.isArray(canvas.nodes) && canvas.nodes.length > 0) ||
|
||||
(Array.isArray(canvas.edges) && canvas.edges.length > 0))
|
||||
);
|
||||
};
|
||||
|
||||
export const shouldUseCachedCanvas = (params: {
|
||||
cachedCanvas: any;
|
||||
initialData: any;
|
||||
useDefault: boolean;
|
||||
}) => {
|
||||
const { cachedCanvas, initialData, useDefault } = params;
|
||||
if (!cachedCanvas) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (hasCanvasContent(cachedCanvas)) {
|
||||
return true;
|
||||
}
|
||||
|
||||
return !hasInitialCanvasData(initialData, useDefault);
|
||||
};
|
||||
|
||||
export const shouldPersistCanvas = (params: {
|
||||
nodes: any[];
|
||||
edges: any[];
|
||||
isRunning?: boolean;
|
||||
}) => {
|
||||
if (params.isRunning) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return hasCanvasContent(params);
|
||||
};
|
||||
@ -0,0 +1,43 @@
|
||||
interface RuntimeReconnectApp {
|
||||
id?: string | number;
|
||||
key?: string | number;
|
||||
instanceId?: string | number;
|
||||
scheduled?: number | string | boolean;
|
||||
}
|
||||
|
||||
interface RuntimeReconnectRequestParams {
|
||||
app: RuntimeReconnectApp | null | undefined;
|
||||
socketId: string | null | undefined;
|
||||
lastReconnectKey: string;
|
||||
}
|
||||
|
||||
export const isScheduledRunning = (app: RuntimeReconnectApp | null | undefined) => {
|
||||
return Boolean(
|
||||
app && (app.scheduled === 1 || app.scheduled === '1' || app.scheduled === true)
|
||||
);
|
||||
};
|
||||
|
||||
export const buildRuntimeReconnectRequest = ({
|
||||
app,
|
||||
socketId,
|
||||
lastReconnectKey,
|
||||
}: RuntimeReconnectRequestParams) => {
|
||||
const instanceId = app?.instanceId ? String(app.instanceId) : '';
|
||||
const newSocketId = socketId ? String(socketId) : '';
|
||||
const appKey = app?.id || app?.key ? String(app.id || app.key) : '';
|
||||
|
||||
if (!isScheduledRunning(app) || !instanceId || !newSocketId || !appKey) {
|
||||
return null;
|
||||
}
|
||||
|
||||
const reconnectKey = `${appKey}:${instanceId}:${newSocketId}`;
|
||||
if (reconnectKey === lastReconnectKey) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return {
|
||||
instanceId,
|
||||
newSocketId,
|
||||
reconnectKey,
|
||||
};
|
||||
};
|
||||
Loading…
Reference in New Issue