fix(sync): block patches before initial hydration (#21)

This commit is contained in:
richard 2024-09-20 15:32:20 -07:00 • committed by GitHub
parent f0acadb4e6
commit 9ba552cb6c
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
6 changed files with 37 additions and 16 deletions

View file

@ -18,8 +18,8 @@ jobs:
with:
version: 8.15.4
- name: Install Aftman
uses: ok-nick/setup-aftman@v0.4.2
- name: Install Rokit
uses: CompeyDev/setup-rokit@v0.1.2
- name: Install dependencies
run: |

View file

@ -10,8 +10,8 @@ jobs:
steps:
- uses: actions/checkout@v4
- name: Install Aftman
uses: ok-nick/setup-aftman@v0.4.2
- name: Install Rokit
uses: CompeyDev/setup-rokit@v0.1.2
- name: Install dependencies
run: wally install

View file

@ -1,11 +0,0 @@
# This file lists tools managed by Aftman, a cross-platform toolchain manager.
# For more information, see https://github.com/LPGhatguy/aftman
# To add a new tool, add an entry to this table.
[tools]
wally = "UpliftGames/wally@0.3.2"
rojo = "rojo-rbx/rojo@7.4.1"
wally-package-types = "JohnnyMorganz/wally-package-types@1.3.1"
stylua = "johnnymorganz/stylua@0.20.0"
selene = "Kampfkarren/selene@0.26.1"
luau-lsp = "johnnymorganz/luau-lsp@1.27.1"

12
rokit.toml Normal file
View file

@ -0,0 +1,12 @@
# This file lists tools managed by Rokit, a toolchain manager for Roblox projects.
# For more information, see https://github.com/rojo-rbx/rokit
# New tools can be added by running `rokit add <tool>` in a terminal.
[tools]
wally = "UpliftGames/wally@0.3.2"
rojo = "rojo-rbx/rojo@7.4.4"
wally-package-types = "JohnnyMorganz/wally-package-types@1.3.2"
stylua = "johnnymorganz/stylua@0.20.0"
selene = "Kampfkarren/selene@0.27.1"
luau-lsp = "johnnymorganz/luau-lsp@1.32.4"

5
src/index.d.ts vendored
View file

@ -290,6 +290,11 @@ declare namespace Charm {
* The atoms to synchronize with the server.
*/
atoms: Atoms;
/**
* Whether to ignore patches sent before the client has been hydrated.
* Defaults to `true`.
*/
ignoreUnhydrated?: boolean;
}
interface ServerOptions<Molecules extends MoleculeMap> {

View file

@ -9,6 +9,11 @@ type ClientOptions = {
The atoms to synchronize with the server.
]=]
atoms: { [string]: Atom<any> },
--[=[
Whether to ignore patches sent before the client has been hydrated.
Default is `true`.
]=]
ignoreUnhydrated: boolean?,
}
type ClientSyncer = {
@ -30,12 +35,22 @@ type ClientSyncer = {
]=]
local function client(options: ClientOptions): ClientSyncer
local atoms = options.atoms
local ignoreUnhydrated = options.ignoreUnhydrated ~= false
local self = {} :: ClientSyncer
local hydrated = false
-- Apply the state changes sent by the server.
function self:sync(...)
for index = 1, select("#", ...) do
local payload = select(index, ...)
local payload: SyncPayload = select(index, ...)
if ignoreUnhydrated and payload.type == "patch" and not hydrated then
-- If the client is not initialized, ignore patches. This is
-- to prevent patching incomplete state.
continue
end
hydrated = true
store.batch(function()
for key, state in next, payload.data do