Giter Club home page Giter Club logo

pe-utils's Introduction

Material-UI logo

Project Error Utilities

A collection of useful utilities for the v8 ScRT within the CFX platform.

license npm next package Discord David Dependabot Status

Installation

This package is available as an npm package.

# npm installation
npm install @project-error/pe-utils

# yarn installation
yarn add @project-error/pe-utils

โš ๏ธNote: This project is currently in active development and may be unstable for the forseeable future

Features

  • Promisified net events
  • RPC system allowing for remote client calls from the server
  • TypeScript wrapped natives allowing for more fluid static typing through generics
  • NUI Proxy system that allows for NUI to fetch server side data without breaking the HTTP request
  • TypeScript first with declarations generated from source (vanilla JavaScript is still compatible)
  • General wrappers to make development easier within the runtime

Official Examples

You can find official examples at this GitHub Repo. These are both in TypeScript as well as regular JavaScript

Function Documentation

Inline documentations is also included for almost all of the current functions & methods.

Promisified Net Events

This package adds the ability to utilize promisified net events. Essentially a small promise wrapper around events. When using this system it is wise to keep in mind that events are transmitted over UDP and not TCP, this can lead to issues if a player has poor internet speeds. There is a default timeout of 15 seconds which can be lowered or raised.

Example

// Client Side
import { ClientUtils, RegisterNuiCB, ServerPromiseResp } from "@project-error/pe-utils";

interface ServerResponseData {
  data1: number;
  data2: number;
  data3: number;
}

const clUtils = new ClientUtils();

// NUI Callback wrapped for v8
RegisterNuiCB("someCallbackEvent", async (data: unknown, cb: (any) => void) => {
  try {
    const serverResp = await clUtils.emitNetPromise<ServerPromiseResp<ServerResponseData>>("fetchEvent");
    cb(serverResp);
  } catch (e) {
    console.error(`Error encountered: ${e.message}`);
  }
});
// Server Side
import { ServerUtils, ServerPromiseResp } from "@project-error/pe-utils";

interface IncomingData {
  thing1: string;
  thing2: number;
}

interface ReturnData {
  respData1: number;
  respData2: string;
}

const svUtils = new ServerUtils();

svUtils.onNetPromise<RequestData, ReturnData>("fetchEvent", (req, resp) => {
  const playerSrc = req.source;

  // Do some logic to get resp data on the server

  const respObj: ServerPromiseResp<ReturnData> = {
    status: "ok",
    data: {
      respData1: 12345,
      respData2: "adadsadasda",
    },
  };
  // Sent back to the client to resolve
  resp(respObj);
});

RPC System

// Client Side
interface ServerDataObj {
  coolStuff: string;
  amICool: boolean;
}

clientUtils.registerRPCListener<ServerDataObj>("niceEvent", data => {
  const playerPed = PlayerPedId();
  // This is the optional data sent by the server, you can
  // use it for logic if needed
  console.dir(data);

  const [x, y, z] = GetEntityCoords(playerPed, false);
  // We return an object here to the server with x, y, z
  return { x, y, z };
});
// Server Side
RegisterCommand(
  "debugTest",
  async (src: number) => {
    try {
      // Lets call our RPC and print the result. This is a promise, so we must await
      const coordObj = await svUtils.callClientRPC<Vector3>("niceEvent", src, {
        coolStuff: "adadadada",
        amICool: false,
      });

      console.log("Returned data:");
      console.dir(coordObj);
    } catch (e) {
      console.error(e);
    }
  },
  false
);

TODO: Further Explanations

pe-utils's People

Contributors

dependabot[bot] avatar depfu[bot] avatar liamdormon avatar napei avatar tasooneasia avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  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.