Skip to content

Commit

Permalink
js: normalize api function names (and comments)
Browse files Browse the repository at this point in the history
  • Loading branch information
David Hartmann authored and JackUrb committed May 2, 2023
1 parent 7067b9e commit 3e600bb
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 57 deletions.
78 changes: 38 additions & 40 deletions js/api/ApiProvider.js
Expand Up @@ -8,13 +8,14 @@ const ApiProvider = ({ children }) => {
const [connected, setConnected] = useState(false);
const [sessionInfo, setSessionInfo] = useState({ id: null, readonly: false });
const _socket = useRef(null);
const onHandlers = useRef(null);
const apiHandlers = useRef(null);

// ---------------- //
// helper functions //
// ---------------- //

// retrieve normalized window.location
// Normalize window.location by removing specific path segments
// and ensuring the pathname ends with a '/'
const correctPathname = () => {
var pathname = window.location.pathname;
if (pathname.indexOf('/env/') > -1) {
Expand All @@ -32,7 +33,7 @@ const ApiProvider = ({ children }) => {
// basic communication //
// ------------------- //

// low-level message to server
// Send a low-level message to the server
const sendSocketMessage = (data) => {
if (!_socket.current) {
// TODO: error? warn?
Expand All @@ -43,7 +44,7 @@ const ApiProvider = ({ children }) => {
return _socket.current.send(msg);
};

// connect to server
// Establish a connection to the server
const connect = () => {
if (_socket.current) {
return;
Expand All @@ -53,7 +54,7 @@ const ApiProvider = ({ children }) => {
setConnected(true);
};
const _onDisconnect = () => {
onHandlers.current.onDisconnect(_socket);
apiHandlers.current.onDisconnect(_socket);
setConnected(false);
};

Expand Down Expand Up @@ -85,7 +86,7 @@ const ApiProvider = ({ children }) => {
_socket.current = socket;
};

// close server connection
// Close the server connection and reset the _socket ref
const disconnect = () => {
_socket.current.close();
_socket.current = null;
Expand All @@ -95,7 +96,9 @@ const ApiProvider = ({ children }) => {
// API receive events //
// -------------------//

// handle server messages
// Process messages received from the server by
// implicitly defining event handlers for
// different types of server-commands
const handleMessage = (evt) => {
var cmd = JSON.parse(evt.data);
switch (cmd.command) {
Expand All @@ -109,26 +112,26 @@ const ApiProvider = ({ children }) => {
case 'pane':
case 'window':
case 'window_update':
onHandlers.current.onWindowMessage({
apiHandlers.current.onWindowMessage({
cmd: cmd,
update: cmd.commmand == 'window_update',
});
break;
case 'reload':
onHandlers.current.onReloadMessage(cmd.data);
apiHandlers.current.onReloadMessage(cmd.data);
break;
case 'close':
onHandlers.current.onCloseMessage(cmd.data);
apiHandlers.current.onCloseMessage(cmd.data);
break;
case 'layout':
case 'layout_update':
onHandlers.current.onLayoutMessage({
apiHandlers.current.onLayoutMessage({
cmd: cmd.data,
update: cmd.commmand == 'layout_update',
});
break;
case 'env_update':
onHandlers.current.onEnvUpdate(cmd.data);
apiHandlers.current.onEnvUpdate(cmd.data);
break;

default:
Expand All @@ -143,8 +146,8 @@ const ApiProvider = ({ children }) => {
// API send events //
// ----------------//

// query env from server
const postForEnv = (envIDs) => {
// Request environment data from the server
const sendEnvQuery = (envIDs) => {
// This kicks off a new stream of events from the socket so there's nothing
// to handle here. We might want to surface the error state.
if (envIDs.length == 1) {
Expand All @@ -164,6 +167,7 @@ const ApiProvider = ({ children }) => {
}
};

// Toggle connection state between online and offline
const toggleOnlineState = () => {
if (connected) {
disconnect();
Expand All @@ -172,14 +176,7 @@ const ApiProvider = ({ children }) => {
}
};

/**
* Send message to backend.
*
* The `data` object is extended by pane and environment Id.
* Note: Only focused panes should call this method.
*
* @param data Data to be sent to backend.
*/
// Send message to server backend for a specific pane and environment.
const sendPaneMessage = (data, targetPaneID, targetEnvID) => {
if (targetPaneID === null || sessionInfo.readonly) {
return;
Expand All @@ -195,6 +192,7 @@ const ApiProvider = ({ children }) => {
});
};

// Send request to revert to the previous set of embeddings in the given pane
const sendEmbeddingPop = (data, targetPaneID, targetEnvID) => {
if (targetPaneID === null || sessionInfo.readonly) {
return;
Expand All @@ -210,22 +208,25 @@ const ApiProvider = ({ children }) => {
});
};

const sendClosePane = (paneID, envID) => {
// Send request to close a specific pane
const sendPaneClose = (paneID, envID) => {
sendSocketMessage({
cmd: 'close',
data: paneID,
eid: envID,
});
};

const sendDeleteEnv = (envID, previousEnv) => {
// Send request to delete an environment
const sendEnvDelete = (envID, previousEnv) => {
sendSocketMessage({
cmd: 'delete_env',
prev_eid: previousEnv,
eid: envID,
});
};

// Send request to save the current environment
const sendEnvSave = (envID, prev_envID, data) => {
sendSocketMessage({
cmd: 'save',
Expand All @@ -235,12 +236,8 @@ const ApiProvider = ({ children }) => {
});
};

/**
* Send layout item state to backend to update backend state.
*
* @param layout Layout to be sent to backend.
*/
const sendLayoutItemState = (
// Update the pane layout item in the backend.
const sendPaneLayoutUpdate = (
envID,
{ i, h, w, x, y, moved, static: staticBool }
) => {
Expand All @@ -252,7 +249,8 @@ const ApiProvider = ({ children }) => {
});
};

const exportLayoutsToServer = (layoutLists) => {
// Save layout lists to the server
const sendLayoutsSave = (layoutLists) => {
// pushes layouts to the server
let objForm = {};
for (let [envName, layoutList] of layoutLists) {
Expand Down Expand Up @@ -289,19 +287,19 @@ const ApiProvider = ({ children }) => {
return (
<ApiContext.Provider
value={{
apiHandlers,
connected,
sendEmbeddingPop,
sendEnvDelete,
sendEnvQuery,
sendEnvSave,
sendLayoutsSave,
sendPaneClose,
sendPaneLayoutUpdate,
sendPaneMessage,
sessionInfo,
setConnected,
onHandlers,
postForEnv,
sendPaneMessage,
sendEmbeddingPop,
exportLayoutsToServer,
toggleOnlineState,
sendEnvSave,
sendDeleteEnv,
sendClosePane,
sendLayoutItemState,
}}
>
{children}
Expand Down
34 changes: 17 additions & 17 deletions js/main.js
Expand Up @@ -63,16 +63,16 @@ const App = () => {

// api variables & functions
const {
apiHandlers,
connected,
sendEnvDelete,
sendEnvQuery,
sendEnvSave,
sendLayoutsSave,
sendPaneClose,
sendPaneLayoutUpdate,
sessionInfo,
toggleOnlineState,
postForEnv,
exportLayoutsToServer,
onHandlers,
sendEnvSave,
sendDeleteEnv,
sendClosePane,
sendLayoutItemState,
} = useContext(ApiContext);

// internal variables
Expand Down Expand Up @@ -267,7 +267,7 @@ const App = () => {
// that is selected that isn't from the compare output, we need to
// reload the compare output
if (selection.envIDs.length > 1 && cmd.has_compare !== true) {
postForEnv(selection.envIDs);
sendEnvQuery(selection.envIDs);
} else if (update) {
updateWindow(cmd);
} else {
Expand Down Expand Up @@ -310,7 +310,7 @@ const App = () => {
delete newPanes[paneID];
if (!keepPosition) {
localStorage.removeItem(keyLS(paneID));
sendClosePane(paneID, selection.envIDs[0]);
sendPaneClose(paneID, selection.envIDs[0]);
}

if (setState) {
Expand Down Expand Up @@ -366,11 +366,11 @@ const App = () => {
}));
setFocusedPaneID(isSameEnv ? focusedPaneID : null);
localStorage.setItem('envIDs', JSON.stringify(selectedNodes));
postForEnv(selectedNodes);
sendEnvQuery(selectedNodes);
};

const onEnvDelete = (env2delete, previousEnv) => {
sendDeleteEnv(env2delete, previousEnv);
sendEnvDelete(env2delete, previousEnv);
};

const onEnvSave = (env) => {
Expand Down Expand Up @@ -624,7 +624,7 @@ const App = () => {
}
let layoutLists = storeMeta.layoutLists;
layoutLists.get(selection.envIDs[0]).set(layoutName, layoutMap);
exportLayoutsToServer(layoutLists);
sendLayoutsSave(layoutLists);
setStoreMeta((prev) => ({
...prev,
layoutLists: layoutLists,
Expand All @@ -639,7 +639,7 @@ const App = () => {
// Deletes the selected view, pushes to server
let layoutLists = storeMeta.layoutLists;
layoutLists.get(selection.envIDs[0]).delete(layoutName);
exportLayoutsToServer(layoutLists);
sendLayoutsSave(layoutLists);
setStoreMeta((prev) => ({
...prev,
layoutLists: layoutLists,
Expand All @@ -664,20 +664,20 @@ const App = () => {

// ask server for envs after registration succeeded
useEffect(() => {
postForEnv(selection.envIDs);
sendEnvQuery(selection.envIDs);
}, [sessionInfo]);

//componentDidUpdate
useEffect(() => {
if (mounted.current) {
if (selection.envIDs.length > 0) {
postForEnv(selection.envIDs);
sendEnvQuery(selection.envIDs);
} else {
setSelection((prev) => ({
...prev,
envIDs: ['main'],
}));
postForEnv(['main']);
sendEnvQuery(['main']);
}
}

Expand Down Expand Up @@ -839,7 +839,7 @@ const App = () => {
};

const onCloseMessage = closePane;
onHandlers.current = {
apiHandlers.current = {
onWindowMessage,
onLayoutMessage,
onReloadMessage,
Expand Down

0 comments on commit 3e600bb

Please sign in to comment.