Giter Site home page Giter Site logo

fxmediator's Introduction

FxMediator

A simple mediator that facilitates strongly typed events in FiveM.

Please note: The current implementation JSON serializes every request. If performance is a main concern then please refrain from using this package.

Requests

Requests are events that are designed to be handled by one handler. Requests can optionally respond to the sender by supplying a response type.

Declare a request

Define requests in your shared project. A request is a class that inherits one of the following:

Base Type Description
IServerRequest A server-sided message with no response
IServerRequest<TResponse> A server-sided message with a response of TResponse
IClientRequest A client-sided message with no response
IClientRequest<TResponse> A client-sided message with a response of TResponse

Example:

// A server request that returns a string
public class HelloWorldMessage : IServerRequest<string> // Could also be IClientRequest<string> if this was a client request
{
    public string Name { get; set; }
}

Add a request handler

To respond to this message: create a new instance of ServerMediator in the server script, or ClientMediator in the client script, and call mediator.AddRequestHandler<TMessage, TResponse>(Handler) like below.

public class ServerMain : BaseScript
{
    private readonly ServerMediator _mediator = new ServerMediator();
    public ServerMain()
    {
        _mediator.AddRequestHandler<HelloWorldMessage, string>(OnHelloWorldMessage);
    }

    private Task<string> OnHelloWorldMessage(HelloWorldMessage message)
    {
        return Task.FromResult($"Hello {message.Name}!");
    }
}

Send the request

To send a request: call mediator.SendToServer(message)

public class ClientMain : BaseScript
{
    private readonly ClientMediator _mediator = new ClientMediator();
    public ClientMain()
    {
        EventHandlers["onClientResourceStart"] += new Func<string, Task>(OnResourceStart);
    }

    private async Task OnResourceStart(string arg)
    {
        if (arg == GetCurrentResourceName())
        {
            var message = await _mediator.SendToServer(new HelloWorldMessage
            {
                Name = "World"
            });
            
            Debug.WriteLine(message);
        }
    }
}

Notifications

Notifications are events that are designed to be handled by many handlers. Notifications should be past tense. Ie. PlayerConnected, or PlayerDied.

Declare a notification

Declare notifications in your shared project. A notification is a class that inherits INotification

public class HelloWorldJustOccurred : INotification
{
    public string Message { get; set; }
}

Add a notification handler

Add a notification handler to either your client or server script via mediator.AddNotificationHandler<TNotification>(Handler)

public class ClientMain : BaseScript
{
    private readonly ClientMediator _mediator = new ClientMediator();

    public ClientMain()
    {
        _mediator.AddNotificationHandler<HelloWorldJustOccurred>(OnHelloWorldJustOccurred);
    }

    private Task OnHelloWorldJustOccurred(HelloWorldJustOccurred @event)
    {
        Debug.WriteLine($"A hello world just occurred! {@event.Message}");
        return Task.FromResult(0);
    }
}

Publish your notification

Use either mediator.PublishToClient(message), mediator.PublishToServer(message), or mediator.PublishToAll(message) to publish your event.

_mediator.PublishToAll(new HelloWorldJustOccurred
{
    Message = message
});

fxmediator's People

Contributors

lfshr avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.