Elisée Maurer avatar Elisée Maurer committed 22b6fc0

Use a single switch instead of lots of 'in' for handling commands

Comments (0)

Files changed (1)

 //------------------------------------------------------------------------------
 exports.handleCommand = function( db, client, data )
 {
-    // Add group
-    if( 'addGroup' in data )
+    for( command in data )
     {
-        exports.clientAddGroup( db, client, data['addGroup']['newGroupName'].toString() );
-    }
-    
-    // Leave group
-    if( 'leaveGroup' in data )
-    {
-        exports.clientLeaveGroup( db, client, parseInt(data['leaveGroup']['groupId']) );
-    }
-    
-    // Get thread gadgets
-    if( 'getThreadGadgets' in data )
-    {
-        exports.sendClientThreadGadgets( db, client, parseInt(data['getThreadGadgets']['threadId']) );
-    }
-    
-    // Get gadget data
-    if( 'getGadgetData' in data )
-    {
-        exports.sendClientGadgetData( db, client, parseInt(data['getGadgetData']['gadgetId']) );
-    }
-    
-    // Update gadget title
-    if( 'updateGadgetTitle' in data )
-    {
-        exports.updateClientGadgetTitle( db, client, parseInt(data['updateGadgetTitle']['gadgetId']), data['updateGadgetTitle']['gadgetTitle'].toString() );
-    }
-    
-    // Gadget command
-    if( 'runGadgetCommand' in data )
-    {
-        exports.handleClientGadgetCommand( db, client, parseInt(data['runGadgetCommand']['gadgetId']), data['runGadgetCommand']['gadgetCommand'] );
-    }
-    
-    // Get thread feed
-    if( 'getThreadFeed' in data )
-    {
-        exports.sendClientThreadFeed( db, client, parseInt(data['getThreadFeed']['threadId']) );
-    }
-    
-    // Create feed entry
-    if( 'createFeedEntry' in data )
-    {
-        var parentEntryId = null;
-        if( 'parentEntryId' in data['createFeedEntry'] )
+        switch( command )
         {
-            parentEntryId = parseInt( data['createFeedEntry']['parentEntryId'] );
+            // Add group
+            case 'addGroup':
+                exports.clientAddGroup( db, client, data['addGroup']['newGroupName'].toString() );
+                break;
+            
+            // Leave group
+            case 'leaveGroup':
+                exports.clientLeaveGroup( db, client, parseInt(data['leaveGroup']['groupId']) );
+                break;
+            
+            // Get thread gadgets
+            case 'getThreadGadgets':
+                exports.sendClientThreadGadgets( db, client, parseInt(data['getThreadGadgets']['threadId']) );
+                break;
+            
+            // Get gadget data
+            case 'getGadgetData':
+                exports.sendClientGadgetData( db, client, parseInt(data['getGadgetData']['gadgetId']) );
+                break;
+            
+            // Update gadget title
+            case 'updateGadgetTitle':
+                exports.updateClientGadgetTitle( db, client, parseInt(data['updateGadgetTitle']['gadgetId']), data['updateGadgetTitle']['gadgetTitle'].toString() );
+                break;
+            
+            // Gadget command
+            case 'runGadgetCommand':
+                exports.handleClientGadgetCommand( db, client, parseInt(data['runGadgetCommand']['gadgetId']), data['runGadgetCommand']['gadgetCommand'] );
+                break;
+            
+            // Get thread feed
+            case 'getThreadFeed':
+                exports.sendClientThreadFeed( db, client, parseInt(data['getThreadFeed']['threadId']) );
+                break;
+            
+            // Create feed entry
+            case 'createFeedEntry':
+                var parentEntryId = null;
+                if( 'parentEntryId' in data['createFeedEntry'] )
+                {
+                    parentEntryId = parseInt( data['createFeedEntry']['parentEntryId'] );
+                }
+            
+                exports.createClientFeedEntry( db, client, parseInt(data['createFeedEntry']['threadId']), parentEntryId, data['createFeedEntry']['content'].toString() );
+                break;
+            
+            // Delete feed entry
+            case 'deleteFeedEntry':
+                exports.deleteClientFeedEntry( db, client, parseInt(data['deleteFeedEntry']['entryId']) );
+                break;
+            
+            // Update feed entry
+            case 'updateFeedEntry':
+                exports.updateClientFeedEntry( db, client, parseInt(data['updateFeedEntry']['entryId']), data['updateFeedEntry']['content'].toString() );
+                break;
+            
+            // Mark feed entry as read
+            case 'markFeedEntryAsRead':
+                exports.markClientFeedEntryAsRead( db, client, parseInt(data['markFeedEntryAsRead']['entryId']) );
+                break;
+            
+            // Like feed entry
+            case 'likeFeedEntry':
+                exports.likeClientFeedEntry( db, client, parseInt(data['likeFeedEntry']['entryId']) );
+                break;
+            
+            // Unlike feed entry
+            case 'unlikeFeedEntry':
+                exports.unlikeClientFeedEntry( db, client, parseInt(data['unlikeFeedEntry']['entryId']) );
+                break;
+            
+            // Add member to group
+            case 'addMemberToGroup':
+                exports.addMemberToClientGroup( db, client, parseInt( data['addMemberToGroup']['groupId'] ), data['addMemberToGroup']['username'].toString() );
+                break;
+            
+            // Start thread
+            case 'startThread':
+                exports.startClientThread( db, client, parseInt( data['startThread']['groupId'] ), data['startThread']['title'].toString() );
+                break;
+            
+            // Add gadget to thread
+            case 'addThreadGadget':
+                exports.addClientThreadGadget( db, client, data['addThreadGadget']['gadgetType'].toString(), parseInt( data['addThreadGadget']['threadId'] ) );
+                break;
+
+            // Delete gadget from thread
+            case 'deleteThreadGadget':
+                exports.deleteClientThreadGadget( db, client, parseInt( data['deleteThreadGadget']['gadgetId'] ) );
+                break;
+            
+            // Search for users
+            case 'searchUsers':
+                exports.searchClientUsers( db, client, parseInt(data['searchUsers']['requestId']), data['searchUsers']['needle'].toString() );
+                break;
         }
-    
-        exports.createClientFeedEntry( db, client, parseInt(data['createFeedEntry']['threadId']), parentEntryId, data['createFeedEntry']['content'].toString() );
-    }
-    
-    // Delete feed entry
-    if( 'deleteFeedEntry' in data )
-    {
-        exports.deleteClientFeedEntry( db, client, parseInt(data['deleteFeedEntry']['entryId']) );
-    }
-    
-    // Update feed entry
-    if( 'updateFeedEntry' in data )
-    {
-        exports.updateClientFeedEntry( db, client, parseInt(data['updateFeedEntry']['entryId']), data['updateFeedEntry']['content'].toString() );
-    }
-    
-    // Mark feed entry as read
-    if( 'markFeedEntryAsRead' in data )
-    {
-        exports.markClientFeedEntryAsRead( db, client, parseInt(data['markFeedEntryAsRead']['entryId']) );
-    }
-    
-    // Like feed entry
-    if( 'likeFeedEntry' in data )
-    {
-        exports.likeClientFeedEntry( db, client, parseInt(data['likeFeedEntry']['entryId']) );
-    }
-    
-    // Unlike feed entry
-    if( 'unlikeFeedEntry' in data )
-    {
-        exports.unlikeClientFeedEntry( db, client, parseInt(data['unlikeFeedEntry']['entryId']) );
-    }
-    
-    // Add member to group
-    if( 'addMemberToGroup' in data )
-    {
-        exports.addMemberToClientGroup( db, client, parseInt( data['addMemberToGroup']['groupId'] ), data['addMemberToGroup']['username'].toString() );
-    }
-    
-    // Start thread
-    if( 'startThread' in data )
-    {
-        exports.startClientThread( db, client, parseInt( data['startThread']['groupId'] ), data['startThread']['title'].toString() );
-    }
-    
-    // Add gadget to thread
-    if( 'addThreadGadget' in data )
-    {
-        exports.addClientThreadGadget( db, client, data['addThreadGadget']['gadgetType'].toString(), parseInt( data['addThreadGadget']['threadId'] ) );
-    }
-
-    // Delete gadget from thread
-    if( 'deleteThreadGadget' in data )
-    {
-        exports.deleteClientThreadGadget( db, client, parseInt( data['deleteThreadGadget']['gadgetId'] ) );
-    }
-    
-    // Search for users
-    if( 'searchUsers' in data )
-    {
-        exports.searchClientUsers( db, client, parseInt(data['searchUsers']['requestId']), data['searchUsers']['needle'].toString() );
     }
 }
 
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.