Skip to content

Commit

Permalink
Sync Typescript typings
Browse files Browse the repository at this point in the history
  • Loading branch information
YetAnotherClown committed Aug 30, 2024
1 parent aa0ea15 commit 80c0798
Show file tree
Hide file tree
Showing 3 changed files with 126 additions and 96 deletions.
2 changes: 1 addition & 1 deletion docs/getting-started/hooks.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ import routes from "shared/routes";

const myRoute = routes.myRoute;

const beginFrame, endFrame = Net.createHook(routes);
const [beginFrame, endFrame] = Net.createHook(routes);
RunService.Heartbeat.Connect(() => {
beginFrame();

Expand Down
2 changes: 1 addition & 1 deletion docs/setup/other.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ import routes from "shared/routes";

const myRoute = routes.myRoute;

const beginFrame, endFrame = Net.createHook(routes);
const [beginFrame, endFrame] = Net.createHook(routes);
RunService.Heartbeat.Connect(() => {
beginFrame();

Expand Down
218 changes: 124 additions & 94 deletions lib/index.d.ts
Original file line number Diff line number Diff line change
@@ -1,116 +1,146 @@
type Recipient = "NET_SERVER" | Player | [Player]
type Recipient = "NET_SERVER" | Player | [Player];

/**
* Allows for modification of queued packets before they're sent.
*/
declare class SendRequest {
/**
* Modifies the recipients of the packet to the one(s) provided in the parameter.
* @param recipient - The Recipient of the Packet
*/
public to(recipient: Recipient): void;
/**
* Modifies the recipients of the packet to the one(s) provided in the parameter.
*
* @param recipient - The Recipient of the Packet
*/
public to(recipient: Recipient): void;
}

type Query<T extends Array<any>> = IterableFunction<LuaTuple<[number, Player | "NET_SERVER", ...T]>>;

/**
* An iterable object returned as the result of `Route.query()` that can filter snapshots
* by Identifier and Senders.
*
* @example
* ```ts
* for (const [position, sender, ...T] in Route.query()) {
* // Do something
* }
* ```
*/
* An iterable object returned as the result of `Route.query()` that can filter snapshots
* by Identifier and Senders.
*
* @example
* ```ts
* for (const [position, sender, ...T] in Route.query()) {
* // Do something
* }
* ```
*
* See [Querying Data](https://yetanotherclown.github.io/YetAnotherNet/docs/getting-started/routes#querying) for more information.
*/
type QueryResult<T extends Array<any>> = Query<T> & {
/**
* Filters Packets from the QueryResult's Snapshot based on the provided Senders.
* @param recipient - The Recipient of the Packet
*/
from(...recipient: Array<Recipient>): QueryResult<T>;
}
/**
* Filters Packets from the QueryResult's Snapshot based on the provided Senders.
*
* @param recipient - The Recipients of the Packet
*/
from(...recipient: Array<Recipient>): QueryResult<T>;
};

declare namespace Net {
const server: "NET_SERVER";
const server: "NET_SERVER";

type Configuration = {
Channel: "Reliable" | "Unreliable" | undefined;
Event: string | undefined;
};

type Configuration = {
Channel: "Reliable" | "Unreliable" | undefined,
Event: string | undefined,
}
class Route<T extends Array<any>> {
public constructor(configuration: Configuration | null);

class Route<T extends Array<any>> {
public constructor(configuration: Configuration | null);
/**
* Sends data to all clients or to specific recipients from the Route's identifier.
*
* By default, `Net.send()` will send the data to all Clients. You can specify which
* Clients to receive the data by chaining `Route.send().to(recipient)` and passing
* `[Player]`, ``Player`, or ``Net.server`.
*
* See [Sending Data](https://yetanotherclown.github.io/YetAnotherNet/docs/getting-started/routes#sending) for more information.
*
* @returns SendRequest - A chainable object for modifying the send request
*/
public send(...data: T): SendRequest;

/**
* Sends data to all clients or to specific recipients from the Route's identifier.
* By default, `Net.send()` will send the data to all Clients. You can specify which
* Clients to receive the data by chaining `Route.send().to(recipient)` and passing
* ``[Player]``, ``Player``, or ``Net.server``.
*
* See [Sending Data](https://yetanotherclown.github.io/YetAnotherNet/docs/getting-started/routes#sending) for more information.
* @returns SendRequest - A chainable object for modifying the send request
*/
public send(...data: T): SendRequest;
/**
* Allows for iteration of all packets of the previous frame.
* You can filter by Senders by chaining the `QueryResult:from()` method onto the query method.
*
* See [Querying Data](https://yetanotherclown.github.io/YetAnotherNet/docs/getting-started/routes#querying) for more information.
*
* @returns QueryResult - A query of packets from that frame
*/
public query(): QueryResult<T>;

/**
* Allows for iteration of all packets of the previous frame.
* You can filter by Senders by chaining the ``QueryResult.from()`` method onto the query method.
* @returns QueryResult - A query of packets from that frame
*/
public query(): QueryResult<T>;
/**
* Sets a function to be ran on Incoming packets before they are processed.
* For example, this would run after the Client receives a Packet from the Server over the network:
* after calling `Route.send()` on the Server and before calling `Route.query()` on the Client.
*
* See [Middleware](https://yetanotherclown.github.io/YetAnotherNet/docs/getting-started/middleware) for more information.
*
* @param middleware - A function that transforms your data
*/
public addIncomingMiddleware(
middleware: (...rawData: Array<unknown>) => LuaTuple<T> | LuaTuple<any> | undefined
): void;

/**
* Sets a function to be ran on Incoming packets before they are processed.
* For example, this would run after the Client receives a Packet from the Server over the network:
* after calling ``Route.send()`` on the Server and before calling ``Route.query()`` on the Client.
*
* See [Middleware](https://yetanotherclown.github.io/YetAnotherNet/docs/getting-started/middleware) for more information.
* @param middleware - A function that transforms your data
*/
public addIncomingMiddleware(middleware: (...rawData: Array<unknown>) => LuaTuple<T> | LuaTuple<any> | undefined): void
/**
* Sets a function to be ran on Outgoing packets before they are sent over the network.
* For example, this would run before the Server sends a Packet to the Client over the network:
* after calling `Route.send()` on the Server and before the Client ever receives the Packet.
*
* See [Middleware](https://yetanotherclown.github.io/YetAnotherNet/docs/getting-started/middleware) for more information.
*
* @param middleware - A function that transforms your data
*/
public addOutgoingMiddleware(middleware: (...rawData: T | any) => LuaTuple<any> | undefined): void;
}

/**
* Sets a function to be ran on Outgoing packets before they are sent over the network.
* For example, this would run before the Server sends a Packet to the Client over the network:
* after calling ``Route.send()`` on the Server and before the Client ever receives the Packet.
*
* See [Middleware](https://yetanotherclown.github.io/YetAnotherNet/docs/getting-started/middleware) for more information.
* @param middleware - A function that transforms your data
*/
public addOutgoingMiddleware(middleware: (...rawData: T | any) => LuaTuple<any> | undefined): void
}
/**
* Initializes your Routes by adding middleware to your Matter Loop.
* This ensures that your Routes run between each frame.
*
* ### Note
*
* Please make sure that the event you set in the Configuration, or the default,
* is the same index you used for your `RunService.Heartbeat` event in your `Loop:begin()` method.
*
* Your Routes are meant to run on the heartbeat, like most systems.
* In some cases you may want to run your Routes on different events,
* in this case it is acceptable to change it to a different event.
*/
function start(loop: any, routes: { [index: string]: Route<any> }): void;

/**
* Initializes your Routes by adding middleware to your Matter Loop.
* This ensures that your Routes run between each frame.
*
* Please make sure that the event you set in the Configuration, or the default,
* is the same index you used for your ``RunService.Heartbeat`` event in your
* ``Loop.begin()`` method.
*
* Your Routes are meant to run on the heartbeat, like most systems.
* In some cases you may want to run your Routes on different events,
* in this case it is acceptable to change it to a different event.
* @param loop - A Matter Loop
* @param routes - An array of your routes
*/
function start(loop: any, routes: {[index: string]: Route<any>}): void
/**
* This function allows you to run Net scheduling code on your own events.
*
* When you provide a table of Routes, this function will return another function
* you can call which will step each Route and process it's Packet Queue.
*
* For example, to run scheduling on the Heartbeat:
* ```ts
* const hook = Net.createHook(routes);
* RunService.Heartbeat.Connect(hook)
* ```
* @param routes - An array of your routes
*/
function createHook(routes: {[index: string]: Route<any>}): () => void
/**
* This function allows you to run the scheduling code on your own events.
*
* Because scheduling should be ran at the beginning and end of each frame,
* this will return two functions which you can use to call the scheduling code for the beginning and end of a frame.
*
* @example
* ```ts
* import { RunService } from "@rbxts/services";
*
* import Net from "@rbxts/yetanothernet";
* import routes from "shared/routes";
*
* const myRoute = routes.myRoute;
*
* const [beginFrame, endFrame] = Net.createHook(routes);
* RunService.Heartbeat.Connect(() => {
* beginFrame();
*
* myRoute.send(...)
* for (const [pos, sender, ...] of myRoute.query()) {
* // Do something
* }
*
* endFrame();
* });
* ```
*
* @param routes - An array of your routes
*/
function createHook(routes: { [index: string]: Route<any> }): LuaTuple<[() => void, () => void]>;
}

export = Net
export = Net;

0 comments on commit 80c0798

Please sign in to comment.