From 49c2d7e8e9461266a5190238959be4f8738b42b6 Mon Sep 17 00:00:00 2001 From: Richard Date: Sun, 7 Jul 2024 01:08:46 -0700 Subject: [PATCH] docs: add `isNone` --- README.md | 31 ++++++++++++++++++++++++++++++- src/index.d.ts | 2 +- 2 files changed, 31 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index d92304d..c421167 100644 --- a/README.md +++ b/README.md @@ -400,7 +400,7 @@ remotes.requestState.connect((player) => { - `server.connect(callback)` registers a callback to send state updates to clients. The callback will receive the player and the payload to send, and should send the payload to the client. The payload should not be mutated, so changes should be applied to a copy of the payload. -- `server.hydrate(player)` sends the initial state to a player, calling the callback passed to `connect` with a payload containing the initial state. +- `server.hydrate(player)` sends the initial state to a player. This calls the function passed to `connect` with a payload containing the initial state. #### Caveats @@ -408,6 +408,35 @@ remotes.requestState.connect((player) => { --- +### `sync.isNone(value)` + +State patches represent the _difference_ between the current state and next state, excluding unchanged values. However, this means both unchanged and removed values would be `nil` in the patch. In these cases, Charm uses the `None` marker to represent a removed value. + +Call `sync.isNone` to check if a value is `None`. + +```ts +import { sync } from "@rbxts/charm"; + +const server = sync.server({ atoms: atomsToSync }); + +server.connect((player, payload) => { + if (sync.isNone(payload.data.todosAtom?.eggs)) { + // 'eggs' will be removed from the client's todo list + } + remotes.syncState.fire(player, payload); +}); +``` + +#### Parameters + +- `value`: Any value. If the value is `None`, `sync.isNone` will return `true`. + +#### Returns + +`sync.isNone` returns a boolean. + +--- + ## 🚀 Examples ### Counter atom diff --git a/src/index.d.ts b/src/index.d.ts index eeee84c..fd5ac0b 100644 --- a/src/index.d.ts +++ b/src/index.d.ts @@ -258,7 +258,7 @@ declare namespace Charm { * A payload that can be sent from the server to the client to synchronize * state between the two. */ - type SyncPayload = + type SyncPayload = | { type: "init"; data: StateOfMap } | { type: "patch"; data: SyncPatch> };