Skip to content

Commit

Permalink
feat: wip event handlers
Browse files Browse the repository at this point in the history
  • Loading branch information
itschip committed Sep 5, 2024
1 parent f2425b7 commit ead69b5
Show file tree
Hide file tree
Showing 7 changed files with 106 additions and 2 deletions.
10 changes: 10 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

26 changes: 26 additions & 0 deletions src/client/event/netEventController.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import { customAlphabet } from "nanoid";

function uniqueId() {
return customAlphabet("1234567890abcdef", 21);
}

export function netEventController<TResponse>(
event: string,
...args: any[]
): Promise<TResponse> {
// For now we just assume the world is perfect and we don't need to handle errors
// This will be fixed
return new Promise((resolve) => {
const eventId = uniqueId();
const listenName = `${event}:${eventId}`;

emitNet("removeEventListener", listenName, ...args);

const eventListener = (data: TResponse) => {
removeEventListener(listenName, eventListener);
resolve(data);
};

onNet(listenName, eventListener);
});
}
12 changes: 12 additions & 0 deletions src/client/handler/client-player.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { netEventController } from "../event/netEventController";

type OpenPhoneData = {
message: string;
};

// asume this is an exported function
async function nuiProxyController(data: any) {
const resp = await netEventController<OpenPhoneData>("openPhone", data);

console.log(resp.message);
}
3 changes: 2 additions & 1 deletion src/client/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
},
"dependencies": {
"@citizenfx/client": "^2.0.4170-1",
"@types/node": "^22.5.2"
"@types/node": "^22.5.2",
"nanoid": "^5.0.7"
}
}
12 changes: 12 additions & 0 deletions src/server/event/context.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
export type Context = {
source: number;
};

// We might be able to add a few more things here, like the phone number for example
export function getEventContext(): Context {
const _source = global.source;

return {
source: _source,
};
}
24 changes: 23 additions & 1 deletion src/server/event/eventController.ts
Original file line number Diff line number Diff line change
@@ -1 +1,23 @@
export function eventController() {}
import { type Context, getEventContext } from "./context";

type Event<T = unknown> = {
body: T;
ctx: Context;
};

export function eventController<TData, TResponse = void>(
event: string,
callback: (req: Event<TData>) => Promise<TResponse>,
) {
onNet(event, async (responseEvent: string, data: TData) => {
const ctx = getEventContext();

// TODO: Add status codes or something to the response
const response = await callback({
body: data,
ctx: ctx,
});

return emitNet(responseEvent, ctx.source, response);
});
}
21 changes: 21 additions & 0 deletions src/server/handler/player-handler.ts
Original file line number Diff line number Diff line change
@@ -1 +1,22 @@
import { eventController } from "../event/eventController";

type PlayerTest = {
name: string;
phoneNumber: string;
};

type PlayerTestResponse = {
message: string;
};

eventController<PlayerTest, PlayerTestResponse>(
"player:test",
async ({ ctx, body }) => {
console.log(ctx.source);
console.log(body.name, body.phoneNumber);

return {
message: "Hello, World!",
};
},
);

0 comments on commit ead69b5

Please sign in to comment.