mirror of
https://github.com/littensy/charm.git
synced 2026-10-23 05:46:27 +00:00
docs: add isNone
This commit is contained in:
parent
3c415d6550
commit
49c2d7e8e9
2 changed files with 31 additions and 2 deletions
31
README.md
31
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.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
|
#### 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
|
## 🚀 Examples
|
||||||
|
|
||||||
### Counter atom
|
### Counter atom
|
||||||
|
|
|
||||||
2
src/index.d.ts
vendored
2
src/index.d.ts
vendored
|
|
@ -258,7 +258,7 @@ declare namespace Charm {
|
||||||
* A payload that can be sent from the server to the client to synchronize
|
* A payload that can be sent from the server to the client to synchronize
|
||||||
* state between the two.
|
* state between the two.
|
||||||
*/
|
*/
|
||||||
type SyncPayload<Atoms> =
|
type SyncPayload<Atoms extends AtomMap> =
|
||||||
| { type: "init"; data: StateOfMap<Atoms> }
|
| { type: "init"; data: StateOfMap<Atoms> }
|
||||||
| { type: "patch"; data: SyncPatch<StateOfMap<Atoms>> };
|
| { type: "patch"; data: SyncPatch<StateOfMap<Atoms>> };
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue