feat(rpc): change redundant init factory class to function

This commit is contained in:
2026-09-28 02:05:40 +01:00
parent 8029f24e9d
commit 50b96eeb80
3 changed files with 61 additions and 62 deletions
+40 -48
View File
@@ -1,7 +1,7 @@
import { RPCInstanceClient } from './core/client' import { RPCInstanceClient } from './core/client'
import { RPCInstanceServer } from './core/server' import { RPCInstanceServer } from './core/server'
import { RPCInstanceWebview } from './core/webview' import { RPCInstanceWebview } from './core/webview'
import { RPCError } from './utils/errors' import { RPCError, unknownEnvironmentMessage } from './utils/errors'
import { import {
type RPCConfig, type RPCConfig,
type RPCEnvironment, type RPCEnvironment,
@@ -10,64 +10,56 @@ import {
} from './utils/types' } from './utils/types'
/** /**
* RPC Factory * Creates the RPC instance for one environment. Create exactly one per
* environment (server, client, webview) and export it from a local module.
*
* @throws {@link RPCError} `UNKNOWN_ENVIRONMENT` if `config.env` is not
* `'server'`, `'client'` or `'webview'`
* *
* @example * @example
* // returns RPCInstanceServer * // server/rpc.ts
* const rpc = new RPCFactory({ env: "server" }).get() * import { createRPC } from '@entityseven/fivem-rpc'
* * export const rpc = createRPC({ env: 'server' })
* @example
* // returns RPCInstanceClient
* const rpc = new RPCFactory({ env: "client" }).get()
*
* @example
* // returns RPCInstanceWebview
* const rpc = new RPCFactory({ env: "webview" }).get()
*
* @class
*/ */
class RPCFactory<T extends RPCEnvironment> { export function createRPC<T extends RPCEnvironment>(
private readonly operator: config: RPCConfig<T>,
| RPCInstanceServer ): RPCEnvironmentResolved<T> {
| RPCInstanceClient switch (config.env) {
| RPCInstanceWebview
/**
* Instance options
* @param {object} opts - Options
* @param {string} opts.env - Instance environment
* @param {boolean} opts.debug - Show additional logs
*/
constructor(opts: RPCConfig<T>) {
console.log('[RPC] Initializing...')
switch (opts.env) {
case 'server': case 'server':
this.operator = new RPCInstanceServer(opts as RPCConfig<'server'>) return new RPCInstanceServer(
break config as RPCConfig<'server'>,
) as RPCEnvironmentResolved<T>
case 'client': case 'client':
this.operator = new RPCInstanceClient(opts as RPCConfig<'client'>) return new RPCInstanceClient(
break config as RPCConfig<'client'>,
) as RPCEnvironmentResolved<T>
case 'webview': case 'webview':
this.operator = new RPCInstanceWebview(opts as RPCConfig<'webview'>) return new RPCInstanceWebview(
break config as RPCConfig<'webview'>,
) as RPCEnvironmentResolved<T>
default: default:
throw new RPCError( throw new RPCError(
RPCErrors.UNKNOWN_ENVIRONMENT, RPCErrors.UNKNOWN_ENVIRONMENT,
RPCErrors.UNKNOWN_ENVIRONMENT, unknownEnvironmentMessage(config.env),
) )
} }
} }
public get(): RPCEnvironmentResolved<T> { export type { RPCInstanceClient } from './core/client'
return this.operator as RPCEnvironmentResolved<T> export type { RPCInstanceServer } from './core/server'
} export type { RPCInstanceWebview } from './core/webview'
}
export { RPCFactory }
export * from './utils/types'
export { RPCError, type RPCErrorDetails } from './utils/errors' export { RPCError, type RPCErrorDetails } from './utils/errors'
export * from './utils/native' export {
export type * from './core/server' NATIVE_CLIENT_EVENTS,
export type * from './core/client' NATIVE_CLIENT_NETWORK_EVENTS,
export type * from './core/webview' NATIVE_SERVER_EVENTS,
} from './utils/native'
export {
type RPCConfig,
type RPCEnvironment,
RPCErrors,
type RPCNativeClientEvents,
type RPCNativeClientNetworkEvents,
type RPCNativeClientNetworkEventsNames,
type RPCNativeServerEvents,
} from './utils/types'
+5
View File
@@ -66,6 +66,11 @@ export function notRegisteredMessage(
return `No listener for "${event}" on ${receiver}. Register it with rpc.${listenerName(receiver, sender)}("${event}", ...) in ${receiver} code.` return `No listener for "${event}" on ${receiver}. Register it with rpc.${listenerName(receiver, sender)}("${event}", ...) in ${receiver} code.`
} }
/** **Internal** */
export function unknownEnvironmentMessage(env: unknown): string {
return `Unknown env ${JSON.stringify(env)}. Use createRPC({ env: 'server' }), 'client' or 'webview'.`
}
/** **Internal** */ /** **Internal** */
export function unknownNativeMessage(event: string, list: string): string { export function unknownNativeMessage(event: string, list: string): string {
return `"${event}" is not in ${list}. Events not listed there can be registered with FiveM's on("${event}", ...) directly.` return `"${event}" is not in ${list}. Events not listed there can be registered with FiveM's on("${event}", ...) directly.`
+9 -7
View File
@@ -17,13 +17,15 @@ export type RPCEnvironmentResolved<T extends RPCEnvironment> =
? RPCInstanceWebview ? RPCInstanceWebview
: never : never
/** /** `createRPC` config */
* `RPCFactory` config. export type RPCConfig<T extends RPCEnvironment = RPCEnvironment> = {
* /** Environment this instance runs in */
* If environment does not match will throw `RPCErrors.UNKNOWN_ENVIRONMENT`
*/
export type RPCConfig<T extends RPCEnvironment | unknown> = {
env: T env: T
/**
* Log every registration, call and incoming payload
*
* @defaultValue false
*/
debug?: boolean debug?: boolean
/** /**
* Milliseconds to wait for a response before the call rejects with * Milliseconds to wait for a response before the call rejects with
@@ -103,7 +105,7 @@ export enum RPCEvents {
export enum RPCErrors { export enum RPCErrors {
EVENT_NOT_REGISTERED = 'Event not registered', EVENT_NOT_REGISTERED = 'Event not registered',
UNKNOWN_NATIVE = 'Unknown native event', UNKNOWN_NATIVE = 'Unknown native event',
UNKNOWN_ENVIRONMENT = 'Unknown environment (must be either "server", "client" or "webview")', UNKNOWN_ENVIRONMENT = 'Unknown environment',
TIMEOUT = 'Timed out waiting for response', TIMEOUT = 'Timed out waiting for response',
HANDLER_ERROR = 'Listener threw an error', HANDLER_ERROR = 'Listener threw an error',
} }