Skip to content

Commit

Permalink
red 2.0.0 release!
Browse files Browse the repository at this point in the history
  • Loading branch information
jackdotink committed Oct 1, 2023
1 parent 23c38df commit ffd21a7
Show file tree
Hide file tree
Showing 26 changed files with 411 additions and 212 deletions.
29 changes: 29 additions & 0 deletions Test/Client/Client.client.luau
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
local ReplicatedStorage = game:GetService("ReplicatedStorage")

local ComplexEvent = require(ReplicatedStorage.ComplexEvent):Client()
local SimpleEvent = require(ReplicatedStorage.SimpleEvent):Client()
local EmptyEvent = require(ReplicatedStorage.EmptyEvent):Client()
local ReadyEvent = require(ReplicatedStorage.ReadyEvent):Client()

print("Registering Listeners")
ComplexEvent:On(function(Value1, Value2, Value3)
print("ComplexEvent", Value1, Value2, Value3)
end)

SimpleEvent:On(function(Value)
print("SimpleEvent", Value)
end)

EmptyEvent:On(function()
print("EmptyEvent")
end)

print("Firing Events")
ComplexEvent:Fire({ one = { "String Literal", 123 }, two = { 123, "String Literal" } }, "hello world again", 123)
SimpleEvent:Fire(123)
EmptyEvent:Fire()

task.wait(1)

print("Firing Ready")
ReadyEvent:Fire()
56 changes: 56 additions & 0 deletions Test/Server/Server.server.luau
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
local ReplicatedStorage = game:GetService("ReplicatedStorage")

local ComplexEvent = require(ReplicatedStorage.ComplexEvent):Server()
local SimpleEvent = require(ReplicatedStorage.SimpleEvent):Server()
local EmptyEvent = require(ReplicatedStorage.EmptyEvent):Server()
local ReadyEvent = require(ReplicatedStorage.ReadyEvent):Server()

print("Registering Listeners")
ComplexEvent:On(function(Player, Value1, Value2, Value3)
print("ComplexEvent", Player, Value1, Value2, Value3)
end)

SimpleEvent:On(function(Player, Value)
print("SimpleEvent", Player, Value)
end)

EmptyEvent:On(function(Player)
print("EmptyEvent", Player)
end)

ReadyEvent:On(function(Player)
print(`[{Player.Name}] Ready, firing events!`)

print(`[{Player.Name}] :Fire`)
SimpleEvent:Fire(Player, 1)
EmptyEvent:Fire(Player)
ComplexEvent:Fire(Player, nil, "hello world again", 1)

print(`[{Player.Name}] :FireAll`)
SimpleEvent:FireAll(2)
EmptyEvent:FireAll()
ComplexEvent:FireAll({ one = { "String Literal", 123 }, two = { 123, "String Literal" } }, "hello world again", 2)

print(`[{Player.Name}] :FireAllExcept`)
SimpleEvent:FireAllExcept(Player, 3)
EmptyEvent:FireAllExcept(Player)
ComplexEvent:FireAllExcept(Player, nil, "this should be skipped", 3)

print(`[{Player.Name}] :FireList`)
SimpleEvent:FireList({ Player }, 4)
EmptyEvent:FireList({ Player })
ComplexEvent:FireList({ Player }, { one = { "String Literal", 8 }, two = { 1, "String Literal" } }, "hi", 4)

print(`[{Player.Name}] :FireWithFilter`)
SimpleEvent:FireWithFilter(function(OtherPlayer)
return OtherPlayer.Name == Player.Name
end, 5)
EmptyEvent:FireWithFilter(function(OtherPlayer)
return OtherPlayer.Name == Player.Name
end)
ComplexEvent:FireWithFilter(function(OtherPlayer)
return OtherPlayer.Name == Player.Name
end, { one = { "String Literal", 17 }, two = { 123, "String Literal" } }, "another string", 5)

print(`[{Player.Name}] Done!`)
end)
11 changes: 11 additions & 0 deletions Test/Shared/ComplexEvent.luau
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
local ReplicatedStorage = game:GetService("ReplicatedStorage")

local Red = require(ReplicatedStorage.Packages.Red)
local Guard = require(ReplicatedStorage.Packages.Guard)

local ValueCheck =
Guard.Optional(Guard.Map(Guard.String, Guard.List(Guard.Or(Guard.Literal("String Literal"), Guard.Number))))

return Red.Event("ComplexEvent", function(Value1, Value2, Value3)
return ValueCheck(Value1), Guard.String(Value2), Guard.Number(Value3)
end)
7 changes: 7 additions & 0 deletions Test/Shared/EmptyEvent.luau
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
local ReplicatedStorage = game:GetService("ReplicatedStorage")

local Red = require(ReplicatedStorage.Packages.Red)

return Red.Event("EmptyEvent", function()
return
end)
7 changes: 7 additions & 0 deletions Test/Shared/ReadyEvent.luau
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
local ReplicatedStorage = game:GetService("ReplicatedStorage")

local Red = require(ReplicatedStorage.Packages.Red)

return Red.Event("ReadyEvent", function()
return
end)
8 changes: 8 additions & 0 deletions Test/Shared/SimpleEvent.luau
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
local ReplicatedStorage = game:GetService("ReplicatedStorage")

local Red = require(ReplicatedStorage.Packages.Red)
local Guard = require(ReplicatedStorage.Packages.Guard)

return Red.Event("SimpleEvent", function(Value)
return Guard.Number(Value)
end)
20 changes: 17 additions & 3 deletions dev.project.json
Original file line number Diff line number Diff line change
@@ -1,9 +1,23 @@
{
"name": "RedDev",
"tree": {
"$path": "Packages",
"Red": {
"$path": "lib"
"$className": "DataModel",
"ServerScriptService": {
"$path": "Test/Server"
},
"ReplicatedStorage": {
"$path": "Test/Shared",
"Packages": {
"$path": "Packages",
"Red": {
"$path": "lib"
}
}
},
"StarterPlayer": {
"StarterPlayerScripts": {
"$path": "Test/Client"
}
}
}
}
1 change: 1 addition & 0 deletions docs/.vitepress/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ function sidebar() {
items: [
{ text: 'What is Red?', link: '/guide/introduction/what-is-red' },
{ text: 'Getting Started', link: '/guide/introduction/getting-started' },
{ text: 'Redblox', link: '/guide/introduction/redblox' },
]
},
{
Expand Down
18 changes: 7 additions & 11 deletions docs/2.0/Red.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,16 @@ Creates a new [event](./Event/index) object.

```lua
<T...>(
Name: string, -- The name of the event
Validate: (...unknown) -> T..., -- Validates event payloads
NameOverride: string?, -- Optional override for the name of the event
) -> Event<T...>
```

This will create an event with the same name as the script calling the function.
This will create an event with the passed name.

::: danger
The name of the event must be unique, using the same name twice will result in an error.
:::

The validation function is used to validate the type of the payloads. The function has three rules:

Expand All @@ -22,19 +26,11 @@ The validation function is used to validate the type of the payloads. The functi
3. The callback must narrow the types of the arguments.

```lua
return Red.Event(function(Number, String)
return Red.Event("EventName", function(Number, String)
return Guard.Number(Number), Guard.String(String)
end)
```

::: tip
It is recommended to use [Guard](https://util.redblox.dev/guard) to typecheck payloads as it also narrows types.
:::

If for whatever reason the name of the calling script cannot be used for the name of the event, you can pass in a string as the second argument to override the name.

```lua
return Red.Event(function(Number, String)
return Guard.Number(Number), Guard.String(String)
end, "MyEvent")
```
2 changes: 1 addition & 1 deletion docs/guide/events/client.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ We'll also use the same event module as on the server.
local Guard = require(Path.To.Guard)
local Red = require(Path.To.Red)

return Red.Event(function(Number, String, Boolean)
return Red.Event("MyEvent", function(Number, String, Boolean)
return Guard.Number(Number), Guard.String(String), Guard.Boolean(Boolean)
end)
```
Expand Down
10 changes: 5 additions & 5 deletions docs/guide/events/declaring.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,15 @@ Create a module in ReplicatedStorage. Inside the module import Red, and return a
```lua
local Red = require(Path.To.Red)

return Red.Event(function()
return Red.Event("MostBasicEvent", function()
return
end)
```

This is the most basic event in Red. Here we've declared an event which has no payload. The event name is gathered from the module name.

::: danger
Make sure the module name is unique - modules with the same name will return the same event. If you need to create events without using module name, `Red.Event` has a second string argument that can be used to override the name of the event. These name overrides must still be unique.
Make sure the event name is unique - using the same name multiple times will result in an error.
:::

## Event Payloads
Expand All @@ -25,7 +25,7 @@ We now have an event - but we can't send any data with it. To send a payload wit
This callback is a validation check callback. This callback's purpose is to assert the types of the payload.

```lua
return Red.Event(function(Value)
return Red.Event("EventWithPayload", function(Value)
assert(type(Value) == "number")

return Value
Expand All @@ -51,7 +51,7 @@ local Guard = require(Path.To.Guard)

local Check = Guard.Map(Guard.String, Guard.List(Guard.Number))

return Red.Event(function(Value)
return Red.Event("EventUsingGuard", function(Value)
return Check(Value)
end)
```
Expand All @@ -71,7 +71,7 @@ local PlayerCheck = function(Value: unknown)
return Value
end

return Red.Event(function(Action, Player)
return Red.Event("EventWithMultiplePayloads", function(Action, Player)
return ActionCheck(Action), PlayerCheck(Player)
end)
```
Expand Down
4 changes: 2 additions & 2 deletions docs/guide/events/server.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ For simplicity we'll be using the same event module throughout this guide.
local Guard = require(Path.To.Guard)
local Red = require(Path.To.Red)

return Red.Event(function(Number, String, Boolean)
return Red.Event("MyEvent", function(Number, String, Boolean)
return Guard.Number(Number), Guard.String(String), Guard.Boolean(Boolean)
end)
```
Expand Down Expand Up @@ -49,7 +49,7 @@ This will fire the event and payload to all players, and as such does not have a
MyEvent:FireAllExcept(Player, 1, "Hello", true)
```

This is the exact same as `FireAll`, *except* that it does not fire to the player passed as the first argument.
This is the exact same as `FireAll`, _except_ that it does not fire to the player passed as the first argument.

### FireList

Expand Down
4 changes: 2 additions & 2 deletions docs/guide/introduction/getting-started.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ Red is available for Rojo-based workflows and Studio workflows through wally.

```toml
[dependencies]
Red = "red-blox/[email protected]-rc.8"
Red = "red-blox/[email protected]"
```

3. Run `wally install` to install packages.
Expand All @@ -23,7 +23,7 @@ Red = "red-blox/[email protected]"
return {
studioWallyServer = "https://studio-wally.fewkz.com",
packages = {
Red = "red-blox/[email protected]-rc.8"
Red = "red-blox/[email protected]"
}
}
```
Expand Down
16 changes: 16 additions & 0 deletions docs/guide/introduction/redblox.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# Redblox

The Redblox project contains the library Red, but also other useful libraries.

- [Red](https://red.redblox.dev/) - A simple, fast, and powerful networking library.
- [Util](https://util.redblox.dev/) - A collection of small utilities for roblox.
- [Bin](https://util.redblox.dev/bin) - Manages cleanup for objects that cannot be garbage collected.
- [Clock](https://util.redblox.dev/clock) - Calls a function at consistent intervals.
- [Collection](https://util.redblox.dev/collection) - Handles instance addition and removal from collections.
- [Fetch](https://util.redblox.dev/fetch) - A Future based HTTP request utility similar to Javascript's fetch.
- [Future](https://util.redblox.dev/future) - A lightweight class to represent asynchronous functions.
- [Guard](https://util.redblox.dev/guard) - A runtime type checker with support for luau types.
- [Promise](https://util.redblox.dev/promise) - A Promise implementation that prioritizes speed and ease of use.
- [Ratelimit](https://util.redblox.dev/ratelimit) - Ratelimits many keys in a very intuitive interface.
- [Signal](https://util.redblox.dev/signal) - A signal implementation without connection objects.
- [Spawn](https://util.redblox.dev/spawn) - A shared "fast spawn" function that reuses threads.
2 changes: 1 addition & 1 deletion lib/ClientEvent.luau
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ function ClientEvent.Start()
debug.profilebegin("Red.ClientEvent.Outgoing")

if ClientEvent.OutgoingMap.Call or ClientEvent.OutgoingMap.Fire then
Remote:FireServer(ClientEvent.OutgoingMap.Fire, ClientEvent.OutgoingMap.Call)
Remote:FireServer(ClientEvent.OutgoingMap.Fire or {}, ClientEvent.OutgoingMap.Call or {})

table.clear(ClientEvent.OutgoingMap)
end
Expand Down
Loading

0 comments on commit ffd21a7

Please sign in to comment.