diff --git a/.eslintrc b/.eslintrc
deleted file mode 100644
index 8f628fe..0000000
--- a/.eslintrc
+++ /dev/null
@@ -1,26 +0,0 @@
-{
- "parser": "@typescript-eslint/parser",
- "parserOptions": {
- "jsx": true,
- "useJSXTextNode": true,
- "ecmaVersion": 2018,
- "sourceType": "module",
- "project": "./tsconfig.json"
- },
- "plugins": [
- "@typescript-eslint",
- "roblox-ts",
- "prettier"
- ],
- "extends": [
- "eslint:recommended",
- "plugin:@typescript-eslint/recommended",
- "plugin:roblox-ts/recommended",
- "plugin:prettier/recommended"
- ],
- "rules": {
- "prettier/prettier": "warn",
- "@typescript-eslint/no-explicit-any": "off",
- "@typescript-eslint/no-unsafe-declaration-merging": "off"
- }
-}
diff --git a/.gitattributes b/.gitattributes
deleted file mode 100644
index 6313b56..0000000
--- a/.gitattributes
+++ /dev/null
@@ -1 +0,0 @@
-* text=auto eol=lf
diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml
index 4369391..912f2ea 100644
--- a/.github/workflows/ci.yml
+++ b/.github/workflows/ci.yml
@@ -1,31 +1,32 @@
name: CI
-on:
- push:
- branches:
- - main
-
- pull_request:
- branches:
- - main
+on: [push, pull_request]
jobs:
analyze:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
+ with:
+ submodules: true
- uses: pnpm/action-setup@v4
with:
run_install: true
+ - uses: CompeyDev/setup-rokit@v0.1.2
- - name: Install Rokit
- uses: CompeyDev/setup-rokit@v0.1.2
-
- - name: Install Wally dependencies
- run: pnpm wally
+ - name: Setup Lute
+ run: lute setup --with-luaurc
- name: Check code quality
- run: pnpm check
+ run: lute run check
+
+ - name: Run ESLint and Prettier
+ run: |
+ pnpm eslint packages
+ pnpm prettier --check packages
+
+ - name: Run tests
+ run: lute run test
- name: Build models
- run: pnpm build
+ run: lute run build
diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml
index 07b75ad..3bd512d 100644
--- a/.github/workflows/release.yml
+++ b/.github/workflows/release.yml
@@ -9,13 +9,13 @@ jobs:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- - uses: pnpm/action-setup@v4
+ - uses: CompeyDev/setup-rokit@v0.1.2
- - name: Install Rokit
- uses: CompeyDev/setup-rokit@v0.1.2
+ - name: Setup Lute
+ run: lute setup --with-luaurc
- name: Build models
- run: pnpm build
+ run: lute run build
- name: Upload release assets
uses: AButler/upload-release-assets@v3.0
diff --git a/.gitignore b/.gitignore
index f3a27c4..d217e96 100644
--- a/.gitignore
+++ b/.gitignore
@@ -1,13 +1,13 @@
/.vscode
-/node_modules
+/.pesde
/build
-/modules/DevPackages
-/packages/**/node_modules
/packages/**/wally.lock
+/packages/**/LICENSE*
/packages/**/README.md
-/packages/**/LICENSE.md
/sourcemap.json
+node_modules
*.tsbuildinfo
-*.tgz
*.rbxm
+*.tgz
.DS_Store
+roblox.d.luau
diff --git a/.gitmodules b/.gitmodules
new file mode 100644
index 0000000..409278b
--- /dev/null
+++ b/.gitmodules
@@ -0,0 +1,3 @@
+[submodule "tests/test"]
+ path = tests/test
+ url = https://gist.github.com/littensy/fa9afcc5d66d25e86538634982be4ee4
diff --git a/.luaurc b/.luaurc
index e2b625c..c48b0f2 100644
--- a/.luaurc
+++ b/.luaurc
@@ -1,3 +1,7 @@
{
- "languageMode": "strict"
+ "languageMode": "strict",
+ "aliases": {
+ "lute": "~/.lute/typedefs/1.0.0/lute",
+ "std": "~/.lute/typedefs/1.0.0/std"
+ }
}
diff --git a/.lute/build.luau b/.lute/build.luau
new file mode 100644
index 0000000..b708003
--- /dev/null
+++ b/.lute/build.luau
@@ -0,0 +1,6 @@
+local fs = require("@std/fs")
+local process = require("@lute/process")
+
+fs.createDirectory("build")
+
+process.run({ "rojo", "build", "--output=build/charm.rbxm", "build.project.json" }, { stdio = "inherit" })
diff --git a/.lute/check.luau b/.lute/check.luau
new file mode 100644
index 0000000..b5ad31b
--- /dev/null
+++ b/.lute/check.luau
@@ -0,0 +1,33 @@
+local fs = require("@std/fs")
+local net = require("@std/net")
+local process = require("@lute/process")
+
+process.run({ "rojo", "sourcemap", "--output=sourcemap.json" }, { stdio = "inherit" })
+
+fs.writeStringToFile(
+ "roblox.d.luau",
+ net.request("https://luau-lsp.pages.dev/globalTypes.None.d.luau", { method = "GET" }).body
+)
+
+local analyze = process.run({
+ "luau-lsp",
+ "analyze",
+ "--sourcemap=sourcemap.json",
+ "--ignore=**/node_modules/**",
+ "--base-luaurc=.luaurc",
+ "--definitions=roblox.d.luau",
+ "--flag:LuauSolverV2=true",
+ "packages",
+ "tests",
+ "benches",
+}, { stdio = "inherit" })
+
+fs.remove("roblox.d.luau")
+
+local stylua = process.run({ "stylua", "--check", "packages", "tests", "benches" }, { stdio = "inherit" })
+
+local eslint = process.run({ "pnpm", "eslint", "packages" }, { stdio = "inherit" })
+
+if not (analyze.ok and stylua.ok and eslint.ok) then
+ process.exit(1)
+end
diff --git a/.lute/test.luau b/.lute/test.luau
new file mode 100644
index 0000000..4abbbb9
--- /dev/null
+++ b/.lute/test.luau
@@ -0,0 +1 @@
+(require)("../tests/run.server")
diff --git a/.npmrc b/.npmrc
new file mode 100644
index 0000000..b644580
--- /dev/null
+++ b/.npmrc
@@ -0,0 +1 @@
+public-hoist-pattern[]=*@rbxts*
diff --git a/LICENSE b/LICENSE
new file mode 100644
index 0000000..d10b611
--- /dev/null
+++ b/LICENSE
@@ -0,0 +1,21 @@
+The MIT License (MIT)
+
+Copyright 2024-present Littensy
+
+Permission is hereby granted, free of charge, to any person obtaining a copy
+of this software and associated documentation files (the "Software"), to deal
+in the Software without restriction, including without limitation the rights
+to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+copies of the Software, and to permit persons to whom the Software is
+furnished to do so, subject to the following conditions:
+
+The above copyright notice and this permission notice shall be included in all
+copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+SOFTWARE.
diff --git a/LICENSE.md b/LICENSE.md
deleted file mode 100644
index ae1bb3d..0000000
--- a/LICENSE.md
+++ /dev/null
@@ -1,7 +0,0 @@
-Copyright 2024 Littensy
-
-Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
-
-The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
-
-THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
diff --git a/README.md b/README.md
index 8ea132b..35895ac 100644
--- a/README.md
+++ b/README.md
@@ -1,10 +1,10 @@
-
+
Charm
- Atomic state management for Roblox.
+ Fine-grained reactivity for Roblox
npm package →
@@ -14,29 +14,139 @@

[](https://www.npmjs.com/package/@rbxts/charm)
-[](LICENSE.md)
+[](LICENSE)
-**Charm** is an atomic and immutable state management library, inspired by [Jotai](https://jotai.org) and [Nanostores](https://github.com/nanostores/nanostores). Store your state in atoms, and write your own functions to read, write, and observe state.
+Charm is a reactive state management library designed for storing data in discrete containers called _signals_. Connect behavior to data with signals, ensuring systems stay up-to-date with their underlying data while reactivity eliminates the need for manual updates.
-See an example of Charm's features in [this example repository](https://github.com/littensy/charm-example).
+**Build game state from simple building blocks:**
-## 🍀 Features
+- Store data in [signals](#signalinitialvalue-equals): reactive state containers that hold a value
+- React to state changes with [effects](#effectcallback): re-run code when signals update
+- Derive new values from data with [computed signals](#computedgetter): memoize functions that access signals
-- ⚛️ **Manage state with _atoms_.** Decompose state into independent containers called _atoms_, as opposed to combining them into a single store.
+**Want to learn more about reactivity?**
-- 💪 **Minimal, yet powerful.** Less boilerplate — write simple functions to read from and write to state.
+- https://dev.to/ryansolid/a-hands-on-introduction-to-fine-grained-reactivity-3ndf
+- https://preactjs.com/blog/introducing-signals
+- https://docs.solidjs.com/advanced-concepts/fine-grained-reactivity
+- https://github.com/roblox/signals
-- 🔬 **Immediate updates.** Listeners run asynchronously by default, avoiding the cascading effects of deferred updates and improving responsiveness.
+[Migrating from an older version of Charm?](#migration)
-- 🦄 **Like magic.** Selector functions can be subscribed to as-is — with implicit dependency tracking, atoms are captured and memoized for you.
+
+Table of Contents
----
+- [Installation](#installation)
+- [Reference](#reference)
+ - [`signal(initialValue, equals?)`](#signalinitialvalue-equals)
+ - [`computed(getter)`](#computedgetter)
+ - [`effect(callback)`](#effectcallback)
+ - [Nested effects](#nested-effects)
+ - [`effectScope(callback)`](#effectscopecallback)
+ - [`listen(getter, callback)`](#listengetter-callback)
+ - [Getter functions](#getter-functions)
+ - [`observe(getter, callback)`](#observegetter-callback)
+ - [`subscribe(getter, callback)`](#subscribegetter-callback)
+ - [`untracked(callback)`](#untrackedcallback)
+ - [`batch(callback)`](#batchcallback)
+ - [`mapped(getter, transform)`](#mappedgetter-transform)
+ - [`onCleanup(callback, failSilently?)`](#oncleanupcallback-failsilently)
+ - [`atom(initialValue, equals?)`](#atominitialvalue-equals)
+ - [`trigger(callback)`](#triggercallback)
+ - [`flags`](#flags)
+- [Client-Server Sync](#client-server-sync)
+ - [Installation](#installation-1)
+ - [Quick Start](#quick-start)
+ - [Server API](#config)
+ - [Client API](#clientaddsignalssetters)
+ - [Sync Caveats](#sync-caveats)
+- [Deep Reactivity](#deep-reactivity)
+ - [Installation](#installation-2)
+ - [`reactive(initialValue)`](#reactiveinitialvalue)
+ - [Mutation vs. update function](#mutation-vs-update-function)
+ - [`toRaw(value)`](#torawvalue)
+ - [`isReactive(value)`](#isreactivevalue)
+- [Migration](#migration)
+- [Examples](#examples)
-## 📦 Setup
+
-Install Charm for roblox-ts using your package manager of choice.
+## At a Glance
+
+```luau
+local getTodos, setTodos = signal({} :: { string })
+local getQuery, setQuery = signal("")
+
+observe(getTodos, function(todo: string, index: number)
+ local instance = Instance.new("TextLabel")
+
+ local getText = computed(function()
+ return getTodos()[index] or ""
+ end)
+
+ effect(function()
+ instance.Text = getText()
+ instance.Visible = string.match(getText(), getQuery()) ~= nil
+ end)
+
+ instance.LayoutOrder = index
+ instance.Size = UDim2.new(1, 0, 0, 40)
+ instance.Parent = screenGui
+
+ return function()
+ instance:Destroy()
+ end
+end)
+
+setTodos({ "Buy milk", "Buy eggs", "Play Roblox" })
+setQuery("Buy")
+```
+
+
+Explain code
+
+```luau
+-- Declare state for a todo list and a search query
+local getTodos, setTodos = signal({} :: { string })
+local getQuery, setQuery = signal("")
+
+-- Create a text label when an item is added to the list
+observe(getTodos, function(todo, index)
+ local instance = Instance.new("TextLabel")
+
+ -- Create a memoized function that only re-runs effects when the todo list
+ -- updates or the function returns new text
+ local getText = computed(function()
+ return getTodos()[index] or ""
+ end)
+
+ -- Update instance properties when the text or query updates
+ effect(function()
+ instance.Text = getText()
+ instance.Visible = string.match(getText(), getQuery()) ~= nil
+ end)
+
+ instance.LayoutOrder = index
+ instance.Size = UDim2.new(1, 0, 0, 40)
+ instance.Parent = screenGui
+
+ -- Destroy the instance when this item is removed
+ return function()
+ instance:Destroy()
+ end
+end)
+
+-- Add items to the todo list
+setTodos({ "Buy milk", "Buy eggs", "Play Roblox" })
+-- Filter for items containing "Buy"
+setQuery("Buy")
+```
+
+
+
+## Installation
```sh
npm install @rbxts/charm
@@ -44,391 +154,461 @@ yarn add @rbxts/charm
pnpm add @rbxts/charm
```
-Alternatively, add `littensy/charm` to your `wally.toml` file.
-
```toml
+# wally.toml
[dependencies]
Charm = "littensy/charm@VERSION"
```
---
-## 🐛 Debugging
+## Reference
-Charm provides a debug mode to help you identify potential bugs in your project. To enable debug mode, set the global `_G.__DEV__` flag to `true` before importing Charm.
+### `signal(initialValue, equals?)`
-Enabling `__DEV__` adds a few helpful features:
-
-- Better error handling for selectors, subscriptions, and batched functions:
-
- - Errors provide the function's name and line number.
- - Yielding in certain functions will throw an error.
-
-- Server state is validated for [remote event limitations](https://create.roblox.com/docs/scripting/events/remote#argument-limitations) before being passed to the client.
-
-Enabling debug mode in unit tests, storybooks, and other development environments can help you catch potential issues early. However, remember to turn off debug mode in production to avoid the performance overhead.
-
----
-
-## 📚 Reference
-
-### `atom(state, options?)`
-
-Atoms are the building blocks of Charm. They are functions that hold a single value, and calling them can read or write to that value. Atoms, or any function that reads from atoms, can also be [subscribed](#subscribecallback-listener) to.
-
-Call `atom` to create a state container initialized with the value `state`.
+Signals are the core of reactivity in Charm. The `signal` function creates a reactive signal that acts as a container for a value. It returns a function to access the value, and another to update the value.
```luau
-local nameAtom = atom("John")
-local todosAtom: Atom<{ string }> = atom({})
+local getCounter, setCounter = signal(0)
+
+setCounter(1)
+setCounter(function(count)
+ return count + 1
+end)
+print(getCounter()) -- 2
```
-#### Parameters
+Accessing the signal's value in an effect or computed signal will subscribe to it as a dependency. Changing the value will immediately notify every effect and computed signal that depends on the signal, ensuring all of your state is correct and up-to-date.
-- `state`: The value to assign to the atom initially.
-
-- **optional** `options`: An object that configures the behavior of this atom.
-
- - **optional** `equals`: An equality function to determine whether the state has changed. By default, strict equality (`==`) is used.
-
-#### Returns
-
-The `atom` constructor returns an atom function with two possible operations:
-
-1. **Read the state.** Call the atom without arguments to get the current state.
-2. **Set the state.** Pass a new value or an updater function to change the state.
+You can also pass a custom equality function to only update the signal if the equality function returns `false`:
```luau
-local function newTodo()
- nameAtom("Jane")
-
- todosAtom(function(todos)
- todos = table.clone(todos)
- table.insert(todos, "Buy milk")
- return todos
- end)
-
- print(nameAtom()) --> "Jane"
-end
-```
-
----
-
-### `subscribe(callback, listener)`
-
-Call `subscribe` to listen for changes in an atom or selector function. When the function's result changes, subscriptions are immediately called with the new state and the previous state.
-
-```luau
-local nameAtom = atom("John")
-
-local cleanup = subscribe(nameAtom, function(name, prevName)
- print(name)
+local getMax, setMax = signal(0, function(current, incoming)
+ return incoming <= current
end)
-nameAtom("Jane") --> "Jane"
+setMax(1) -- 1
+setMax(2) -- 2
+setMax(-1) -- 2
```
-You may also pass a selector function that calls other atoms. The function will be memoized and only runs when its atom dependencies update.
-
-```luau
-local function getUppercase()
- return string.upper(nameAtom())
-end
-
-local cleanup = subscribe(getUppercase, function(name)
- print(name)
-end)
-
-nameAtom("Jane") --> "JANE"
-```
-
-#### Parameters
-
-- `callback`: The function to subscribe to. This may be an atom or a selector function that depends on an atom.
-
-- `listener`: The listener is called when the result of the callback changes. It receives the new state and the previous state as arguments.
-
-#### Returns
-
-`subscribe` returns a cleanup function.
+> [!NOTE]
+> Looking for atoms? You can still use [`atom()`](#atominitialvalue-equals) to create a signal with a unified getter and setter.
---
### `effect(callback)`
-Call `effect` to track state changes in all atoms read within the callback. The callback will run once to retrieve its dependencies, and then again whenever they change. Your callback may return a cleanup function to run when the effect is removed or about to re-run.
+Effects are fundamental to reactivity, allowing you to react to signal updates. The `effect` function subscribes to signals accessed by the effect callback, and when a dependency updates, the callback will re-execute.
```luau
-local nameAtom = atom("John")
+local getCounter, setCounter = signal(0)
-local cleanup = effect(function()
- print(nameAtom())
+effect(function()
+ print(`Count is {getCounter()}`)
+end) -- Count is 0
+
+setCounter(1) -- Count is 1
+```
+
+You can also return a cleanup function that will run once, either before the effect re-runs or when the effect is disposed:
+
+```luau
+local getCounter, setCounter = signal(0)
+
+local dispose = effect(function()
+ local count = getCounter()
return function()
- print("Cleanup function called!")
+ print(`Cleanup {count}`)
end
end)
+
+setCounter(1) -- Cleanup 0
+dispose() -- Cleanup 1
```
-Because `effect` implicitly tracks all atoms read within the callback, it might be useful to exclude atoms that should not trigger a re-run. You can use [`peek`](#peekvalue) to read from atoms without tracking them as dependencies.
+### Nested Effects
-#### Parameters
-
-- `callback`: The function to track for state changes. The callback will run once to retrieve its dependencies, and then again whenever they change.
-
-#### Returns
-
-`effect` returns a cleanup function.
-
-#### Caveats
-
-- **If your effect should disconnect itself, use the `cleanup` argument.** Because effects run immediately, your effect may run before a `cleanup` function is returned. To disconnect an effect from the inside, use the argument passed to your effect instead:
- ```lua
- effect(function(cleanup)
- if condition() then
- cleanup()
- end
- end)
- ```
-
----
-
-### `computed(callback, options?)`
-
-Call `computed` when you want to derive a new atom from one or more atoms. The callback will be memoized, meaning that subsequent calls to the atom return a cached value that is only re-calculated when the dependencies change.
+An effect is _nested_ if it was created during the execution of another effect. In Charm, when an effect with nested effects re-runs or gets cleaned up, the nested effects from the previous run are automatically cleaned up and re-created if needed. This prevents memory leaks and ensures that outer effects always run before their inner effects:
```luau
-local todosAtom: Atom<{ string }> = atom({})
-local mapToUppercase = computed(function()
- local result = table.clone(todosAtom())
- for key, todo in result do
- result[key] = string.upper(todo)
+local getPrintCount, setPrintCount = signal(true)
+local getCount, setCount = signal(1)
+
+effect(function()
+ if getPrintCount() then
+ -- This inner effect is created when getPrintCount() is true
+ effect(function()
+ print(`Count is {getCount()}`)
+ end)
end
- return result
-end)
+end) -- Count is 1
+
+setCount(2) -- Count is 2
+
+-- This re-runs the outer effect and cleans up old inner effects
+setPrintCount(false)
+
+setCount(3) -- No output
```
-Because `computed` implicitly tracks all atoms read within the callback, it might be useful to exclude atoms that should not trigger a re-run. You can use [`peek`](#peekvalue) to read from atoms without tracking them as dependencies.
-
-This function is also useful for optimizing `effect` calls that depend on multiple atoms. For instance, if an effect derives some value from two atoms, it will run twice if both atoms change at the same time. Using `computed` can group these dependencies together and avoid re-running side effects.
-
-#### Parameters
-
-- `callback`: A function that returns a new value depending on one or more atoms.
-
-- **optional** [`options`](#parameters): An object that configures the behavior of this atom.
-
-#### Returns
-
-`computed` returns a read-only atom.
-
----
-
-### `observe(callback, factory)`
-
-Call `observe` to create an instance of `factory` for each key present in a dictionary or array. Your factory can return a cleanup function to run when the key is removed or the observer is cleaned up.
-
> [!NOTE]
-> Because `observe` tracks the lifetime of each key in your data, your keys must be unique and unchanging. If your data is not keyed by unique and stable identifiers, consider using [`mapped`](#mappedcallback-mapper) to transform it into a keyed object before passing it to `observe`.
+> To run code that is "detached" from the parent effect or scope, use `untracked()` or a detached effect scope. If you suspect that the new nested effect behavior is causing issues with migration, try disabling the `flags.trackInnerEffects` flag to assist with debugging.
+
+---
+
+### `computed(getter)`
+
+The `computed` function creates a read-only signal whose value is derived from other signals. The computed signal caches the getter function's last result, and the value is only re-computed if a dependency has updated since the last computation.
```luau
-local todosAtom: Atom<{ [string]: Todo }> = atom({})
+local getName, setName = signal("John")
+local getSurname, setSurname = signal("Doe")
+local getFullName = computed(function()
+ return `{getName()} {getSurname()}`
+end)
-local cleanup = observe(todosAtom, function(todo, key)
- print(`Added {key}: {todo.name}`)
+print(getFullName()) -- "John Doe"
+setName("Jane")
+print(getFullName()) -- "Jane Doe"
+```
+
+The getter function also receives the previous result (or `nil` during the initial run). You can use this for computed signals that depend on the previous result:
+
+```luau
+local getCounter, setCounter = signal(10)
+local getMax = computed(function(prevMax)
+ return math.max(getCounter(), prevMax or 0)
+end)
+
+print(getMax()) -- 10
+setCounter(5)
+print(getMax()) -- 10
+```
+
+---
+
+### `effectScope(callback)`
+
+Scopes allow you to dispose multiple effects at once. The `effectScope` function creates a scope that tracks inner effects, so effects created during the execution of the callback will clean up when the scope disposes.
+
+```luau
+local getCounter, setCounter = signal(0)
+
+local dispose = effectScope(function()
+ effect(function()
+ print(`Count 1 is {getCounter()}`)
+ end)
+ effect(function()
+ print(`Count 2 is {getCounter()}`)
+ end)
+end)
+
+setCounter(1) -- Count 1 is 1, Count 2 is 1
+dispose()
+setCounter(2) -- No output; effects got disposed
+```
+
+Similar to `effect()`, the callback can return a cleanup function that runs when the effect scope is disposed.
+
+---
+
+### `listen(getter, callback)`
+
+The `listen` function creates an effect that only subscribes to the signals accessed by `getter`. Signals accessed by the callback will not be subscribed to, avoiding accidental subscriptions when you want to run side effects.
+
+The callback also receives the previous value, or `nil` when running for the first time.
+
+```luau
+local getCounter, setCounter = signal(0)
+
+listen(getCounter, function(count, prevCount)
+ print(`Count is {count} (was {prevCount})`)
+end) -- Count is 0 (was nil)
+
+setCounter(1) -- Count is 1 (was 0)
+```
+
+Note that the listener callback runs in `untracked()`, so nested effects are not cleaned up.
+
+### Getter functions
+
+In most Charm APIs, you can also subscribe to getter functions that call one or more signals, and they will automatically be tracked:
+
+```luau
+local getCounter, setCounter = signal(0)
+
+local function floorCounter()
+ return math.floor(getCounter())
+end
+
+listen(floorCounter, function(count, prevCount)
+ print(`Floor of count is {count} (was {prevCount})`)
+end) -- Floor of count is 0 (was nil)
+
+setCounter(0.5) -- Doesn't print anything, floor is still 0
+setCounter(1) -- Floor of count is 1 (was 0)
+```
+
+---
+
+### `observe(getter, callback)`
+
+[Observers](https://sleitnick.github.io/RbxObservers/docs/observer-pattern) allow you to track the lifetime of a given state. The `observe` function executes the callback for every unique key added to a table, and disposes the callback when that key is removed.
+
+```luau
+local getItems, setItems = signal({ a = 0, b = 0 })
+
+observe(getItems, function(value, key)
+ print(`Added {key}`)
return function()
print(`Removed {key}`)
end
-end)
+end) -- Added a, Added b
+
+setItems({ a = 0, c = 0 }) -- Removed b, Added c
```
-#### Parameters
+The callback runs in an effect scope, so effects created in the callback will be disposed when the key is removed:
-- `callback`: An atom or selector function that returns a dictionary or an array of values. When a key is added to the state, the factory will be called with the new key and its initial value.
+```luau
+local getItems, setItems = signal({})
-- `factory`: A function that will be called whenever a key is added or removed from the atom's state. The callback will receive the key and the entry's initial value as arguments, and may return a cleanup function.
+local dispose = observe(getItems, function(value, key)
+ local getValue = computed(function(prevValue)
+ return getItems()[key] or prevValue
+ end)
-#### Returns
+ effect(function()
+ local value = getValue()
+ print(`Set {key} = {value}`)
+ return function()
+ print(`Cleanup {key} = {value}`)
+ end
+ end)
+end)
-`observe` returns a cleanup function.
+setItems({ a = 0, b = 0 }) -- Set a = 0, Set b = 0
+setItems({ a = 1, b = 0 }) -- Cleanup a = 0, Set a = 1
+setItems({ a = 1 }) -- Cleanup b = 0
+dispose() -- Cleanup a = 1
+```
---
-### `mapped(callback, mapper)`
+### `subscribe(getter, callback)`
-Call `mapped` to transform the keys and values of your state. The `mapper` function will be called for each key-value pair in the atom's state, and the new keys and atoms will be stored in a new atom.
+The `subscribe` function is identical to `listen()`, but the callback does not run initially. The callback only runs when the value returned by the getter function changes.
```luau
-local todosAtom: Atom<{ Todo }> = atom({})
-local todosById = mapped(todosAtom, function(todo, index)
- return todo, todo.id
+local getCounter, setCounter = signal(0)
+
+-- Does not output anything initially
+subscribe(getCounter, function(count, prevCount)
+ print(`Count is {count} (was {prevCount})`)
end)
+
+setCounter(1) -- Count is 1 (was 0)
```
-#### Parameters
-
-- `callback`: The function whose result you want to map over. This can be an atom or a selector function that reads from atoms.
-
-- `mapper`: The mapper is called for each key in your state. Given the current value and key, it should return a new corresponding value and key:
-
- 1. Return a single value to map the table's original key to a new value.
- 2. Return two values, the first being the value and the second being the key, to update both keys and values.
- 3. Return `nil` for the value to remove the key from the resulting table.
-
-#### Returns
-
-`mapped` returns a read-only atom.
-
---
-### `peek(value, ...)`
+### `untracked(callback)`
-Call `peek` to call a function without tracking it as the dependency of an effect or a selector function.
+In case you want to opt-out of dependency tracking in an effect, you can use `untracked()` to call a function _outside_ the current scope, preventing signals and effects in the function from being tracked.
```luau
-local nameAtom = atom("John")
-local ageAtom = atom(25)
+local getTracked, setTracked = signal(0)
+local getUntracked, setUntracked = signal(0)
effect(function()
- local name = nameAtom()
- local age = peek(ageAtom)
-end)
+ print(`Tracked: {getTracked()}, Untracked: {untracked(getUntracked)}`)
+end) -- Tracked: 0, Untracked: 0
+
+setTracked(1) -- Tracked: 1, Untracked: 0
+setUntracked(1) -- No output
+setTracked(2) -- Tracked: 2, Untracked: 1
```
-#### Parameters
+Because `untracked()` executes the callback outside the current effect, nested effects created during the execution of the callback will not be tracked by the parent effect:
-- `value`: Any value. If the value is a function, `peek` will call it without tracking dependencies and return the result.
+```luau
+local stopEffect
+local stopScope = effectScope(function()
+ untracked(function()
+ stopEffect = effect(function()
+ return function()
+ print("Cleaned up untracked effect")
+ end
+ end)
+ end)
+end)
-- **optional** `...args`: Additional arguments to pass to the value if it is a function.
-
-#### Returns
-
-`peek` returns the result of the given function. If the value is not a function, it will return the value as-is.
+stopScope() -- No output, the scope did not track the effect
+stopEffect() -- Cleaned up untracked effect
+```
---
### `batch(callback)`
-Call `batch` to defer state changes until after the callback has run. This is useful when you need to make multiple changes to the state and only want listeners to be notified once.
+Combines multiple signal updates made by the callback into a single commit that triggers effects once the callback completes.
```luau
-local nameAtom = atom("John")
-local ageAtom = atom(25)
+local getName, setName = signal("John")
+local getSurname, setSurname = signal("Doe")
+effect(function()
+ print(`Full name: {getName()} {getSurname()}`)
+end)
+
+-- Combines both writes into a single update.
+-- Once the callback completes, outputs "Full name: Foo Bar"
batch(function()
- nameAtom("Jane")
- ageAtom(26)
+ setName("Foo")
+ setSurname("Bar")
end)
```
-#### Parameters
-
-- `callback`: A function that updates atoms. Listeners will only be notified once all changes have been applied.
-
-#### Returns
-
-`batch` does not return anything.
-
---
-## 📦 React
+### `mapped(getter, transform)`
-### Setup
+The `mapped` function iterates over every key in a table and uses the transform function to assign them to a new key and value. The result is returned as a read-only signal containing the new keys and values. When a key's value changes, or a new key is added to the table, `transform` is called for that key and its current value.
-Install the React bindings for Charm using your package manager of choice.
-
-```sh
-npm install @rbxts/react-charm
-yarn add @rbxts/react-charm
-pnpm add @rbxts/react-charm
-```
-
-```toml
-[dependencies]
-ReactCharm = "littensy/react-charm@VERSION"
-```
-
----
-
-### `useAtom(callback, dependencies?)`
-
-Call `useAtom` at the top-level of a React component to read from an atom or selector. The component will re-render when the value changes.
+The first value returned by the transform function is used as the new value:
```luau
-local todosAtom = require(script.Parent.todosAtom)
+local getList, setList = signal({ "a", "b", "c" })
-local function Todos()
- local todos = useAtom(todosAtom)
- -- ...
-end
+local getUppercase = mapped(getList, function(value)
+ return string.upper(value)
+end)
+
+print(getUppercase()) -- { "A", "B", "C" }
```
-If your selector depends on the component's state or props, remember to pass them in a dependency array. This prevents skipped updates when an untracked parameter of the selector changes.
+If the transform function returns two values, the second value is used as the new key:
```luau
-local todos = useAtom(function()
- return searchTodos(props.filter)
-end, { props.filter })
-```
+local getList, setList = signal({ "a", "b", "c" })
-#### Parameters
+local getSwapped = mapped(getList, function(value, key)
+ return key, value
+end)
-- `callback`: An atom or selector function that depends on an atom.
-
-- **optional** `dependencies`: An array of outside values that the selector depends on. If the dependencies change, the subscription is re-created and the component re-renders with the new state.
-
-#### Returns
-
-`useAtom` returns the current state of the atom.
-
----
-
-## 📦 Vide
-
-### Setup
-
-Install the Vide bindings for Charm using your package manager of choice.
-
-```sh
-npm install @rbxts/vide-charm
-yarn add @rbxts/vide-charm
-pnpm add @rbxts/vide-charm
-```
-
-```toml
-[dependencies]
-VideCharm = "littensy/vide-charm@VERSION"
+print(getSwapped()) -- { a = 1, b = 2, c = 3 }
```
---
-### `useAtom(callback)`
+### `onCleanup(callback, failSilently?)`
-Call `useAtom` in any scope to create a Vide source that returns the current state of an atom or selector.
+The `onCleanup` function binds the callback to the currently running effect or effect scope. Multiple cleanup functions can be bound to the same effect.
+
+Unless `failSilently` is set to `true`, this function will emit a warning if there is no active effect or scope.
```luau
-local todosAtom = require(script.Parent.todosAtom)
+local dispose = effectScope(function()
+ onCleanup(function()
+ print("Cleaned up")
+ end)
+end)
-local function Todos()
- local todos = useAtom(todosAtom)
- -- ...
-end
+dispose() -- Cleaned up
```
-#### Parameters
-
-- `callback`: An atom or selector function that depends on an atom.
-
-#### Returns
-
-`useAtom` returns a Vide source.
-
---
-## 📗 Charm Sync
+### `atom(initialValue, equals?)`
-### Setup
+The `atom` function creates a new reactive signal and returns a single function that acts as both a getter and setter.
-The Charm Sync package provides server-client synchronization for your Charm atoms. Install it using your package manager of choice.
+If the atom is called with 0 arguments, the atom returns the current value and subscribes to the signal. Otherwise, when called with 1 or more arguments, the atom will update the signal's value.
+
+```luau
+local counter = atom(0)
+
+print(counter()) -- 0
+counter(1)
+counter(function(count)
+ return count + 1
+end)
+```
+
+You can also pass a custom equality function to only update the signal if the new value is _not_ equal to the current value:
+
+```luau
+local max = atom(0, function(current, incoming)
+ return incoming <= current
+end)
+
+max(1) -- 1
+max(-1) -- 1
+```
+
+---
+
+### `trigger(callback)`
+
+The `trigger()` function allows you to manually trigger updates for downstream dependencies when you've directly mutated a signal's value without using the signal setter:
+
+```luau
+local array = signal({} :: { number })
+local length = computed(function()
+ return #array()
+end)
+
+print(length()) -- 0
+
+-- Direct mutation doesn't automatically trigger updates
+table.insert(array(), 1)
+print(length()) -- Still 0
+
+-- Manually trigger updates
+trigger(array)
+print(length()) -- 1
+```
+
+You can also trigger multiple signals at once:
+
+```luau
+local src1 = signal({} :: { number })
+local src2 = signal({} :: { number })
+local total = computed(function()
+ return #src1() + #src2()
+end)
+
+table.insert(src1(), 1)
+table.insert(src2(), 2)
+
+trigger(function()
+ src1()
+ src2()
+end)
+
+print(total()) -- 2
+```
+
+---
+
+### `flags`
+
+Charm exposes the following global flags to customize behavior:
+
+| Flag | Default | Description |
+| ----------------- | -------- | ------------------------------------------------------------------------------------------------------------------------------------------------------ |
+| strict | `true`\* | Enforces synchronous, non-yielding behavior in signals, effects, and other critical code. |
+| frozen | `true`\* | Enforces data immutability by deep-freezing tables passed to signals, excluding objects with metatables. |
+| trackInnerEffects | `true` | Whether nested effects should be tracked and cleaned up when the parent effect re-runs. This should only be disabled to debug issues during migration. |
+
+The `strict` and `frozen` flags are automatically enabled in Roblox Studio and other development environments where the Luau optimization level is `O1` or lower.
+
+---
+
+## Client-Server Sync
+
+### Installation
```sh
npm install @rbxts/charm-sync
@@ -441,273 +621,459 @@ pnpm add @rbxts/charm-sync
CharmSync = "littensy/charm-sync@VERSION"
```
----
+### Quick Start
-### `server(options)`
-
-Call `server` to create a server sync object. This synchronizes every client's atoms with the server's state by sending partial patches that the client merges into its state.
+Start by specifying the signals that the server should sync to clients. For this example, we'll use the first and last name signals:
```luau
-local syncer = CharmSync.server({
- -- A dictionary of the atoms to sync, matching the client's
- atoms = atomsToSync,
- -- The minimum interval between state updates
- interval = 0,
- -- Whether to send a full history of changes made to the atoms (slower)
- preserveHistory = false,
- -- Whether to apply fixes for remote event limitations. Disable this option
- -- when using a network library with custom ser/des, like ByteNet or Zap.
- autoSerialize = true,
-})
+-- nameStore
+local getName, setName = signal("John")
+local getSurname, setSurname = signal("Doe")
+local ageAtom = atom(20)
--- Sends state updates to clients when a synced atom changes.
--- Omitting sensitive information and data serialization can be done here.
-syncer:connect(function(player, ...)
- remotes.syncState:fire(player, ...)
-end)
-
--- Sends the initial state to a player upon request. This should fire when a
--- player joins the game.
-remotes.requestState:connect(function(player)
- syncer:hydrate(player)
-end)
+return {
+ getName = getName,
+ setName = setName,
+ getSurname = getSurname,
+ setSurname = setSurname,
+ ageAtom = ageAtom,
+}
```
-#### Parameters
+When a player notifies the server that they're ready to start syncing, call `server.addSignalsToClient` with the signals that the client should receive updates for. Once they leave, call `server.removeClient` to unsubscribe them from all updates.
-- `options`: An object to configure sync behavior.
+Then, use `server.connect` to specify how state updates should be sent to each client. Pass a callback function that fires a remote with the given target player and the state updates they subscribed to:
- - `atoms`: A dictionary of the atoms to sync. The keys should match the keys on the client.
+```luau
+playerReadyEvent.OnServerEvent:Connect(function(player)
+ -- Sync signal getters, computed signals, atoms, or reactive proxies
+ server.addSignalsToClient(player, {
+ name = nameStore.getName,
+ surname = nameStore.getSurname,
+ age = nameStore.ageAtom,
+ })
+end)
- - **optional** `interval`: The interval at which to batch state updates to clients. Defaults to `0`, meaning updates are batched every frame.
+Players.PlayerRemoving:Connect(function(player)
+ server.removeClient(player)
+end)
- - **optional** `preserveHistory`: Whether to sync an exhaustive history of changes made to the atoms since the last sync event. If `true`, the server sends multiple payloads instead of one. Defaults to `false` for performance.
-
- - **optional** `autoSerialize`: Whether to apply validation and workarounds to certain [remote argument limitations](https://create.roblox.com/docs/scripting/events/remote#table-indexing). Defaults to `true`, but you should set it to `false` if you serialize remote arguments (i.e. if you use [ByteNet](https://github.com/ffrostfall/ByteNet) or [Zap](https://github.com/red-blox/zap)).
+server.connect(function(player, updates)
+ -- Customize how you send state updates to clients
+ syncEvent:FireClient(player, updates)
+end)
+```
> [!NOTE]
-> Charm sends table updates in the form of partial tables, so arrays will contain `nil` values, which has undefined behavior in remotes without serialization.
->
-> Charm's default `autoSerialize` behavior fixes this, but it can interfere with custom serialization. Disable this option if you use a network library that serializes remote event arguments.
+> On the server, make sure each key corresponds to the same data across all players. If two players subscribe to the same key, but were given different signals, Charm will output a warning.
-#### Returns
+To sync the client with the server's state, call `client.addSignals` with a table of writable signals (setter functions or atoms) whose keys match their server counterparts.
-`server` returns an object with the following methods:
-
-- `syncer:connect(callback)`: Registers a callback to send state updates to clients. The callback will receive the player and the payload(s) to send, and should fire a remote event. The payload is read-only, so any changes should be applied to a copy of the payload.
-
-- `syncer:hydrate(player)`: Sends the player a full state update for all synced atoms.
-
-#### Caveats
-
-- **Do not use values that cannot be sent over remotes** in your shared atoms. This includes functions, threads, and non-string keys in dictionaries. [Read more about argument limitations in remotes.](https://create.roblox.com/docs/scripting/events/remote#argument-limitations)
-
-- **By default, Charm omits the individual changes made to atoms** between sync events (i.e. a `counterAtom` set to `1` and then `2` will only send the final state of `2`). If you need to preserve a history of changes, set `preserveHistory` to `true`.
-
-- **Charm does not handle network communication.** Use remote events or a network library to send sync payloads - and remember to set `autoSerialize` accordingly!
-
----
-
-### `client(options)`
-
-Call `client` to create a client sync object. This synchronizes the client's atoms with the server's state by merging partial patches sent by the server into each atom.
+After the client receives updates from the server, call `client.patch` to patch the client's signals with the incoming state updates.
```luau
-local syncer = CharmSync.client({
- atoms = atomsToSync, -- A dictionary of the atoms to sync, matching the server's
- ignoreUnhydrated = true, -- Whether to ignore state updates before the initial update
+-- Add signal setters, atoms, or reactive proxies
+client.addSignals({
+ name = nameStore.setName,
+ surname = nameStore.setSurname,
+ age = nameStore.ageAtom,
})
--- Applies state updates from the server to the client's atoms.
--- Data deserialization can be done here.
-remotes.syncState:connect(function(...)
- syncer:sync(...)
+-- Update client signals when receiving updates from the server
+syncEvent.OnClientEvent:Connect(function(updates)
+ client.patch(updates)
end)
-
--- Requests the initial state from the server when the client joins the game.
--- Before this runs, the client uses the atoms' default values.
-remotes.requestState:fire()
```
-#### Parameters
-
-- `options`: An object to configure sync behavior.
-
- - `atoms`: A dictionary of the atoms to sync. The keys should match the keys on the server.
-
- - **optional** `ignoreUnhydrated`: Whether to ignore state updates before setting the initial state. Defaults to `true`.
-
-#### Returns
-
-`client` returns an object with the following methods:
-
-- `syncer:sync(...payloads)` applies a state update from the server.
-
-#### Caveats
-
-- **Charm does not handle network communication.** Use remote events or a network library to receive sync payloads. This includes requesting the initial state, which is implemented via `requestState` in the example above.
-
---
-### `isNone(value)`
+### `config`
-Call `isNone` to check if a value is `None`. Charm's partial state patches omit values that did not change between sync events, so to mark keys for deletion, Charm uses the `None` marker.
+A configuration table that customizes the behavior of Charm Sync on the server.
-This function can be used to check whether a value is about to be removed from an atom.
-
-```luau
-local syncer = CharmSync.server({ atoms = atomsToSync })
-
-syncer:connect(function(player, payload)
- if
- payload.type === "patch"
- and payload.data.todosAtom
- and CharmSync.isNone(payload.data.todosAtom.eggs)
- then
- -- 'eggs' will be removed from the client's todo list
- end
- remotes.syncState.fire(player, payload)
-end)
-```
-
-#### Parameters
-
-- `value`: Any value. If the value is `None`, `isNone` will return `true`.
-
-#### Returns
-
-`isNone` returns a boolean.
+| Option | Default | Description |
+| --------------- | ------- | --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
+| interval | `0` | The frequency at which the server will send patches to the client, in seconds. A value of `0` sends updates on the next frame. Set to a negative value to disable the interval. |
+| preserveHistory | `false` | Whether to preserve a full history of state changes since the last sync, at the cost of performance. This is useful if you need to replicate each individual change that occurs between sync events. |
+| fixArrays | `true` | When `true`, Charm will attempt to work around remote event limitations that cause data loss in array patches. Disable this if your networking library serializes remote arguments (Zap, ByteNet, etc.). |
+| validatePatches | `true` | When `true`, and both `fixArrays` and strict mode are enabled, synced values containing unsafe sparse arrays or mixed tables will emit a warning. See the [remote argument limitations](https://create.roblox.com/docs/scripting/events/remote#argument-limitations). |
---
-## 🚀 Examples
+### `server.addSignalsToClient(client, getters)`
-### Counter atom
+The `addSignalsToClient` function subscribes a client to updates in the given signals. When an update occurs, the client will receive a state patch of only the values that changed.
+
+You can pass signal getter functions, computed signals, atoms, and [reactive objects](#reactiveinitialvalue) in the `getters` table. This function can also be called multiple times on the same client to subscribe to new signals.
```luau
-local counterAtom = atom(0)
-
--- Create a selector that returns double the counter value
-local function doubleCounter()
- return counterAtom() * 2
-end
-
--- Runs after counterAtom is updated and prints double the new value
-subscribe(doubleCounter, function(value)
- print(value)
+playerReadyEvent.OnServerEvent:Connect(function(player)
+ server.addSignalsToClient(player, {
+ name = nameStore.getName,
+ surname = nameStore.getSurname,
+ age = nameStore.ageAtom,
+ })
end)
-
-counterAtom(1) --> 2
-counterAtom(function(count)
- return count + 1
-end) --> 4
```
-### React component
+You're also allowed to create new signals to sync to specific players, as long as the key is unique to that data:
```luau
-local counter = require(script.Parent.counter)
-local counterAtom = counter.counterAtom
-local incrementCounter = counter.incrementCounter
+playerReadyEvent.OnServerEvent:Connect(function(player)
+ server.addSignalsToClient(player, {
+ -- 🟢 Good: Player-specific data is synced with a unique key
+ [`data-{player.UserId}`] = computed(function()
+ return getPlayerData(player.UserId)
+ end),
+
+ -- 🔴 Bad: Syncing different data with the same keys does not work
+ playerData = computed(function()
+ return getPlayerData(player.UserId)
+ end),
+ })
+end)
+```
+
+---
+
+### `server.removeSignalsFromClient(client, ...keys)`
+
+The `removeSignalsFromClient` function unsubscribes the client from a list of keys that were previously subscribed to via `addSignalsToClient`.
+
+```luau
+server.addSignalsToClient(player, {
+ name = nameStore.getName,
+ surname = nameStore.getSurname,
+})
+
+server.removeSignalsFromClient(player, "surname")
+```
+
+---
+
+### `server.removeClient(client)`
+
+The `removeClient` function unsubscribes a client from receiving all state updates from the server. You should call this function when a player leaves the game.
+
+```luau
+playerReadyEvent.OnServerEvent:Connect(function(player)
+ server.addSignalsToClient(player, signals)
+end)
+
+Players.PlayerRemoving:Connect(function(player)
+ server.removeClient(player)
+end)
+```
+
+---
+
+### `server.connect(onSync)`
+
+Binds a callback to run when sending state updates a client. Scheduled updates will be sent periodically at the interval specified in [`config.interval`](#config).
+
+When a synced signal updates, the `onSync` function is scheduled to run for each client subscribed to that signal on the next sync event.
+
+```luau
+server.connect(function(player, updates)
+ syncEvent:FireServer(player, updates)
+end)
+```
+
+---
+
+### `server.disconnect()`
+
+Stops syncing state updates to clients at the automatic interval. This doesn't unbind the last callback, so you can still manually trigger sync events after calling this function by calling `server.flush()`.
+
+```luau
+server.connect(function(player, updates)
+ syncEvent:FireServer(player, updates)
+end)
+
+-- Stops calling the function at the automatic interval
+server.disconnect()
+
+-- Flushing still sends pending updates
+server.flush()
+```
+
+---
+
+### `client.addSignals(setters)`
+
+Subscribes the given writable signals to the states with the corresponding keys on the server. When the server sends updates, the functions associated with each key in the state will be called with the patched values.
+
+You can pass either writable signals, atoms, or [reactive objects](#reactiveinitialvalue) to this function:
+
+```luau
+client.addSignals({
+ name = nameStore.setName,
+ surname = nameStore.setSurname,
+ age = nameStore.ageAtom,
+})
+```
+
+---
+
+### `client.removeSignals(...keys)`
+
+Unsubscribes from each signal with the corresponding keys. The signals will retain their current values, but will no longer receive updates from the server.
+
+```luau
+client.addSignals({
+ name = nameStore.setName,
+ surname = nameStore.setSurname,
+})
+
+client.removeSignals("name")
+```
+
+---
+
+### `client.patch(updates)`
+
+The `patch` function patches the client's state with the updates sent from the server. The initial update sent by the server will be the full state, and later updates will only include the values that changed.
+
+You should call `patch` when receiving updates from the server from a remote event:
+
+```luau
+client.addSignals({
+ name = nameStore.setName,
+ surname = nameStore.setSurname,
+})
+
+syncEvent.OnClientEvent:Connect(function(updates)
+ client.patch(updates)
+end)
+```
+
+---
+
+### `signalToAtom(getter, setter)`
+
+If you have a lot of signals to sync between the server and clients, it might become difficult to keep track of many getters and setters. The `signalToAtom()` function unifies a signal's `get()` and `set()` functions, allowing you to reuse the same values for `client.addSignals` and `server.addSignalsToClient`.
+
+```luau
+local sharedState = {
+ name = signalToAtom(nameStore.getName, nameStore.setName),
+ surname = signalToAtom(nameStore.getSurname, nameStore.setSurname),
+}
+
+client.addSignals(sharedState)
+server.addSignalsToClient(client, sharedState)
+```
+
+---
+
+### Sync Caveats
+
+Charm Sync will only send clients the differences between the current state and the last-synced state through a process called _delta compression_. In this case, tables are recursively scanned for changes, and unchanged properties are omitted by setting them to `nil`.
+
+But it's hard to differentiate between an unchanged value and a removed value, as both cases are represented by `nil`. We chose to address this by representing deleted values with a special `None` symbol denoted by `{ __none = "__none" }`.
+
+This means nilable values may be replaced with `None` in patches, and code working with update payloads (usually for remote argument serialization) should account for nilable values possibly being sent as `None` in the payload.
+
+---
+
+## Deep Reactivity
+
+Charm's reactivity system is _shallow_ by default: only the top-level value is reactive, so table properties are not checked when determining whether a signal has updated. As a result, tables in Charm should be immutable (copied before writing) in order to signal that a table's properties have changed.
+
+Deep reactivity, on the other hand, uses proxy tables to perform dependency tracking on properties. You can subscribe to a property by indexing the proxy table, and setting a property will notify its subscribers. This approach to reactivity lets you work with mutable data, making state management more intuitive at the cost of added overhead.
+
+You can opt-in to deep reactivity with the Deep Charm library:
+
+### Installation
+
+> [!WARNING]
+> Deep Charm is an experimental library that is not fully documented, and the API is subject to breaking changes.
+
+```sh
+npm install @rbxts/deep-charm
+yarn add @rbxts/deep-charm
+pnpm add @rbxts/deep-charm
+```
+
+```toml
+[dependencies]
+DeepCharm = "littensy/deep-charm@VERSION"
+```
+
+---
+
+### `reactive(initialValue)`
+
+The `reactive()` function takes a mutable table and wraps it in a reactive proxy. Reading properties through the proxy will perform dependency tracking, and nested tables will also be wrapped in a reactive proxy.
+
+```luau
+local users, updateUsers = reactive({
+ { name = "John", surname = "Doe" },
+})
+
+effect(function()
+ print(`{users[1].name} {users[1].surname}`)
+end) -- Output: John Doe
+
+updateUsers(function(raw)
+ raw[1].name = "Jane"
+ raw[1].surname = "Smith"
+ table.insert(raw, { name = "Steve", surname = "Doe" })
+end) -- Output: Jane Smith
+
+-- You can also mutate the reactive proxy directly:
+users[1].name = "John" -- John Smith
+```
+
+### Mutation vs. update function
+
+Because reactive proxies use metatables for reading and writing, functions like `table.insert` will not work on the proxy. Table functions should only be called on the raw table value, which you can access through the update function returned by `reactive()`.
+
+This update function (`updateUsers()` in the example above) passes the raw table for you to mutate. Once your callback is done executing, the updater will manually notify subscriptions to the reactive proxy and its nested properties. This process does not use metatables, so you should use this for table operations or batching updates.
+
+---
+
+### `toRaw(value)`
+
+If you need to access the raw table through the reactive proxy, use the `toRaw()` function:
+
+```luau
+local raw = {}
+local proxy = reactive(raw)
+
+print(toRaw(proxy) == raw) -- true
+```
+
+Calling `toRaw()` on a proxy also subscribes to its changes. This allows you to use proxies with `observe()` and other Charm APIs that accept a getter function:
+
+```luau
+local proxy, updateProxy = reactive({})
+
+observe(function()
+ return toRaw(proxy)
+end, function(value, key)
+ print(`Value {value} added at key {key}`)
+end)
+
+updateProxy(function(state)
+ table.insert(state, "foo")
+end) -- Value foo added at key 1
+```
+
+---
+
+### `isReactive(value)`
+
+The `isReactive()` function returns `true` if the given value is a reactive proxy by checking its metatable.
+
+```lua
+local raw = {}
+local proxy = reactive(raw)
+
+print(isReactive(proxy)) -- true
+print(isReactive(raw)) -- false
+```
+
+---
+
+## Migration
+
+Charm v0.11 introduces several breaking changes, so this section should help you migrate from an older version.
+
+For reference, a signal is a state container with a separated getter and setter function. Atoms are a signal with a single function for getting and setting the state.
+
+**What to look out for:**
+
+1. Address all of the type errors introduced in your project after updating Charm. Most of them are caused by changes like:
+ - `peek()` was changed to `untracked()` for parity with other state managers
+ - The second arguments of `atom()` changed from an `options` table to an equality function
+ - Removed the second argument of `computed()` (you can do your own equality checks since the computed callback now receives the previous value)
+ - Removed the cleanup argument in effect callbacks (`effect(function(cleanup) end)`)
+
+2. If you use Charm Sync, you'll have to rewrite a lot of your sync code. Fortunately, most of the changes should make your code _less_ complicated:
+ - You can now sync signals per-client, including computed signals. You shouldn't have to modify sync payloads to filter data anymore.
+ - Instead of creating client/server syncers, these modules now act like singletons. Sync APIs are called directly through `CharmSync.client`/`server`.
+ - [Read the updated docs for syncing state →](#client-server-sync)
+
+3. The [`strict` and `frozen` flags](#flags) are automatically enabled in Roblox Studio, so unsafe Charm code will start throwing errors. The flags have the following behaviors:
+ - `strict`: Yielding in effects, signals, and other critical Charm functions will throw an error
+ - `frozen`: Tables passed to signals are deeply frozen to strictly enforce data immutability and prevent accidental mutations
+
+4. Nested effects automatically clean up when the parent effect re-runs or gets disposed. In other words, all effects created during the execution of another effect will be added as a "child" and clean up with the parent effect. This might cause issues in code that relied on the old behavior, where effects were detached from the parent.
+ - This feature applies to the observer function in `observe()`, which also automatically cleans up inner effects to prevent memory leaks. However, this does not apply to `subscribe()` or `listen()`.
+ - Effects that should not be tracked by a parent effect/scope should be wrapped in [`untracked()`](#untrackedcallback).
+ - This feature can introduce runtime bugs in migrated code. If you suspect this to be the cause, to help identify the issue, you can temporarily disable this feature by setting [`flags.trackInnerEffects`](#flags) to `false`.
+
+5. Consider refactoring your code to use some new quality-of-life features. Many of these are made possible thanks to [alien-signals](https://github.com/stackblitz/alien-signals)!
+ - [`signal()`](#signalinitialvalue-equals): make atom reads and writes more explicit
+ - [`listen()`](#listengetter-callback): create a subscription that also runs once immediately
+ - [`effectScope()`](#effectscopecallback): collect and clean up multiple effects at once
+ - [`onCleanup()`](#oncleanupcallback-failsilently): bind a cleanup function to the active effect or scope
+ - [`trigger()`](#triggercallback): trigger updates for table mutations
+
+---
+
+## Examples
+
+- https://github.com/littensy/fishing-minigame: Fisch clone using Charm for server and client state
+
+### React Counter
+
+```luau
+local Charm = require("@packages/charm")
+local ReactCharm = require("@packages/react-charm")
+local React = require("@packages/react")
+
+local getCounter, setCounter = Charm.signal(0)
local function Counter()
- local count = useAtom(counterAtom)
+ local count = ReactCharm.useSignalState(getCounter)
return React.createElement("TextButton", {
- [React.Event.Activated] = incrementCounter,
+ [React.Event.Activated] = function()
+ setCounter(count + 1)
+ end,
Text = `Count: {count}`,
- Size = UDim2.new(0, 100, 0, 50),
+ Size = UDim2.fromOffset(100, 50),
})
end
```
-### Vide component
+### Vide Counter
```luau
-local counter = require(script.Parent.counter)
-local counterAtom = counter.counterAtom
-local incrementCounter = counter.incrementCounter
+local Charm = require("@packages/charm")
+local VideCharm = require("@packages/vide-charm")
+local Vide = require("@packages/vide")
+
+local create = Vide.create
+
+local getCounter, setCounter = Charm.signal(0)
local function Counter()
- local count = useAtom(counterAtom)
+ local count = VideCharm.useSignalState(getCounter)
return create "TextButton" {
- Activated = incrementCounter,
+ Activated = function()
+ setCounter(function(count)
+ return count + 1
+ end)
+ end,
Text = function()
return `Count: {count()}`
end,
- Size = UDim2.new(0, 100, 0, 50),
+ Size = UDim2.fromOffset(100, 50),
}
end
```
-### Server-client sync
-
-Charm is designed for both client and server use, but there are often cases where the client needs to reference state that lives on the server. The CharmSync package provides a way to synchronize atoms between the server and its clients using remote events.
-
-Start by creating a set of atoms to sync between the server and clients. Export these atoms from a module to be shared between the server and client:
-
-```luau
--- atoms.luau
-local counter = require(script.Parent.counter)
-local todos = require(script.Parent.todos)
-
-return {
- counterAtom = counter.counterAtom,
- todosAtom = todos.todosAtom,
-}
-```
-
-Then, on the server, create a server sync object and pass in the atoms to sync. Use remote events to broadcast state updates and send initial state to clients upon request.
-
-> [!NOTE]
-> If `preserveHistory` is `true`, the server will send multiple payloads to the client, so the callback passed to `connect` should accept a variadic `...payloads` parameter. Otherwise, you only need to handle a single `payload` parameter.
-
-```luau
--- sync.server.luau
-local atoms = require(script.Parent.atoms)
-
-local syncer = CharmSync.server({ atoms = atoms })
-
--- Sends state updates to clients when a synced atom changes.
--- Omitting sensitive information and data serialization can be done here.
-syncer:connect(function(player, payload)
- remotes.syncState.fire(player, payload)
-end)
-
--- Sends the initial state to a player upon request. This should fire when a
--- player joins the game.
-remotes.requestState:connect(function(player)
- syncer:hydrate(player)
-end)
-```
-
-Finally, on the client, create a client sync object and use it to apply incoming state changes.
-
-```luau
--- sync.client.luau
-local atoms = require(script.Parent.atoms)
-
-local syncer = CharmSync.client({ atoms = atoms })
-
--- Applies state updates from the server to the client's atoms.
-remotes.syncState:connect(function(payload)
- syncer:sync(payload)
-end)
-
--- Requests the initial state from the server when the client joins the game.
--- Before this runs, the client uses the atoms' default values.
-remotes.requestState:fire()
-```
-
---
-Charm is released under the MIT License.
+Charm is released under the MIT License.
-[](LICENSE.md)
+[](LICENSE)
diff --git a/images/logo.png b/assets/logo.png
similarity index 100%
rename from images/logo.png
rename to assets/logo.png
diff --git a/benches/bench.luau b/benches/bench.luau
new file mode 100644
index 0000000..ffb839d
--- /dev/null
+++ b/benches/bench.luau
@@ -0,0 +1,48 @@
+local profiles: { [string]: number } = {}
+local times: { [string]: number } = {}
+local stack: { string } = {}
+
+local function run(name: string, test: () -> ())
+ local start = os.clock()
+ local iter = 0
+
+ table.clear(profiles)
+ table.clear(times)
+ table.clear(stack)
+
+ while os.clock() - start < 2 do
+ iter += 1
+ test()
+ end
+
+ local elapsed = os.clock() - start
+ local results = {}
+
+ table.insert(results, `{name}: \t{string.format("%.2f", elapsed / iter * 1000 * 1000)} us/iter`)
+
+ for name, duration in times do
+ table.insert(results, `{name}: \t{string.format("%.2f", duration / iter * 1000 * 1000)} us/iter`)
+ end
+
+ print(table.concat(results, " | "))
+end
+
+local function profilebegin(name: string)
+ profiles[name] = os.clock()
+ table.insert(stack, name)
+end
+
+local function profileend()
+ local name = table.remove(stack)
+
+ if name then
+ times[name] = (times[name] or 0) + os.clock() - profiles[name]
+ profiles[name] = nil
+ end
+end
+
+return {
+ run = run,
+ profilebegin = profilebegin,
+ profileend = profileend,
+}
diff --git a/benches/mapped.luau b/benches/mapped.luau
new file mode 100644
index 0000000..695b7f5
--- /dev/null
+++ b/benches/mapped.luau
@@ -0,0 +1,84 @@
+local Charm = require("../packages/charm/src")
+local bench = require("./bench")
+
+local computed = Charm.computed
+local mapped = Charm.mapped
+local signal = Charm.signal
+
+type Key = number
+type Value = number
+
+local count = 512
+local initialState: { [Key]: Value } = table.create(count, 0)
+
+local function expensiveOperation()
+ for _ = 1, 100 do
+ continue
+ end
+end
+
+local function simpleMapped(
+ source: () -> { [Key]: Value },
+ mapper: (Value, Key) -> (Value, Key?)
+): () -> { [Key]: Value }
+ return computed(function()
+ local state = source()
+ local nextState = {}
+
+ for keyIn, valueIn in state do
+ local valueOut, keyOut = mapper(valueIn, keyIn)
+ nextState[if keyOut == nil then keyIn else keyOut] = valueOut
+ end
+
+ return nextState
+ end)
+end
+
+local function updateRandomly(updateChance: number): ({ [Key]: Value }) -> { [Key]: Value }
+ return function(state)
+ local nextState = table.clone(state)
+ for key in state do
+ if math.random() < updateChance then
+ nextState[key] += 1
+ end
+ end
+ return nextState
+ end
+end
+
+for _, updateChance in { 0.1, 0.5, 1 } do
+ local function mapper(value: Value, key: Key): (Value, Key?)
+ expensiveOperation()
+ return value + 1, key + 1
+ end
+
+ bench.run(`mapped {updateChance}`, function()
+ local src, setSrc = signal(initialState)
+ local out = mapped(src, mapper)
+
+ bench.profilebegin("init")
+ out()
+ bench.profileend()
+
+ setSrc(updateRandomly(updateChance))
+
+ bench.profilebegin("update")
+ out()
+ bench.profileend()
+ end)
+
+ bench.run(`control {updateChance}`, function()
+ local src, setSrc = signal(initialState)
+ local out = simpleMapped(src, mapper)
+
+ bench.profilebegin("init")
+ out()
+ bench.profileend()
+
+ setSrc(updateRandomly(updateChance))
+
+ bench.profilebegin("update")
+ out()
+ bench.profileend()
+ end)
+end
diff --git a/benches/multi.luau b/benches/multi.luau
new file mode 100644
index 0000000..6c8235c
--- /dev/null
+++ b/benches/multi.luau
@@ -0,0 +1,188 @@
+local Charm = require("../packages/charm/src")
+local bench = require("./bench")
+
+type Atom = Charm.Atom
+
+local signal = Charm.atom
+local computed = Charm.computed
+local effect = Charm.effect
+
+local function busy(): number
+ local a = 0
+ for i = 1, 100 do
+ a += 1
+ end
+ return a
+end
+
+do
+ local head = signal(0)
+ local computed1 = computed(function()
+ return head()
+ end)
+ local computed2 = computed(function()
+ computed1()
+ return 0
+ end)
+ local computed3 = computed(function()
+ busy() -- heavy computation
+ return computed2() + 1
+ end)
+ local computed4 = computed(function()
+ return computed3() + 2
+ end)
+ local computed5 = computed(function()
+ return computed4() + 3
+ end)
+ effect(function()
+ computed5()
+ busy() -- heavy side effect
+ end)
+
+ bench.run("avoidablePropagation", function()
+ head(1)
+ assert(computed5() == 6)
+ for i = 0, 999 do
+ head(i)
+ end
+ assert(computed5() == 6)
+ end)
+end
+
+do
+ local head = signal(0)
+ local double = computed(function()
+ return head() * 2
+ end)
+ local inverse = computed(function()
+ return -head()
+ end)
+ local current = computed(function()
+ local result = 0
+ for i = 0, 19 do
+ result += if head() % 2 == 1 then double() else inverse()
+ end
+ return result
+ end)
+
+ local callCounter = 0
+ effect(function()
+ current()
+ callCounter += 1
+ end)
+
+ bench.run("unstable", function()
+ head(1)
+ assert(current() == 40)
+ callCounter = 0
+ for i = 0, 99 do
+ head(i)
+ end
+ assert(callCounter == 100)
+ end)
+end
+
+do
+ local head = signal(0)
+ local double = computed(function()
+ return head() * 2
+ end)
+ local inverse = computed(function()
+ return -head()
+ end)
+ local current = computed(function()
+ local result = 0
+ for i = 0, 19 do
+ result += if head() % 2 == 1 and i % 2 == 1 then double() else inverse()
+ end
+ return result
+ end)
+
+ local callCounter = 0
+ effect(function()
+ current()
+ callCounter += 1
+ end)
+
+ bench.run("unstable 50%", function()
+ head(1)
+ assert(current() == 10)
+ callCounter = 0
+ for i = 0, 99 do
+ head(i)
+ end
+ assert(callCounter == 100)
+ end)
+end
+
+do
+ local head = signal(0)
+ local last: () -> number = head
+ local callCounter = 0
+
+ for i = 0, 49 do
+ local current = computed(function()
+ return head() + i
+ end)
+ local current2 = computed(function()
+ return current() + 1
+ end)
+ effect(function()
+ current2()
+ callCounter += 1
+ end)
+ last = current2
+ end
+
+ bench.run("broadPropagation", function()
+ head(1)
+ callCounter = 0
+ for i = 0, 49 do
+ head(i)
+ assert(last() == i + 50)
+ end
+ assert(callCounter == 50 * 50)
+ end)
+end
+
+do
+ local heads = {}
+ for index = 0, 99 do
+ heads[index] = signal(0)
+ end
+
+ local mux = computed(function()
+ local result = {}
+ for index = 0, 99 do
+ result[index] = heads[index]()
+ end
+ return result
+ end)
+
+ local splited = {}
+ for index = 0, 99 do
+ local x = computed(function()
+ return mux()[index]
+ end)
+ splited[index] = computed(function()
+ return x() + 1
+ end)
+ end
+
+ for _, x in splited do
+ effect(function()
+ x()
+ end)
+ end
+
+ bench.run("mux", function()
+ for i = 0, 9 do
+ heads[i](i)
+ assert(splited[i]() == i + 1)
+ end
+ for i = 0, 9 do
+ heads[i](i * 2)
+ assert(splited[i]() == i * 2 + 1)
+ end
+ end)
+end
diff --git a/benches/propagate.luau b/benches/propagate.luau
new file mode 100644
index 0000000..463ee2e
--- /dev/null
+++ b/benches/propagate.luau
@@ -0,0 +1,47 @@
+local Charm = require("../packages/charm/src")
+local bench = require("./bench")
+
+local computed = Charm.computed
+local effect = Charm.effect
+local listen = Charm.listen
+local signal = Charm.signal
+
+for _, w in { 10, 100 } do
+ for _, h in { 10, 100 } do
+ bench.run(`propagate {w}*{h}`, function()
+ local src, setSrc = signal(1)
+ for _ = 1, w do
+ local last = src
+ for _ = 1, h do
+ local prev = last
+ last = computed(function()
+ return prev() + 1
+ end)
+ end
+ effect(function()
+ last()
+ end)
+ end
+ setSrc(src() + 1)
+ end)
+ end
+end
+
+for _, w in { 10, 100 } do
+ for _, h in { 10, 100 } do
+ bench.run(`propagate (listen) {w}*{h}`, function()
+ local src, setSrc = signal(1)
+ for _ = 1, w do
+ local last = src
+ for _ = 1, h do
+ local prev = last
+ last = computed(function()
+ return prev() + 1
+ end)
+ end
+ listen(last, function() end)
+ end
+ setSrc(src() + 1)
+ end)
+ end
+end
diff --git a/benches/reactive.luau b/benches/reactive.luau
new file mode 100644
index 0000000..c272651
--- /dev/null
+++ b/benches/reactive.luau
@@ -0,0 +1,88 @@
+local Charm = require("../packages/charm/src")
+local DeepCharm = require("../packages/deep-charm/src")
+local bench = require("./bench")
+
+local effect = Charm.effect
+local reactive = DeepCharm.reactive
+
+do
+ local value = reactive({ a = 1 })
+ bench.run("track reactive property", function()
+ effect(function()
+ local _ = value.a
+ end)
+ end)
+end
+
+do
+ local value = reactive({ a = 1 })
+ bench.run("write reactive property", function()
+ value.a += 1
+ end)
+end
+
+for _, amount in { 10, 100, 1000 } do
+ print("---")
+
+ do
+ local rawArray = table.create(amount, 1)
+ local array = reactive(rawArray)
+
+ bench.run(`track for loop, {amount} elements`, function()
+ local sum = 0
+ effect(function()
+ for i = 1, #array do
+ sum += array[i]
+ end
+ end)
+ end)
+ end
+
+ do
+ local rawArray = table.create(amount, 1)
+ local array = reactive(rawArray)
+
+ bench.run(`track iteration, {amount} elements`, function()
+ local sum = 0
+ effect(function()
+ for _, x in array do
+ sum += x
+ end
+ end)
+ end)
+ end
+
+ do
+ local rawArray = table.create(amount, 1)
+ local array = reactive(rawArray)
+
+ effect(function()
+ local sum = 0
+ for _, x in array do
+ sum += x
+ end
+ end)
+
+ bench.run(`track index mutation (1st only), {amount} elements`, function()
+ array[1] += 1
+ end)
+ end
+
+ do
+ local rawArray = table.create(amount, 1)
+ local array = reactive(rawArray)
+
+ effect(function()
+ local sum = 0
+ for _, x in array do
+ sum += x
+ end
+ end)
+
+ bench.run(`track index mutation (all), {amount} elements`, function()
+ for i = 1, #array do
+ array[i] += 1
+ end
+ end)
+ end
+end
diff --git a/bin/build.sh b/bin/build.sh
deleted file mode 100644
index 547e4e7..0000000
--- a/bin/build.sh
+++ /dev/null
@@ -1,5 +0,0 @@
-mkdir build
-rojo build packages/charm -o build/charm.rbxm
-rojo build packages/charm-sync -o build/charm-sync.rbxm
-rojo build packages/react -o build/react-charm.rbxm
-rojo build packages/vide -o build/vide-charm.rbxm
diff --git a/bin/check.sh b/bin/check.sh
deleted file mode 100644
index 0e948f8..0000000
--- a/bin/check.sh
+++ /dev/null
@@ -1,29 +0,0 @@
-curl -o bin/roblox.d.luau https://raw.githubusercontent.com/JohnnyMorganz/luau-lsp/main/scripts/globalTypes.d.lua
-
-rojo sourcemap -o sourcemap.json
-
-check() {
- echo "Checking $1"
-
- luau-lsp analyze \
- --defs=bin/roblox.d.luau \
- --defs=bin/testez.d.luau \
- --flag:LuauFixIndexerSubtypingOrdering=true \
- --flag:LuauInstantiateInSubtyping=true \
- --sourcemap=sourcemap.json \
- --ignore="**/_Index/**" \
- $1
-
- selene $1
- stylua --check $1
-}
-
-eslint packages
-
-for i in packages/*/src; do
- check $i
-done
-
-check tests
-
-rm bin/roblox.d.luau
diff --git a/bin/install.sh b/bin/install.sh
deleted file mode 100644
index 53ccca2..0000000
--- a/bin/install.sh
+++ /dev/null
@@ -1,4 +0,0 @@
-cd modules
-wally install
-cd ..
-rojo sourcemap --output sourcemap.json
diff --git a/bin/testez.d.luau b/bin/testez.d.luau
deleted file mode 100644
index 28bef92..0000000
--- a/bin/testez.d.luau
+++ /dev/null
@@ -1,24 +0,0 @@
-declare function afterAll(callback: () -> ()): ()
-declare function afterEach(callback: () -> ()): ()
-
-declare function beforeAll(callback: () -> ()): ()
-declare function beforeEach(callback: () -> ()): ()
-
-declare function describe(phrase: string, callback: () -> ()): ()
-declare function describeFOCUS(phrase: string, callback: () -> ()): ()
-declare function fdescribe(phrase: string, callback: () -> ()): ()
-declare function describeSKIP(phrase: string, callback: () -> ()): ()
-declare function xdescribe(phrase: string, callback: () -> ()): ()
-
-declare function expect(value: any): any
-
-declare function FIXME(optionalMessage: string?): ()
-declare function FOCUS(): ()
-declare function SKIP(): ()
-
-declare function it(phrase: string, callback: () -> ()): ()
-declare function itFOCUS(phrase: string, callback: () -> ()): ()
-declare function fit(phrase: string, callback: () -> ()): ()
-declare function itSKIP(phrase: string, callback: () -> ()): ()
-declare function xit(phrase: string, callback: () -> ()): ()
-declare function itFIXME(phrase: string, callback: () -> ()): ()
diff --git a/build.project.json b/build.project.json
new file mode 100644
index 0000000..53d32b4
--- /dev/null
+++ b/build.project.json
@@ -0,0 +1,11 @@
+{
+ "name": "Packages",
+ "tree": {
+ "$className": "Folder",
+ "Charm": { "$path": "packages/charm/src" },
+ "CharmSync": { "$path": "packages/charm-sync/src" },
+ "DeepCharm": { "$path": "packages/deep-charm/src" },
+ "ReactCharm": { "$path": "packages/react-charm/src" },
+ "VideCharm": { "$path": "packages/vide-charm/src" }
+ }
+}
diff --git a/default.project.json b/default.project.json
index a43197e..85d8f55 100644
--- a/default.project.json
+++ b/default.project.json
@@ -1,28 +1,59 @@
{
"name": "charm-test",
+ "globIgnorePaths": [
+ "**/default.project.json",
+ "node_modules/charm",
+ "node_modules/charm-sync",
+ "node_modules/react-charm",
+ "node_modules/vide-charm"
+ ],
+ "emitLegacyScripts": false,
"tree": {
"$className": "DataModel",
"ReplicatedStorage": {
"$className": "ReplicatedStorage",
- "DevPackages": {
- "$path": "modules/DevPackages",
- "Charm": {
- "$path": "packages/charm"
+ "packages": {
+ "$className": "Folder",
+ "vide": {
+ "$path": "node_modules/@rbxts/vide"
},
- "CharmSync": {
- "$path": "packages/charm-sync"
+ "charm": {
+ "$className": "Folder",
+ "src": { "$path": "packages/charm/src" },
+ "test": { "$path": "packages/charm/test" }
},
- "ReactCharm": {
- "$path": "packages/react"
+ "deep-charm": {
+ "$className": "Folder",
+ "src": { "$path": "packages/deep-charm/src" },
+ "test": { "$path": "packages/deep-charm/test" },
+ "Charm": { "$path": "packages/deep-charm/Charm.luau" }
},
- "VideCharm": {
- "$path": "packages/vide"
+ "charm-sync": {
+ "$className": "Folder",
+ "src": { "$path": "packages/charm-sync/src" },
+ "test": { "$path": "packages/charm-sync/test" },
+ "Charm": { "$path": "packages/charm-sync/Charm.luau" }
+ },
+ "react-charm": {
+ "$className": "Folder",
+ "src": { "$path": "packages/react-charm/src" },
+ "test": { "$path": "packages/react-charm/test" },
+ "React": { "$path": "packages/react-charm/React.luau" },
+ "ReactRoblox": { "$path": "packages/react-charm/ReactRoblox.luau" },
+ "Charm": { "$path": "packages/react-charm/Charm.luau" }
+ },
+ "vide-charm": {
+ "$className": "Folder",
+ "src": { "$path": "packages/vide-charm/src" },
+ "test": { "$path": "packages/vide-charm/test" },
+ "Vide": { "$path": "packages/vide-charm/Vide.luau" },
+ "Charm": { "$path": "packages/vide-charm/Charm.luau" }
}
- }
- },
- "ServerScriptService": {
- "$className": "ServerScriptService",
- "Tests": {
+ },
+ "@rbxts-js": {
+ "$path": "node_modules/@rbxts-js"
+ },
+ "tests": {
"$path": "tests"
}
}
diff --git a/eslint.config.js b/eslint.config.js
new file mode 100644
index 0000000..bfccfb4
--- /dev/null
+++ b/eslint.config.js
@@ -0,0 +1,20 @@
+const eslint = require("@eslint/js");
+const { defineConfig, globalIgnores } = require("eslint/config");
+const prettierRecommended = require("eslint-plugin-prettier/recommended");
+const roblox = require("eslint-plugin-roblox-ts");
+const tseslint = require("typescript-eslint");
+
+module.exports = defineConfig(
+ eslint.configs.recommended,
+ tseslint.configs.recommended,
+ roblox.configs.tsRecommendedCompat,
+ roblox.configs.recommended,
+ {
+ rules: {
+ "@typescript-eslint/no-explicit-any": "off",
+ "roblox-ts/no-any": "off",
+ },
+ },
+ globalIgnores(["out/**", "eslint.config.js"]),
+ prettierRecommended,
+);
diff --git a/modules/wally.lock b/modules/wally.lock
deleted file mode 100644
index 7294d77..0000000
--- a/modules/wally.lock
+++ /dev/null
@@ -1,103 +0,0 @@
-# This file is automatically @generated by Wally.
-# It is not intended for manual editing.
-registry = "test"
-
-[[package]]
-name = "centau/vide"
-version = "0.3.1"
-dependencies = []
-
-[[package]]
-name = "jsdotlua/boolean"
-version = "1.2.7"
-dependencies = [["number", "jsdotlua/number@1.2.7"]]
-
-[[package]]
-name = "jsdotlua/collections"
-version = "1.2.7"
-dependencies = [["es7-types", "jsdotlua/es7-types@1.2.7"], ["instance-of", "jsdotlua/instance-of@1.2.7"]]
-
-[[package]]
-name = "jsdotlua/console"
-version = "1.2.7"
-dependencies = [["collections", "jsdotlua/collections@1.2.7"]]
-
-[[package]]
-name = "jsdotlua/es7-types"
-version = "1.2.7"
-dependencies = []
-
-[[package]]
-name = "jsdotlua/instance-of"
-version = "1.2.7"
-dependencies = []
-
-[[package]]
-name = "jsdotlua/luau-polyfill"
-version = "1.2.7"
-dependencies = [["boolean", "jsdotlua/boolean@1.2.7"], ["collections", "jsdotlua/collections@1.2.7"], ["console", "jsdotlua/console@1.2.7"], ["es7-types", "jsdotlua/es7-types@1.2.7"], ["instance-of", "jsdotlua/instance-of@1.2.7"], ["math", "jsdotlua/math@1.2.7"], ["number", "jsdotlua/number@1.2.7"], ["string", "jsdotlua/string@1.2.7"], ["symbol-luau", "jsdotlua/symbol-luau@1.0.1"], ["timers", "jsdotlua/timers@1.2.7"]]
-
-[[package]]
-name = "jsdotlua/math"
-version = "1.2.7"
-dependencies = []
-
-[[package]]
-name = "jsdotlua/number"
-version = "1.2.7"
-dependencies = []
-
-[[package]]
-name = "jsdotlua/promise"
-version = "3.5.2"
-dependencies = []
-
-[[package]]
-name = "jsdotlua/react"
-version = "17.2.1"
-dependencies = [["luau-polyfill", "jsdotlua/luau-polyfill@1.2.7"], ["shared", "jsdotlua/shared@17.2.1"]]
-
-[[package]]
-name = "jsdotlua/react-reconciler"
-version = "17.2.1"
-dependencies = [["luau-polyfill", "jsdotlua/luau-polyfill@1.2.7"], ["promise", "jsdotlua/promise@3.5.2"], ["react", "jsdotlua/react@17.2.1"], ["scheduler", "jsdotlua/scheduler@17.2.1"], ["shared", "jsdotlua/shared@17.2.1"]]
-
-[[package]]
-name = "jsdotlua/react-roblox"
-version = "17.2.1"
-dependencies = [["luau-polyfill", "jsdotlua/luau-polyfill@1.2.7"], ["react", "jsdotlua/react@17.2.1"], ["react-reconciler", "jsdotlua/react-reconciler@17.2.1"], ["scheduler", "jsdotlua/scheduler@17.2.1"], ["shared", "jsdotlua/shared@17.2.1"]]
-
-[[package]]
-name = "jsdotlua/scheduler"
-version = "17.2.1"
-dependencies = [["luau-polyfill", "jsdotlua/luau-polyfill@1.2.7"], ["shared", "jsdotlua/shared@17.2.1"]]
-
-[[package]]
-name = "jsdotlua/shared"
-version = "17.2.1"
-dependencies = [["luau-polyfill", "jsdotlua/luau-polyfill@1.2.7"]]
-
-[[package]]
-name = "jsdotlua/string"
-version = "1.2.7"
-dependencies = [["es7-types", "jsdotlua/es7-types@1.2.7"], ["number", "jsdotlua/number@1.2.7"]]
-
-[[package]]
-name = "jsdotlua/symbol-luau"
-version = "1.0.1"
-dependencies = []
-
-[[package]]
-name = "jsdotlua/timers"
-version = "1.2.7"
-dependencies = [["collections", "jsdotlua/collections@1.2.7"]]
-
-[[package]]
-name = "littensy/charm"
-version = "0.1.0"
-dependencies = [["React", "jsdotlua/react@17.2.1"], ["ReactRoblox", "jsdotlua/react-roblox@17.2.1"], ["TestEZ", "roblox/testez@0.4.1"], ["Vide", "centau/vide@0.3.1"]]
-
-[[package]]
-name = "roblox/testez"
-version = "0.4.1"
-dependencies = []
diff --git a/modules/wally.toml b/modules/wally.toml
deleted file mode 100644
index 096ab3f..0000000
--- a/modules/wally.toml
+++ /dev/null
@@ -1,11 +0,0 @@
-[package]
-name = "littensy/charm"
-version = "0.1.0"
-registry = "https://github.com/UpliftGames/wally-index"
-realm = "shared"
-
-[dev-dependencies]
-React = "jsdotlua/react@17.2.0"
-ReactRoblox = "jsdotlua/react-roblox@17.2.0"
-TestEZ = "roblox/testez@0.4.1"
-Vide = "centau/vide@0.3.1"
diff --git a/package.json b/package.json
index 73e2375..d5f8c09 100644
--- a/package.json
+++ b/package.json
@@ -1,24 +1,29 @@
{
"private": true,
"scripts": {
- "build": "sh bin/build.sh",
- "check": "sh bin/check.sh",
- "wally": "sh bin/install.sh",
+ "test": "lute run test",
+ "build": "lute run build",
+ "check": "lute run check",
"publish:npm": "pnpm publish -r --access public",
"publish:wally": "pnpm -r --workspace-concurrency 1 publish:wally"
},
+ "packageManager": "pnpm@10.33.3",
"devDependencies": {
+ "@eslint/js": "^9.39.4",
"@rbxts/compiler-types": "3.0.0-types.0",
- "@rbxts/types": "^1.0.811",
- "@typescript-eslint/eslint-plugin": "^7.18.0",
- "@typescript-eslint/parser": "^7.18.0",
- "eslint": "^8.57.1",
- "eslint-config-prettier": "^9.1.0",
- "eslint-plugin-prettier": "^5.2.1",
- "eslint-plugin-roblox-ts": "^0.0.36",
- "prettier": "^3.3.3",
+ "@rbxts/react": "17.3.7-ts.1",
+ "@rbxts/react-roblox": "17.3.7-ts.1",
+ "@rbxts/types": "^1.0.913",
+ "@rbxts/vide": "^0.6.1",
+ "@typescript-eslint/eslint-plugin": "^8.58.0",
+ "@typescript-eslint/parser": "^8.58.0",
+ "eslint": "^9.39.4",
+ "eslint-config-prettier": "^10.1.8",
+ "eslint-plugin-prettier": "^5.5.5",
+ "eslint-plugin-roblox-ts": "^1.4.0",
+ "prettier": "^3.8.1",
"roblox-ts": "^3.0.0",
- "typescript": "~5.5.4"
- },
- "packageManager": "pnpm@9.15.4"
+ "typescript": "^5.9.3",
+ "typescript-eslint": "^8.58.0"
+ }
}
diff --git a/packages/charm-sync/Charm.luau b/packages/charm-sync/Charm.luau
new file mode 100644
index 0000000..480f2d5
--- /dev/null
+++ b/packages/charm-sync/Charm.luau
@@ -0,0 +1,9 @@
+if not script then
+ return require("../charm/src")
+end
+
+local Charm = require(script.Parent.Parent.charm.src)
+
+export type Atom = Charm.Atom
+
+return Charm
diff --git a/packages/charm-sync/default.project.json b/packages/charm-sync/default.project.json
index 5115ff0..9c44c7c 100644
--- a/packages/charm-sync/default.project.json
+++ b/packages/charm-sync/default.project.json
@@ -1,5 +1,5 @@
{
- "name": "CharmSync",
+ "name": "charm-sync",
"tree": {
"$path": "src"
}
diff --git a/packages/charm-sync/package.json b/packages/charm-sync/package.json
index 5256c2d..3efa745 100644
--- a/packages/charm-sync/package.json
+++ b/packages/charm-sync/package.json
@@ -1,16 +1,27 @@
{
"name": "@rbxts/charm-sync",
- "version": "0.3.0",
- "description": "Sync server state with clients using Charm",
- "repository": "littensy/charm",
+ "version": "0.4.0-rc.7",
+ "description": "Simple server-to-client sync for Charm",
"license": "MIT",
"main": "src/init.luau",
"types": "src/index.d.ts",
+ "files": [
+ "src",
+ "*.luau",
+ "!wally.toml",
+ "!wally.lock",
+ "!default.project.json",
+ "!test"
+ ],
+ "repository": {
+ "type": "git",
+ "url": "git+https://github.com/littensy/charm.git"
+ },
"scripts": {
- "prepare": "cp ../../LICENSE.md . && cp ../../README.md .",
+ "prepare": "cp ../../LICENSE . && cp ../../README.md .",
"publish:wally": "wally publish"
},
- "peerDependencies": {
- "@rbxts/charm": "workspace:*"
+ "dependencies": {
+ "@rbxts/charm": "workspace:^"
}
}
diff --git a/packages/charm-sync/src/client.luau b/packages/charm-sync/src/client.luau
index 8f3cfb1..a514b75 100644
--- a/packages/charm-sync/src/client.luau
+++ b/packages/charm-sync/src/client.luau
@@ -1,83 +1,108 @@
-local Charm = require(script.Parent.Parent.Charm)
-local patch = require(script.Parent.patch)
-local types = require(script.Parent.types)
-type AtomMap = types.AtomMap
+local Charm = require("../Charm")
+local patch = require("./patch")
+local types = require("./types")
+
type SyncPayload = types.SyncPayload
+type ReactiveProxy = types.ReactiveProxy
-type ClientOptions = {
- --[=[
- The atoms to synchronize with the server.
- ]=]
- atoms: AtomMap,
- --[=[
- Whether to ignore patches sent before the client has been hydrated.
- Default is `true`.
- ]=]
- ignoreUnhydrated: boolean?,
-}
+local trackedSignals: { [string]: (any) -> any } = {}
+local trackedReactives: { [string]: ReactiveProxy } = {}
-type ClientSyncer = {
- --[=[
- Applies a patch or initializes the state of the atoms with the given
- payload from the server.
-
- @param ...payloads The patches or hydration payloads to apply.
- ]=]
- sync: (self: ClientSyncer, ...SyncPayload) -> (),
-}
-
---[=[
- Creates a `ClientSyncer` object that receives patches from the server and
- applies them to the local state.
-
- @param options The atoms to synchronize with the server.
- @return A `ClientSyncer` object.
-]=]
-local function client(options: ClientOptions): ClientSyncer
- local atoms = options.atoms
- local ignoreUnhydrated = options.ignoreUnhydrated ~= false
-
- local self = {} :: ClientSyncer
- local hydrated = false
-
- local function hydrate(state: { [string | number]: any })
- hydrated = true
-
- for key, value in next, state do
- local atom = atoms[key]
- atom(value)
- end
+local function shallowMergeReactive(reactive: ReactiveProxy, source: { [any]: any })
+ local target = reactive.__target
+ for key, value in source do
+ target[key] = value
end
-
- local function apply(data: { [string | number]: any })
- local target = {}
-
- for key, atom in next, atoms do
- target[key] = atom()
- end
-
- target = patch.apply(target, data)
-
- for key, atom in next, atoms do
- atom(target[key])
- end
- end
-
- function self:sync(...)
- for index = 1, select("#", ...) do
- local payload: SyncPayload = select(index, ...)
-
- Charm.batch(function()
- if payload.type == "init" then
- hydrate(payload.data)
- elseif not ignoreUnhydrated or hydrated then
- apply(payload.data)
- end
- end)
- end
- end
-
- return self
+ Charm.trigger(reactive.__track)
end
-return client
+--[[
+ Registers a map of setters to the state with the corresponding keys on the
+ server. When the server sends updates, these setters will be called with
+ the new values.
+
+ Atoms, signals converted to atoms, and signal setter functions are allowed.
+ Note that `server.addSignalsToClient` still requires some way to get the
+ state.
+
+ @param signals A map of setter functions to sync with the server.
+]]
+local function addSignals(signals: { [string]: ((any) -> any) | {} })
+ for key, signal in signals do
+ if type(signal) == "function" then
+ trackedSignals[key] = signal
+ else
+ trackedReactives[key] = (signal :: any) :: ReactiveProxy
+ end
+ end
+end
+
+--[[
+ Unregisters from server state updates for the given keys. The signals
+ will retain their current values, but will no longer receive updates
+ from the server.
+]]
+local function removeSignals(...: string)
+ for index = 1, select("#", ...) do
+ local key = select(index, ...)
+ trackedSignals[key] = nil
+ trackedReactives[key] = nil
+ end
+end
+
+--[[
+ Unsubscribes from all server state updates.
+]]
+local function removeAllSignals()
+ table.clear(trackedSignals)
+ table.clear(trackedReactives)
+end
+
+local function applyInitPayload(data: { [string]: any })
+ for key, value in data do
+ if trackedSignals[key] then
+ trackedSignals[key](value)
+ elseif trackedReactives[key] then
+ shallowMergeReactive(trackedReactives[key], value)
+ end
+ end
+end
+
+local function applyPatchPayload(data: { [string]: any })
+ for key, diffs in data do
+ local trackedSignal = trackedSignals[key]
+ local trackedReactive = trackedReactives[key]
+
+ if trackedSignal then
+ trackedSignal(function(value)
+ return patch.apply(value, diffs)
+ end)
+ elseif trackedReactive then
+ patch.applyMutable(trackedReactive.__target, diffs)
+ Charm.trigger(trackedReactive.__track)
+ end
+ end
+end
+
+--[[
+ Merges the server's state patches into the client's state, either
+ initializing it with the full state or merging a partial patch.
+
+ @param payloads The state updates received from the server.
+]]
+local function applyPatches(payloads: { SyncPayload })
+ for _, payload in payloads do
+ if payload.type == "init" then
+ Charm.batch(applyInitPayload, payload.data)
+ elseif payload.type == "patch" then
+ Charm.batch(applyPatchPayload, payload.data)
+ end
+ end
+end
+
+return {
+ addSignals = addSignals,
+ removeSignals = removeSignals,
+ removeAllSignals = removeAllSignals,
+ patch = applyPatches,
+}
diff --git a/packages/charm-sync/src/config.luau b/packages/charm-sync/src/config.luau
new file mode 100644
index 0000000..c3ce27c
--- /dev/null
+++ b/packages/charm-sync/src/config.luau
@@ -0,0 +1,31 @@
+return {
+ --[[
+ The interval at which to send patches to the client, in seconds.
+ Defaults to `0` (patches are sent up to once per frame). Set to a
+ negative value to disable automatic syncing.
+ ]]
+ interval = 0,
+ --[[
+ Whether the history of state changes since the client's last update
+ should be preserved. This is useful for values that change multiple
+ times per frame, where each individual change is important. Defaults
+ to `false` for performance reasons.
+ ]]
+ preserveHistory = false,
+ --[[
+ When `true`, Charm will modify sparse arrays to prevent data loss from
+ remote event limitations. When an unsafe array is detected, numeric
+ indices will be converted to string keys so that the array can be sent
+ safely and then converted back to an array on the receiving end.
+
+ This option should be disabled if your network library uses a custom
+ serialization method (i.e. Zap, ByteNet) to prevent interference.
+ ]]
+ fixArrays = true,
+ --[[
+ When `true`, synced state containing unsafe sparse arrays or mixed
+ tables will emit a warning. Only checked in strict mode and if
+ `fixArrays` is enabled. Defaults to `true`.
+ ]]
+ validatePatches = true,
+}
diff --git a/packages/charm-sync/src/flatten.luau b/packages/charm-sync/src/flatten.luau
deleted file mode 100644
index 9c8d012..0000000
--- a/packages/charm-sync/src/flatten.luau
+++ /dev/null
@@ -1,28 +0,0 @@
-local types = require(script.Parent.types)
-type AtomMap = types.AtomMap
-
-type NestedAtomMap = {
- [string]: NestedAtomMap | () -> (),
-}
-
-local function flatten(atoms: NestedAtomMap): AtomMap
- local result: AtomMap = {}
-
- local function visit(node: NestedAtomMap, path: string)
- for key, value in node do
- local location = if path == "" then key else path .. "/" .. key
-
- if type(value) == "table" then
- visit(value, location)
- else
- result[location] = value
- end
- end
- end
-
- visit(atoms, "")
-
- return result
-end
-
-return flatten
diff --git a/packages/charm-sync/src/index.d.ts b/packages/charm-sync/src/index.d.ts
index 52ff658..bb40eba 100644
--- a/packages/charm-sync/src/index.d.ts
+++ b/packages/charm-sync/src/index.d.ts
@@ -1,214 +1,292 @@
-import { Atom, Selector } from "@rbxts/charm";
-
-export = CharmSync;
-export as namespace CharmSync;
-
-type Cleanup = () => void;
+import { Atom, Setter } from "@rbxts/charm";
type Key = string | number | symbol;
-declare namespace CharmSync {
- type AtomMap = Record>;
+/**
+ * Represents the removal of a value from the state.
+ */
+export interface None {
+ readonly __none: "__none";
+}
- type SelectorMap = Record>;
+export type MaybeNone = undefined extends T ? None : never;
+type ReactiveObject = { [key: Key]: any };
+
+type ServerSignalMap = Record any) | ReactiveObject>;
+
+type ClientSignalMap = Record | ReactiveObject>;
+
+/**
+ * Infers the type of the return values produced by a map of functions.
+ */
+export type StateOfMap = {
+ readonly [P in keyof T]: T[P] extends () => infer State ? State : never;
+};
+
+type DataTypes = {
+ [P in keyof CheckableTypes as P extends keyof CheckablePrimitives ? never : P]: CheckableTypes[P];
+};
+
+type DataType = DataTypes[keyof DataTypes];
+
+/**
+ * A partial patch that can be applied to the state to update it. Represents
+ * the difference between the current state and the next state.
+ *
+ * If a value was removed, it is replaced with `None`. This can be checked
+ * using the `isNone` function.
+ */
+// TODO: Optimize type, this looks overly complex
+export type SyncPatch = MaybeNone | State extends
+ | ReadonlyMap
+ | Map
+ ? ReadonlyMap | None>
+ : State extends Set | ReadonlySet
+ ? ReadonlyMap
+ : State extends readonly (infer T)[]
+ ? FixArrays extends true
+ ? ReadonlyMap | None>
+ : readonly (SyncPatch | None | undefined)[]
+ : State extends DataType
+ ? State
+ : State extends object
+ ? { readonly [P in keyof State]?: SyncPatch }
+ : State;
+
+/**
+ * A payload that can be sent from the server to the client to synchronize
+ * state between the two.
+ */
+export type SyncPayload =
+ | { type: "init"; data: StateOfMap }
+ | { type: "patch"; data: SyncPatch, FixArrays> };
+
+/**
+ * Global configuration options that affect the behavior of Charm Sync.
+ */
+export const config: {
/**
- * @deprecated Use `SelectorMap` instead.
- */
- type MoleculeMap = SelectorMap;
-
- /**
- * Infers the type of the return values produced by a map of functions.
- */
- type StateOfMap = {
- readonly [P in keyof T]: T[P] extends Selector ? State : never;
- };
-
- /**
- * Represents the removal of a value from the state.
- */
- interface None {
- readonly __none: "__none";
- }
-
- type MaybeNone = undefined extends T ? None : never;
-
- /**
- * Creates a `ClientSyncer` object that receives patches from the server and
- * applies them to the local state.
- *
- * @client
- * @param options The atoms to synchronize with the server.
- * @returns A `ClientSyncer` object.
- */
- function client(options: ClientOptions): ClientSyncer;
-
- /**
- * Creates a `ServerSyncer` object that sends patches to the client and
- * hydrates the client's state.
+ * The interval at which to send patches to the client, in seconds.
+ * Defaults to `0` (patches are sent up to once per frame). Set to a
+ * negative value to disable automatic syncing.
*
* @server
- * @param options The atoms to synchronize with the client.
- * @returns A `ServerSyncer` object.
*/
- function server(
- options: ServerOptions,
- ): ServerSyncer;
-
+ interval: number;
/**
- * Checks whether a value is `None`. If `true`, the value is scheduled to be
- * removed from the state when the patch is applied.
+ * Whether the history of state changes since the client's last update
+ * should be preserved. This is useful for values that change multiple
+ * times per frame, where each individual change is important. Defaults
+ * to `false` for performance reasons.
*
- * @param value The value to check.
- * @returns `true` if the value is `None`, otherwise `false`.
+ * @server
*/
- function isNone(value: unknown): value is None;
-
- interface NestedAtomMap {
- readonly [key: string]: NestedAtomMap | Atom;
- }
-
- type AppendPath = undefined extends Path
- ? Name
- : Name extends string
- ? Path extends string
- ? `${Path}/${Name}`
- : never
- : never;
-
- type UnionToIntersection = (U extends any ? (k: U) => void : never) extends (k: infer I) => void ? I : never;
-
- type IntersectValues = UnionToIntersection<{ readonly [K in keyof T]: T[K] }[keyof T]>;
-
- type FlattenNestedAtoms = IntersectValues<{
- readonly [Name in keyof Atoms as AppendPath]: Atoms[Name] extends Atom
- ? { readonly [K in AppendPath]: Atoms[Name] }
- : Atoms[Name] extends NestedAtomMap
- ? FlattenNestedAtoms>
- : never;
- }>;
-
+ preserveHistory: boolean;
/**
- * Flattens a nested atom map into a single object with slash-separated
- * keys. Useful for recursively collecting atoms returned by modules.
+ * When `true`, Charm will modify sparse arrays to prevent data loss from
+ * remote event limitations. When an unsafe array is detected, numeric
+ * indices will be converted to string keys so that the array can be sent
+ * safely and then converted back to an array on the receiving end.
*
- * @param atoms The nested atom map to flatten.
- * @return A flattened atom map.
- */
- function flatten(atoms: Atoms): FlattenNestedAtoms;
-
- type DataTypes = {
- [P in keyof CheckableTypes as P extends keyof CheckablePrimitives ? never : P]: CheckableTypes[P];
- };
-
- /**
- * A type that should not be made partial in patches.
- */
- type DataType = DataTypes[keyof DataTypes];
-
- /**
- * A partial patch that can be applied to the state to update it. Represents
- * the difference between the current state and the next state.
+ * This option should be disabled if your network library uses a custom
+ * serialization method (i.e. Zap, ByteNet) to prevent interference.
*
- * If a value was removed, it is replaced with `None`. This can be checked
- * using the `sync.isNone` function.
+ * @server
*/
- type SyncPatch = MaybeNone | State extends
- | ReadonlyMap
- | Map
- ? ReadonlyMap | None>
- : State extends Set | ReadonlySet
- ? ReadonlyMap
- : State extends readonly (infer T)[]
- ? Serialize extends true
- ? ReadonlyMap | None>
- : readonly (SyncPatch | None | undefined)[]
- : State extends DataType
- ? State
- : State extends object
- ? { readonly [P in keyof State]?: SyncPatch }
- : State;
+ fixArrays: boolean;
+ /**
+ * When `true`, synced state containing unsafe sparse arrays or mixed
+ * tables will emit a warning. Only checked in strict mode and if
+ * `fixArrays` is enabled. Defaults to `true`.
+ *
+ * @server
+ */
+ validatePatches: boolean;
+};
+
+/**
+ * @client
+ */
+export namespace client {
+ /**
+ * Registers a map of setters to the state with the corresponding keys on the
+ * server. When the server sends updates, these setters will be called with
+ * the new values.
+ *
+ * Atoms, signals converted to atoms, and signal setter functions are allowed.
+ * Note that `server.addSignalsToClient` still requires some way to get the
+ * state.
+ *
+ * @param signals A map of setter functions to sync with the server.
+ */
+ export function addSignals(signals: Signals): void;
/**
- * A payload that can be sent from the server to the client to synchronize
- * state between the two.
+ * Unregisters from server state updates for the given keys. The signals
+ * will retain their current values, but will no longer receive updates
+ * from the server.
*/
- type SyncPayload =
- | { readonly type: "init"; readonly data: StateOfMap }
- | { readonly type: "patch"; readonly data: SyncPatch, Serialize> };
+ export function removeSignals(
+ ...keys: (keyof Signals)[]
+ ): void;
- interface ClientOptions {
- /**
- * 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;
- }
+ /**
+ * Unsubscribes from all server state updates.
+ */
+ export function removeAllSignals(): void;
- interface ServerOptions {
- /**
- * The atoms to synchronize with the client.
- */
- atoms: Selectors;
- /**
- * The interval at which to send patches to the client, in seconds.
- * Defaults to `0` (patches are sent up to once per frame). Set to a
- * negative value to disable automatic syncing.
- */
- interval?: number;
- /**
- * Whether the history of state changes since the client's last update
- * should be preserved. This is useful for values that change multiple times
- * per frame, where each individual change is important. Defaults to `false`.
- *
- * If `true`, the broadcaster will send a list of payloads to the client
- * instead of a single payload. The client will apply each payload in order
- * to reconstruct the state's changes over time.
- */
- preserveHistory?: boolean;
- /**
- * When `true`, Charm will apply validation and serialize unsafe arrays
- * to address remote event argument limitations. Defaults to `true`.
- *
- * This option should be disabled if your network library uses a custom
- * serialization method (i.e. Zap, ByteNet) to prevent interference.
- */
- autoSerialize?: Serialize;
- }
-
- interface ClientSyncer {
- /**
- * Applies a patch or initializes the state of the atoms with the given
- * payload from the server.
- *
- * @param payloads The patches or hydration payloads to apply.
- */
- sync(...payloads: SyncPayload[]): void;
- }
-
- interface ServerSyncer {
- /**
- * Sets up a subscription to each atom that schedules a patch to be sent to
- * the client whenever the state changes. When a change occurs, the `callback`
- * is called with the player and the payloads to send.
- *
- * Note that a `payload` object should not be mutated. If you need to modify
- * a payload, apply the changes to a copy of the object.
- *
- * @param callback The function to call when the state changes.
- * @returns A cleanup function that unsubscribes all listeners.
- */
- connect(callback: (player: Player, ...payloads: SyncPayload[]) => void): Cleanup;
- /**
- * Hydrates the client's state with the server's state. This should be
- * called when a player joins the game and requires the server's state.
- *
- * @param player The player to hydrate.
- */
- hydrate(player: Player): void;
- }
+ /**
+ * Merges the server's state patches into the client's state, either
+ * initializing it with the full state or merging a partial patch.
+ *
+ * @param payloads The state updates received from the server.
+ */
+ export function patch(
+ payloads: SyncPayload[],
+ ): void;
}
+
+/**
+ * @server
+ */
+export namespace server {
+ /**
+ * Subscribes a client to the given signals. When an update occurs, the client
+ * will receive a partial state patch to merge into their local state. May be
+ * called multiple times to subscribe to additional keys.
+ *
+ * Atoms, computed signals, and signal getter functions are allowed. Note
+ * that `client.addSignals` still requires some way to set the state.
+ *
+ * @param client The client receiving state updates.
+ * @param signals A map of getter functions to sync with the client.
+ */
+ export function addSignalsToClient(
+ client: Player,
+ signals: Signals,
+ ): void;
+
+ /**
+ * Unsubscribes a client from receiving all state updates. To only unsubscribe
+ * from specific keys, use `removeSignalsFromClient` instead.
+ *
+ * @param client The client receiving state updates.
+ */
+ export function removeClient(client: Player): void;
+
+ /**
+ * Unsubscribes a client from receiving updates for the given keys.
+ *
+ * @param client The client receiving state updates.
+ * @param keys The keys of the state to unsubscribe from.
+ */
+ export function removeSignalsFromClient(
+ client: Player,
+ ...keys: (keyof Signals)[]
+ ): void;
+
+ /**
+ * Uses the callback to send state updates to clients that are subscribed
+ * to state changes. Starts automatically sending updates at the interval
+ * specified in `config.interval`.
+ *
+ * @param onSync Called when sending patches to a client.
+ */
+ export function connect(
+ onSync: (client: Player, payloads: SyncPayload[]) => void,
+ ): void;
+
+ /**
+ * Stops syncing state updates to clients at the automatic interval.
+ * Flushing can still be performed manually via `flush`.
+ */
+ export function disconnect(): void;
+
+ /**
+ * Immediately sends pending updates to all clients. Normally, updates are
+ * sent at the interval specified in `config.interval`, but this function
+ * can be used to force an immediate sync.
+ */
+ export function flush(): void;
+
+ /**
+ * Unsubscribes all clients, stops tracking all state, and disconnects
+ * from the automatic sync interval.
+ */
+ export function reset(): void;
+}
+
+export namespace patch {
+ /**
+ * Converts `nil` values to the special `None` symbol. This is useful when
+ * preparing data for serialization, since `nil` can represent both the
+ * absence of a value and the removal of a value in patches.
+ */
+ export function nilToNone(value: T | undefined): T | None;
+
+ /**
+ * Compares two states and returns a patch that can be applied to the
+ * current state to produce the next state.
+ *
+ * @param currentState The current state.
+ * @param nextState The next state.
+ * @returns A patch that can be applied to the current state.
+ */
+ export function diff(
+ currentState: State,
+ nextState: State,
+ ): SyncPatch;
+
+ /**
+ * Applies a patch to a state and returns the resulting value.
+ *
+ * @param currentState The current state.
+ * @param statePatch The patches to apply.
+ * @returns The new state with the patch applied.
+ */
+ export function applyImmutable(
+ currentState: State,
+ statePatch: SyncPatch,
+ ): State;
+}
+
+type DeepFlattenImpl = {
+ [K in keyof T]: T[K] extends DataType | Callback
+ ? { [P in `${Prefix}${Exclude}`]: T[K] }
+ : T[K] extends object
+ ? DeepFlattenImpl}/`>
+ : { [P in `${Prefix}${Exclude}`]: T[K] };
+}[keyof T];
+
+type DeepFlatten = Reconstruct>>;
+
+/**
+ * Flattens a nested table into a single table with slash-separated keys.
+ * Useful for recursively collecting atoms returned by modules.
+ *
+ * @param input A nested table.
+ * @return A flattened table with slash-separated keys.
+ */
+export function flatten(input: T): DeepFlatten;
+
+/**
+ * Checks whether a value is `None`. If `true`, the value is scheduled to be
+ * removed from the state when the patch is applied.
+ *
+ * @param value The value to check.
+ * @returns `true` if the value is `None`, otherwise `false`.
+ */
+export function isNone(value: unknown): value is None;
+
+/**
+ * Converts a signal getter and setter into an atom. This is useful for
+ * syncing client signals with the server, as the sync system requires both
+ * the getter and setter to update state.
+ *
+ * @param getter A function that returns the current value of the signal.
+ * @param setter A function that sets the value of the signal.
+ * @returns An atom that can either get or set the signal's value.
+ * @see https://github.com/littensy/charm?tab=readme-ov-file#signaltoatomgetter-setter
+ */
+export function signalToAtom(getter: () => T, setter: (value: ((prev: T) => T) | T) => T): Atom;
diff --git a/packages/charm-sync/src/init.luau b/packages/charm-sync/src/init.luau
index 5fc9171..53a6353 100644
--- a/packages/charm-sync/src/init.luau
+++ b/packages/charm-sync/src/init.luau
@@ -1,18 +1,19 @@
-local client = require(script.client)
-local flatten = require(script.flatten)
-local patch = require(script.patch)
-local server = require(script.server)
-local types = require(script.types)
+local client = require("@self/client")
+local config = require("@self/config")
+local patch = require("@self/patch")
+local server = require("@self/server")
+local types = require("@self/types")
+local utils = require("@self/utils")
+export type None = types.None
export type SyncPayload = types.SyncPayload
---[=[
- Synchronizes state between the client and server. The server sends patches
- to the client, which applies them to its local state.
-]=]
return {
+ config = config,
client = client,
server = server,
- flatten = flatten,
+ patch = patch,
isNone = patch.isNone,
+ flatten = utils.flatten,
+ signalToAtom = utils.signalToAtom,
}
diff --git a/packages/charm-sync/src/interval.luau b/packages/charm-sync/src/interval.luau
deleted file mode 100644
index 31df067..0000000
--- a/packages/charm-sync/src/interval.luau
+++ /dev/null
@@ -1,30 +0,0 @@
-local RunService = game:GetService("RunService")
-
---[=[
- Schedules a function to be called at a fixed interval of `seconds`.
-
- @param callback The function to call.
- @param seconds The interval in seconds.
- @return A function that cancels the interval.
-]=]
-local function interval(callback: () -> (), seconds: number): () -> ()
- if seconds < 0 then
- return function() end
- end
-
- local clock = 0
-
- local connection = RunService.Heartbeat:Connect(function(delta)
- clock += delta
- if clock >= seconds then
- clock = 0
- callback()
- end
- end)
-
- return function()
- connection:Disconnect()
- end
-end
-
-return interval
diff --git a/packages/charm-sync/src/patch.luau b/packages/charm-sync/src/patch.luau
index 3d021b5..d6b940f 100644
--- a/packages/charm-sync/src/patch.luau
+++ b/packages/charm-sync/src/patch.luau
@@ -1,28 +1,45 @@
-local validate = require(script.Parent.validate)
+local Charm = require("../Charm")
+local config = require("./config")
+local types = require("./types")
+local validate = require("./validate")
-local __DEV__ = _G.__DEV__
+local flags = Charm.flags
---[=[
- A special symbol that denotes the absence of a value. Used to represent
- deleted values in patches.
-]=]
-local None = { __none = "__none" }
+export type None = types.None
+local None: None = { __none = "__none" }
+
+--[[
+ Checks whether a value is `None`. If `true`, the value is scheduled to be
+ removed from the state when the patch is applied.
+
+ @param value The value to check.
+ @return `true` if the value is `None`, otherwise `false`.
+]]
local function isNone(value: any): boolean
return type(value) == "table" and value.__none == "__none"
end
---[=[
+--[[
+ Converts `nil` values to the special `None` symbol. This is useful when
+ preparing data for serialization, since `nil` can represent both the absence
+ of a value and the removal of a value in patches.
+]]
+local function nilToNone(value: T?): T | None
+ return if value == nil then None else value
+end
+
+--[[
JSON serialization can drop the last values of a sparse array. For example,
`{ [1] = 1, [3] = 3 }` becomes `[1]` in some cases. Ambiguity in the
serialization of sparse arrays can be resolved by converting them to
dictionaries with string keys.
- The `apply` function will convert these dictionaries back into arrays as
+ The `patch` functions will convert these dictionaries back into arrays as
long as the target array is not sparse. This function ensures that the diff
function produces a patch that can be correctly applied.
-]=]
-local function stringifySparseArray(object: { any }): { [string | number]: any }
+]]
+local function stringifySparseArray(object: { [any]: any }): { [string | number]: any }
local maxn = table.maxn(object)
if maxn == 0 or maxn == #object then
@@ -32,86 +49,91 @@ local function stringifySparseArray(object: { any }): { [string | number]: any }
local dictionary = {}
- for index, value in next, object do
+ for index, value in object do
dictionary[tostring(index)] = value
end
return dictionary
end
---[=[
- Compares two states and returns a patch that can be applied to the previous
- state to produce the next state.
+local function diff(oldState: any, newState: any): any
+ if oldState == newState then
+ return nil
+ elseif newState == nil then
+ return None
+ elseif type(oldState) ~= "table" or type(newState) ~= "table" then
+ return newState
+ end
- @param prevState The previous state.
- @param nextState The next state.
- @param serialize Whether to convert sparse arrays into dictionaries. Defaults to `true`.
- @returns A patch that can be applied to the previous state.
-]=]
-local function diff(prevState: { [any]: any }, nextState: { [any]: any }, serialize: boolean?)
- serialize = serialize ~= false
+ local diffs = table.clone(newState)
- local patches = table.clone(nextState)
+ for key, oldValue in oldState do
+ diffs[key] = diff(oldValue, newState[key])
+ end
- for key, previous in next, prevState do
- local current = nextState[key]
+ if next(diffs) == nil then
+ return nil
+ end
- if previous == current then
- patches[key] = nil
- elseif current == nil then
- patches[key] = None
- elseif type(previous) == "table" and type(current) == "table" then
- patches[key] = diff(previous, current, serialize)
+ if config.fixArrays then
+ -- Convert sparse array patches that could be misserialized
+ if oldState[1] ~= nil or newState[1] ~= nil then
+ diffs = stringifySparseArray(diffs)
+ end
+ if flags.strict and config.validatePatches then
+ validate(diffs)
end
end
- -- Coerce sparse array patches into dictionaries
- if serialize and (prevState[1] ~= nil or nextState[1] ~= nil) then
- patches = stringifySparseArray(patches)
- end
-
- if serialize and __DEV__ then
- for key, value in next, patches do
- validate(value, key)
- end
- end
-
- return patches
+ return diffs
end
---[=[
- Applies a patch to a state and returns the next state.
-
- @param state The current state.
- @param patches The patches to apply.
- @returns The next state.
-]=]
-local function apply(state: any, patches: any): any
- if type(patches) == "table" and patches.__none == "__none" then
+local function apply(oldState: any, diffs: any): any
+ if isNone(diffs) then
return nil
- elseif type(state) ~= "table" or type(patches) ~= "table" then
- return patches
+ elseif type(oldState) ~= "table" or type(diffs) ~= "table" then
+ return diffs
end
- local nextState = table.clone(state)
- local stateIsArray = state[1] ~= nil
+ local newState = table.clone(oldState)
+ local stateIsArray = oldState[1] ~= nil
- for key, patch in next, patches do
+ for key, value in diffs do
-- Diff-checking an array produces a sparse array, which will not be
-- preserved when converted to JSON. To prevent this, we turn string
-- keys back into numeric keys.
if stateIsArray and type(key) == "string" then
key = tonumber(key) or key
end
-
- nextState[key] = apply(nextState[key], patch)
+ newState[key] = apply(newState[key], value)
end
- return nextState
+ return newState
+end
+
+local function applyMutable(targetState: any, diffs: any): any
+ if isNone(diffs) then
+ return nil
+ elseif type(targetState) ~= "table" or type(diffs) ~= "table" then
+ return diffs
+ end
+
+ local targetIsArray = targetState[1] ~= nil
+
+ for key, value in diffs do
+ if targetIsArray and type(key) == "string" then
+ key = tonumber(key) or key
+ end
+ targetState[key] = applyMutable(targetState[key], value)
+ end
+
+ return targetState
end
return {
isNone = isNone,
+ nilToNone = nilToNone,
diff = diff,
apply = apply,
+ applyMutable = applyMutable,
}
diff --git a/packages/charm-sync/src/server.luau b/packages/charm-sync/src/server.luau
index f2c336e..ca04ba7 100644
--- a/packages/charm-sync/src/server.luau
+++ b/packages/charm-sync/src/server.luau
@@ -1,267 +1,416 @@
-local Players = game:GetService("Players")
+local Charm = require("../Charm")
+local config = require("./config")
+local patch = require("./patch")
+local types = require("./types")
+local utils = require("./utils")
-local Charm = require(script.Parent.Parent.Charm)
-local interval = require(script.Parent.interval)
-local patch = require(script.Parent.patch)
-local types = require(script.Parent.types)
-type AtomMap = types.AtomMap
type SyncPayload = types.SyncPayload
-type ServerOptions = {
- --[=[
- The atoms to synchronize with the client.
- ]=]
- atoms: AtomMap,
- --[=[
- The interval at which to send patches to the client, in seconds.
- Defaults to `0` (patches are sent up to once per frame). Set to a
- negative value to disable automatic syncing.
- ]=]
- interval: number?,
- --[=[
- Whether the history of state changes since the client's last update
- should be preserved. This is useful for values that change multiple times
- per frame, where each individual change is important. Defaults to `false`.
- ]=]
- preserveHistory: boolean?,
- --[=[
- When `true`, Charm will apply validation and serialize unsafe arrays
- to address remote event argument limitations. Defaults to `true`.
+type SyncStatus = number
- This option should be disabled if your network library uses a custom
- serialization method (i.e. Zap, ByteNet) to prevent interference.
- ]=]
- autoSerialize: boolean?,
+type ClientKeys = { [string]: SyncStatus }
+
+type TrackedState = {
+ identifier: unknown,
+ currentValue: T,
+ lastSyncedValue: T,
+ isReactive: boolean,
+ dispose: () -> (),
}
-type ServerSyncer = {
- --[=[
- Sets up a subscription to each atom that schedules a patch to be sent to
- the client whenever the state changes. When a change occurs, the `callback`
- is called with the player and the payload to send.
-
- Note that the `payload` object should not be mutated. If you need to
- modify the payload, apply the changes to a copy of the object.
-
- @param callback The function to call when the state changes.
- @return A cleanup function that unsubscribes all listeners.
- ]=]
- connect: (self: ServerSyncer, callback: (player: Player, ...SyncPayload) -> ()) -> () -> (),
- --[=[
- Hydrates the client's state with the server's state. This should be
- called when a player joins the game and requires the server's state.
-
- @param player The player to hydrate.
- ]=]
- hydrate: (self: ServerSyncer, player: Player) -> (),
- --[=[
- @deprecated For internal use only.
- ]=]
- _sendPatch: (self: ServerSyncer, player: Player) -> (),
+type StateUpdate = {
+ key: string,
+ newValue: any,
+ oldValue: any,
}
---[=[
- Creates a `ServerSyncer` object that sends patches to the client and
- hydrates the client's state.
-
- @param options The atoms to synchronize with the client.
- @return A `ServerSyncer` object.
-]=]
-local function server(options: ServerOptions): ServerSyncer
- local atoms = options.atoms
- local autoSerialize = options.autoSerialize ~= false
- local preserveHistory = options.preserveHistory
- local syncRate = options.interval or 0
+local PENDING_INITIAL_STATE: SyncStatus = 0
+local LISTENING_FOR_CHANGES: SyncStatus = 1
- local self = {} :: ServerSyncer
- local sync: (player: Player, payload: SyncPayload) -> ()
+local trackedStates: { [string]: TrackedState } = {}
+local globalUpdates: { StateUpdate } = {}
+local clients: { [any]: ClientKeys } = {}
- local function createSnapshot()
- local snapshot = {}
+local connection: RBXScriptConnection?
+local syncCallback: ((client: any, payloads: { SyncPayload }) -> ())?
- for key, atom in next, atoms do
- snapshot[key] = atom()
+--[[
+ Manually converts a reactive proxy to its raw representation to potentially
+ support custom implementations of reactive proxies.
+]]
+local function toRaw(value: T): T
+ value = value :: types.ReactiveProxy
+ if type(value) == "table" and type(value.__target) == "table" and type(value.__track) == "function" then
+ value.__track()
+ return toRaw(value.__target)
+ end
+ return value
+end
+
+local function trackSignal(key: string, getter: () -> T, identifier: unknown)
+ if trackedStates[key] then
+ -- Warn against name collisions with conflicting signals or proxies
+ if trackedStates[key].identifier ~= identifier then
+ warn("[Charm] Attempted to sync different states with the same key:", key)
end
-
- return snapshot
+ return
end
- -- Send the initial state to a player when they join the server.
- function self:hydrate(player)
- assert(sync, "connect() must be called before hydrate()")
+ local trackedState: TrackedState
+ local initialValue = getter()
+ local rawInitialValue = toRaw(initialValue)
+ local snapshot = if initialValue ~= rawInitialValue then utils.deepCopy(rawInitialValue) else initialValue
- sync(player, {
- type = "init",
- data = createSnapshot(),
- })
- end
+ local dispose = Charm.effect(function()
+ local newValue = getter()
+ local rawValue = toRaw(newValue)
+ local isReactive = newValue ~= rawValue
- -- If the history of state changes should be preserved, keep a list of
- -- previous snapshots and diff-check each one from the last.
- if preserveHistory then
- local snapshots = { createSnapshot() }
- local changed = false
-
- function self:connect(callback)
- local subscriptions = {}
-
- sync = callback
-
- local function pushSnapshot(key: string | number, current: any, previous: any)
- local lastSnapshot = snapshots[#snapshots]
- local previousSnapshot = snapshots[#snapshots - 1]
-
- -- Optimize snapshots by updating the most recent snapshot if the
- -- previous and current values are the same, since this allows us
- -- to group multiple changes into a single snapshot.
- if previousSnapshot and previousSnapshot[key] == previous and lastSnapshot[key] == previous then
- lastSnapshot[key] = current
- else
- local nextSnapshot = table.clone(lastSnapshot)
- nextSnapshot[key] = current
- table.insert(snapshots, nextSnapshot)
- end
- end
-
- -- Populate the snapshot with the initial state of each atom.
- -- Subscribe to each atom and update the state when it changes.
- for key, atom in next, atoms do
- subscriptions[key] = Charm.subscribe(atom, function(current, previous)
- pushSnapshot(key, current, previous)
- changed = true
- end)
- end
-
- local disconnect = interval(function()
- if not changed then
- return
- end
-
- local payloads: { SyncPayload } = {}
- local lastSnapshot
-
- for index, snapshot in next, snapshots do
- lastSnapshot = snapshot
-
- if index == 1 then
- continue
- end
-
- table.insert(payloads, {
- type = "patch",
- data = patch.diff(snapshots[index - 1], snapshot, autoSerialize),
- })
- end
-
- snapshots = { lastSnapshot }
- changed = false
-
- for _, player in next, Players:GetPlayers() do
- callback(player, unpack(payloads))
- end
- end, syncRate)
-
- return function()
- disconnect()
-
- for _, unsubscribe in next, subscriptions do
- unsubscribe()
- end
- end
- end
-
- function self:_sendPatch(player: Player)
- assert(sync, "connect() must be called before _sendPatch()")
-
- if not changed then
- return
- end
-
- local payloads: { SyncPayload } = {}
-
- for index, snapshot in next, snapshots do
- if index == 1 then
- continue
- end
-
- table.insert(payloads, {
- type = "patch",
- data = patch.diff(snapshots[index - 1], snapshot, autoSerialize),
- })
- end
-
- snapshots = { snapshots[#snapshots] }
- changed = false
-
- sync(player, unpack(payloads))
- end
-
- return self
- end
-
- local previousSnapshot = createSnapshot()
- local currentSnapshot = table.clone(previousSnapshot)
- local changed = false
-
- function self:connect(callback)
- local subscriptions = {}
-
- sync = callback
-
- -- Subscribe to each atom and update the state when one changes.
- for key, atom in next, atoms do
- subscriptions[key] = Charm.subscribe(atom, function(state)
- currentSnapshot[key] = state
- changed = true
- end)
- end
-
- local disconnect = interval(function()
- if not changed then
- return
- end
-
- local payload: SyncPayload = {
- type = "patch",
- data = patch.diff(previousSnapshot, currentSnapshot, autoSerialize),
- }
-
- previousSnapshot = table.clone(currentSnapshot)
- changed = false
-
- for _, player in next, Players:GetPlayers() do
- callback(player, payload)
- end
- end, syncRate)
-
- return function()
- disconnect()
-
- for _, unsubscribe in next, subscriptions do
- unsubscribe()
- end
- end
- end
-
- function self:_sendPatch(player)
- assert(sync, "connect() must be called before _sendPatch()")
-
- if not changed then
+ if not trackedState then
return
end
- local payload: SyncPayload = {
- type = "patch",
- data = patch.diff(previousSnapshot, currentSnapshot, autoSerialize),
- }
+ -- When preserving history, we need to keep track of previous values
+ -- so we can create granular patches for each change.
+ if config.preserveHistory then
+ local prevSnapshot = snapshot
+ snapshot = if isReactive then utils.deepCopy(rawValue) else newValue
+ table.insert(globalUpdates, {
+ key = key,
+ newValue = snapshot,
+ oldValue = prevSnapshot,
+ })
+ end
- previousSnapshot = table.clone(currentSnapshot)
- changed = false
+ trackedState.currentValue = rawValue
+ trackedState.isReactive = isReactive
+ end)
- sync(player, payload)
- end
+ trackedState = {
+ identifier = identifier,
+ currentValue = rawInitialValue,
+ lastSyncedValue = snapshot,
+ isReactive = initialValue ~= rawInitialValue,
+ dispose = dispose,
+ }
- return self
+ trackedStates[key] = trackedState
end
-return server
+local function stopTracking(key: string)
+ local trackedState = trackedStates[key]
+ if trackedState then
+ trackedState.dispose()
+ trackedStates[key] = nil
+ end
+end
+
+local function purgeUntracked()
+ local active: { [string]: boolean } = {}
+
+ for _, clientKeys in clients do
+ for key in clientKeys do
+ active[key] = true
+ end
+ end
+
+ for key in trackedStates do
+ if not active[key] then
+ stopTracking(key)
+ end
+ end
+end
+
+local function diffGlobalUpdateBuffer(): { any }
+ local patchCount = #globalUpdates
+ local patches = table.create(patchCount)
+
+ for _, update in globalUpdates do
+ table.insert(patches, patch.diff(update.oldValue, update.newValue))
+ end
+
+ return patches
+end
+
+local function selectFromGlobalPatchList(client: any, globalPatches: { any }): { SyncPayload }?
+ local clientKeys = clients[client]
+
+ if not clientKeys then
+ return nil
+ end
+
+ local payloads: { SyncPayload } = {}
+
+ -- Iterate over all global updates and see if the client is interested in
+ -- any of them. If so, we create a patch payload for the update.
+ for index, update in globalUpdates do
+ local patchData = globalPatches[index]
+
+ if not patchData then
+ continue
+ end
+
+ local key = update.key
+ local status = clientKeys[key]
+
+ if status == LISTENING_FOR_CHANGES then
+ local payload: SyncPayload = {
+ type = "patch",
+ data = { [key] = patchData },
+ }
+ table.insert(payloads, payload)
+ elseif status == PENDING_INITIAL_STATE then
+ local payload: SyncPayload = {
+ type = "init",
+ data = { [key] = update.newValue },
+ }
+ table.insert(payloads, payload)
+ clientKeys[key] = LISTENING_FOR_CHANGES
+ end
+ end
+
+ -- If a pending signal was never updated, we still need to send its initial
+ -- state. This can happen if a signal was registered after its last change.
+ local initData = {}
+
+ for key, status in clientKeys do
+ local trackedState = trackedStates[key]
+
+ if status == PENDING_INITIAL_STATE and trackedState then
+ initData[key] = trackedState.currentValue
+ clientKeys[key] = LISTENING_FOR_CHANGES
+ end
+ end
+
+ if next(initData) ~= nil then
+ table.insert(payloads, 1, { type = "init" :: "init", data = initData })
+ end
+
+ return payloads
+end
+
+local function diffGlobalState(): { [string]: any }?
+ local currentGlobalState: { [string]: any } = {}
+ local previousGlobalState: { [string]: any } = {}
+
+ for key, trackedState in trackedStates do
+ local currentValue = if trackedState.isReactive
+ then utils.deepCopy(trackedState.currentValue)
+ else trackedState.currentValue
+
+ currentGlobalState[key] = currentValue
+ previousGlobalState[key] = trackedState.lastSyncedValue
+ trackedState.lastSyncedValue = currentValue
+ end
+
+ return patch.diff(previousGlobalState, currentGlobalState)
+end
+
+local function selectFromGlobalPatch(client: any, globalPatch: { [string]: any }?): { SyncPayload }?
+ local clientKeys = clients[client]
+
+ if not clientKeys then
+ return nil
+ end
+
+ local payloads: { SyncPayload } = {}
+ local initData: { [string]: any } = {}
+ local patchData: { [string]: any } = {}
+
+ -- Create patches for initialized signals that changed, and add signals
+ -- that are still pending initial state to the init payload.
+ for key, status in clientKeys do
+ local trackedState = trackedStates[key]
+
+ if status == LISTENING_FOR_CHANGES and globalPatch then
+ patchData[key] = globalPatch[key]
+ elseif status == PENDING_INITIAL_STATE then
+ initData[key] = trackedState.currentValue
+ clientKeys[key] = LISTENING_FOR_CHANGES
+ end
+ end
+
+ if next(initData) ~= nil then
+ local payload: SyncPayload = {
+ type = "init",
+ data = initData,
+ }
+ table.insert(payloads, payload)
+ end
+
+ if next(patchData) ~= nil then
+ local payload: SyncPayload = {
+ type = "patch",
+ data = patchData,
+ }
+ table.insert(payloads, payload)
+ end
+
+ return payloads
+end
+
+local function startInterval(callback: () -> ())
+ if not game then
+ return
+ end
+
+ if connection then
+ connection:Disconnect()
+ end
+
+ local timestamp = os.clock()
+
+ connection = game:GetService("RunService").Heartbeat:Connect(function()
+ if config.interval >= 0 and os.clock() >= timestamp + config.interval then
+ timestamp = os.clock()
+ debug.profilebegin("CharmSync")
+ callback()
+ debug.profileend()
+ end
+ end)
+end
+
+--[[
+ Subscribes a client to the given signals. When an update occurs, the client
+ will receive a partial state patch to merge into their local state. May be
+ called multiple times to subscribe to additional keys.
+
+ Atoms, signals converted to atoms, and signal getter functions are allowed.
+ Note that `client.addSignals` still requires some way to set the state.
+
+ @param client The client receiving state updates.
+ @param signals A map of getter functions to sync with the client.
+]]
+local function addSignalsToClient(client: any, signals: { [string]: {} | () -> any })
+ if not clients[client] then
+ clients[client] = {}
+ end
+
+ local clientKeys = clients[client]
+
+ for key, signal in signals do
+ if type(signal) == "function" then
+ trackSignal(key, signal, signal)
+ else
+ trackSignal(key, function()
+ return signal
+ end, signal)
+ end
+
+ if not clientKeys[key] then
+ -- Mark the signal as pending initial state so we send its full
+ -- value the next time we sync.
+ clientKeys[key] = PENDING_INITIAL_STATE
+ end
+ end
+end
+
+--[[
+ Unsubscribes a client from receiving all state updates. To only unsubscribe
+ from specific keys, use `unsubscribeClientFrom` instead.
+
+ @param client The client receiving state updates.
+]]
+local function removeClient(client: any)
+ clients[client] = nil
+ purgeUntracked()
+end
+
+--[[
+ Unsubscribes a client from receiving updates for the given keys.
+
+ @param client The client receiving state updates.
+ @param keys The keys of the state to unsubscribe from.
+]]
+local function removeSignalsFromClient(client: any, ...: string)
+ local clientKeys = clients[client]
+
+ if not clientKeys then
+ return
+ end
+
+ for index = 1, select("#", ...) do
+ local key = select(index, ...)
+ clientKeys[key] = nil
+ end
+
+ if next(clientKeys) == nil then
+ removeClient(client)
+ else
+ purgeUntracked()
+ end
+end
+
+--[[
+ Immediately sends pending updates to all clients. Normally, updates are
+ sent at the interval specified in `config.interval`, but this function
+ can be used to force an immediate sync.
+]]
+local function flush()
+ if not syncCallback then
+ return
+ end
+
+ local granular = config.preserveHistory
+ local globalPatch: { [any]: any }? = if granular then diffGlobalUpdateBuffer() else diffGlobalState()
+
+ for client in clients do
+ local payloads = if granular
+ then selectFromGlobalPatchList(client, globalPatch :: { any })
+ else selectFromGlobalPatch(client, globalPatch)
+
+ if payloads and payloads[1] then
+ if task then
+ task.spawn(syncCallback, client, payloads)
+ else
+ syncCallback(client, payloads)
+ end
+ end
+ end
+
+ table.clear(globalUpdates)
+end
+
+--[[
+ Uses the callback to send state updates to clients that are subscribed
+ to state changes. Starts automatically sending updates at the interval
+ specified in `config.interval`.
+
+ @param onSync Called when sending patches to a client.
+]]
+local function connect(onSync: (client: any, payloads: { SyncPayload }) -> ())
+ syncCallback = onSync
+ startInterval(flush)
+end
+
+--[[
+ Stops syncing state updates to clients at the automatic interval.
+ Flushing can still be performed manually via `flush`.
+]]
+local function disconnect()
+ if connection then
+ connection:Disconnect()
+ connection = nil
+ end
+end
+
+--[[
+ Unsubscribes all clients, stops tracking all state, and disconnects
+ from the automatic sync interval.
+]]
+local function reset()
+ table.clear(trackedStates)
+ table.clear(clients)
+ table.clear(globalUpdates)
+ disconnect()
+end
+
+return {
+ addSignalsToClient = addSignalsToClient,
+ removeClient = removeClient,
+ removeSignalsFromClient = removeSignalsFromClient,
+ connect = connect,
+ disconnect = disconnect,
+ flush = flush,
+ reset = reset,
+}
diff --git a/packages/charm-sync/src/types.luau b/packages/charm-sync/src/types.luau
index 641f744..664ee61 100644
--- a/packages/charm-sync/src/types.luau
+++ b/packages/charm-sync/src/types.luau
@@ -1,20 +1,30 @@
-local Charm = require(script.Parent.Parent.Charm)
-type Atom = Charm.Atom
-
---[=[
+--[[
A payload that can be sent from the server to the client to synchronize
- state between the two.
-]=]
-export type SyncPayload = {
- type: "init",
- data: { [string | number]: any },
-} | {
- type: "patch",
- data: { [string | number]: any },
-}
+ the states of the client's atoms with the server.
+]]
+export type SyncPayload =
+ | {
+ type: "init",
+ data: { [string]: any },
+ }
+ | {
+ type: "patch",
+ data: { [string]: any },
+ }
-export type AtomMap = {
- [string | number]: Atom,
-}
+--[[
+ A special type that represents a removed value in a patch. This is used to
+ differentiate between a value that was set to `nil` and an unchanged value.
+]]
+export type None = { __none: "__none" }
-return nil
+export type ReactiveProxy = typeof(setmetatable(
+ {} :: {
+ __track: () -> (),
+ __target: T,
+ __signals: { [unknown]: () -> unknown },
+ },
+ {} :: { __index: T }
+))
+
+return {}
diff --git a/packages/charm-sync/src/utils.luau b/packages/charm-sync/src/utils.luau
new file mode 100644
index 0000000..4a49871
--- /dev/null
+++ b/packages/charm-sync/src/utils.luau
@@ -0,0 +1,65 @@
+local Charm = require("../Charm")
+
+type Atom = Charm.Atom
+
+--[[
+ Flattens a nested table into a single table with slash-separated keys.
+ Useful for recursively collecting atoms returned by modules.
+
+ @param input A nested table.
+ @return A flattened table with slash-separated keys.
+]]
+local function flatten(input: { [any]: any }): Flattened
+ local result = {}
+
+ local function visit(node: { [any]: any }, path: string)
+ for key, value in node do
+ local location = if path == "" then tostring(key) else path .. "/" .. tostring(key)
+
+ if type(value) == "table" then
+ visit(value, location)
+ else
+ result[location] = value
+ end
+ end
+ end
+
+ visit(input, "")
+
+ return result :: any
+end
+
+--[[
+ Converts a signal getter and setter into an atom. This is useful for
+ syncing client signals with the server, as the sync system requires both
+ the getter and setter to update state.
+
+ @param getter A function that returns the current value of the signal.
+ @param setter A function that sets the value of the signal.
+ @return An atom that can either get or set the signal's value.
+]]
+local function signalToAtom(getter: () -> T, setter: (value: ((T) -> T) | T) -> T): Atom
+ return function(...)
+ return if select("#", ...) == 0 then getter() else setter((...))
+ end
+end
+
+--[[
+ Returns a deep copy of the given table. Skips tables with metatables to
+ prevent copying reactive proxies.
+]]
+local function deepCopy(object: T): T
+ local copy: any = table.clone(object :: any)
+ for key, value in copy do
+ if type(value) == "table" and getmetatable(value) == nil then
+ copy[key] = deepCopy(value)
+ end
+ end
+ return copy
+end
+
+return {
+ flatten = flatten,
+ signalToAtom = signalToAtom,
+ deepCopy = deepCopy,
+}
diff --git a/packages/charm-sync/src/validate.luau b/packages/charm-sync/src/validate.luau
index c832542..dfba716 100644
--- a/packages/charm-sync/src/validate.luau
+++ b/packages/charm-sync/src/validate.luau
@@ -1,67 +1,46 @@
-local SAFE_KEYS = { string = true, number = true }
-local UNSAFE_VALUES = { ["function"] = true, thread = true }
-
local function isTableUnsafe(object: { [any]: any })
- local keyType = nil
- local objectSize = 0
+ local firstKey = next(object)
- -- All keys must have the same type
- for key in next, object do
- local currentType = type(key)
-
- if not keyType and SAFE_KEYS[currentType] then
- keyType = currentType
- elseif keyType ~= currentType then
- return true
- end
-
- objectSize += 1
+ if firstKey == nil then
+ return false
end
- -- If there are more keys than the length of the array, it's an array with
- -- non-sequential keys.
- if objectSize > #object and keyType == "number" then
+ local keyType = type(firstKey)
+ local maxKey = 0
+
+ -- Keys must all be the same type
+ for key in object do
+ if type(key) ~= keyType then
+ return true
+ elseif type(key) == "number" and key > maxKey then
+ maxKey = key
+ end
+ end
+
+ -- Array keys must be sequential
+ if keyType == "number" and maxKey > #object then
return true
end
return false
end
---[=[
- Validates a value to ensure it can be synced over a remote event.
+--[[
+ Emits a warning if the value is a table that cannot be synced via
+ RemoteEvents or RemoteFunctions. This only aims to catch common mistakes
+ with remote argument limitations, such as sparse arrays or mixed tables.
- @param value The value to validate.
- @param key The key of the value in the table.
- @error Throws an error if the value cannot be synced.
-]=]
-local function validate(value: any, key: any)
- local typeOfKey = type(key)
- local typeOfValue = type(value)
-
- if not SAFE_KEYS[typeOfKey] then
- error(`Invalid key type '{typeOfKey}' at key '{key}'`)
- elseif UNSAFE_VALUES[typeOfValue] then
- error(`Invalid value type '{typeOfValue}' at key '{key}'`)
- elseif typeOfValue == "table" then
- if getmetatable(value) ~= nil then
- error(`Cannot sync tables with metatables! Got {value} at key '{key}'`)
- elseif isTableUnsafe(value) then
- error(
- `Cannot sync tables unsupported by remote events! The value has the key '{key}'.\n\n`
- .. "This can be for the following reasons:\n"
- .. "1. The object is an array with non-sequential keys\n"
- .. "2. The object is a dictionary with mixed key types (e.g. string and number)\n\n"
- .. "Read more: https://create.roblox.com/docs/scripting/events/remote#argument-limitations"
- )
- end
- end
-
- if typeOfKey == "number" then
- if key == math.huge or key == -math.huge then
- error("Cannot sync infinity as key")
- elseif key ~= math.floor(key) then
- error("Cannot sync non-integer number as key")
- end
+ @param value The table value.
+]]
+local function validate(value: { [any]: any })
+ if isTableUnsafe(value) then
+ warn(
+ "Cannot sync tables unsupported by remote events! This can be for the following reasons:\n"
+ .. "1. The table is a sparse array with non-sequential indices\n"
+ .. "2. The table is a mixed table (not all keys have the same type)\n\n"
+ .. "Read more: https://create.roblox.com/docs/scripting/events/remote#argument-limitations\n",
+ value
+ )
end
end
diff --git a/packages/charm-sync/test/client.test.luau b/packages/charm-sync/test/client.test.luau
new file mode 100644
index 0000000..20c41a8
--- /dev/null
+++ b/packages/charm-sync/test/client.test.luau
@@ -0,0 +1,137 @@
+local Charm = require("../Charm")
+local DeepCharm = require("../../deep-charm/src")
+local client = require("../src/client")
+local test = require("../../../tests/test")
+
+test.afterEach(function()
+ client.removeAllSignals()
+end)
+
+test("should hydrate atom", function()
+ local a = Charm.atom(1)
+
+ client.addSignals({ a = a })
+ client.patch({
+ { type = "init", data = { a = 2 } },
+ })
+
+ assert(a() == 2, "should set a to the initial value")
+end)
+
+test("should merge patch", function()
+ local a = Charm.atom({ b = 1, c = 2 })
+ local previous = a()
+
+ client.addSignals({ a = a })
+ client.patch({
+ { type = "init", data = { a = a() } },
+ })
+
+ assert(a() == previous, "should set a to the initial value")
+
+ client.patch({
+ { type = "patch", data = { a = { b = 3 } } },
+ })
+
+ assert(a() ~= previous, "should apply patch")
+ assert(a().b == 3, "should set b to the new value")
+ assert(a().c == 2, "should not change c")
+end)
+
+test("should merge patch (reactive)", function()
+ local a = DeepCharm.reactive({ b = 1, c = 2 })
+ local runs = 0
+
+ Charm.effect(function()
+ local _ = a.b + a.c
+ runs += 1
+ end)
+
+ client.addSignals({ a = a })
+ client.patch({
+ { type = "init", data = { a = { b = a.b, c = a.c } } },
+ })
+
+ assert(runs == 1, "should not trigger effect for redundant init")
+
+ client.patch({
+ { type = "patch", data = { a = { b = 3 } } },
+ })
+
+ assert(a.b == 3, "should set b to the new value")
+ assert(a.c == 2, "should not change c")
+ assert(runs == 2, "should trigger effect for patch")
+end)
+
+test("should fix sparse array patches", function()
+ local a = Charm.atom({ 1 })
+
+ client.addSignals({ a = a })
+ client.patch({
+ { type = "init", data = { a = a() } },
+ })
+
+ -- This patch would be the result of a diff between { 1 } and { 1, 2 } that
+ -- was JSON-serialized and deserialized. The client should be able to catch
+ -- this and convert the string key back to a number.
+ client.patch({
+ { type = "patch", data = { a = { ["2"] = 2 } } },
+ })
+
+ assert(a()["2" :: any] == nil, "should convert serialized arrays to use numbers")
+ assert(a()[2] == 2, "should convert serialized arrays to use numbers")
+end)
+
+test("should merge multiple patches", function()
+ local a = Charm.atom(1)
+ local calls = {}
+
+ client.addSignals({ a = a })
+
+ Charm.subscribe(a, function(value)
+ table.insert(calls, value)
+ end)
+
+ client.patch({
+ { type = "init", data = { a = a() } },
+ })
+
+ client.patch({
+ { type = "patch", data = { a = 2 } },
+ { type = "patch", data = { a = 3 } },
+ })
+
+ assert(a() == 3, "should apply the last patch")
+ assert(table.concat(calls) == "23", `wrong order: {table.concat(calls)}`)
+end)
+
+test("should batch patches", function()
+ local a = Charm.atom(0)
+ local b = Charm.atom(0)
+ local states = {}
+
+ client.addSignals({ a = a, b = b })
+
+ Charm.subscribe(a, function()
+ states.b = b()
+ end)
+
+ Charm.subscribe(b, function()
+ states.a = a()
+ end)
+
+ client.patch({
+ { type = "init", data = { a = a(), b = b() } },
+ })
+
+ client.patch({
+ { type = "patch", data = { a = 1, b = 2 } },
+ })
+
+ assert(a() == 1, "should set a to the new value")
+ assert(b() == 2, "should set b to the new value")
+ assert(states.a == 1, "sub to a should run after b updates")
+ assert(states.b == 2, "sub to b should run after a updates")
+end)
+
+return {}
diff --git a/packages/charm-sync/test/e2e.test.luau b/packages/charm-sync/test/e2e.test.luau
new file mode 100644
index 0000000..62ca4a7
--- /dev/null
+++ b/packages/charm-sync/test/e2e.test.luau
@@ -0,0 +1,97 @@
+local Charm = require("../Charm")
+local DeepCharm = require("../../deep-charm/src")
+local client = require("../src/client")
+local config = require("../src/config")
+local server = require("../src/server")
+local test = require("../../../tests/test")
+
+test.afterEach(function()
+ server.reset()
+ client.removeAllSignals()
+ config.preserveHistory = false
+end)
+
+test("should replicate server state to client", function()
+ local target = {}
+ local serverA = Charm.atom({ count = 0 })
+ local serverB = DeepCharm.reactive({ count = 0 })
+ local clientA = Charm.atom({ count = -1 })
+ local clientB = DeepCharm.reactive({ count = -1 })
+
+ client.addSignals({ a = clientA, b = clientB })
+ server.addSignalsToClient(target, { a = serverA, b = serverB })
+ server.connect(function(syncTarget, payloads)
+ if syncTarget == target then
+ client.patch(payloads)
+ end
+ end)
+
+ server.flush()
+ assert(clientA().count == 0, "Client atom A should be initialized to 0")
+ assert(clientB.count == 0, "Client reactive B should be initialized to 0")
+
+ serverA({ count = 1 })
+ serverB.count = 1
+ server.flush()
+ assert(clientA().count == 1, "Client atom A should be updated to 1")
+ assert(clientB.count == 1, "Client reactive B should be updated to 1")
+
+ local runs = -1
+ Charm.effect(function()
+ local _ = clientB.count
+ runs += 1
+ end)
+
+ serverA({ count = 2 })
+ server.flush()
+ assert(clientA().count == 2, "Client atom A should be updated to 2")
+ assert(runs == 0, "Client reactive B should not trigger update")
+end)
+
+test("should replicate historical server state to client", function()
+ local target = {}
+ local serverA = Charm.atom({ count = 0 })
+ local serverB = DeepCharm.reactive({ count = 0 })
+ local clientA = Charm.atom({ count = -1 })
+ local clientB = DeepCharm.reactive({ count = -1 })
+
+ config.preserveHistory = true
+
+ client.addSignals({ a = clientA, b = clientB })
+ server.addSignalsToClient(target, { a = serverA, b = serverB })
+ server.connect(function(syncTarget, payloads)
+ if syncTarget == target then
+ client.patch(payloads)
+ end
+ end)
+
+ local expectedA = { -1, 0, 1, 2, 3, 4 }
+ local expectedB = { -1, 0, 1, 2 }
+
+ Charm.effect(function()
+ local expected = table.remove(expectedA, 1)
+ assert(expected == clientA().count, `Client reactive A should be {expected}`)
+ end)
+
+ Charm.effect(function()
+ local expected = table.remove(expectedB, 1)
+ assert(expected == clientB.count, `Client reactive B should be {expected}`)
+ end)
+
+ server.flush()
+
+ serverA({ count = 1 })
+ serverA({ count = 2 })
+ serverB.count = 1
+ serverB.count = 2
+ server.flush()
+ assert(clientA().count == 2, "Client atom A should be updated to 2")
+ assert(clientB.count == 2, "Client reactive B should be updated to 2")
+
+ serverA({ count = 3 })
+ serverA({ count = 4 })
+ server.flush()
+ assert(clientA().count == 4, "Client atom A should be updated to 4")
+end)
+
+return {}
diff --git a/packages/charm-sync/test/init.luau b/packages/charm-sync/test/init.luau
new file mode 100644
index 0000000..111dc1d
--- /dev/null
+++ b/packages/charm-sync/test/init.luau
@@ -0,0 +1,23 @@
+local test = require("../../tests/test")
+
+test.describe("client", function()
+ require("@self/client.test")
+end)
+
+test.describe("e2e", function()
+ require("@self/e2e.test")
+end)
+
+test.describe("utils", function()
+ require("@self/utils.test")
+end)
+
+test.describe("patch", function()
+ require("@self/patch.test")
+end)
+
+test.describe("server", function()
+ require("@self/server.test")
+end)
+
+return {}
diff --git a/packages/charm-sync/test/patch.test.luau b/packages/charm-sync/test/patch.test.luau
new file mode 100644
index 0000000..076544e
--- /dev/null
+++ b/packages/charm-sync/test/patch.test.luau
@@ -0,0 +1,133 @@
+local config = require("../src/config")
+local patch = require("../src/patch")
+local test = require("../../../tests/test")
+
+local function mockArraySerdes(object: { [any]: any }): { [any]: any }
+ if object[1] then
+ local copy = {}
+ for index = 1, #object do
+ copy[index] = object[index]
+ end
+ return copy
+ elseif table.maxn(object) ~= #object then
+ local copy = {}
+ for key, value in object do
+ copy[tostring(key)] = value
+ end
+ return copy
+ else
+ return object
+ end
+end
+
+test.afterEach(function()
+ config.fixArrays = true
+ config.validatePatches = true
+end)
+
+test("returns a patch", function()
+ local prevState = { foo = "bar", baz = "qux" }
+ local nextState = { foo = "bar", baz = "quux" }
+ local diffs = patch.diff(prevState, nextState)
+
+ assert(diffs ~= nil, "should return a patch")
+ assert(diffs.baz == "quux", "should return the changed value")
+ assert(diffs.foo == nil, "should omit unchanged values")
+end)
+
+test("applies a patch", function()
+ local prevState = { foo = "bar", baz = "qux" }
+ local diffs = { baz = "quux", qaz = "qux" }
+ local applied = patch.apply(prevState, diffs)
+
+ assert(applied ~= nil, "should return a new state")
+ assert(applied.baz == "quux", "should apply the changed value")
+ assert(applied.foo == "bar", "should keep the unchanged value")
+ assert(applied.qaz == "qux", "should add the new value")
+end)
+
+test("allows nested diffs", function()
+ local prevState = { foo = { bar = "baz" } }
+ local nextState = { foo = { bar = "qux" } }
+ local diffs = patch.diff(prevState, nextState)
+ local applied = patch.apply(prevState, diffs)
+
+ assert(applied.foo.bar == "qux", "should apply the nested patch")
+end)
+
+test("preserves array density when applying", function()
+ local prevState = { "a", "b", "c", "d" }
+ local nextState = { ["2"] = "B", ["4"] = "D" }
+ local applied = patch.apply(prevState, nextState)
+
+ assert(applied[1] == "a", "should keep a")
+ assert(applied[2] == "B", "should apply B")
+ assert(applied[3] == "c", "should keep c")
+ assert(applied[4] == "D", "should apply D")
+
+ assert(applied["2"] == nil, "should convert serialized arrays to use numbers")
+ assert(applied["4"] == nil, "should convert serialized arrays to use numbers")
+end)
+
+test("preserves array density after serdes", function()
+ -- This particular change results in a patch that cannot be serialized
+ -- without losing information. Charm resolves this issue internally.
+ local prevState = { "a", "b", "c", "d", "e" }
+ local nextState = { "A", "b", "c", "D" }
+
+ local diffs = mockArraySerdes(patch.diff(prevState, nextState))
+ local applied = patch.apply(prevState, diffs)
+
+ for index in prevState do
+ assert(applied[index] == nextState[index], `expected {applied[index]} to be {nextState[index]}`)
+ end
+
+ for index in applied do
+ assert(applied[index] == nextState[index], `expected {applied[index]} to be {nextState[index]}`)
+ end
+end)
+
+test("cannot preserve sparse arrays", function()
+ config.validatePatches = false
+
+ local prevState = {}
+ local nextState = { [2] = "foo" }
+
+ local diffs = mockArraySerdes(patch.diff(prevState, nextState))
+ local applied = patch.apply(prevState, diffs)
+
+ -- Not enough information is available to determine whether 'diffs'
+ -- should keep the string keys added during remote event serialization.
+ assert(applied["2"] ~= nil, "will fail to convert string keys to numbers")
+ assert(applied[2] == nil, "will fail to convert string keys to numbers")
+end)
+
+test("optionally fixes sparse arrays", function()
+ config.fixArrays = true
+
+ for index in patch.diff({ 1, 1, 1, 1, 1 }, { 2, 1, 1, 2 }) do
+ assert(type(index) == "string", `expected {type(index)} {index} to be string`)
+ end
+
+ config.fixArrays = false
+
+ for index in patch.diff({ 1, 1, 1, 1, 1 }, { 2, 1, 1, 2 }) do
+ assert(type(index) == "number", `expected {type(index)} {index} to be number`)
+ end
+end)
+
+test("patches mutable state", function()
+ local targetState: { [string]: string } = { foo = "bar", baz = "qux" }
+ patch.applyMutable(targetState, { baz = "quux", qaz = "qux" })
+ assert(targetState.baz == "quux", "should apply the changed value")
+ assert(targetState.foo == "bar", "should keep the unchanged value")
+ assert(targetState.qaz == "qux", "should add the new value")
+end)
+
+test("removes table from mutable state", function()
+ local targetState = { foo = { bar = "baz" } }
+ patch.applyMutable(targetState, { foo = { __none = "__none" } })
+ assert(targetState.foo == nil, "should remove the table")
+end)
+
+return {}
diff --git a/packages/charm-sync/test/server.test.luau b/packages/charm-sync/test/server.test.luau
new file mode 100644
index 0000000..380fbb0
--- /dev/null
+++ b/packages/charm-sync/test/server.test.luau
@@ -0,0 +1,408 @@
+local Charm = require("../Charm")
+local DeepCharm = require("../../deep-charm/src")
+local config = require("../src/config")
+local patch = require("../src/patch")
+local server = require("../src/server")
+local test = require("../../../tests/test")
+local types = require("../src/types")
+
+test.afterEach(function()
+ server.reset()
+end)
+
+test("should replicate state changes", function()
+ local client = {}
+ local a = Charm.atom(0)
+ local b = Charm.atom(0)
+ local c = Charm.atom(0)
+ local target: unknown, payloads: { types.SyncPayload }
+
+ server.addSignalsToClient(client, { a = a, b = b, c = c })
+ server.connect(function(...)
+ target, payloads = ...
+ end)
+
+ server.flush()
+ assert(target == client, "Expected target to be the client")
+ assert(payloads[1].type == "init", "Expected first payload to be init")
+ assert(payloads[1].data.a == a(), "Expected init payload to contain atom 'a'")
+ assert(payloads[1].data.b == b(), "Expected init payload to contain atom 'b'")
+ assert(payloads[1].data.c == c(), "Expected init payload to contain atom 'c'")
+
+ a(1)
+ b(2)
+ server.flush()
+ assert(target == client, "Expected target to be the client")
+ assert(payloads[1].type == "patch", "Expected first payload to be patch")
+ assert(payloads[1].data.a == a(), "Expected patch payload to contain atom 'a'")
+ assert(payloads[1].data.b == b(), "Expected patch payload to contain atom 'b'")
+end)
+
+test("should replicate partial state patches", function()
+ local client = {}
+ local a = Charm.atom({ x = 0, y = 0 })
+ local b = Charm.atom(0 :: number?)
+ local target: unknown, payloads: { types.SyncPayload }
+
+ server.addSignalsToClient(client, { a = a, b = b })
+ server.connect(function(...)
+ target, payloads = ...
+ end)
+
+ server.flush()
+ assert(target == client, "Expected target to be the client")
+ assert(payloads[1].type == "init", "Expected first payload to be init")
+ assert(payloads[1].data.a.x == a().x, "Expected init payload to contain atom 'a.x'")
+ assert(payloads[1].data.a.y == a().y, "Expected init payload to contain atom 'a.y'")
+ assert(payloads[1].data.b == b(), "Expected init payload to contain atom 'b'")
+
+ a({ x = 1, y = 0 })
+ b(nil)
+ server.flush()
+ assert(target == client, "Expected target to be the client")
+ assert(payloads[1].type == "patch", "Expected first payload to be patch")
+ assert(payloads[1].data.a.x == 1, "Expected patch payload to contain atom 'a.x'")
+ assert(payloads[1].data.a.y == nil, "Expected patch payload to not contain atom 'a.y'")
+ assert(patch.isNone(payloads[1].data.b), "Expected patch payload to contain atom 'b' as None")
+end)
+
+test("should replicate partial state patches (reactive)", function()
+ local client = {}
+ local a = DeepCharm.reactive({ x = { y = 0 }, z = 0 :: number? })
+ local target: unknown, payloads: { types.SyncPayload }
+
+ server.addSignalsToClient(client, { a = a })
+ server.connect(function(...)
+ target, payloads = ...
+ end)
+
+ server.flush()
+ assert(target == client, "Expected target to be the client")
+ assert(payloads[1].type == "init", "Expected first payload to be init")
+ assert(payloads[1].data.a.x ~= nil, "Expected init payload to contain value 'a.x'")
+ assert(payloads[1].data.a.x.y == a.x.y, "Expected init payload to contain value 'a.x.y'")
+ assert(payloads[1].data.a.z == a.z, "Expected init payload to contain value 'a.z'")
+
+ a.x.y = 1
+ a.z = nil
+ server.flush()
+ assert(target == client, "Expected target to be the client")
+ assert(payloads[1].type == "patch", "Expected first payload to be patch")
+ assert(payloads[1].data.a.x.y == 1, "Expected patch payload to contain value 'a.x'")
+ assert(patch.isNone(payloads[1].data.a.z), "Expected patch payload to contain value 'a.z' as None")
+
+ a.z = 1
+ server.flush()
+ assert(target == client, "Expected target to be the client")
+ assert(payloads[1].type == "patch", "Expected first payload to be patch")
+ assert(payloads[1].data.a.z == 1, "Expected patch payload to contain value 'a.z'")
+ assert(payloads[1].data.a.x == nil, "Expected patch payload to not contain value 'a.x'")
+end)
+
+test("should handle nested proxies", function()
+ local client = {}
+ local src = DeepCharm.reactive({ (DeepCharm.reactive({})) })
+ local payloads: {}? = nil
+
+ server.addSignalsToClient(client, { src = src })
+ server.connect(function(_, newPayloads)
+ payloads = newPayloads
+ end)
+ server.flush()
+
+ payloads = nil
+ server.flush()
+ assert(payloads == nil, "Expected no payloads to be sent for unchanged nested proxy")
+end)
+
+test("should init new clients", function()
+ local clientA = {}
+ local clientB = {}
+ local a = Charm.atom(0)
+ local payloadsA: { types.SyncPayload }, payloadsB: { types.SyncPayload }
+
+ server.addSignalsToClient(clientA, { a = a })
+ server.connect(function(target, payloads)
+ if target == clientA then
+ payloadsA = payloads
+ elseif target == clientB then
+ payloadsB = payloads
+ end
+ end)
+
+ a(1)
+ server.flush()
+ assert(payloadsA[1].type == "init", "Expected first payload to be init for client A")
+ assert(payloadsA[1].data.a == a(), "Expected init payload to contain atom 'a' for client A")
+
+ a(2)
+ server.addSignalsToClient(clientB, { a = a })
+ server.flush()
+ assert(payloadsA[1].type == "patch", "Expected first payload to be patch for client A")
+ assert(payloadsA[1].data.a == a(), "Expected patch payload to contain atom 'a' for client A")
+ assert(payloadsB[1].type == "init", "Expected first payload to be init for client B")
+ assert(payloadsB[1].data.a == a(), "Expected init payload to contain atom 'a' for client B")
+end)
+
+test("should init new atoms", function()
+ local client = {}
+ local a = Charm.atom(0)
+ local b = Charm.atom(0)
+ local target: unknown, payloads: { types.SyncPayload }
+
+ server.addSignalsToClient(client, { a = a })
+ server.connect(function(...)
+ target, payloads = ...
+ end)
+
+ server.flush()
+ server.addSignalsToClient(client, { b = b })
+ a(1)
+ b(1)
+ server.flush()
+ assert(target == client, "Expected target to be the client")
+ assert(payloads[1].type == "init", "Expected first payload to be init")
+ assert(payloads[1].data.b == b(), "Expected init payload to contain atom 'b'")
+ assert(payloads[2].type == "patch", "Expected second payload to be patch")
+ assert(payloads[2].data.a == a(), "Expected patch payload to contain atom 'a'")
+ assert(payloads[2].data.b == nil, "Expected patch payload to not contain atom 'b'")
+end)
+
+test("should only replicate to synced clients", function()
+ local clientA = {}
+ local clientB = {}
+ local a = Charm.atom(0)
+ local b = Charm.atom(0)
+ local payloadsA: { types.SyncPayload }, payloadsB: { types.SyncPayload }
+
+ server.addSignalsToClient(clientA, { a = a })
+ server.addSignalsToClient(clientB, { b = b })
+ server.connect(function(target, payloads)
+ if target == clientA then
+ payloadsA = payloads
+ elseif target == clientB then
+ payloadsB = payloads
+ end
+ end)
+
+ server.flush()
+ assert(payloadsA[1].type == "init", "Expected first payload to be init for client A")
+ assert(payloadsA[1].data.a == a(), "Expected init payload to contain atom 'a' for client A")
+ assert(payloadsA[1].data.b == nil, "Expected init payload to not contain atom 'b' for client A")
+ assert(payloadsB[1].type == "init", "Expected first payload to be init for client B")
+ assert(payloadsB[1].data.b == b(), "Expected init payload to contain atom 'b' for client B")
+ assert(payloadsB[1].data.a == nil, "Expected init payload to not contain atom 'a' for client B")
+
+ payloadsA = {}
+ payloadsB = {}
+ a(1)
+ server.flush()
+ assert(payloadsA[1].type == "patch", "Expected first payload to be patch for client A")
+ assert(payloadsA[1].data.a == a(), "Expected patch payload to contain atom 'a' for client A")
+ assert(payloadsA[1].data.b == nil, "Expected patch payload to not contain atom 'b' for client A")
+ assert(#payloadsB == 0, "Expected no payloads for client B")
+
+ payloadsA = {}
+ payloadsB = {}
+ b(1)
+ server.flush()
+ assert(#payloadsA == 0, "Expected no payloads for client A")
+ assert(payloadsB[1].type == "patch", "Expected first payload to be patch for client B")
+ assert(payloadsB[1].data.b == b(), "Expected patch payload to contain atom 'b' for client B")
+ assert(payloadsB[1].data.a == nil, "Expected patch payload to not contain atom 'a' for client B")
+end)
+
+test("should serialize state", function()
+ local client = {}
+ local a = Charm.atom({ 1, 2, 3, 4, 5 })
+ local payloads: { types.SyncPayload }
+
+ server.addSignalsToClient(client, { a = a })
+ server.connect(function(_, p)
+ payloads = p
+ end)
+ server.flush()
+
+ a({ 1, 2, 0, 4 })
+ server.flush()
+ assert(payloads[1].data.a["3"] == 0, "Expected index 3 to be serialized as string key")
+ assert(patch.isNone(payloads[1].data.a["5"]), "Expected index 5 to be serialized as string key")
+end)
+
+test("should not send unchanged state", function()
+ local client = {}
+ local a = Charm.atom(0)
+ local payloads: { types.SyncPayload }
+
+ server.addSignalsToClient(client, { a = a })
+ server.connect(function(_, p)
+ payloads = p
+ end)
+
+ server.flush()
+ assert(payloads[1].type == "init", "Expected first payload to be init")
+ assert(payloads[1].data.a == 0, "Expected init payload to contain atom 'a'")
+
+ payloads = {}
+ a(0)
+ server.flush()
+ assert(#payloads == 0, "Expected no payloads to be sent for unchanged state")
+end)
+
+test.describe("historical", function()
+ test.beforeEach(function()
+ config.preserveHistory = true
+ end)
+
+ test.afterEach(function()
+ config.preserveHistory = false
+ end)
+
+ test("should replicate granular state changes", function()
+ local client = {}
+ local a = Charm.atom(0)
+ local b = Charm.atom(0)
+ local c = Charm.atom(0)
+ local target: unknown, payloads: { types.SyncPayload }
+
+ server.addSignalsToClient(client, { a = a, b = b, c = c })
+ server.connect(function(...)
+ target, payloads = ...
+ end)
+
+ server.flush()
+ assert(target == client, "Expected target to be the client")
+ assert(payloads[1].type == "init", "Expected first payload to be init")
+ assert(payloads[1].data.a == 0, "Expected init payload to contain atom 'a'")
+ assert(payloads[1].data.b == 0, "Expected init payload to contain atom 'b'")
+ assert(payloads[1].data.c == 0, "Expected init payload to contain atom 'c'")
+
+ a(1)
+ a(2)
+ b(2)
+ server.flush()
+ assert(target == client, "Expected target to be the client")
+ assert(payloads[1].type == "patch", "Expected first payload to be patch")
+ assert(payloads[1].data.a == 1, "Expected patch payload to contain atom 'a'")
+ assert(payloads[2].data.a == 2, "Expected patch payload to contain atom 'a'")
+ assert(payloads[3].data.b == 2, "Expected patch payload to contain atom 'b'")
+ end)
+
+ test("should replicate granular state changes (reactive)", function()
+ local client = {}
+ local a = DeepCharm.reactive({ value = 0 })
+ local b = DeepCharm.reactive({ value = 0 })
+ local c = DeepCharm.reactive({ value = 0 })
+ local target: unknown, payloads: { types.SyncPayload }
+
+ server.addSignalsToClient(client, { a = a, b = b, c = c })
+ server.connect(function(...)
+ target, payloads = ...
+ end)
+
+ server.flush()
+ assert(target == client, "Expected target to be the client")
+ assert(payloads[1].type == "init", "Expected first payload to be init")
+ assert(payloads[1].data.a.value == 0, "Expected init payload to contain atom 'a'")
+ assert(payloads[1].data.b.value == 0, "Expected init payload to contain atom 'b'")
+ assert(payloads[1].data.c.value == 0, "Expected init payload to contain atom 'c'")
+
+ a.value = 1
+ a.value = 2
+ b.value = 2
+ server.flush()
+ assert(target == client, "Expected target to be the client")
+ assert(payloads[1].type == "patch", "Expected first payload to be patch")
+ assert(payloads[1].data.a.value == 1, "Expected patch payload to contain atom 'a'")
+ assert(payloads[2].data.a.value == 2, "Expected patch payload to contain atom 'a'")
+ assert(payloads[3].data.b.value == 2, "Expected patch payload to contain atom 'b'")
+ end)
+
+ test("should init new clients", function()
+ local clientA = {}
+ local clientB = {}
+ local a = Charm.atom(0)
+ local payloadsA: { types.SyncPayload }, payloadsB: { types.SyncPayload }
+
+ server.addSignalsToClient(clientA, { a = a })
+ server.connect(function(target, payloads)
+ if target == clientA then
+ payloadsA = payloads
+ elseif target == clientB then
+ payloadsB = payloads
+ end
+ end)
+
+ a(1)
+ a(2)
+ server.flush()
+ assert(payloadsA[1].type == "init", "Expected first payload to be init for client A")
+ assert(payloadsA[1].data.a == 1, "Expected init payload to contain atom 'a' for client A")
+ assert(payloadsA[2].type == "patch", "Expected second payload to be patch for client A")
+ assert(payloadsA[2].data.a == 2, "Expected second patch payload to contain atom 'a' with value 1 for client A")
+
+ server.addSignalsToClient(clientB, { a = a })
+ a(3)
+ a(4)
+ server.flush()
+ assert(payloadsA[1].type == "patch", "Expected first payload to be patch for client A")
+ assert(payloadsA[1].data.a == 3, "Expected patch payload to contain atom 'a' for client A")
+ assert(payloadsA[2].data.a == 4, "Expected patch payload to contain atom 'a' for client A")
+ assert(payloadsB[1].type == "init", "Expected first payload to be init for client B")
+ assert(payloadsB[1].data.a == 3, "Expected init payload to contain atom 'a' for client B")
+ assert(payloadsB[2].type == "patch", "Expected second payload to be patch for client B")
+ assert(payloadsB[2].data.a == 4, "Expected second patch payload to contain atom 'a' with value 3 for client B")
+ end)
+
+ test("should init new atoms", function()
+ local client = {}
+ local a = Charm.atom(0)
+ local b = Charm.atom(0)
+ local payloads: { types.SyncPayload }
+
+ server.addSignalsToClient(client, { a = a })
+ server.connect(function(_, ...)
+ payloads = ...
+ end)
+
+ server.flush()
+ assert(payloads[1].type == "init", "Expected first payload to be init")
+ assert(payloads[1].data.a == 0, "Expected init payload to contain atom 'a'")
+
+ server.addSignalsToClient(client, { b = b })
+ a(1)
+ a(2)
+ b(1)
+ b(2)
+ server.flush()
+ assert(payloads[1].type == "patch", "Expected first payload to be patch")
+ assert(payloads[1].data.a == 1, "Expected patch payload to contain atom 'a'")
+ assert(payloads[2].data.a == 2, "Expected patch payload to contain atom 'a'")
+ assert(payloads[3].type == "init", "Expected third payload to be init")
+ assert(payloads[3].data.b == 1, "Expected init payload to contain atom 'b'")
+ assert(payloads[4].type == "patch", "Expected fourth payload to be patch")
+ assert(payloads[4].data.b == 2, "Expected patch payload to contain atom 'b'")
+ end)
+
+ test("should not send unchanged state", function()
+ local client = {}
+ local a = Charm.atom(0)
+ local payloads: { types.SyncPayload }
+
+ server.addSignalsToClient(client, { a = a })
+ server.connect(function(_, p)
+ payloads = p
+ end)
+
+ server.flush()
+ assert(payloads[1].type == "init", "Expected first payload to be init")
+ assert(payloads[1].data.a == 0, "Expected init payload to contain atom 'a'")
+
+ payloads = {}
+ a(0)
+ server.flush()
+ assert(#payloads == 0, "Expected no payloads to be sent for unchanged state")
+ end)
+end)
+
+return {}
diff --git a/packages/charm-sync/test/utils.test.luau b/packages/charm-sync/test/utils.test.luau
new file mode 100644
index 0000000..15dcabf
--- /dev/null
+++ b/packages/charm-sync/test/utils.test.luau
@@ -0,0 +1,22 @@
+local Charm = require("../Charm")
+local test = require("../../../tests/test")
+local utils = require("../src/utils")
+
+test("should flatten functions", function()
+ local input = {
+ a = Charm.atom(0),
+ b = { c = Charm.atom(0) },
+ d = { e = { f = utils.signalToAtom(Charm.signal(0)) } },
+ }
+
+ local result = utils.flatten<<{ [string]: Charm.Atom }>>(input)
+
+ assert(result.a == input.a, "should flatten the first level")
+ assert(result["b/c"] == input.b.c, "should flatten the second level")
+ assert(result["d/e/f"] == input.d.e.f, "should flatten the third level")
+
+ assert(result.b == nil, "should remove the original nested tables")
+ assert(result.d == nil, "should remove the original nested tables")
+end)
+
+return {}
diff --git a/packages/charm-sync/wally.toml b/packages/charm-sync/wally.toml
index f1a78f0..28a6fc9 100644
--- a/packages/charm-sync/wally.toml
+++ b/packages/charm-sync/wally.toml
@@ -1,8 +1,15 @@
[package]
name = "littensy/charm-sync"
-version = "0.3.0"
+version = "0.4.0-rc.7"
registry = "https://github.com/UpliftGames/wally-index"
realm = "shared"
+exclude = [
+ "node_modules",
+ "test",
+ "src/index.d.ts",
+ "package.json",
+ "Charm.luau",
+]
[dependencies]
-charm = "littensy/charm@^0.10"
+Charm = "littensy/charm@^0.11.0-rc.6"
diff --git a/packages/charm/default.project.json b/packages/charm/default.project.json
index 89d3ada..a485e4d 100644
--- a/packages/charm/default.project.json
+++ b/packages/charm/default.project.json
@@ -1,5 +1,5 @@
{
- "name": "Charm",
+ "name": "charm",
"tree": {
"$path": "src"
}
diff --git a/packages/charm/package.json b/packages/charm/package.json
index ea40fa6..28f5c7c 100644
--- a/packages/charm/package.json
+++ b/packages/charm/package.json
@@ -1,13 +1,24 @@
{
"name": "@rbxts/charm",
- "version": "0.10.0",
- "description": "An atomic state management library for Roblox",
- "repository": "littensy/charm",
+ "version": "0.11.0-rc.6",
+ "description": "Atomic state management based on alien-signals",
"license": "MIT",
"main": "src/init.luau",
"types": "src/index.d.ts",
+ "files": [
+ "src",
+ "*.luau",
+ "!wally.toml",
+ "!wally.lock",
+ "!default.project.json",
+ "!test"
+ ],
+ "repository": {
+ "type": "git",
+ "url": "git+https://github.com/littensy/charm.git"
+ },
"scripts": {
- "prepare": "cp ../../LICENSE.md . && cp ../../README.md .",
+ "prepare": "cp ../../LICENSE . && cp ../../README.md .",
"publish:wally": "wally publish"
}
}
diff --git a/packages/charm/src/atom.luau b/packages/charm/src/atom.luau
deleted file mode 100644
index f10dbd6..0000000
--- a/packages/charm/src/atom.luau
+++ /dev/null
@@ -1,42 +0,0 @@
-local store = require(script.Parent.store)
-local types = require(script.Parent.types)
-type Atom = types.Atom
-type AtomOptions = types.AtomOptions
-
---[=[
- Creates a new atom with the given state.
-
- @param state The initial state.
- @param options Optional configuration.
- @return A new atom.
-]=]
-local function atom(state: T, options: AtomOptions?): Atom
- local equals = options and options.equals
-
- local function atom(...)
- if select("#", ...) == 0 then
- local index = store.capturing.index
-
- if index > 0 then
- store.capturing.stack[index][atom] = true
- end
-
- return state
- end
-
- local nextState = store.peek(..., state)
-
- if state ~= nextState and not (equals and equals(state, nextState)) then
- state = nextState
- store.notify(atom)
- end
-
- return state
- end
-
- store.listeners[atom] = setmetatable({}, { __mode = "v" })
-
- return atom
-end
-
-return atom
diff --git a/packages/charm/src/computed.luau b/packages/charm/src/computed.luau
deleted file mode 100644
index e786c45..0000000
--- a/packages/charm/src/computed.luau
+++ /dev/null
@@ -1,37 +0,0 @@
-local atom = require(script.Parent.atom)
-local store = require(script.Parent.store)
-local types = require(script.Parent.types)
-type AtomOptions = types.AtomOptions
-type Selector = types.Selector
-
---[=[
- Creates a read-only atom that derives its state from one or more atoms.
- Used to avoid unnecessary recomputations if multiple listeners depend on
- the same atoms.
-
- @param callback The function that produces the state.
- @param options Optional configuration.
- @return A new read-only atom.
-]=]
-local function computed(callback: Selector, options: AtomOptions?): Selector
- local dependencies, state = store.capture(callback)
- local computedAtom = atom(state, options)
- local computedRef = setmetatable({ current = computedAtom }, { __mode = "v" })
-
- local function listener()
- local computedAtom = computedRef.current
-
- if computedAtom then
- store.disconnect(dependencies, listener)
- dependencies, state = store.capture(callback)
- store.connect(dependencies, listener, computedAtom)
- computedAtom(state)
- end
- end
-
- store.connect(dependencies, listener, computedAtom)
-
- return computedAtom
-end
-
-return computed
diff --git a/packages/charm/src/effect.luau b/packages/charm/src/effect.luau
deleted file mode 100644
index 23f3294..0000000
--- a/packages/charm/src/effect.luau
+++ /dev/null
@@ -1,53 +0,0 @@
-local store = require(script.Parent.store)
-
-type Cleanup = () -> ()
-
---[=[
- Runs the given callback immediately and whenever any atom it depends on
- changes. Returns a cleanup function that unsubscribes the callback.
-
- @param callback The function to run.
- @return A function that unsubscribes the callback.
-]=]
-local function effect(callback: (cleanup: Cleanup) -> Cleanup?): Cleanup
- local dependencies = {}
- local cleanup: Cleanup?
- local disconnected = false
- local disconnect
-
- local function listener()
- if cleanup then
- cleanup()
- end
-
- store.disconnect(dependencies, listener)
- dependencies, cleanup = store.capture(callback, disconnect)
-
- if not disconnected then
- store.connect(dependencies, listener)
- end
- end
-
- function disconnect()
- if disconnected then
- return
- end
-
- disconnected = true
- store.disconnect(dependencies, listener)
-
- if cleanup then
- cleanup()
- end
- end
-
- dependencies, cleanup = store.capture(callback, disconnect)
-
- if not disconnected then
- store.connect(dependencies, listener)
- end
-
- return disconnect
-end
-
-return effect :: (callback: ((cleanup: Cleanup) -> ()) | ((cleanup: Cleanup) -> Cleanup?)) -> Cleanup
diff --git a/packages/charm/src/index.d.ts b/packages/charm/src/index.d.ts
index 8336177..4e4ad8d 100644
--- a/packages/charm/src/index.d.ts
+++ b/packages/charm/src/index.d.ts
@@ -1,187 +1,280 @@
-export = Charm;
-export as namespace Charm;
+type Key = string | number | symbol;
type Cleanup = () => void;
-declare namespace Charm {
- /**
- * A primitive state container that can be read from or written to. When
- * the state changes, all subscribers are notified.
- *
- * @param state The next state or a function that produces the next state.
- * @returns The current state.
- */
- interface Atom extends Selector {
- (nextState: State | ((prevState: State) => State)): State;
- }
-
- /**
- * A function that depends on one or more atoms and produces a value. Can be
- * used to derive state from atoms.
- *
- * @returns The current state.
- */
- type Selector = () => State;
-
- /**
- * A function that depends on one or more atoms and produces a value. Can be
- * used to derive state from atoms.
- *
- * @deprecated Use `Selector` instead.
- * @returns The current state.
- */
- type Molecule = Selector;
-
- /**
- * Infers the type of the state produced by the given function.
- */
- type StateOf = T extends Selector ? State : never;
-
- interface AtomOptions {
- /**
- * A function that determines whether the state has changed. By default,
- * a strict equality check (`===`) is used.
- */
- equals?: (prev: State, next: State) => boolean;
- }
-
- /**
- * Creates a new atom with the given state.
- *
- * @param state The initial state.
- * @param options Optional configuration.
- * @returns A new atom.
- */
- function atom(state: State, options?: AtomOptions): Atom;
-
- // Overload for no arguments
- function atom(state?: State, options?: AtomOptions): Atom;
-
- /**
- * Creates a read-only atom that derives its state from one or more atoms.
- * Used to avoid unnecessary recomputations if multiple listeners depend on
- * the same atoms.
- *
- * @param callback The function that produces the state.
- * @param options Optional configuration.
- * @returns A new read-only atom.
- */
- function computed(callback: Selector, options?: AtomOptions): Selector;
-
- /**
- * Subscribes to changes in the given atom or selector. The callback is
- * called with the current state and the previous state immediately after a
- * change occurs.
- *
- * @param callback The atom or selector to subscribe to.
- * @param listener The function to call when the state changes.
- * @returns A function that unsubscribes the callback.
- */
- function subscribe(callback: Selector, listener: (state: State, prev: State) => void): Cleanup;
-
- /**
- * Runs the given callback immediately and whenever any atom it depends on
- * changes. Returns a cleanup function that unsubscribes the callback.
- *
- * @param callback The function to run.
- * @returns A function that unsubscribes the callback.
- */
- function effect(callback: (cleanup: Cleanup) => Cleanup | void): Cleanup;
-
- /**
- * Returns the result of the function without subscribing to changes. If a
- * non-function value is provided, it is returned as is.
- *
- * @param callback The atom or selector to get the state of.
- * @param args Arguments to pass to the function.
- * @returns The current state.
- */
- function peek(callback: State | ((...args: Args) => State), ...args: Args): State;
-
- /**
- * Returns whether the given value is an atom.
- *
- * @param value The value to check.
- * @returns `true` if the value is an atom, otherwise `false`.
- */
- function isAtom(value: unknown): value is Atom;
-
- /**
- * Runs the given function and schedules listeners to be notified only once
- * after the function has completed. Useful for batching multiple changes.
- *
- * @param callback The function to run.
- */
- function batch(callback: () => void): void;
-
- /**
- * Captures all atoms that are read during the function call and returns them
- * along with the result of the function. Useful for tracking dependencies.
- *
- * @param callback The function to run.
- * @returns A tuple containing the captured atoms and the result of the function.
- */
- function capture(callback: Selector): LuaTuple<[dependencies: Set>, state: State]>;
-
- /**
- * Notifies all subscribers of the given atom that the state has changed.
- * Mainly for running effects, since subscriptions check for equality.
- *
- * @param atom The atom to notify.
- */
- function notify(atom: Atom): void;
-
- /**
- * Creates an instance of `factory` for each item in the atom's state, and
- * cleans up the instance when the item is removed. Returns a cleanup function
- * that unsubscribes all instances.
- *
- * @param callback The atom or selector to observe.
- * @param factory The function that tracks the lifecycle of each item.
- * @returns A function that unsubscribes all instances.
- */
- function observe- (
- callback: Selector,
- factory: (item: Item, index: number) => Cleanup | void,
- ): Cleanup;
-
- function observe(
- callback: Selector