diff --git a/docs/getting-started/hooks.mdx b/docs/getting-started/hooks.mdx index de79e5f..2da3955 100644 --- a/docs/getting-started/hooks.mdx +++ b/docs/getting-started/hooks.mdx @@ -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(); diff --git a/docs/setup/other.mdx b/docs/setup/other.mdx index 89a7431..ad82041 100644 --- a/docs/setup/other.mdx +++ b/docs/setup/other.mdx @@ -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(); diff --git a/lib/index.d.ts b/lib/index.d.ts index a694f81..9a1679f 100644 --- a/lib/index.d.ts +++ b/lib/index.d.ts @@ -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> = IterableFunction>; /** - * 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> = Query & { - /** - * Filters Packets from the QueryResult's Snapshot based on the provided Senders. - * @param recipient - The Recipient of the Packet - */ - from(...recipient: Array): QueryResult; -} + /** + * Filters Packets from the QueryResult's Snapshot based on the provided Senders. + * + * @param recipient - The Recipients of the Packet + */ + from(...recipient: Array): QueryResult; +}; 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> { + public constructor(configuration: Configuration | null); - class Route> { - 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; - /** - * 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; + /** + * 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) => LuaTuple | LuaTuple | 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) => LuaTuple | LuaTuple | 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 | 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 | 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 }): 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}): 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}): () => 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 }): LuaTuple<[() => void, () => void]>; } -export = Net \ No newline at end of file +export = Net;