webssh2-server
Version:
A Websocket to SSH2 gateway using xterm.js, socket.io, ssh2
62 lines (61 loc) • 1.83 kB
JavaScript
/**
* Root session reducer combining all sub-reducers
*/
import { authReducer } from './auth-reducer.js';
import { connectionReducer } from './connection-reducer.js';
import { terminalReducer } from './terminal-reducer.js';
import { metadataReducer } from './metadata-reducer.js';
import { createInitialState } from '../types.js';
/**
* Main session reducer - combines all sub-reducers
*/
export const sessionReducer = (state, action) => {
if (action.type === 'SESSION_RESET') {
return createInitialState(state.id);
}
if (action.type === 'SESSION_END') {
return applySessionEnd(state);
}
return reduceSubReducers(state, action);
};
function applySessionEnd(state) {
return {
...state,
connection: {
...state.connection,
status: 'closed',
connectionId: null,
errorMessage: 'Session ended'
},
auth: {
...state.auth,
status: 'pending',
username: null
}
};
}
function reduceSubReducers(state, action) {
const newAuth = authReducer(state.auth, action);
const newConnection = connectionReducer(state.connection, action);
const newTerminal = terminalReducer(state.terminal, action);
const newMetadata = metadataReducer(state.metadata, action);
if (newAuth === state.auth &&
newConnection === state.connection &&
newTerminal === state.terminal &&
newMetadata === state.metadata) {
return state;
}
return {
id: state.id,
auth: newAuth,
connection: newConnection,
terminal: newTerminal,
metadata: newMetadata
};
}
/**
* Helper to check if state actually changed
*/
export const hasStateChanged = (oldState, newState) => {
return oldState !== newState;
};