-
-
Notifications
You must be signed in to change notification settings - Fork 277
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
106 additions
and
2 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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!", | ||
}; | ||
}, | ||
); |