Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add ESX.Switch function #1595

Closed
wants to merge 3 commits into from
Closed

Conversation

Zykem
Copy link
Contributor

@Zykem Zykem commented Jan 14, 2025

Description


This PR adds a new ESX.Switch function. It enhances the existing logic by enabling flexible argument passing to case functions and improving error handling. The function is similar to the switch statement found in other programming languages like JavaScript, offering an elegant way to handle multiple conditions. It allows handling various cases based on a given value.

Motivation


Motivation of this PR is to simplify and clean up decision-making logic. Instead of constnatly using if-elseif blocks, it offers a more readable and maintainable way of handling cases.

Implementation Details


It was implemented to receive a value, a table of possible cases, an optional default function and extra arguments that are passed to the default function (if exists).

Each case is a key-value pair in caseTable, where the key corresponds to a value and the value is a function.

It alsoo allows passing additional parameters to the matching case function by using lua's ... syntax.

Error handling is already included using core functions like ESX.IsFunctionReference, ESX.AssertType or lua's assert

Usage Example


As an example, i took this piece of code you could use for a player manager resource (similiar to EasyAdmin) where you can ban/kick players. You could use one net-event for all this and just store the actual event functions in the caseTable

local playerHandlers = {
    ban = function(playerId, reason)
        -- ban logic
    end,
    kick = function(playerId, reason)
        DropPlayer(playerId, reason or 'Unknown reason')
    end
}

RegisterNetEvent('playerActionEvent', function(action, ...)
     local source = source
    ESX.Switch(action, playerHandlers, function()
         -- default function was called, did not find a match!
    end, source, ...)
end)

PR Checklist

  • [] My commit messages and PR title follow the Conventional Commits standard.
  • [] My changes have been tested locally and function as expected.
  • [] My PR does not introduce any breaking changes.
  • [] I have provided a clear explanation of what my PR does, including the reasoning behind the changes and any relevant context.

@Kenshiin13 Kenshiin13 changed the base branch from main to dev January 14, 2025 11:55
@Arctos2win
Copy link
Contributor

Arctos2win commented Jan 14, 2025

Looks like a very unoptimized way to do an else if statement
and this will just send unnecessary data from resource to resource.

@Arctos2win Arctos2win closed this Jan 14, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
Status: Recycle Bin
Development

Successfully merging this pull request may close these issues.

2 participants