diff --git a/src/index.d.ts b/src/index.d.ts index 5aedfac..95f9e99 100644 --- a/src/index.d.ts +++ b/src/index.d.ts @@ -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 { diff --git a/src/sync/client.luau b/src/sync/client.luau index 284660c..7ff95d6 100644 --- a/src/sync/client.luau +++ b/src/sync/client.luau @@ -9,6 +9,11 @@ type ClientOptions = { The atoms to synchronize with the server. ]=] atoms: { [string]: Atom }, + --[=[ + Whether to ignore patches sent before the client has been hydrated. + Default is `true`. + ]=] + ignoreUnhydrated: boolean?, } type ClientSyncer = { @@ -30,21 +35,22 @@ type ClientSyncer = { ]=] local function client(options: ClientOptions): ClientSyncer local atoms = options.atoms + local ignoreUnhydrated = options.ignoreUnhydrated ~= false local self = {} :: ClientSyncer - local ready = false + local hydrated = false -- Apply the state changes sent by the server. function self:sync(...) for index = 1, select("#", ...) do local payload: SyncPayload = select(index, ...) - if payload.type ~= "init" and not ready then - -- If the client has not been hydrated yet, ignore patches. - -- This is to prevent the client from patching incomplete state. + if ignoreUnhydrated and payload.type ~= "init" and not hydrated then + -- If the client is not initialized, ignore patches. This is + -- to prevent patching incomplete state. continue end - ready = true + hydrated = true store.batch(function() for key, state in next, payload.data do