Skip to content

Commit

Permalink
Rewrite networking (#15)
Browse files Browse the repository at this point in the history
Rewrite networking to use remotes instead of relying on value instances
  • Loading branch information
jeparlefrancais authored Nov 20, 2024
1 parent 9249479 commit 74f2686
Show file tree
Hide file tree
Showing 23 changed files with 557 additions and 547 deletions.
3 changes: 2 additions & 1 deletion .darklua-bundle-dev.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@
"sources": {
"@pkg": "node_modules/.luau-aliases"
}
}
},
"excludes": ["@lune/**"]
},
"rules": [
"remove_types",
Expand Down
3 changes: 2 additions & 1 deletion .darklua-bundle.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@
"sources": {
"@pkg": "node_modules/.luau-aliases"
}
}
},
"excludes": ["@lune/**"]
},
"rules": [
"remove_types",
Expand Down
2 changes: 0 additions & 2 deletions .darklua-dev.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
{
"rules": [
"remove_comments",
"remove_spaces",
{
"rule": "convert_require",
"current": {
Expand Down
2 changes: 0 additions & 2 deletions .darklua-tests.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
{
"rules": [
"remove_comments",
"remove_spaces",
{
"rule": "convert_require",
"current": {
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ jobs:
run: yarn run prepare

- name: Run linter
run: yarn run lint:selene
run: yarn run lint

- name: Verify code style
run: yarn run style-check
Expand Down
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

## Unreleased Changes

- rewrite data syncing logic ([#15](https://github.com/seaofvoices/crosswalk-channels/pull/15))

## 0.1.3

- fix data sync issues with arrays ([#11](https://github.com/seaofvoices/crosswalk-channels/pull/11))
Expand Down
6 changes: 3 additions & 3 deletions foreman.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
[tools]
rojo = { github = "rojo-rbx/rojo", version = "=7.4.1"}
rojo = { github = "rojo-rbx/rojo", version = "=7.4.4"}
selene = { github = "Kampfkarren/selene", version = "=0.27.1"}
stylua = { github = "JohnnyMorganz/StyLua", version = "=0.20.0"}
darklua = { github = "seaofvoices/darklua", version = "=0.13.0"}
luau-lsp = { github = "JohnnyMorganz/luau-lsp", version = "=1.29.0"}
darklua = { github = "seaofvoices/darklua", version = "=0.14.0"}
luau-lsp = { github = "JohnnyMorganz/luau-lsp", version = "=1.35.0"}
run-in-roblox = { github = "rojo-rbx/run-in-roblox", version = "=0.3.0" }
9 changes: 6 additions & 3 deletions model.project.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,12 @@
"tree": {
"$path": "./channels.lua",
"impl": {
"$path": "./src",
"__modules__": {
"$path": "./node_modules"
"$className": "Folder",
"src": {
"$path": "src"
},
"node_modules": {
"$path": "node_modules"
}
}
}
Expand Down
1 change: 1 addition & 0 deletions scripts/analyze.sh
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,6 @@ if [ ! -f "$TYPES_FILE" ]; then
fi

luau-lsp analyze --base-luaurc=.luaurc --settings=.luau-analyze.json \
--ignore '**/node_modules/**' --ignore 'node_modules/**' \
--definitions=$TYPES_FILE \
src
9 changes: 2 additions & 7 deletions scripts/build-roblox-model.sh
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,8 @@ set -e
DARKLUA_CONFIG=$1
BUILD_OUTPUT=$2

if [ ! -d node_modules ]; then
rm -rf temp
yarn install
fi
if [ ! -d node_modules/.luau-aliases ]; then
yarn run prepare
fi
yarn workspaces focus --production
yarn dlx npmluau

rm -rf temp
mkdir -p temp
Expand Down
102 changes: 70 additions & 32 deletions src/__tests__/Channels.test.lua
Original file line number Diff line number Diff line change
Expand Up @@ -7,26 +7,19 @@ local afterEach = jestGlobals.afterEach
local beforeEach = jestGlobals.beforeEach

local ChannelsBuilder = require('..')

local function mockInstance<T>(tableValue, fallbackInstance): T
return setmetatable(tableValue, {
__index = function(_, index)
local result = (fallbackInstance :: any)[index]
if type(result) == 'function' then
return function(_self, ...)
return result(fallbackInstance, ...)
end
else
return result
end
local Constants = require('../impl/Constants')
local mockInstance = require('./mockInstance')

local function mockEvent()
local bindable = Instance.new('BindableEvent')
return mockInstance({
Fire = function(_self, ...)
bindable:Fire(...)
end,
__newindex = function(_, index, value)
fallbackInstance[index] = value
end,
}) :: any
}, bindable.Event)
end

local function mockServices()
local function mockServices(remote, unreliableRemote)
local playerGuiMock = Instance.new('Folder')
playerGuiMock.Name = 'PlayerGui'
local playerInstanceMock = Instance.new('Folder')
Expand All @@ -42,8 +35,24 @@ local function mockServices()
playerGuiMock.Parent = playerInstanceMock
playerInstanceMock.Parent = players
return {
ReplicatedStorage = Instance.new('Folder') :: Instance,
Players = mockInstance({ LocalPlayer = player }, players),
ReplicatedStorage = mockInstance({
WaitForChild = function(_self, name)
if name == Constants.EventName then
return remote
elseif name == Constants.FastEventName then
return unreliableRemote
end
error(`unable to find child {name}`)
end,
}, Instance.new('Folder')),
Players = mockInstance({
LocalPlayer = player,
PlayerAdded = mockEvent(),
PlayerRemoving = mockEvent(),
GetPlayers = function()
return { player }
end,
}, players),
}
end

Expand Down Expand Up @@ -108,12 +117,34 @@ for caseName, value in valueCases do
local services
local serverChannels
local clientChannels
local flushSignal
local remoteMock
local unreliableRemoteMock

local channelName = 'one'

beforeEach(function()
services = mockServices()
flushSignal = mockEvent()
local newRemoteMock = {
FireClient = jest.fn(function(self, _player, ...)
self.OnClientEvent:Fire(...)
end),
OnClientEvent = mockEvent(),
}
local newUnreliableRemoteMock = {
FireClient = jest.fn(function(self, ...)
self.OnClientEvent:Fire(...)
end),
OnClientEvent = mockEvent(),
}
unreliableRemoteMock = newUnreliableRemoteMock
remoteMock = newRemoteMock
services = mockServices(remoteMock, unreliableRemoteMock)
serverChannels = ChannelsBuilder({}, services, true)
serverChannels.configure({
flushSignal = flushSignal,
remote = remoteMock,
})
clientChannels = ChannelsBuilder({}, services, false)
end)

Expand All @@ -135,13 +166,14 @@ for caseName, value in valueCases do
it(
'sends the value, initializes the player, bind to channel and the value is received',
function()
serverChannels.Init()
serverChannels.Start()
serverChannels.Send(channelName, value)
clientChannels.Init()
clientChannels.Start()

local fn, fnFunction = jest.fn()
teardown = bindToChannel(channelName, fnFunction)

flushSignal:Fire(0.02)
task.wait()

expect(fn).toHaveBeenCalledTimes(1)
Expand All @@ -152,14 +184,15 @@ for caseName, value in valueCases do
it(
'initializes the player, bind to channel, sends the value and the value is received',
function()
serverChannels.Init()
clientChannels.Init()
serverChannels.Start()
clientChannels.Start()

local fn, fnFunction = jest.fn()
teardown = bindToChannel(channelName, fnFunction)

serverChannels.Send(channelName, value)

flushSignal:Fire(0.02)
task.wait()

expect(fn).toHaveBeenCalledTimes(1)
Expand All @@ -170,14 +203,15 @@ for caseName, value in valueCases do
it(
'initializes the player, sends the value, bind to channel, and the value is received',
function()
serverChannels.Init()
clientChannels.Init()
serverChannels.Start()
clientChannels.Start()

serverChannels.Send(channelName, value)
local fn, fnFunction = jest.fn()

teardown = bindToChannel(channelName, fnFunction)

flushSignal:Fire(0.02)
task.wait()

expect(fn).toHaveBeenCalledTimes(1)
Expand Down Expand Up @@ -207,17 +241,18 @@ for caseName, value in valueCases do
it(
'sends the value, initializes the player, bind to channel and the value is received',
function()
serverChannels.Init()
serverChannels.Start()
serverChannels.SendLocal(
services.Players.LocalPlayer,
channelName,
value
)
clientChannels.Init()
clientChannels.Start()

local fn, fnFunction = jest.fn()
teardown = bindToChannel(channelName, fnFunction)

flushSignal:Fire(0.02)
task.wait()

expect(fn).toHaveBeenCalledTimes(1)
Expand All @@ -228,8 +263,8 @@ for caseName, value in valueCases do
it(
'initializes the player, bind to channel, sends the value and the value is received',
function()
serverChannels.Init()
clientChannels.Init()
serverChannels.Start()
clientChannels.Start()

local fn, fnFunction = jest.fn()
teardown = bindToChannel(channelName, fnFunction)
Expand All @@ -239,6 +274,7 @@ for caseName, value in valueCases do
channelName,
value
)
flushSignal:Fire(0.02)

task.wait()

Expand All @@ -250,14 +286,16 @@ for caseName, value in valueCases do
it(
'initializes the player, sends the value, bind to channel, and the value is received',
function()
serverChannels.Init()
clientChannels.Init()
serverChannels.Start()
clientChannels.Start()

serverChannels.SendLocal(
services.Players.LocalPlayer,
channelName,
value
)
flushSignal:Fire(0.02)

local fn, fnFunction = jest.fn()

teardown = bindToChannel(channelName, fnFunction)
Expand Down
27 changes: 27 additions & 0 deletions src/__tests__/mockInstance.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
local jestGlobals = require('@pkg/@jsdotlua/jest-globals')

local jest = jestGlobals.jest

local function mockInstance<T>(tableValue, fallbackInstance): T
return setmetatable(tableValue, {
__index = function(_, index)
local result = (fallbackInstance :: any)[index]

if type(result) == 'function' then
local mockInstance = jest.fn(function(_self, ...)
return result(fallbackInstance, ...)
end)
rawset(tableValue, index, mockInstance)

return mockInstance
else
return result
end
end,
__newindex = function(_, index, value)
fallbackInstance[index] = value
end,
}) :: any
end

return mockInstance
Loading

0 comments on commit 74f2686

Please sign in to comment.