Skip to content
This repository has been archived by the owner on Jul 31, 2024. It is now read-only.

Commit

Permalink
Merge pull request #186 from Sleitnick/remote-property
Browse files Browse the repository at this point in the history
RemoteProperty support
  • Loading branch information
Sleitnick authored Dec 20, 2021
2 parents c2f8fb6 + 8d124a4 commit 31c2360
Show file tree
Hide file tree
Showing 7 changed files with 102 additions and 7 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
## 1.3.0

- Add support for RemoteProperties via Comm library

## 1.2.1

- Add updated dependencies to the `wally_bundle.toml` dependency list
Expand Down
40 changes: 40 additions & 0 deletions src/KnitClient.lua
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,46 @@ end
within the Client table of the given service. Returns `nil` if the
service is not found.
If a service's Client table contains RemoteSignals and/or RemoteProperties,
these values are reflected as
[ClientRemoteSignals](https://sleitnick.github.io/RbxUtil/api/ClientRemoteSignal) and
[ClientRemoteProperties](https://sleitnick.github.io/RbxUtil/api/ClientRemoteProperty).
```lua
-- Server-side service creation:
local MyService = Knit.CreateService {
Name = "MyService";
Client = {
MySignal = Knit.CreateSignal();
MyProperty = Knit.CreateProperty("Hello");
};
}
function MyService:AddOne(player, number)
return number + 1
end
-------------------------------------------------
-- Client-side service reflection:
local MyService = Knit.GetService("MyService")
-- Call a method:
local num = MyService:AddOne(5) --> 6
-- Fire a signal to the server:
MyService.MySignal:Fire("Hello")
-- Listen for signals from the server:
MyService.MySignal:Connect(function(message)
print(message)
end)
-- Observe the initial value and changes to properties:
MyService.MyProperty:Observe(function(value)
print(value)
end)
```
:::caution
Services are only exposed to the client if the service has remote-based
content in the Client table. If not, the service will not be visible
Expand Down
55 changes: 51 additions & 4 deletions src/KnitServer.lua
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,11 @@ getmetatable(SIGNAL_MARKER).__tostring = function()
return "SIGNAL_MARKER"
end

local PROPERTY_MARKER = newproxy(true)
getmetatable(PROPERTY_MARKER).__tostring = function()
return "PROPERTY_MARKER"
end

local knitRepServiceFolder = Instance.new("Folder")
knitRepServiceFolder.Name = "Services"

Expand Down Expand Up @@ -236,19 +241,58 @@ end
local MyService = Knit.CreateService {
Name = "MyService";
Client = {
MySignal = Knit.CreateSignal(); -- Create the signal marker
-- Create the signal marker, which will turn into a
-- RemoteSignal when Knit.Start() is called:
MySignal = Knit.CreateSignal()
}
}
-- Connect to the signal:
MyService.Client.MySignal:Connect(function(player, ...) end)
function MyService:KnitInit()
-- Connect to the signal:
self.Client.MySignal:Connect(function(player, ...) end)
end
```
]=]
function KnitServer.CreateSignal()
return SIGNAL_MARKER
end


--[=[
@param initialValue any
@return PROPERTY_MARKER
Returns a marker that will transform the current key into
a RemoteProperty once the service is created. Should only
be called within the Client table of a service. An initial
value can be passed along as well.
RemoteProperties are great for replicating data to all of
the clients. Different data can also be set per client.
See [RemoteProperty](https://sleitnick.github.io/RbxUtil/api/RemoteProperty)
documentation for more info.
```lua
local MyService = Knit.CreateService {
Name = "MyService";
Client = {
-- Create the property marker, which will turn into a
-- RemoteProperty when Knit.Start() is called:
MyProperty = Knit.CreateProperty("HelloWorld")
}
}
function MyService:KnitInit()
-- Change the value of the property:
self.Client.MyProperty:Set("HelloWorldAgain")
end
```
]=]
function KnitServer.CreateProperty(initialValue: any)
return {PROPERTY_MARKER, initialValue}
end


--[=[
@param options KnitOptions?
@return Promise
Expand All @@ -258,7 +302,8 @@ end
Knit's custom configurations.
:::caution
Be sure that all services have been created _before_ calling `Start`. Services cannot be added later.
Be sure that all services have been created _before_
calling `Start`. Services cannot be added later.
:::
```lua
Expand Down Expand Up @@ -310,6 +355,8 @@ function KnitServer.Start(options: KnitOptions?)
service.KnitComm:WrapMethod(service.Client, k, selectedOptions.InboundMiddleware, selectedOptions.OutboundMiddleware)
elseif v == SIGNAL_MARKER then
service.Client[k] = service.KnitComm:CreateSignal(k, selectedOptions.InboundMiddleware, selectedOptions.OutboundMiddleware)
elseif type(v) == "table" and v[1] == PROPERTY_MARKER then
service.Client[k] = service.KnitComm:CreateProperty(k, v[2], selectedOptions.InboundMiddleware, selectedOptions.OutboundMiddleware)
end
end
end
Expand Down
4 changes: 2 additions & 2 deletions src/wally.toml
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
[package]
name = "sleitnick/knit"
description = "Knit is a lightweight game framework"
version = "1.2.1"
version = "1.3.0"
license = "MIT"
registry = "https://github.com/UpliftGames/wally-index"
realm = "shared"

[dependencies]
Comm = "sleitnick/comm@0.1"
Comm = "sleitnick/comm@0.2"
Promise = "evaera/[email protected]"
2 changes: 1 addition & 1 deletion src/wally_bundle.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,5 @@ Signal = "sleitnick/signal@1"
TableUtil = "sleitnick/table-util@1"
Timer = "sleitnick/timer@1"
Trove = "sleitnick/[email protected]"
Comm = "sleitnick/comm@0.1"
Comm = "sleitnick/comm@0.2"
Promise = "evaera/[email protected]"
3 changes: 3 additions & 0 deletions test/client/KnitClientTest.client.lua
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@ function MyController:KnitInit()
MyService:TestMethod("Hello world from client"):andThen(function(result)
print("Result from server:", result)
end)
MyService.TestProperty:Observe(function(value)
print("TestProperty value:", value)
end)
end

Knit.Start({ServicePromises = true}):andThen(function()
Expand Down
1 change: 1 addition & 0 deletions test/server/KnitServerTest.server.lua
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ local MyService = Knit.CreateService {
Name = "MyService";
Client = {
TestEvent = Knit.CreateSignal();
TestProperty = Knit.CreateProperty("Hello");
};
}

Expand Down

0 comments on commit 31c2360

Please sign in to comment.