Skip to content

Commit

Permalink
Async remote function callbacks
Browse files Browse the repository at this point in the history
  • Loading branch information
Fireboltofdeath committed Oct 9, 2021
1 parent b1bec96 commit 862f82e
Show file tree
Hide file tree
Showing 7 changed files with 41 additions and 20 deletions.
4 changes: 1 addition & 3 deletions src/events/createClientHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,7 @@ export function createClientHandler<S, C>(
const middlewareProcessor = createMiddlewareProcessor(
middlewareFactoryList?.[name as never],
networkInfo,
(_, ...args) => {
bindables.get(name)?.Fire(...args);
},
(_, ...args) => bindables.get(name)?.Fire(...args),
);

processors.set(name, middlewareProcessor);
Expand Down
4 changes: 1 addition & 3 deletions src/events/createServerHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,7 @@ export function createServerHandler<S, C>(
const middlewareProcessor = createMiddlewareProcessor(
middlewareFactoryList?.[name as never],
networkInfo,
(player, ...args) => {
bindables.get(name)?.Fire(player, ...args);
},
(player, ...args) => bindables.get(name)?.Fire(player, ...args),
);

remote.OnServerEvent.Connect((player, ...args) => {
Expand Down
16 changes: 13 additions & 3 deletions src/functions/createClientHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ export {};
import { t } from "@rbxts/t";
import { getFunctionError, NetworkingFunctionError } from "./errors";
import { createMiddlewareProcessor } from "../middleware/createMiddlewareProcessor";
import { FunctionMiddlewareList, Middleware, MiddlewareFactory } from "../middleware/types";
import { FunctionMiddlewareList, Middleware, MiddlewareFactory, MiddlewareProcessor } from "../middleware/types";
import { NetworkInfo } from "../types";
import { ArbitaryGuards, ClientHandler, ClientReceiver, ClientSender, RequestInfo } from "./types";
import { Skip } from "middleware/skip";
Expand All @@ -18,7 +18,7 @@ export function createClientHandler<S, C>(
middlewareFactoryList?: FunctionMiddlewareList<C>,
): ClientHandler<S, C> {
const handler = {} as ClientHandler<S, C>;
const processors = new Map<string, Middleware<unknown[], unknown>>();
const processors = new Map<string, MiddlewareProcessor<unknown[], unknown>>();
const requestInfo: RequestInfo = {
nextId: 0,
requests: new Map(),
Expand Down Expand Up @@ -75,7 +75,13 @@ export function createClientHandler<S, C>(
const processor = processors.get(name);
if (processor) {
const result = processor(undefined, ...args);
remote.FireServer(id, result === Skip ? NetworkingFunctionError.Cancelled : true, result);
result
.then((value) => remote.FireServer(id, getProcessResult(value), value))
.catch((reason) => {
warn(`Failed to process request to ${name}`);
warn(reason);
remote.FireServer(id, false);
});
} else {
remote.FireServer(id, false);
}
Expand Down Expand Up @@ -149,3 +155,7 @@ function createClientMethod(
function timeout(timeout: number, rejectValue: unknown) {
return Promise.delay(timeout).then(() => Promise.reject(rejectValue));
}

function getProcessResult(value: unknown) {
return value === Skip ? NetworkingFunctionError.Cancelled : true;
}
16 changes: 13 additions & 3 deletions src/functions/createServerHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { Players } from "@rbxts/services";
import { t } from "@rbxts/t";
import { getFunctionError, NetworkingFunctionError } from "./errors";
import { createMiddlewareProcessor } from "../middleware/createMiddlewareProcessor";
import { FunctionMiddlewareList, Middleware, MiddlewareFactory } from "../middleware/types";
import { FunctionMiddlewareList, Middleware, MiddlewareFactory, MiddlewareProcessor } from "../middleware/types";
import { NetworkInfo } from "../types";
import { ArbitaryGuards, RequestInfo, ServerHandler, ServerReceiver, ServerSender } from "./types";
import { Skip } from "middleware/skip";
Expand All @@ -17,7 +17,7 @@ export function createServerHandler<S, C>(
middlewareFactoryList?: FunctionMiddlewareList<S>,
): ServerHandler<C, S> {
const handler = {} as ServerHandler<C, S>;
const processors = new Map<string, Middleware<unknown[], unknown>>();
const processors = new Map<string, MiddlewareProcessor<unknown[], unknown>>();
const players = new Map<Player, RequestInfo>();

function createMethod(name: string, networkInfo: NetworkInfo, remote: RemoteEvent) {
Expand Down Expand Up @@ -73,7 +73,13 @@ export function createServerHandler<S, C>(
const processor = processors.get(name);
if (processor) {
const result = processor(player, ...args);
remote.FireClient(player, id, result === Skip ? NetworkingFunctionError.Cancelled : true, result);
result
.then((value) => remote.FireClient(player, id, getProcessResult(value), value))
.catch((reason) => {
warn(`Failed to process request to ${name}`);
warn(reason);
remote.FireClient(player, id, false);
});
} else {
remote.FireClient(player, id, false);
}
Expand Down Expand Up @@ -165,3 +171,7 @@ function getRequestInfo(player: Player, map: Map<Player, RequestInfo>) {
function timeout(timeout: number, rejectValue: unknown) {
return Promise.delay(timeout).then(() => Promise.reject(rejectValue));
}

function getProcessResult(value: unknown) {
return value === Skip ? NetworkingFunctionError.Cancelled : true;
}
4 changes: 2 additions & 2 deletions src/functions/types.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ export interface ServerReceiver<I extends unknown[], O> {
* @param callback The callback that will be invoked
* @param guards A list of guards that will only be used on this connection
*/
setCallback(callback: (player: Player, ...args: I) => O): void;
setCallback(callback: (player: Player, ...args: I) => O | Promise<O>): void;

/**
* Invokes a server function using player as the sender.
Expand All @@ -43,7 +43,7 @@ export interface ClientReceiver<I extends unknown[], O> {
* @param event The function to connect to
* @param callback The callback that will be invoked
*/
setCallback(callback: (...args: I) => O): void;
setCallback(callback: (...args: I) => O | Promise<O>): void;

/**
* Invokes a client function.
Expand Down
9 changes: 5 additions & 4 deletions src/middleware/createMiddlewareProcessor.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,22 @@
import { NetworkInfo } from "../types";
import { Middleware, MiddlewareFactory } from "./types";
import { Middleware, MiddlewareFactory, MiddlewareProcessor } from "./types";

export function createMiddlewareProcessor<I extends readonly unknown[], O>(
middlewareFactories: MiddlewareFactory<I, O>[] | undefined,
networkInfo: NetworkInfo,
finalize: Middleware<I, O>,
) {
): MiddlewareProcessor<I, O> {
const middleware = new Array<Middleware<I, O>>();

if (!middlewareFactories || middlewareFactories.size() === 0) {
middleware[0] = finalize;
} else {
for (let i = middlewareFactories.size() - 1; i >= 0; i--) {
const factory = middlewareFactories[i];
middleware[i] = factory(middleware[i + 1] ?? finalize, networkInfo);
const processNext = middleware[i + 1] ?? finalize;
middleware[i] = factory(async (player, ...args) => processNext(player, ...args), networkInfo);
}
}

return middleware[0];
return async (player?: Player, ...args: I) => middleware[0](player, ...args);
}
8 changes: 6 additions & 2 deletions src/middleware/types.d.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
import { NetworkInfo } from "../types";
import { Skip } from "./skip";

export type Middleware<I extends readonly unknown[] = unknown[], O = void> = (player?: Player, ...args: I) => O;
export type MiddlewareProcessor<I extends readonly unknown[], O> = (player?: Player, ...args: I) => Promise<O>;
export type Middleware<I extends readonly unknown[] = unknown[], O = void> = (
player?: Player,
...args: I
) => O | Promise<O>;
export type MiddlewareFactory<I extends readonly unknown[] = [], O = void> = (
processNext: Middleware<I, O>,
processNext: MiddlewareProcessor<I, O>,
event: NetworkInfo,
) => Middleware<I, O>;

Expand Down

0 comments on commit 862f82e

Please sign in to comment.