diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml
index 912f2ea..af91488 100644
--- a/.github/workflows/ci.yml
+++ b/.github/workflows/ci.yml
@@ -14,19 +14,11 @@ jobs:
run_install: true
- uses: CompeyDev/setup-rokit@v0.1.2
- - name: Setup Lute
- run: lute setup --with-luaurc
-
- name: Check code quality
- run: lute run check
-
- - name: Run ESLint and Prettier
- run: |
- pnpm eslint packages
- pnpm prettier --check packages
+ run: sh bin/check.sh
- name: Run tests
- run: lute run test
+ run: lune run test/spec.server.luau
- name: Build models
- run: lute run build
+ run: sh bin/build.sh
diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml
index 3bd512d..5bdee21 100644
--- a/.github/workflows/release.yml
+++ b/.github/workflows/release.yml
@@ -11,11 +11,8 @@ jobs:
- uses: actions/checkout@v4
- uses: CompeyDev/setup-rokit@v0.1.2
- - name: Setup Lute
- run: lute setup --with-luaurc
-
- name: Build models
- run: lute run build
+ run: sh bin/build.sh
- name: Upload release assets
uses: AButler/upload-release-assets@v3.0
diff --git a/.gitignore b/.gitignore
index d217e96..52e0af1 100644
--- a/.gitignore
+++ b/.gitignore
@@ -2,7 +2,7 @@
/.pesde
/build
/packages/**/wally.lock
-/packages/**/LICENSE*
+/packages/**/LICENSE.md
/packages/**/README.md
/sourcemap.json
node_modules
diff --git a/.gitmodules b/.gitmodules
index 409278b..e1cba9d 100644
--- a/.gitmodules
+++ b/.gitmodules
@@ -1,3 +1,3 @@
-[submodule "tests/test"]
- path = tests/test
+[submodule "test/suite"]
+ path = test/suite
url = https://gist.github.com/littensy/fa9afcc5d66d25e86538634982be4ee4
diff --git a/.luaurc b/.luaurc
index c48b0f2..4c8043d 100644
--- a/.luaurc
+++ b/.luaurc
@@ -1,7 +1,6 @@
{
"languageMode": "strict",
- "aliases": {
- "lute": "~/.lute/typedefs/1.0.0/lute",
- "std": "~/.lute/typedefs/1.0.0/std"
+ "lint": {
+ "LocalShadow": false
}
}
diff --git a/.lute/build.luau b/.lute/build.luau
deleted file mode 100644
index b708003..0000000
--- a/.lute/build.luau
+++ /dev/null
@@ -1,6 +0,0 @@
-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
deleted file mode 100644
index b5ad31b..0000000
--- a/.lute/check.luau
+++ /dev/null
@@ -1,33 +0,0 @@
-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
deleted file mode 100644
index 4abbbb9..0000000
--- a/.lute/test.luau
+++ /dev/null
@@ -1 +0,0 @@
-(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
deleted file mode 100644
index d10b611..0000000
--- a/LICENSE
+++ /dev/null
@@ -1,21 +0,0 @@
-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
new file mode 100644
index 0000000..ae1bb3d
--- /dev/null
+++ b/LICENSE.md
@@ -0,0 +1,7 @@
+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 07d9641..46e04d8 100644
--- a/README.md
+++ b/README.md
@@ -4,7 +4,7 @@
Charm
- Fine-grained reactivity for Roblox
+ Manage state with reactive signals
npm package →
@@ -14,26 +14,25 @@

[](https://www.npmjs.com/package/@rbxts/charm)
-[](LICENSE)
+[](LICENSE.md)
-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.
+**Charm** is a Roblox state management library inspired by the [reactive signals](https://preactjs.com/blog/introducing-signals/) from libraries like Preact and Solid. It's built on [alien-signals](https://github.com/stackblitz/alien-signals), modified for a wide range of use cases: from rendering user interfaces to handling game logic.
-**Build game state from simple building blocks:**
+**Charm works under a few core principles.**
-- 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 reactive signals: state containers that hold a value
+- React to state updates: reading a signal automatically subscribes to it
+- Combine multiple signals: derive new values from existing state that stay up-to-date
+- Fine-grained reactivity: make targeted updates in response to specific changes in the state
+- Data should be immutable: values are compared directly (`==`) to optimize change detection
-**Want to learn more about reactivity?**
+**Want to learn more about signals?**
-- 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
-
-[Migrating from an older version of Charm?](#migration)
+- https://angular.dev/guide/signals
Table of Contents
@@ -43,31 +42,25 @@ Charm is a reactive state management library designed for storing data in discre
- [`signal(initialValue, equals?)`](#signalinitialvalue-equals)
- [`computed(getter)`](#computedgetter)
- [`effect(callback)`](#effectcallback)
- - [Nested effects](#nested-effects)
+ - [`untracked(callback)`](#untrackedcallback)
+ - [`peek(callback)`](#peekcallback)
+ - [`batch(callback)`](#batchcallback)
- [`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)
+ - [`observe(getter, callback)`](#observegetter-callback)
+ - [`mapped(getter, mapper)`](#mappedgetter-mapper)
- [`onCleanup(callback, failSilently?)`](#oncleanupcallback-failsilently)
- [`atom(initialValue, equals?)`](#atominitialvalue-equals)
- - [`trigger(callback)`](#triggercallback)
- - [`flags`](#flags)
+ - [`recursive()`](#recursive)
+ - [`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)
@@ -76,19 +69,19 @@ Charm is a reactive state management library designed for storing data in discre
## At a Glance
```luau
-local getTodos, setTodos = signal({} :: { string })
+local getTodos, setTodos = signal({} :: { [number]: string })
local getQuery, setQuery = signal("")
-observe(getTodos, function(todo: string, index: number)
+observe(getTodos, function(todo, index)
local instance = Instance.new("TextLabel")
- local getText = computed(function()
- return getTodos()[index] or ""
+ local getText = computed(function(prevText)
+ return getTodos()[index] or prevText
end)
effect(function()
instance.Text = getText()
- instance.Visible = string.match(getText(), getQuery()) ~= nil
+ instance.Visible = getText():match(getQuery()) ~= nil
end)
instance.LayoutOrder = index
@@ -101,31 +94,30 @@ observe(getTodos, function(todo: string, index: number)
end)
setTodos({ "Buy milk", "Buy eggs", "Play Roblox" })
-setQuery("Buy")
+setQuery("^Buy")
```
Explain code
```luau
--- Declare state for a todo list and a search query
-local getTodos, setTodos = signal({} :: { string })
+local getTodos, setTodos = signal({} :: { [number]: 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 ""
+ -- Create a computed signal that stores the current text, or the previous
+ -- value if the item was removed
+ local getText = computed(function(prevText)
+ return getTodos()[index] or prevText
end)
- -- Update instance properties when the text or query updates
+ -- Set properties when the text or query updates
effect(function()
instance.Text = getText()
- instance.Visible = string.match(getText(), getQuery()) ~= nil
+ instance.Visible = getText():match(getQuery()) ~= nil
end)
instance.LayoutOrder = index
@@ -140,8 +132,8 @@ end)
-- Add items to the todo list
setTodos({ "Buy milk", "Buy eggs", "Play Roblox" })
--- Filter for items containing "Buy"
-setQuery("Buy")
+-- Filter for items starting with "Buy"
+setQuery("^Buy")
```
@@ -155,7 +147,6 @@ pnpm add @rbxts/charm
```
```toml
-# wally.toml
[dependencies]
Charm = "littensy/charm@VERSION"
```
@@ -171,11 +162,11 @@ Signals are the core of reactivity in Charm. The `signal` function creates a rea
```luau
local getCounter, setCounter = signal(0)
+print(getCounter()) -- 0
setCounter(1)
setCounter(function(count)
return count + 1
end)
-print(getCounter()) -- 2
```
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.
@@ -193,7 +184,42 @@ setMax(-1) -- 2
```
> [!NOTE]
-> Looking for atoms? You can still use [`atom()`](#atominitialvalue-equals) to create a signal with a unified getter and setter.
+> Looking for atoms? You can still use [`atom()`](#atominitialvalue-equals) to create a signal with a combined getter and setter.
+
+---
+
+### `computed(getter)`
+
+The `computed` function creates a new signal with a value derived from multiple signals. The computed signal can be reacted to like a normal signal, and it returns the getter function's last return value.
+
+The computed signal will subscribe to signals accessed by the getter and track them as dependencies. Changes to these dependencies will re-execute the getter, and if it returns a new value, then the computed signal is updated to that value.
+
+```luau
+local getName, setName = signal("John")
+local getSurname, setSurname = signal("Doe")
+local getFullName = computed(function()
+ return `{getName()} {getSurname()}`
+end)
+
+print(getFullName()) -- "John Doe"
+setName("Jane")
+print(getFullName()) -- "Jane Doe"
+```
+
+The getter function also receives the previous state (or `nil` if running for the first time). You can use this for more complex state:
+
+```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
+```
+
+Computed signals are updated lazily, meaning the getter will only re-execute once the computed signal is called _and_ if a dependency updated since the last call.
---
@@ -204,9 +230,10 @@ Effects are fundamental to reactivity, allowing you to react to signal updates.
```luau
local getCounter, setCounter = signal(0)
+-- Count is 0
effect(function()
print(`Count is {getCounter()}`)
-end) -- Count is 0
+end)
setCounter(1) -- Count is 1
```
@@ -227,29 +254,20 @@ setCounter(1) -- Cleanup 0
dispose() -- Cleanup 1
```
-### Nested Effects
-
-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:
+Effects can be nested and they will clean up automatically when the parent effect re-runs or gets disposed. This means you don't have to manually clean up inner effects, and this applies to functions like `subscribe()` and `observe()`.
```luau
-local getPrintCount, setPrintCount = signal(true)
-local getCount, setCount = signal(1)
+local getCounter, setCounter = signal(0)
effect(function()
- if getPrintCount() then
- -- This inner effect is created when getPrintCount() is true
- effect(function()
- print(`Count is {getCount()}`)
- end)
- end
-end) -- Count is 1
+ print(`Outer: {getCounter()}`)
+ effect(function()
+ print(`Inner: {getcounter()}`)
+ end)
+end)
-setCount(2) -- Count is 2
-
--- This re-runs the outer effect and cleans up old inner effects
-setPrintCount(false)
-
-setCount(3) -- No output
+setCounter(1) -- Outer: 1, Inner: 1
+setCounter(2) -- Outer: 2, Inner: 2
```
> [!NOTE]
@@ -257,40 +275,86 @@ setCount(3) -- No output
---
-### `computed(getter)`
+### `untracked(callback)`
-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.
+In case you want to read signals but don't want to subscribe to them, you can use `untracked()` to essentially run code _outside_ the current effect, preventing signals and effects in the callback from being tracked.
+
+```luau
+local getTracked, setTracked = signal(0)
+local getUntracked, setUntracked = signal(0)
+
+-- Tracked: 0, Untracked: 0
+effect(function()
+ print(`Tracked: {getTracked()}, Untracked: {untracked(getUntracked)}`)
+end)
+
+setTracked(1) -- Tracked: 1, Untracked: 0
+setUntracked(1) -- No output
+setTracked(2) -- Tracked: 2, Untracked: 1
+```
+
+Because `untracked()` executes the callback outside the current effect, nested effects created in the callback do not get tracked by the parent effect:
+
+```luau
+local stopEffect
+local stopScope = effectScope(function()
+ untracked(function()
+ stopEffect = effect(function()
+ return function()
+ print("Cleaned up untracked effect")
+ end
+ end)
+ end)
+end)
+
+stopScope() -- No output, the scope did not track the effect
+stopEffect() -- Cleaned up untracked effect
+```
+
+---
+
+### `peek(callback)`
+
+Similar to `untracked()`, but does not prevent the parent effect from tracking nested effects created in the callback. If your callback reads a signal and creates effects, only the signals get untracked.
+
+```luau
+local getCounter, setCounter = signal(0)
+
+-- Count is 0
+local disposeOuter = effect(function()
+ print(`Count is {peek(getCounter)}`)
+end)
+
+setCounter(1) -- No output; count was accessed in peek()
+```
+
+---
+
+### `batch(callback)`
+
+Combines multiple signal updates made by the callback into a single commit that triggers effects once the callback completes.
```luau
local getName, setName = signal("John")
local getSurname, setSurname = signal("Doe")
-local getFullName = computed(function()
- return `{getName()} {getSurname()}`
+
+effect(function()
+ print(`Full name: {getName()} {getSurname()}`)
end)
-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)
+-- Combines both writes into a single update.
+-- Once the callback completes, outputs "Full name: Foo Bar"
+batch(function()
+ setName("Foo")
+ setSurname("Bar")
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.
+Scopes allow you to dispose multiple effects at once. The `effectScope` function creates an effect with no dependencies, and effects created inside the callback clean up when the effect scope disposes.
```luau
local getCounter, setCounter = signal(0)
@@ -322,77 +386,15 @@ The callback also receives the previous value, or `nil` when running for the fir
```luau
local getCounter, setCounter = signal(0)
+-- Count is 0 (was nil)
listen(getCounter, function(count, prevCount)
print(`Count is {count} (was {prevCount})`)
-end) -- Count is 0 (was nil)
+end)
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) -- Added a, Added b
-
-setItems({ a = 0, c = 0 }) -- Removed b, Added c
-```
-
-The callback runs in an effect scope, so effects created in the callback will be disposed when the key is removed:
-
-```luau
-local getItems, setItems = signal({})
-
-local dispose = observe(getItems, function(value, key)
- local getValue = computed(function(prevValue)
- return getItems()[key] or prevValue
- end)
-
- effect(function()
- local value = getValue()
- print(`Set {key} = {value}`)
- return function()
- print(`Cleanup {key} = {value}`)
- end
- end)
-end)
-
-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
-```
+Note that nested effects can be created inside `listen`, and they will clean up automatically when the listener re-runs or gets disposed.
---
@@ -411,72 +413,59 @@ end)
setCounter(1) -- Count is 1 (was 0)
```
+Note that nested effects can be created inside `subscribe`, and they will clean up automatically when the subscription re-runs or gets disposed.
+
---
-### `untracked(callback)`
+### `observe(getter, callback)`
-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.
+[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 getTracked, setTracked = signal(0)
-local getUntracked, setUntracked = signal(0)
+local getItems, setItems = signal({ a = 0, b = 0 })
-effect(function()
- print(`Tracked: {getTracked()}, Untracked: {untracked(getUntracked)}`)
-end) -- Tracked: 0, Untracked: 0
+-- Added a, Added b
+observe(getItems, function(value, key)
+ print(`Added {key}`)
+ return function()
+ print(`Removed {key}`)
+ end
+end)
-setTracked(1) -- Tracked: 1, Untracked: 0
-setUntracked(1) -- No output
-setTracked(2) -- Tracked: 2, Untracked: 1
+setItems({ a = 0, c = 0 }) -- Removed b, Added c
```
-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:
+The callback runs in an effect scope, so effects created in the callback will be disposed when the key is removed:
```luau
-local stopEffect
-local stopScope = effectScope(function()
- untracked(function()
- stopEffect = effect(function()
- return function()
- print("Cleaned up untracked effect")
- end
- end)
+local getItems, setItems = signal({ a = 0, b = 0 })
+
+local dispose = observe(getItems, function(value, key)
+ local getValue = computed(function(prevValue)
+ return getItems()[key] or prevValue
+ end)
+
+ effect(function()
+ local value = getValue()
+ print(`{key} = {value}`)
+ return function()
+ print(`Cleanup {key} = {value}`)
+ end
end)
end)
-stopScope() -- No output, the scope did not track the effect
-stopEffect() -- Cleaned up untracked effect
+setItems({ a = 1, b = 0 }) -- Cleanup a = 0, a = 1
+setItems({ a = 1 }) -- Cleanup b = 0
+dispose() -- Cleanup a = 1
```
---
-### `batch(callback)`
+### `mapped(getter, mapper)`
-Combines multiple signal updates made by the callback into a single commit that triggers effects once the callback completes.
+The `mapped` function iterates over every key in a table and uses the mapper 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, the mapper is called for that key and its current value.
-```luau
-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()
- setName("Foo")
- setSurname("Bar")
-end)
-```
-
----
-
-### `mapped(getter, transform)`
-
-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.
-
-The first value returned by the transform function is used as the new value:
+The first value returned by the mapper is used as the new value:
```luau
local getList, setList = signal({ "a", "b", "c" })
@@ -488,7 +477,7 @@ end)
print(getUppercase()) -- { "A", "B", "C" }
```
-If the transform function returns two values, the second value is used as the new key:
+If the mapper returns two values, the second value is used as the new key:
```luau
local getList, setList = signal({ "a", "b", "c" })
@@ -506,7 +495,7 @@ print(getSwapped()) -- { a = 1, b = 2, c = 3 }
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.
+If there is no active effect, a warning is logged, unless `failSilently` is set to `true`.
```luau
local dispose = effectScope(function()
@@ -549,6 +538,26 @@ max(-1) -- 1
---
+### `recursive()`
+
+By design, Charm's reactive system [does not allow recursion](https://github.com/stackblitz/alien-signals/issues/90#issuecomment-3489711800) in effects or computed signals. In case you need to opt out of recursion checks for an effect, you can call `recursive()` at the top of the effect callback:
+
+```luau
+local getCounter, setCounter = signal(0)
+
+effect(function()
+ recursive()
+ print(getCounter())
+ setCounter(function(count)
+ return math.min(count + 1, 3)
+ end)
+end)
+
+-- Output: 0, 1, 2, 3
+```
+
+---
+
### `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:
@@ -596,13 +605,13 @@ print(total()) -- 2
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. |
+| Flag | Default | Description |
+| ----------------- | -------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------ |
+| strict | `true`/`false` | Enforces synchronous, non-yielding behavior in signals, effects, and other critical code. |
+| frozen | `true`/`false` | Enforces data immutability by deep-freezing tables passed to signals. |
+| 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.
+The `strict` and `frozen` flags are automatically enabled in Roblox Studio. More accurately, they are enabled for the Luau optimization levels `O1` and lower.
---
@@ -623,99 +632,68 @@ CharmSync = "littensy/charm-sync@VERSION"
### Quick Start
-Start by specifying the signals that the server should sync to clients. For this example, we'll replicate a round status and timer signal, and each player's inventory:
+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
--- store.luau
-
-local getStatus, setStatus = signal("intermission")
-local timerAtom = atom(20)
-local getInventories, setInventories = signal({} :: { [Username]: Inventory })
+-- nameStore
+local getName, setName = signal("John")
+local getSurname, setSurname = signal("Doe")
return {
- getStatus = getStatus,
- setStatus = setStatus,
- timerAtom = timerAtom,
- getInventories = getInventories,
- setInventories = setInventories,
+ getName = getName,
+ setName = setName,
+ getSurname = getSurname,
+ setSurname = setSurname,
}
```
-#### Server setup
+When a player joins on the server, call `server.addSignalsToClient` with the keyed signals that the client should receive updates for. Once they leave, call `server.removeClient` to unsubscribe them from all updates.
-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.
-
-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:
+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.
```luau
--- sync.server.luau
-
-playerReadyEvent.OnServerEvent:Connect(function(player)
- -- Sync signal getters, computed signals, atoms, or reactive proxies
+local function onPlayerAdded(player: Player)
+ -- Add signal getters, computed signals, or atoms
server.addSignalsToClient(player, {
- status = store.getStatus,
- timer = store.timerAtom,
- -- Signals unique to a player should also have a key unique to
- -- that player, to avoid overwriting other synced signals
- [`inventory-{player.UserId}`] = computed(function()
- return store.getInventories()[player.Name]
- end),
+ name = nameStore.getName,
+ surname = nameStore.getSurname,
})
-end)
+end
+
+for _, player in Players:GetPlayers() do
+ onPlayerAdded(player)
+end
+
+Players.PlayerAdded:Connect(onPlayerAdded)
Players.PlayerRemoving:Connect(function(player)
server.removeClient(player)
end)
server.connect(function(player, updates)
- -- Customize how you replicate state updates to clients
+ -- Customize how you send state updates to clients
syncEvent:FireClient(player, updates)
end)
-
--- Changes now get replicated to clients
-store.setStatus("started")
-store.timerAtom(30)
```
-> [!WARNING]
-> On the server, make sure each key corresponds to the same signal across all players. In other words, if you're syncing a signal that is unique to a player, ensure that the key is also unique to that player.
->
-> If two players subscribe to the same key, but were assigned different signals for that key, Charm will output a warning.
-
-#### Client setup
+> [!NOTE]
+> On the server, make sure each key corresponds to the same signal across all players. If two players subscribe to the same key, but were given different signals, Charm will output a warning.
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.
After the client receives updates from the server, call `client.patch` to patch the client's signals with the incoming state updates.
```luau
--- sync.client.luau
-
--- The server is replicating a signal containing the client's
--- inventory, so this signal will receive those updates
-local getInventory, setInventory = signal({} :: Inventory)
-
--- Add signal setters, atoms, or reactive proxies
+-- Add signal setters or atoms
client.addSignals({
- status = store.setStatus,
- timer = store.timerAtom,
- -- Signals unique to a player should also have a key unique to
- -- that player, to avoid name collisions
- [`inventory-{Players.LocalPlayer.UserId}`] = setInventory,
+ name = nameStore.setName,
+ surname = nameStore.setSurname,
})
-- Update client signals when receiving updates from the server
syncEvent.OnClientEvent:Connect(function(updates)
client.patch(updates)
end)
-
--- Notify the server that the client is ready to receive updates
-playerReadyEvent:FireServer()
-
--- The signals will now update to match the server's state
-subscribe(getInventory, function(inventory)
- print("My inventory is", table.concat(inventory))
-end)
```
---
@@ -728,49 +706,36 @@ A configuration table that customizes the behavior of Charm Sync on the server.
| --------------- | ------- | --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| 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). |
+| fixArrays | `true` | When `true`, Charm will attempt to work around Roblox remote event limitations regarding 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 throw an error. See the [remote argument limitations](https://create.roblox.com/docs/scripting/events/remote#argument-limitations). |
---
### `server.addSignalsToClient(client, getters)`
-The `addSignalsToClient` function replicates changes in the given signals to a specific client. When an update occurs, the client will receive a delta-compressed state patch containing only the values that changed.
+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 proxies](#reactiveinitialvalue) in the `getters` table. This function can also be called multiple times on the same client to subscribe to new signals.
+You can pass signal getter functions, computed signals, and atoms in the `getters` table. This function can also be called multiple on the same client to subscribe to new signals.
```luau
-playerReadyEvent.OnServerEvent:Connect(function(player)
+Players.PlayerAdded:Connect(function(player)
server.addSignalsToClient(player, {
- status = store.getStatus,
- timer = store.timerAtom,
+ name = nameStore.getName,
+ surname = nameStore.getSurname,
})
end)
```
-You're also allowed to create new signals to sync to specific players, as long as the key is unique to that signal:
+You're also allowed to create new signals to sync to specific players, as long as the key is also unique to that player:
```luau
-playerReadyEvent.OnServerEvent:Connect(function(player)
- server.addSignalsToClient(player, {
- -- 🟢 Good: Player-specific signals are synced with a unique key
- [`playerData-{player.UserId}`] = computed(function()
- return clientStore.getPlayerData(player.UserId)
- end),
-
- -- 🔴 Bad: Syncing different signals with the same keys does not work
- playerData = computed(function()
- return clientStore.getPlayerData(player.UserId)
- end),
- })
-end)
+server.addSignalsToClient(player, {
+ [`data-{player.UserId}`] = computed(function()
+ return getPlayerData(player.UserId)
+ end),
+})
```
-> [!NOTE]
-> On the server, make sure each key corresponds to the same signal across all players. In other words, if you're syncing a signal that is unique to a player, ensure that the key is also unique to that player.
->
-> If two players subscribe to the same key, but were assigned different signals for that key, Charm will output a warning.
-
---
### `server.removeSignalsFromClient(client, ...keys)`
@@ -779,11 +744,10 @@ The `removeSignalsFromClient` function unsubscribes the client from a list of ke
```luau
server.addSignalsToClient(player, {
- status = store.getStatus,
- timer = store.timerAtom,
+ surname = nameStore.getSurname,
})
-server.removeSignalsFromClient(player, "status")
+server.removeSignalsFromClient(player, "surname")
```
---
@@ -793,7 +757,7 @@ server.removeSignalsFromClient(player, "status")
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)
+Players.PlayerAdded:Connect(function(player)
server.addSignalsToClient(player, signals)
end)
@@ -840,15 +804,12 @@ server.flush()
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:
+You can pass either writable signals or atoms to this function:
```luau
client.addSignals({
- status = store.setStatus,
- timer = store.timerAtom,
- -- Signals unique to a player should also have a key unique to
- -- that player, to avoid name collisions
- [`inventory-{Players.LocalPlayer.UserId}`] = clientStore.setInventory,
+ name = nameStore.setName,
+ surname = nameStore.setSurname,
})
```
@@ -860,11 +821,10 @@ Unsubscribes from each signal with the corresponding keys. The signals will reta
```luau
client.addSignals({
- status = store.setStatus,
- timer = store.timerAtom,
+ name = nameStore.setName,
})
-client.removeSignals("status")
+client.removeSignals("name")
```
---
@@ -877,9 +837,8 @@ You should call `patch` when receiving updates from the server from a remote eve
```luau
client.addSignals({
- status = store.setStatus,
- timer = store.timerAtom,
- [`inventory-{Players.LocalPlayer.UserId}`] = clientStore.setInventory,
+ name = nameStore.setName,
+ surname = nameStore.setSurname,
})
syncEvent.OnClientEvent:Connect(function(updates)
@@ -889,144 +848,35 @@ 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 = {
- status = signalToAtom(store.getStatus, store.setStatus),
- inventories = signalToAtom(store.getInventories, store.setInventories),
-}
-
-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`.
+Charm Sync will only send clients the differences between the current state and the previously-synced state, which is a practice 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`. Charm addresses this by representing deleted values with a special `None` value denoted by `{ __none = "__none" }`.
+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
-```
+This means nilable values can be represented as `None`, and code working with update payloads (usually for remote argument serialization) should account for nilable values possibly being sent as `None` in the payload.
---
## Migration
-Charm v0.11 introduces several breaking changes, so this section should help you migrate from an older version.
+Charm v0.11 introduces _a lot_ of breaking changes, so below are some tips that might 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.
+
+Terminology changes
+
+- Signal: A state container with one function to get the state, and another to update it
+- Atom: A signal with the getter and setter combined into one function
+- Effect: Main way to react to state changes
+- `subscribe(getter, callback)`: Creates an effect that only subscribes to signals accessed in the getter
+
+
**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 second argument of `computed()` (you can do your own equality checks now)
- 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:
@@ -1034,39 +884,46 @@ For reference, a signal is a state container with a separated getter and setter
- 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:
+3. The [`strict` and `frozen` flags](#flags) are automatically enabled in Roblox Studio, so unsafe Charm code will start throwing errors if you didn't use the old `__DEV__` flag. The flags have the following behavior:
- `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()`.
+ - This feature applies to all reaction APIs, including the listener function in `subscribe()` and the observer function in `observe()`.
- 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`.
+ - This feature can introduce runtime bugs in migrated code. If you suspect this to be the cause, to help identify the issue, you can 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
+> [!NOTE]
+> An example of nested effects causing a bug is an old implementation of [`VideCharm.useAtom`](./packages/vide-charm/src/init.luau) that did not wrap the source update in `untracked()`. Because Vide effects run immediately after a source updates, Vide will notify components during the execution of the Charm effect in `useAtom`.
+>
+> This meant effects created as a side effect of a source update would implicitly get added as a child of the `useAtom` effect, and they could get disposed at the wrong time and desync UI.
+
+5. Recursion is now disallowed in effects and computed signals by default. This change may introduce bugs in code relying on the old behavior.
+ - Recursive checks are opt-out. You can allow recursion for a specific effect by calling [`recursive()`](#recursive) at the top of the effect callback.
+
+6. 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)!
+ - Added [`signal()`](#signalinitialvalue-equals) to make reads and writes more explicit
+ - Added [`listen()`](#listengetter-callback) to create a subscription that runs once immediately
+ - Added [`effectScope()`](#effectscopecallback) for collecting and cleaning up multiple effects at once
+ - Added [`onCleanup()`](#oncleanupcallback-failsilently) to bind a cleanup function to the active effect
+ - Added [`trigger()`](#triggercallback) to trigger updates for table mutations
+ - The [`computed()`](#computedgetter) callback now gets called with the previous computed value
+ - Optimized `computed()` to use lazy evaluation instead of eager updates
+ - Optimized `mapped()` to only map values that changed in the original table
---
## Examples
-- https://github.com/littensy/fishing-minigame: Fisch clone using Charm for server and client state
+Check out this simple [example project →](https://github.com/littensy/charm-example)
### 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 getCounter, setCounter = signal(0)
local function Counter()
- local count = ReactCharm.useSignalState(getCounter)
+ local count = useSignalState(getCounter)
return React.createElement("TextButton", {
[React.Event.Activated] = function()
@@ -1081,16 +938,10 @@ end
### Vide Counter
```luau
-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 getCounter, setCounter = signal(0)
local function Counter()
- local count = VideCharm.useSignalState(getCounter)
+ local count = useSignalState(getCounter)
return create "TextButton" {
Activated = function()
@@ -1109,11 +960,11 @@ end
---
-Charm is released under the MIT License.
+Charm is released under the MIT License.
-[](LICENSE)
+[](LICENSE.md)
diff --git a/benches/bench.luau b/benches/bench.luau
index ffb839d..9c24c6e 100644
--- a/benches/bench.luau
+++ b/benches/bench.luau
@@ -20,8 +20,8 @@ local function run(name: string, test: () -> ())
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`)
+ for name, elapsed in times do
+ table.insert(results, `{name}: \t{string.format("%.2f", elapsed / iter * 1000 * 1000)} us/iter`)
end
print(table.concat(results, " | "))
diff --git a/benches/mapped.luau b/benches/mapped.luau
index 695b7f5..6b6d5dc 100644
--- a/benches/mapped.luau
+++ b/benches/mapped.luau
@@ -3,80 +3,75 @@ local bench = require("./bench")
local computed = Charm.computed
local mapped = Charm.mapped
+local setActiveSub = Charm.setActiveSub
local signal = Charm.signal
-type Key = number
-type Value = number
+type Entry = { number }
local count = 512
-local initialState: { [Key]: Value } = table.create(count, 0)
+local initialState: { Entry } = table.create(count)
-local function expensiveOperation()
- for _ = 1, 100 do
- continue
- end
+for i = 1, count do
+ initialState[i] = table.create(64, i)
end
-local function simpleMapped(
- source: () -> { [Key]: Value },
- mapper: (Value, Key) -> (Value, Key?)
-): () -> { [Key]: Value }
+local function mapper(value: Entry, key: number): (Entry, number)
+ return table.clone(value), key + 1
+end
+
+local function simpleMapped(source: () -> { Entry }, mapper: (Entry, number) -> (Entry, number)): () -> { Entry }
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
+ for index, value in state do
+ local currentSub = setActiveSub(nil)
+ local success, valueOut, keyOut = pcall(mapper, value, index)
+ setActiveSub(currentSub)
+
+ if success then
+ nextState[keyOut] = valueOut
+ end
end
- return nextState
+ return state
end)
end
-local function updateRandomly(updateChance: number): ({ [Key]: Value }) -> { [Key]: Value }
+local function updatePercentage(percent: number): ({ Entry }) -> { Entry }
return function(state)
local nextState = table.clone(state)
- for key in state do
- if math.random() < updateChance then
- nextState[key] += 1
+ for index, value in state do
+ if math.random() < percent then
+ nextState[index] = table.clone(value)
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()
+for _, percent in { 0.1, 0.5, 1 } do
+ bench.run(`mapped {percent}`, function()
local src, setSrc = signal(initialState)
local out = mapped(src, mapper)
bench.profilebegin("init")
out()
bench.profileend()
-
- setSrc(updateRandomly(updateChance))
-
+ setSrc(updatePercentage(percent))
bench.profilebegin("update")
out()
bench.profileend()
end)
- bench.run(`control {updateChance}`, function()
+ bench.run(`control {percent}`, function()
local src, setSrc = signal(initialState)
local out = simpleMapped(src, mapper)
bench.profilebegin("init")
out()
bench.profileend()
-
- setSrc(updateRandomly(updateChance))
-
+ setSrc(updatePercentage(percent))
bench.profilebegin("update")
out()
bench.profileend()
diff --git a/benches/multi.luau b/benches/multi.luau
deleted file mode 100644
index 6c8235c..0000000
--- a/benches/multi.luau
+++ /dev/null
@@ -1,188 +0,0 @@
-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/patch.bench.luau b/benches/patch.bench.luau
new file mode 100644
index 0000000..dc8f918
--- /dev/null
+++ b/benches/patch.bench.luau
@@ -0,0 +1,54 @@
+local ReplicatedStorage = game:GetService("ReplicatedStorage")
+
+local CharmSync = require(ReplicatedStorage.packages["charm-sync"].src)
+
+local patch = CharmSync.patch
+
+local USERS = 1000
+
+local function generate()
+ local users = {}
+
+ for user = 1, USERS do
+ users[user] = {
+ coins = math.random(1, 2),
+ gems = math.random(1, 2),
+ }
+ end
+
+ return { users = users }
+end
+
+local function update(state)
+ state = table.clone(state)
+ state.users = table.clone(state.users)
+
+ for user = 1, USERS do
+ local rng = math.random()
+
+ if rng < 1 / 3 then
+ continue
+ elseif rng < 2 / 3 then
+ state.users[user] = nil
+ else
+ state.users[user] = {
+ coins = math.random(1, 2),
+ gems = math.random(1, 2),
+ }
+ end
+ end
+
+ return state
+end
+
+return {
+ ParameterGenerator = function()
+ local state = generate()
+ return state, update(state)
+ end,
+ Functions = {
+ diff = function(_, a, b)
+ patch.diff(a, b)
+ end,
+ },
+}
diff --git a/benches/reactive.luau b/benches/reactive.luau
deleted file mode 100644
index c272651..0000000
--- a/benches/reactive.luau
+++ /dev/null
@@ -1,88 +0,0 @@
-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
new file mode 100644
index 0000000..f680537
--- /dev/null
+++ b/bin/build.sh
@@ -0,0 +1,4 @@
+#!/usr/bin/env bash
+
+mkdir build
+rojo build build.project.json -o build/charm.rbxm
diff --git a/bin/check.sh b/bin/check.sh
new file mode 100644
index 0000000..091c4a4
--- /dev/null
+++ b/bin/check.sh
@@ -0,0 +1,19 @@
+curl -o bin/roblox.d.luau https://raw.githubusercontent.com/JohnnyMorganz/luau-lsp/main/scripts/globalTypes.d.luau
+
+rojo sourcemap -o sourcemap.json
+
+luau-lsp analyze \
+ --defs=bin/roblox.d.luau \
+ --flag:LuauFixIndexerSubtypingOrdering=true \
+ --flag:LuauInstantiateInSubtyping=true \
+ --sourcemap=sourcemap.json \
+ --ignore="**/node_modules/**" \
+ packages test benches
+
+selene packages test benches
+
+stylua --check packages test benches
+
+pnpm eslint packages
+
+rm bin/roblox.d.luau
diff --git a/build.project.json b/build.project.json
index 53d32b4..8141b86 100644
--- a/build.project.json
+++ b/build.project.json
@@ -4,7 +4,6 @@
"$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 85d8f55..43a7d1a 100644
--- a/default.project.json
+++ b/default.project.json
@@ -19,25 +19,16 @@
},
"charm": {
"$className": "Folder",
- "src": { "$path": "packages/charm/src" },
- "test": { "$path": "packages/charm/test" }
- },
- "deep-charm": {
- "$className": "Folder",
- "src": { "$path": "packages/deep-charm/src" },
- "test": { "$path": "packages/deep-charm/test" },
- "Charm": { "$path": "packages/deep-charm/Charm.luau" }
+ "src": { "$path": "packages/charm/src" }
},
"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" }
@@ -45,7 +36,6 @@
"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" }
}
@@ -53,8 +43,8 @@
"@rbxts-js": {
"$path": "node_modules/@rbxts-js"
},
- "tests": {
- "$path": "tests"
+ "test": {
+ "$path": "test"
}
}
}
diff --git a/eslint.config.js b/eslint.config.js
deleted file mode 100644
index bfccfb4..0000000
--- a/eslint.config.js
+++ /dev/null
@@ -1,20 +0,0 @@
-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/eslint.config.ts b/eslint.config.ts
new file mode 100644
index 0000000..21ece76
--- /dev/null
+++ b/eslint.config.ts
@@ -0,0 +1,20 @@
+import eslint from "@eslint/js";
+import { defineConfig, globalIgnores } from "eslint/config";
+import prettierRecommended from "eslint-plugin-prettier/recommended";
+import roblox from "eslint-plugin-roblox-ts";
+import tseslint from "typescript-eslint";
+
+export default 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/**"]),
+ prettierRecommended,
+);
diff --git a/package.json b/package.json
index d0bf03d..b9b8739 100644
--- a/package.json
+++ b/package.json
@@ -1,29 +1,30 @@
{
"private": true,
"scripts": {
- "test": "lute run test",
- "build": "lute run build",
- "check": "lute run check",
+ "test": "lune run test/spec.server.luau",
+ "build": "sh bin/build.sh",
+ "check": "sh bin/check.sh",
"publish:npm": "pnpm publish -r --access public",
"publish:wally": "pnpm -r --workspace-concurrency 1 publish:wally"
},
- "packageManager": "pnpm@12.8.1",
+ "packageManager": "pnpm@10.22.0",
"devDependencies": {
- "@eslint/js": "^9.39.4",
+ "@eslint/js": "^9.36.0",
"@rbxts/compiler-types": "3.0.0-types.0",
- "@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",
+ "@rbxts/react": "17.3.5-alpha.1",
+ "@rbxts/react-roblox": "17.3.5-alpha.1",
+ "@rbxts/types": "^1.0.883",
+ "@rbxts/vide": "^0.5.7",
+ "@typescript-eslint/eslint-plugin": "^8.44.1",
+ "@typescript-eslint/parser": "^8.44.1",
+ "eslint": "^9.36.0",
"eslint-config-prettier": "^10.1.8",
- "eslint-plugin-prettier": "^5.5.5",
- "eslint-plugin-roblox-ts": "^1.4.0",
- "prettier": "^3.8.1",
+ "eslint-plugin-prettier": "^5.5.4",
+ "eslint-plugin-roblox-ts": "^1.2.1",
+ "jiti": "^2.6.0",
+ "prettier": "^3.6.2",
"roblox-ts": "^3.0.0",
- "typescript": "^5.9.3",
- "typescript-eslint": "^8.58.0"
+ "typescript": "^5.9.2",
+ "typescript-eslint": "^8.44.1"
}
}
diff --git a/packages/charm-sync/Charm.luau b/packages/charm-sync/Charm.luau
index 480f2d5..9a55fe0 100644
--- a/packages/charm-sync/Charm.luau
+++ b/packages/charm-sync/Charm.luau
@@ -4,6 +4,6 @@ end
local Charm = require(script.Parent.Parent.charm.src)
-export type Atom = Charm.Atom
+export type Atom = Charm.Atom
return Charm
diff --git a/packages/charm-sync/package.json b/packages/charm-sync/package.json
index f2c84a3..d41be6c 100644
--- a/packages/charm-sync/package.json
+++ b/packages/charm-sync/package.json
@@ -1,6 +1,6 @@
{
"name": "@rbxts/charm-sync",
- "version": "0.4.1",
+ "version": "0.4.0-rc.1",
"description": "Simple server-to-client sync for Charm",
"license": "MIT",
"main": "src/init.luau",
@@ -11,17 +11,17 @@
"!wally.toml",
"!wally.lock",
"!default.project.json",
- "!test"
+ "!**/__tests__"
],
"repository": {
"type": "git",
"url": "git+https://github.com/littensy/charm.git"
},
"scripts": {
- "prepare": "cp ../../LICENSE . && cp ../../README.md .",
+ "prepare": "cp ../../LICENSE.md . && cp ../../README.md .",
"publish:wally": "wally publish"
},
"dependencies": {
- "@rbxts/charm": "workspace:^"
+ "@rbxts/charm": "workspace:*"
}
}
diff --git a/packages/charm-sync/test/client.test.luau b/packages/charm-sync/src/__tests__/client.spec.luau
similarity index 76%
rename from packages/charm-sync/test/client.test.luau
rename to packages/charm-sync/src/__tests__/client.spec.luau
index 20c41a8..29a172e 100644
--- a/packages/charm-sync/test/client.test.luau
+++ b/packages/charm-sync/src/__tests__/client.spec.luau
@@ -1,9 +1,11 @@
-local Charm = require("../Charm")
-local DeepCharm = require("../../deep-charm/src")
-local client = require("../src/client")
-local test = require("../../../tests/test")
+local Charm = require("../../Charm")
+local client = require("../client")
+local suite = require("../../../../test/suite")
-test.afterEach(function()
+local test = suite.test
+local afterEach = suite.afterEach
+
+afterEach(function()
client.removeAllSignals()
end)
@@ -38,31 +40,6 @@ test("should merge patch", function()
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 })
diff --git a/packages/charm-sync/test/e2e.test.luau b/packages/charm-sync/src/__tests__/e2e.spec.luau
similarity index 61%
rename from packages/charm-sync/test/e2e.test.luau
rename to packages/charm-sync/src/__tests__/e2e.spec.luau
index 62ca4a7..09c036c 100644
--- a/packages/charm-sync/test/e2e.test.luau
+++ b/packages/charm-sync/src/__tests__/e2e.spec.luau
@@ -1,11 +1,13 @@
-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")
+local Charm = require("../../Charm")
+local client = require("../client")
+local config = require("../config")
+local server = require("../server")
+local suite = require("../../../../test/suite")
-test.afterEach(function()
+local afterEach = suite.afterEach
+local test = suite.test
+
+afterEach(function()
server.reset()
client.removeAllSignals()
config.preserveHistory = false
@@ -14,9 +16,9 @@ end)
test("should replicate server state to client", function()
local target = {}
local serverA = Charm.atom({ count = 0 })
- local serverB = DeepCharm.reactive({ count = 0 })
+ local serverB = Charm.atom({ count = 0 })
local clientA = Charm.atom({ count = -1 })
- local clientB = DeepCharm.reactive({ count = -1 })
+ local clientB = Charm.atom({ count = -1 })
client.addSignals({ a = clientA, b = clientB })
server.addSignalsToClient(target, { a = serverA, b = serverB })
@@ -28,32 +30,27 @@ test("should replicate server state to client", function()
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")
+ assert(clientB().count == 0, "Client atom B should be initialized to 0")
serverA({ count = 1 })
- serverB.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)
+ assert(clientB().count == 1, "Client atom B should be updated to 1")
+ local prevClientB = clientB()
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")
+ assert(clientB() == prevClientB, "Client atom B should not be updated")
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 serverB = Charm.atom({ count = 0 })
local clientA = Charm.atom({ count = -1 })
- local clientB = DeepCharm.reactive({ count = -1 })
+ local clientB = Charm.atom({ count = -1 })
config.preserveHistory = true
@@ -70,28 +67,30 @@ test("should replicate historical server state to client", function()
Charm.effect(function()
local expected = table.remove(expectedA, 1)
- assert(expected == clientA().count, `Client reactive A should be {expected}`)
+ assert(expected == clientA().count, `Client atom A should be {expected}`)
end)
Charm.effect(function()
local expected = table.remove(expectedB, 1)
- assert(expected == clientB.count, `Client reactive B should be {expected}`)
+ assert(expected == clientB().count, `Client atom B should be {expected}`)
end)
server.flush()
serverA({ count = 1 })
serverA({ count = 2 })
- serverB.count = 1
- serverB.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")
+ assert(clientB().count == 2, "Client atom B should be updated to 2")
+ local prevClientB = clientB()
serverA({ count = 3 })
serverA({ count = 4 })
server.flush()
assert(clientA().count == 4, "Client atom A should be updated to 4")
+ assert(clientB() == prevClientB, "Client atom B should not be updated")
end)
return {}
diff --git a/packages/charm-sync/test/patch.test.luau b/packages/charm-sync/src/__tests__/patch.spec.luau
similarity index 66%
rename from packages/charm-sync/test/patch.test.luau
rename to packages/charm-sync/src/__tests__/patch.spec.luau
index 076544e..a62b456 100644
--- a/packages/charm-sync/test/patch.test.luau
+++ b/packages/charm-sync/src/__tests__/patch.spec.luau
@@ -1,6 +1,9 @@
-local config = require("../src/config")
-local patch = require("../src/patch")
-local test = require("../../../tests/test")
+local config = require("../config")
+local patch = require("../patch")
+local suite = require("../../../../test/suite")
+
+local test = suite.test
+local afterEach = suite.afterEach
local function mockArraySerdes(object: { [any]: any }): { [any]: any }
if object[1] then
@@ -20,7 +23,7 @@ local function mockArraySerdes(object: { [any]: any }): { [any]: any }
end
end
-test.afterEach(function()
+afterEach(function()
config.fixArrays = true
config.validatePatches = true
end)
@@ -28,17 +31,17 @@ end)
test("returns a patch", function()
local prevState = { foo = "bar", baz = "qux" }
local nextState = { foo = "bar", baz = "quux" }
- local diffs = patch.diff(prevState, nextState)
+ local patches = 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")
+ assert(patches ~= nil, "should return a patch")
+ assert(patches.baz == "quux", "should return the changed value")
+ assert(patches.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)
+ local patches = { baz = "quux", qaz = "qux" }
+ local applied = patch.apply(prevState, patches)
assert(applied ~= nil, "should return a new state")
assert(applied.baz == "quux", "should apply the changed value")
@@ -46,11 +49,11 @@ test("applies a patch", function()
assert(applied.qaz == "qux", "should add the new value")
end)
-test("allows nested diffs", function()
+test("allows nested patches", function()
local prevState = { foo = { bar = "baz" } }
local nextState = { foo = { bar = "qux" } }
- local diffs = patch.diff(prevState, nextState)
- local applied = patch.apply(prevState, diffs)
+ local patches = patch.diff(prevState, nextState)
+ local applied = patch.apply(prevState, patches)
assert(applied.foo.bar == "qux", "should apply the nested patch")
end)
@@ -75,8 +78,8 @@ test("preserves array density after serdes", function()
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)
+ local patches = mockArraySerdes(patch.diff(prevState, nextState))
+ local applied = patch.apply(prevState, patches)
for index in prevState do
assert(applied[index] == nextState[index], `expected {applied[index]} to be {nextState[index]}`)
@@ -93,10 +96,10 @@ test("cannot preserve sparse arrays", function()
local prevState = {}
local nextState = { [2] = "foo" }
- local diffs = mockArraySerdes(patch.diff(prevState, nextState))
- local applied = patch.apply(prevState, diffs)
+ local patches = mockArraySerdes(patch.diff(prevState, nextState))
+ local applied = patch.apply(prevState, patches)
- -- Not enough information is available to determine whether 'diffs'
+ -- Not enough information is available to determine whether 'patches'
-- 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")
@@ -116,18 +119,20 @@ test("optionally fixes sparse arrays", function()
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("optionally validates state", function()
+ local success = pcall(function()
+ config.validatePatches = true
+ patch.diff({}, { [1] = 1, a = true })
+ 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")
+ assert(not success, "should not allow mixed tables")
+
+ success = pcall(function()
+ config.validatePatches = false
+ patch.diff({}, { [1] = 1, a = true })
+ end)
+
+ assert(success, "should skip validation if not requested")
end)
return {}
diff --git a/packages/charm-sync/test/server.test.luau b/packages/charm-sync/src/__tests__/server.spec.luau
similarity index 60%
rename from packages/charm-sync/test/server.test.luau
rename to packages/charm-sync/src/__tests__/server.spec.luau
index d24970c..f26b753 100644
--- a/packages/charm-sync/test/server.test.luau
+++ b/packages/charm-sync/src/__tests__/server.spec.luau
@@ -1,12 +1,15 @@
-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")
+local Charm = require("../../Charm")
+local config = require("../config")
+local patch = require("../patch")
+local server = require("../server")
+local suite = require("../../../../test/suite")
-test.afterEach(function()
+local afterEach = suite.afterEach
+local beforeEach = suite.beforeEach
+local describe = suite.describe
+local test = suite.test
+
+afterEach(function()
server.reset()
end)
@@ -15,7 +18,7 @@ test("should replicate state changes", function()
local a = Charm.atom(0)
local b = Charm.atom(0)
local c = Charm.atom(0)
- local target: unknown, payloads: { types.SyncPayload }
+ local target, payloads
server.addSignalsToClient(client, { a = a, b = b, c = c })
server.connect(function(...)
@@ -42,7 +45,7 @@ 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 }
+ local target, payloads
server.addSignalsToClient(client, { a = a, b = b })
server.connect(function(...)
@@ -66,60 +69,11 @@ test("should replicate partial state patches", function()
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 }
+ local payloadsA, payloadsB
server.addSignalsToClient(clientA, { a = a })
server.connect(function(target, payloads)
@@ -135,8 +89,8 @@ test("should init new clients", function()
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 })
+ a(2)
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")
@@ -148,7 +102,7 @@ test("should init new atoms", function()
local client = {}
local a = Charm.atom(0)
local b = Charm.atom(0)
- local target: unknown, payloads: { types.SyncPayload }
+ local target, payloads
server.addSignalsToClient(client, { a = a })
server.connect(function(...)
@@ -173,7 +127,7 @@ test("should only replicate to synced clients", function()
local clientB = {}
local a = Charm.atom(0)
local b = Charm.atom(0)
- local payloadsA: { types.SyncPayload }, payloadsB: { types.SyncPayload }
+ local payloadsA, payloadsB
server.addSignalsToClient(clientA, { a = a })
server.addSignalsToClient(clientB, { b = b })
@@ -212,10 +166,24 @@ test("should only replicate to synced clients", function()
assert(payloadsB[1].data.a == nil, "Expected patch payload to not contain atom 'a' for client B")
end)
+test("should validate state", function()
+ local client = {}
+ local a = Charm.atom({})
+
+ server.addSignalsToClient(client, { a = a })
+ server.connect(function() end)
+
+ a({ [0.5] = true })
+ assert(not pcall(server.flush), "Expected server.flush to fail")
+
+ a({ [0.5] = true, valid = true })
+ server.flush()
+end)
+
test("should serialize state", function()
local client = {}
local a = Charm.atom({ 1, 2, 3, 4, 5 })
- local payloads: { types.SyncPayload }
+ local payloads
server.addSignalsToClient(client, { a = a })
server.connect(function(_, p)
@@ -229,32 +197,12 @@ test("should serialize state", function()
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()
+describe("historical", function()
+ beforeEach(function()
config.preserveHistory = true
end)
- test.afterEach(function()
+ afterEach(function()
config.preserveHistory = false
end)
@@ -263,7 +211,7 @@ test.describe("historical", function()
local a = Charm.atom(0)
local b = Charm.atom(0)
local c = Charm.atom(0)
- local target: unknown, payloads: { types.SyncPayload }
+ local target, payloads
server.addSignalsToClient(client, { a = a, b = b, c = c })
server.connect(function(...)
@@ -288,41 +236,11 @@ test.describe("historical", function()
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 }
+ local payloadsA, payloadsB
server.addSignalsToClient(clientA, { a = a })
server.connect(function(target, payloads)
@@ -358,7 +276,7 @@ test.describe("historical", function()
local client = {}
local a = Charm.atom(0)
local b = Charm.atom(0)
- local payloads: { types.SyncPayload }
+ local payloads
server.addSignalsToClient(client, { a = a })
server.connect(function(_, ...)
@@ -383,74 +301,6 @@ test.describe("historical", function()
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)
-
- test("should maintain patch list order for stale update", function()
- local a = Charm.atom({ x = 1 })
- local b = Charm.atom({ y = 1 })
- local payloads
-
- server.connect(function(_client, _payloads)
- payloads = _payloads
- end)
-
- server.addSignalsToClient("client", { a = a, b = b })
- server.flush()
-
- payloads = nil
- a({ x = 1 }) -- new table, same contents: diff is nil
- b({ y = 2 })
- server.flush()
- assert(payloads, "Expected payloads to be sent")
- assert(payloads[1].type == "patch", "Expected payload to be patch")
- assert(payloads[1].data.b.y == 2, "Expected payload to contain atom 'b'")
- end)
-end)
-
-test("addSignalsToClient should ignore tracking context", function()
- local counter = Charm.atom(0)
- local payloads: { any }?
-
- server.connect(function(_client, _payloads)
- payloads = _payloads
- end)
-
- local dispose = Charm.effectScope(function()
- server.addSignalsToClient("PLAYER_ID", { counter = counter })
- end)
-
- server.flush()
- assert(payloads, "Expected payloads to be sent")
- assert(payloads[1].type == "init", "Expected first payload to be init")
- assert(payloads[1].data.counter == 0, "Expected init payload to contain atom 'counter'")
-
- dispose()
-
- payloads = nil
- counter(1)
- server.flush()
- assert(payloads, "Expected payloads to be sent")
- assert(payloads[1].type == "patch", "Expected first payload to be patch")
- assert(payloads[1].data.counter == 1, "Expected patch payload to contain atom 'counter'")
end)
return {}
diff --git a/packages/charm-sync/src/__tests__/spec.luau b/packages/charm-sync/src/__tests__/spec.luau
new file mode 100644
index 0000000..5f8513c
--- /dev/null
+++ b/packages/charm-sync/src/__tests__/spec.luau
@@ -0,0 +1,27 @@
+local suite = require("../../../../test/suite")
+
+suite.describe("client", function()
+ require("./client.spec")
+end)
+
+suite.describe("e2e", function()
+ require("./e2e.spec")
+end)
+
+suite.describe("utils", function()
+ require("./utils.spec")
+end)
+
+suite.describe("patch", function()
+ require("./patch.spec")
+end)
+
+suite.describe("server", function()
+ require("./server.spec")
+end)
+
+suite.describe("validate", function()
+ require("./validate.spec")
+end)
+
+return {}
diff --git a/packages/charm-sync/test/utils.test.luau b/packages/charm-sync/src/__tests__/utils.spec.luau
similarity index 65%
rename from packages/charm-sync/test/utils.test.luau
rename to packages/charm-sync/src/__tests__/utils.spec.luau
index 1d50f55..47d2e39 100644
--- a/packages/charm-sync/test/utils.test.luau
+++ b/packages/charm-sync/src/__tests__/utils.spec.luau
@@ -1,15 +1,16 @@
-local Charm = require("../Charm")
-local test = require("../../../tests/test")
-local utils = require("../src/utils")
+local Charm = require("../../Charm")
+local suite = require("../../../../test/suite")
+local utils = require("../utils")
+
+local test = suite.test
test("should flatten functions", function()
local input = {
a = Charm.atom(0),
b = { c = Charm.atom(0) },
- d = { e = { f = utils.signalToAtom<>(Charm.signal(0)) } },
+ d = { e = { f = utils.signalToAtom(Charm.signal(0)) } },
}
-
- local result = utils.flatten<<{ [string]: Charm.Atom }>>(input)
+ local result = utils.flatten(input)
assert(result.a == input.a, "should flatten the first level")
assert(result["b/c"] == input.b.c, "should flatten the second level")
diff --git a/packages/charm-sync/src/__tests__/validate.spec.luau b/packages/charm-sync/src/__tests__/validate.spec.luau
new file mode 100644
index 0000000..1f107fd
--- /dev/null
+++ b/packages/charm-sync/src/__tests__/validate.spec.luau
@@ -0,0 +1,24 @@
+local suite = require("../../../../test/suite")
+local validate = require("../validate")
+
+local test = suite.test
+
+test("validates inputs", function()
+ local cases: { [string]: { value: any, expected: any } } = {
+ array = { value = { 1, 2, 3 }, expected = true },
+ arraySparse = { value = { 1, nil :: any, 3 }, expected = true },
+ arraySparseInvalid = { value = { [1] = 0, [3] = 0 }, expected = false },
+ mixedTable = { value = { [1] = "foo", a = true }, expected = false },
+ }
+
+ for key, case in cases do
+ local success, result: any = pcall(validate, case.value)
+ if case.expected and not success then
+ error(`Expected {key} to be valid, threw {result}`)
+ elseif not case.expected and success then
+ error(`Expected {key} to be invalid, but it was valid`)
+ end
+ end
+end)
+
+return {}
diff --git a/packages/charm-sync/src/client.luau b/packages/charm-sync/src/client.luau
index 973a983..3427726 100644
--- a/packages/charm-sync/src/client.luau
+++ b/packages/charm-sync/src/client.luau
@@ -3,98 +3,66 @@ local patch = require("./patch")
local types = require("./types")
type SyncPayload = types.SyncPayload
-type ReactiveProxy = types.ReactiveProxy
-local trackedSignals: { [string]: (any) -> any } = {}
-local trackedReactives: { [string]: ReactiveProxy } = {}
+type Setter = (update: ((T) -> T) | T) -> ()
-local function shallowMergeReactive(reactive: ReactiveProxy, source: { [any]: any })
- local target = reactive.__target
- for key, value in source do
- target[key] = value
- end
- Charm.trigger(reactive.__track)
-end
+local trackedSetters: { [string]: Setter } = {}
---[[
- 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
+--- 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 setters A map of setter functions to sync with the server.
+local function addSignals(setters: { [string]: Setter })
+ for key, setter in setters do
+ trackedSetters[key] = setter
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.
-]]
+--- 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
+ trackedSetters[key] = nil
end
end
---[[
- Unsubscribes from all server state updates.
-]]
+--- Unsubscribes from all server state updates.
local function removeAllSignals()
- table.clear(trackedSignals)
- table.clear(trackedReactives)
+ table.clear(trackedSetters)
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)
- else
- warn(`charm-sync failed to sync key '{key}' because it is not currently registered by addSignals()`)
+ local setter = trackedSetters[key]
+ if setter then
+ setter(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)
+ for key, value in data do
+ local setter = trackedSetters[key]
+ if setter then
+ setter(function(current)
+ return patch.apply(current, value)
end)
- elseif trackedReactive then
- patch.applyMutable(trackedReactive.__target, diffs)
- Charm.trigger(trackedReactive.__track)
- else
- warn(`charm-sync failed to sync key '{key}' because it is not currently registered by addSignals()`)
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 })
+--- 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 patch(payloads: { SyncPayload })
for _, payload in payloads do
if payload.type == "init" then
Charm.batch(applyInitPayload, payload.data)
@@ -108,5 +76,5 @@ return {
addSignals = addSignals,
removeSignals = removeSignals,
removeAllSignals = removeAllSignals,
- patch = applyPatches,
+ patch = patch,
}
diff --git a/packages/charm-sync/src/config.luau b/packages/charm-sync/src/config.luau
index c3ce27c..9b528fc 100644
--- a/packages/charm-sync/src/config.luau
+++ b/packages/charm-sync/src/config.luau
@@ -1,31 +1,20 @@
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.
- ]]
+ --- 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.
- ]]
+ --- 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 = 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.
- ]]
+ --- 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.
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`.
- ]]
+ --- When `true`, synced state containing unsafe sparse arrays or mixed
+ --- tables will throw an error. Only checked in strict mode and if
+ --- `fixArrays` is enabled. Defaults to `true`.
validatePatches = true,
}
diff --git a/packages/charm-sync/src/index.d.ts b/packages/charm-sync/src/index.d.ts
index bb40eba..69381b6 100644
--- a/packages/charm-sync/src/index.d.ts
+++ b/packages/charm-sync/src/index.d.ts
@@ -11,11 +11,8 @@ export interface None {
export type MaybeNone = undefined extends T ? None : never;
-type ReactiveObject = { [key: Key]: any };
-
-type ServerSignalMap = Record any) | ReactiveObject>;
-
-type ClientSignalMap = Record | ReactiveObject>;
+type GetterMap = Record any>;
+type SetterMap = Record>;
/**
* Infers the type of the return values produced by a map of functions.
@@ -58,7 +55,7 @@ export type SyncPatch = MaybeNone =
+export type SyncPayload =
| { type: "init"; data: StateOfMap }
| { type: "patch"; data: SyncPatch, FixArrays> };
@@ -78,17 +75,14 @@ export const config: {
* 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.
+ * to `false`.
*
* @server
*/
preserveHistory: boolean;
/**
- * 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.
- *
+ * 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.
*
@@ -97,7 +91,7 @@ export const config: {
fixArrays: boolean;
/**
* When `true`, synced state containing unsafe sparse arrays or mixed
- * tables will emit a warning. Only checked in strict mode and if
+ * tables will throw an error. Only checked in strict mode and if
* `fixArrays` is enabled. Defaults to `true`.
*
* @server
@@ -118,18 +112,16 @@ export namespace client {
* Note that `server.addSignalsToClient` still requires some way to get the
* state.
*
- * @param signals A map of setter functions to sync with the server.
+ * @param setters A map of setter functions to sync with the server.
*/
- export function addSignals(signals: Signals): void;
+ export function addSignals(setters: Setters): void;
/**
* 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.
*/
- export function removeSignals(
- ...keys: (keyof Signals)[]
- ): void;
+ export function removeSignals(...keys: (keyof Setters)[]): void;
/**
* Unsubscribes from all server state updates.
@@ -142,7 +134,7 @@ export namespace client {
*
* @param payloads The state updates received from the server.
*/
- export function patch(
+ export function patch(
payloads: SyncPayload[],
): void;
}
@@ -160,12 +152,9 @@ export namespace server {
* 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.
+ * @param getters A map of getter functions to sync with the client.
*/
- export function addSignalsToClient(
- client: Player,
- signals: Signals,
- ): void;
+ export function addSignalsToClient(client: Player, getters: Getters): void;
/**
* Unsubscribes a client from receiving all state updates. To only unsubscribe
@@ -181,9 +170,9 @@ export namespace server {
* @param client The client receiving state updates.
* @param keys The keys of the state to unsubscribe from.
*/
- export function removeSignalsFromClient(
+ export function removeSignalsFromClient(
client: Player,
- ...keys: (keyof Signals)[]
+ ...keys: (keyof Getters)[]
): void;
/**
@@ -193,7 +182,7 @@ export namespace server {
*
* @param onSync Called when sending patches to a client.
*/
- export function connect(
+ export function connect(
onSync: (client: Player, payloads: SyncPayload[]) => void,
): void;
@@ -245,7 +234,7 @@ export namespace patch {
* @param statePatch The patches to apply.
* @returns The new state with the patch applied.
*/
- export function applyImmutable(
+ export function apply(
currentState: State,
statePatch: SyncPatch,
): State;
@@ -287,6 +276,5 @@ export function isNone(value: unknown): value is None;
* @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/patch.luau b/packages/charm-sync/src/patch.luau
index d6b940f..f56a118 100644
--- a/packages/charm-sync/src/patch.luau
+++ b/packages/charm-sync/src/patch.luau
@@ -7,39 +7,35 @@ local flags = Charm.flags
export type None = types.None
+--- A special symbol that denotes the absence of a value. Used to represent
+--- deleted values in patches.
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`.
-]]
+--- 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.
-]]
+--- 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 `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]: any }): { [string | number]: any }
+--- 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
+--- 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 maxn = table.maxn(object)
if maxn == 0 or maxn == #object then
@@ -56,78 +52,68 @@ local function stringifySparseArray(object: { [any]: any }): { [string | number]
return dictionary
end
-local function diff(oldState: any, newState: any): any
- if oldState == newState then
+--- 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.
+--- @return A patch that can be applied to the current state.
+local function diff(currentState: any, nextState: any): any
+ if currentState == nextState then
return nil
- elseif newState == nil then
+ elseif nextState == nil then
return None
- elseif type(oldState) ~= "table" or type(newState) ~= "table" then
- return newState
+ elseif type(currentState) ~= "table" or type(nextState) ~= "table" then
+ return nextState
end
- local diffs = table.clone(newState)
+ local patches: { [any]: any } = table.clone(nextState)
- for key, oldValue in oldState do
- diffs[key] = diff(oldValue, newState[key])
- end
-
- if next(diffs) == nil then
- return nil
+ for key, previous in currentState do
+ patches[key] = diff(previous, nextState[key])
end
if config.fixArrays then
-- Convert sparse array patches that could be misserialized
- if oldState[1] ~= nil or newState[1] ~= nil then
- diffs = stringifySparseArray(diffs)
+ if currentState[1] ~= nil or nextState[1] ~= nil then
+ patches = stringifySparseArray(patches)
end
+
if flags.strict and config.validatePatches then
- validate(diffs)
+ validate(patches)
end
end
- return diffs
+ return patches
end
-local function apply(oldState: any, diffs: any): any
- if isNone(diffs) then
+--- Applies a patch to a state and returns the resulting value.
+---
+--- @param currentState The current state.
+--- @param statePatch The patches to apply.
+--- @return The new state with the patch applied.
+local function apply(currentState: any, statePatch: any): any
+ if isNone(statePatch) then
return nil
- elseif type(oldState) ~= "table" or type(diffs) ~= "table" then
- return diffs
+ elseif type(currentState) ~= "table" or type(statePatch) ~= "table" then
+ return statePatch
end
- local newState = table.clone(oldState)
- local stateIsArray = oldState[1] ~= nil
+ local nextState = table.clone(currentState)
+ local stateIsArray = currentState[1] ~= nil
- for key, value in diffs do
+ for key, value in statePatch 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
- newState[key] = apply(newState[key], value)
+
+ nextState[key] = apply(nextState[key], value)
end
- 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
+ return nextState
end
return {
@@ -135,5 +121,4 @@ return {
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 6485e18..8979e0c 100644
--- a/packages/charm-sync/src/server.luau
+++ b/packages/charm-sync/src/server.luau
@@ -2,7 +2,6 @@ local Charm = require("../Charm")
local config = require("./config")
local patch = require("./patch")
local types = require("./types")
-local utils = require("./utils")
type SyncPayload = types.SyncPayload
@@ -10,11 +9,10 @@ type SyncStatus = number
type ClientKeys = { [string]: SyncStatus }
-type TrackedState = {
- identifier: unknown,
+type GetterState = {
+ getter: () -> T,
currentValue: T,
lastSyncedValue: T,
- isReactive: boolean,
dispose: () -> (),
}
@@ -27,81 +25,50 @@ type StateUpdate = {
local PENDING_INITIAL_STATE: SyncStatus = 0
local LISTENING_FOR_CHANGES: SyncStatus = 1
-local trackedStates: { [string]: TrackedState } = {}
+local trackedGetters: { [string]: GetterState } = {}
local globalUpdates: { StateUpdate } = {}
local clients: { [any]: ClientKeys } = {}
local connection: RBXScriptConnection?
-local syncCallback: ((client: any, payloads: { SyncPayload }) -> ())?
+local syncCallback: (client: any, payloads: { SyncPayload }) -> ()?
---[[
- Manually converts a reactive proxy to its raw representation to potentially
- support custom implementations of reactive proxies.
-]]
-local function toRaw(value: T): T
- local proxy = value :: types.ReactiveProxy
- if type(proxy) == "table" and type(proxy.__target) == "table" and type(proxy.__track) == "function" then
- proxy.__track()
- return toRaw(proxy.__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
+local function startTracking(key: string, getter: () -> T)
+ if trackedGetters[key] then
+ -- Ignore duplicates, but warn against name collisions
+ if trackedGetters[key].getter ~= getter then
warn("[Charm] Attempted to sync different states with the same key:", key)
end
return
end
- local trackedState: TrackedState
- local initialValue = getter()
- local rawInitialValue = toRaw(initialValue)
- local snapshot = if initialValue ~= rawInitialValue then utils.deepCopy(rawInitialValue) else initialValue
-
- local dispose = Charm.untracked(Charm.effect, function()
- local newValue = getter()
- local rawValue = toRaw(newValue)
- local isReactive = newValue ~= rawValue
-
- if not trackedState then
- return
- end
+ local value = getter()
+ local trackedState = {
+ getter = getter,
+ currentValue = value,
+ lastSyncedValue = value,
+ }
+ trackedState.dispose = Charm.subscribe(getter, function(newValue, oldValue)
-- 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,
+ newValue = newValue,
+ oldValue = oldValue,
})
end
-
- trackedState.currentValue = rawValue
- trackedState.isReactive = isReactive
+ trackedState.currentValue = newValue
end)
- trackedState = {
- identifier = identifier,
- currentValue = rawInitialValue,
- lastSyncedValue = snapshot,
- isReactive = initialValue ~= rawInitialValue,
- dispose = dispose,
- }
-
- trackedStates[key] = trackedState
+ trackedGetters[key] = trackedState
end
local function stopTracking(key: string)
- local trackedState = trackedStates[key]
+ local trackedState = trackedGetters[key]
if trackedState then
trackedState.dispose()
- trackedStates[key] = nil
+ trackedGetters[key] = nil
end
end
@@ -114,7 +81,7 @@ local function purgeUntracked()
end
end
- for key in trackedStates do
+ for key in trackedGetters do
if not active[key] then
stopTracking(key)
end
@@ -146,7 +113,7 @@ local function selectFromGlobalPatchList(client: any, globalPatches: { any }): {
for index, update in globalUpdates do
local patchData = globalPatches[index]
- if patchData == nil then
+ if not patchData then
continue
end
@@ -154,27 +121,25 @@ local function selectFromGlobalPatchList(client: any, globalPatches: { any }): {
local status = clientKeys[key]
if status == LISTENING_FOR_CHANGES then
- local payload: SyncPayload = {
+ table.insert(payloads, {
type = "patch",
data = { [key] = patchData },
- }
- table.insert(payloads, payload)
+ })
elseif status == PENDING_INITIAL_STATE then
- local payload: SyncPayload = {
+ table.insert(payloads, {
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 = {}
+ local initData: { [string]: any } = {}
for key, status in clientKeys do
- local trackedState = trackedStates[key]
+ local trackedState = trackedGetters[key]
if status == PENDING_INITIAL_STATE and trackedState then
initData[key] = trackedState.currentValue
@@ -189,24 +154,20 @@ local function selectFromGlobalPatchList(client: any, globalPatches: { any }): {
return payloads
end
-local function diffGlobalState(): { [string]: any }?
+local function diffGlobalState(): 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
+ for key, trackedState in trackedGetters do
+ currentGlobalState[key] = trackedState.currentValue
previousGlobalState[key] = trackedState.lastSyncedValue
- trackedState.lastSyncedValue = currentValue
+ trackedState.lastSyncedValue = trackedState.currentValue
end
return patch.diff(previousGlobalState, currentGlobalState)
end
-local function selectFromGlobalPatch(client: any, globalPatch: { [string]: any }?): { SyncPayload }?
+local function selectFromGlobalPatch(client: any, globalPatch: any): { SyncPayload }?
local clientKeys = clients[client]
if not clientKeys then
@@ -220,9 +181,9 @@ local function selectFromGlobalPatch(client: any, globalPatch: { [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]
+ local trackedState = trackedGetters[key]
- if status == LISTENING_FOR_CHANGES and globalPatch then
+ if status == LISTENING_FOR_CHANGES then
patchData[key] = globalPatch[key]
elseif status == PENDING_INITIAL_STATE then
initData[key] = trackedState.currentValue
@@ -231,97 +192,55 @@ local function selectFromGlobalPatch(client: any, globalPatch: { [string]: any }
end
if next(initData) ~= nil then
- local payload: SyncPayload = {
- type = "init",
- data = initData,
- }
- table.insert(payloads, payload)
+ table.insert(payloads, { type = "init", data = initData })
end
if next(patchData) ~= nil then
- local payload: SyncPayload = {
- type = "patch",
- data = patchData,
- }
- table.insert(payloads, payload)
+ table.insert(payloads, { type = "patch", data = patchData })
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 })
+--- 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 getters A map of getter functions to sync with the client.
+local function addSignalsToClient(client: any, getters: { [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
+ for key, getter in getters do
+ startTracking(key, if type(getter) == "function" then getter else getter.get)
if not clientKeys[key] then
- -- Mark the signal as pending initial state so we send its full
+ -- Mark the getter 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.
-]]
+--- 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.
-]]
+--- 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]
@@ -336,27 +255,23 @@ local function removeSignalsFromClient(client: any, ...: string)
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.
-]]
+--- 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()
+ local globalPatch = if granular then diffGlobalUpdateBuffer() else diffGlobalState()
for client in clients do
local payloads = if granular
- then selectFromGlobalPatchList(client, globalPatch :: { any })
+ then selectFromGlobalPatchList(client, globalPatch)
else selectFromGlobalPatch(client, globalPatch)
if payloads and payloads[1] then
@@ -368,25 +283,46 @@ local function flush()
end
end
+ purgeUntracked()
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.
-]]
+--- 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 }) -> ())
+ if connection then
+ connection:Disconnect()
+ end
+
syncCallback = onSync
- startInterval(flush)
+
+ if not game then
+ return
+ end
+
+ local timer = 0
+
+ connection = game:GetService("RunService").Heartbeat:Connect(function(deltaTime)
+ if config.interval < 0 then
+ return
+ end
+
+ timer += deltaTime
+
+ if timer >= config.interval then
+ timer = 0
+ debug.profilebegin("CharmSync")
+ flush()
+ debug.profileend()
+ end
+ end)
end
---[[
- Stops syncing state updates to clients at the automatic interval.
- Flushing can still be performed manually via `flush`.
-]]
+--- 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()
@@ -394,12 +330,10 @@ local function disconnect()
end
end
---[[
- Unsubscribes all clients, stops tracking all state, and disconnects
- from the automatic sync interval.
-]]
+--- Unsubscribes all clients, stops tracking all state, and disconnects
+--- from the automatic sync interval.
local function reset()
- table.clear(trackedStates)
+ table.clear(trackedGetters)
table.clear(clients)
table.clear(globalUpdates)
disconnect()
diff --git a/packages/charm-sync/src/types.luau b/packages/charm-sync/src/types.luau
index 664ee61..ac8ffb4 100644
--- a/packages/charm-sync/src/types.luau
+++ b/packages/charm-sync/src/types.luau
@@ -1,7 +1,5 @@
---[[
- A payload that can be sent from the server to the client to synchronize
- the states of the client's atoms with the server.
-]]
+--- A payload that can be sent from the server to the client to synchronize
+--- the states of the client's atoms with the server.
export type SyncPayload =
| {
type: "init",
@@ -12,19 +10,8 @@ export type SyncPayload =
data: { [string]: any },
}
---[[
- 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.
-]]
+--- 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" }
-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
index 4a49871..6a7f7b0 100644
--- a/packages/charm-sync/src/utils.luau
+++ b/packages/charm-sync/src/utils.luau
@@ -2,22 +2,28 @@ 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.
+type NestedTable = {
+ [any]: NestedTable | any,
+}
- @param input A nested table.
- @return A flattened table with slash-separated keys.
-]]
-local function flatten(input: { [any]: any }): Flattened
- local result = {}
+type FlattenedTable = {
+ [any]: any,
+}
- local function visit(node: { [any]: any }, path: string)
+--- 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: NestedTable): FlattenedTable
+ local result: FlattenedTable = {}
+
+ local function visit(node: NestedTable, path: string)
for key, value in node do
- local location = if path == "" then tostring(key) else path .. "/" .. tostring(key)
+ local location = if path == "" then key else path .. "/" .. key
if type(value) == "table" then
- visit(value, location)
+ visit(value :: NestedTable, location)
else
result[location] = value
end
@@ -26,40 +32,23 @@ local function flatten(input: { [any]: any }): Flattened
visit(input, "")
- return result :: any
+ return result
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.
-]]
+--- 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 dfba716..f350bb7 100644
--- a/packages/charm-sync/src/validate.luau
+++ b/packages/charm-sync/src/validate.luau
@@ -1,18 +1,18 @@
local function isTableUnsafe(object: { [any]: any })
- local firstKey = next(object)
+ local key = next(object)
- if firstKey == nil then
+ if key == nil then
return false
end
- local keyType = type(firstKey)
+ local keyType = type(key)
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
+ elseif keyType == "number" and key > maxKey then
maxKey = key
end
end
@@ -25,21 +25,25 @@ local function isTableUnsafe(object: { [any]: any })
return false
end
---[[
- 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 table value.
-]]
+--- Throws an error 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 table value.
+--- @param key The key of the value.
+--- @error Throws an error if the value cannot be synced.
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"
+ -- Silence warnings during testing
+ if game then
+ warn("Cannot sync table:", value)
+ end
+ error(
+ `Cannot sync tables unsupported by remote events! See the warning above for more info.\n\n`
+ .. "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
+ .. "Read more: https://create.roblox.com/docs/scripting/events/remote#argument-limitations"
)
end
end
diff --git a/packages/charm-sync/test/init.luau b/packages/charm-sync/test/init.luau
deleted file mode 100644
index 111dc1d..0000000
--- a/packages/charm-sync/test/init.luau
+++ /dev/null
@@ -1,23 +0,0 @@
-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/wally.toml b/packages/charm-sync/wally.toml
index 589e7ed..6d6257c 100644
--- a/packages/charm-sync/wally.toml
+++ b/packages/charm-sync/wally.toml
@@ -1,15 +1,15 @@
[package]
name = "littensy/charm-sync"
-version = "0.4.1"
+version = "0.4.0-rc.1"
registry = "https://github.com/UpliftGames/wally-index"
realm = "shared"
exclude = [
"node_modules",
- "test",
+ "src/__tests__",
"src/index.d.ts",
"package.json",
"Charm.luau",
]
[dependencies]
-Charm = "littensy/charm@^0.11.0"
+Charm = "littensy/charm@0.11.0-rc.1"
diff --git a/packages/charm/package.json b/packages/charm/package.json
index 7ef2e29..f4167c5 100644
--- a/packages/charm/package.json
+++ b/packages/charm/package.json
@@ -1,6 +1,6 @@
{
"name": "@rbxts/charm",
- "version": "0.11.1",
+ "version": "0.11.0-rc.1",
"description": "Atomic state management based on alien-signals",
"license": "MIT",
"main": "src/init.luau",
@@ -11,14 +11,14 @@
"!wally.toml",
"!wally.lock",
"!default.project.json",
- "!test"
+ "!**/__tests__"
],
"repository": {
"type": "git",
"url": "git+https://github.com/littensy/charm.git"
},
"scripts": {
- "prepare": "cp ../../LICENSE . && cp ../../README.md .",
+ "prepare": "cp ../../LICENSE.md . && cp ../../README.md .",
"publish:wally": "wally publish"
}
}
diff --git a/packages/charm/src/__tests__/computed.spec.luau b/packages/charm/src/__tests__/computed.spec.luau
new file mode 100644
index 0000000..3dd7284
--- /dev/null
+++ b/packages/charm/src/__tests__/computed.spec.luau
@@ -0,0 +1,106 @@
+local Charm = require("../")
+local suite = require("../../../../test/suite")
+
+local computed = Charm.computed
+local effect = Charm.effect
+local signal = Charm.signal
+local test = suite.test
+
+test("should correctly propagate changes through computed core", function()
+ local src, setSrc = signal(0)
+ local c1 = computed(function()
+ return src() % 2
+ end)
+ local c2 = computed(function()
+ return c1()
+ end)
+ local c3 = computed(function()
+ return c2()
+ end)
+
+ c3()
+ setSrc(1) -- c1 -> dirty, c2 -> toCheckDirty, c3 -> toCheckDirty
+ c2() -- c1 -> none, c2 -> none
+ setSrc(3) -- c1 -> dirty, c2 -> toCheckDirty
+
+ assert(c3() == 1, "bad final value")
+end)
+
+test("should propagate updated source value through chained computations", function()
+ local src, setSrc = signal(0)
+ local a = computed(function()
+ return src()
+ end)
+ local b = computed(function()
+ return a() % 2
+ end)
+ local c = computed(function()
+ return src()
+ end)
+ local d = computed(function()
+ return b() + c()
+ end)
+
+ assert(d() == 0, "bad initial value")
+ setSrc(2)
+ assert(d() == 2, "bad updated value")
+end)
+
+test("should handle flags are indirectly updated during checkDirty", function()
+ local a, setA = signal(false)
+ local b = computed(function()
+ return a()
+ end)
+ local c = computed(function()
+ b()
+ return 0
+ end)
+ local d = computed(function()
+ c()
+ return b()
+ end)
+
+ assert(d() == false, "bad initial value")
+ setA(true)
+ assert(d() == true, "bad updated value")
+end)
+
+test("should not update if the signal value is reverted", function()
+ local times = 0
+
+ local src, setSrc = signal(0)
+ local c1 = computed(function()
+ times += 1
+ return src()
+ end)
+ c1()
+ assert(times == 1, "computed did not run")
+ setSrc(1)
+ setSrc(0)
+ c1()
+ assert(times == 1, "computed ran when it should not")
+end)
+
+test("should receive previous value", function()
+ local calls = {}
+ local src, setSrc = signal(0)
+ local c1 = computed(function(current): number
+ local incoming = src()
+ return if current == incoming or incoming % 2 == 1 then current or incoming else incoming
+ end)
+
+ effect(function()
+ table.insert(calls, c1())
+ end)
+
+ setSrc(2)
+ setSrc(3)
+ assert(c1() == 2, "should skip odd value")
+
+ setSrc(4)
+ setSrc(5)
+ assert(c1() == 4, "should skip odd value")
+ assert(table.concat(calls) == "024", `should only call on even updates, got {table.concat(calls)}`)
+end)
+
+return {}
diff --git a/packages/charm/test/effect.test.luau b/packages/charm/src/__tests__/effect.spec.luau
similarity index 55%
rename from packages/charm/test/effect.test.luau
rename to packages/charm/src/__tests__/effect.spec.luau
index 17cd0a0..56fe9c1 100644
--- a/packages/charm/test/effect.test.luau
+++ b/packages/charm/src/__tests__/effect.spec.luau
@@ -1,14 +1,16 @@
-local Charm = require("../src")
-local test = require("../../../tests/test")
+local Charm = require("../")
+local suite = require("../../../../test/suite")
local computed = Charm.computed
local effect = Charm.effect
local effectScope = Charm.effectScope
local endBatch = Charm.endBatch
local onCleanup = Charm.onCleanup
+local recursive = Charm.recursive
local setActiveSub = Charm.setActiveSub
local signal = Charm.signal
local startBatch = Charm.startBatch
+local test = suite.test
test("should clear subscriptions when untracked by all subscribers", function()
local bRunTimes = 0
@@ -239,11 +241,8 @@ test("should handle side effect with inner effects", function()
table.clear(order)
setB(1)
setA(1)
+ assert(table.concat(order) == "ba", `wrong order: {table.concat(order)}`)
end)
-
- -- DEVIATION: This assertion was moved outside of the effect so that it
- -- runs after the batch updates have been processed
- assert(table.concat(order) == "ba", `wrong order: {table.concat(order)}`)
end)
test("should handle flags are indirectly updated during checkDirty", function()
@@ -271,52 +270,33 @@ test("should handle flags are indirectly updated during checkDirty", function()
assert(triggers == 2, "did not update indirectly")
end)
-test("should not execute skipped effects from previous failed flush when updating unrelated signal", function()
- local a, setA = signal(0)
- local b, setB = signal(0)
- local c, setC = signal(0)
- local d = computed(function()
- c()
- return 0
- end)
+test("should handle effect recursion for the first execution", function()
+ local src1, setSrc1 = signal(0)
+ local src2, setSrc2 = signal(0)
- local effect3Executed = false
+ local triggers1 = 0
+ local triggers2 = 0
effect(function()
- a()
+ triggers1 += 1
+ setSrc1(math.min(src1() + 1, 5))
end)
effect(function()
- if a() == 2 then
- error("Error in effect 2")
- end
- end)
- effect(function()
- a()
- d()
- effect3Executed = true
- end)
- effect(function()
- b()
+ triggers2 += 1
+ setSrc2(math.min(src2() + 1, 5))
+ src2()
end)
- setA(1)
-
- effect3Executed = false
- local success, err = pcall(setA, 2)
- assert(not success and string.find(tostring(err), "Error in effect 2"), `bad error: {err}`)
-
- assert(not effect3Executed, "effect 3 should not have executed after failed flush")
- setB(1)
- assert(not effect3Executed, "effect 3 should not have executed after updating unrelated signal b")
- setC(1)
- assert(effect3Executed, "effect 3 should have executed after updating signal c")
+ assert(triggers1 == 1, `bad triggers1: {triggers1}`)
+ assert(triggers2 == 1, `bad triggers2: {triggers2}`)
end)
-test("should allow finite recursion", function()
+test("should opt in to recursion", function()
local src, setSrc = signal(0)
local order = {}
effect(function()
+ recursive()
table.insert(order, src())
setSrc(math.min(src() + 1, 5))
end)
@@ -408,186 +388,4 @@ test("should run cleanups outside any evaluation context", function()
assert(calls == 0, "should not run after b update")
end)
-test("should not retrigger the effect if the cleanup modifies one of the dependencies", function()
- local a, setA = signal(0)
- local calls = {}
-
- effect(function()
- table.insert(calls, a())
- return function()
- setA(2)
- end
- end)
- assert(#calls == 1, "should have run once initially")
- table.clear(calls)
-
- setA(1)
- assert(#calls == 1, "should run once after an update")
- assert(calls[1] == 2, `bad call: {calls[1]}`)
-end)
-
-test("should run the cleanup if the effect disposes itself", function()
- local a, setA = signal(0)
- local calls = 0
- local dispose: () -> ()
-
- dispose = effect(function()
- if a() > 0 then
- dispose()
- return function()
- calls += 1
- end
- end
- end)
- assert(calls == 0, "should not have called cleanup initially")
- setA(1)
- assert(calls == 1, "should have called cleanup once after first update")
- setA(2)
- assert(calls == 1, "should not call cleanup again after second update")
-end)
-
-test("should not run the effect if the cleanup function disposes it", function()
- local a, setA = signal(0)
- local calls = 0
- local dispose: () -> ()
-
- dispose = effect(function()
- a()
- calls += 1
- return function()
- dispose()
- end
- end)
- assert(calls == 1, "should have run effect once initially")
- setA(1)
- assert(calls == 1, "should not run effect again after update")
-end)
-
-test("should reset the cleanup if the effect throws", function()
- local a, setA = signal(0)
- local calls = 0
-
- effect(function()
- if a() == 0 then
- return function()
- calls += 1
- end
- else
- error("hello")
- end
- end)
- assert(calls == 0, "should not have called cleanup initially")
- local success, err = pcall(setA, 1)
- assert(not success and string.find(tostring(err), "hello"), `bad error: {err}`)
- assert(calls == 1, "should have called cleanup once after throwing")
- setA(0)
- assert(calls == 1, "should not call cleanup again after resetting value")
-end)
-
-test("recursive effects should run and cleanup in order", function()
- local a, setA = signal(0)
- local order: { string } = {}
-
- effect(function()
- local value = a()
- table.insert(order, `+{value}`)
- if value < 3 then
- setA(value + 1)
- end
- return function()
- table.insert(order, `-{value}`)
- end
- end)
- assert(table.concat(order) == "+0-0+1-1+2-2+3", `bad order: {table.concat(order)}`)
- table.clear(order)
- setA(100)
- assert(table.concat(order) == "-3+100", `bad order: {table.concat(order)}`)
-end)
-
-test("should effects dispose in order", function()
- local order: { number } = {}
-
- local src, setSrc = signal(false)
- local stopEffect = effect(function()
- src()
- for i = 1, 3 do
- effect(function()
- return function()
- table.insert(order, i)
- end
- end)
- end
- end)
-
- assert(#order == 0, "should not cleanup immediately")
- setSrc(true)
- assert(table.concat(order) == "123", `bad rerun order: {table.concat(order)}`)
- table.clear(order)
- stopEffect()
- assert(table.concat(order) == "123", `bad dispose order: {table.concat(order)}`)
- table.clear(order)
- setSrc(false)
- assert(#order == 0, "should not cleanup after dispose")
-end)
-
-test("should handle inner effect clearing outer effect's deps on rerun", function()
- local src, setSrc = signal(false)
- local stopEffect: () -> ()
- stopEffect = effect(function()
- src()
- effect(function()
- return function()
- stopEffect()
- end
- end)
- effect(function() end)
- end)
- setSrc(true)
-end)
-
-test.skip("should handle inner effect cleanup disposing outer effect", function()
- local order: { number } = {}
-
- local src, setSrc = signal(false)
- local stopEffect: () -> ()
- stopEffect = effect(function()
- src()
- table.insert(order, 0)
- for i = 1, 4 do
- effect(function()
- return function()
- table.insert(order, i)
- stopEffect()
- end
- end)
- end
- end)
- assert(table.concat(order) == "0", "should not cleanup immediately")
-
- table.clear(order)
- setSrc(true)
- assert(table.concat(order) == "1234", `bad rerun order: {table.concat(order)}`)
-
- table.clear(order)
- setSrc(false)
- assert(#order == 0, "should not cleanup after dispose")
-end)
-
-test.skip("should old inner effect dispose before outer effect re-runs", function()
- local order: { string } = {}
- local src, setSrc = signal(false)
- effect(function()
- src()
- table.insert(order, "outer")
- effect(function()
- return function()
- table.insert(order, "inner")
- end
- end)
- end)
- table.clear(order)
- setSrc(true)
- assert(table.concat(order) == "innerouter", `bad rerun order: {table.concat(order)}`)
-end)
-
return {}
diff --git a/packages/charm/test/effectScope.test.luau b/packages/charm/src/__tests__/effectScope.spec.luau
similarity index 73%
rename from packages/charm/test/effectScope.test.luau
rename to packages/charm/src/__tests__/effectScope.spec.luau
index 964b7d5..c484151 100644
--- a/packages/charm/test/effectScope.test.luau
+++ b/packages/charm/src/__tests__/effectScope.spec.luau
@@ -1,10 +1,11 @@
-local Charm = require("../src")
-local test = require("../../../tests/test")
+local Charm = require("../")
+local suite = require("../../../../test/suite")
local effect = Charm.effect
local effectScope = Charm.effectScope
local signal = Charm.signal
local onCleanup = Charm.onCleanup
+local test = suite.test
test("should not trigger after stop", function()
local count, setCount = signal(1)
@@ -31,8 +32,8 @@ end)
test("should run indirect scope cleanup", function()
local cleanups = 0
- local cleanup: () -> ()
+ local cleanup
local stopScope = effectScope(function()
cleanup = effectScope(function()
onCleanup(function()
@@ -50,8 +51,8 @@ end)
test("should run indirect effect cleanup", function()
local cleanups = 0
- local a, setA = signal(false)
+ local a, setA = signal(false)
local stopScope = effectScope(function()
effect(function()
a()
@@ -70,26 +71,13 @@ test("should run indirect effect cleanup", function()
assert(cleanups == 2, "should not cleanup after update")
end)
-test("should dispose inner effects if created in an effect", function()
- local source, setSource = signal(1)
- local triggers = 0
- local dispose: () -> ()
-
- effect(function()
- dispose = effectScope(function()
- effect(function()
- source()
- triggers += 1
- end)
+test("should disallow yielding", function()
+ local success = pcall(function()
+ effectScope(function()
+ (if task then task.wait else coroutine.yield :: never)()
end)
- assert(triggers == 1, "did not run initially")
end)
-
- setSource(2)
- assert(triggers == 2, "did not run after update")
- dispose()
- setSource(3)
- assert(triggers == 2, "ran after dispose")
+ assert(not success, "should error on yield")
end)
return {}
diff --git a/packages/charm/src/__tests__/gc.spec.luau b/packages/charm/src/__tests__/gc.spec.luau
new file mode 100644
index 0000000..4189f87
--- /dev/null
+++ b/packages/charm/src/__tests__/gc.spec.luau
@@ -0,0 +1,66 @@
+local Charm = require("../")
+local suite = require("../../../../test/suite")
+
+local atom = Charm.atom
+local computed = Charm.computed
+local effect = Charm.effect
+local effectScope = Charm.effectScope
+local test = suite.test
+
+--- Creates a weak reference to a value.
+local function weakRef(value: T): { value: T? }
+ return setmetatable({ value = value }, { __mode = "v" }) :: any
+end
+
+--- Triggers garbage collection by creating a large number of tables.
+local function gc()
+ for _ = 1, 2000 do
+ local _ = table.create(100, 0)
+ end
+end
+
+--- In alien-signals, a computed value unlinks from dependencies when its last
+--- parent scope is disposed. However, calling a computed value outside of a
+--- scope still links it to dependencies, which act as strong references.
+--- Computed values called in this way won't GC until unlinked from a scope.
+---
+--- This function releases a computed value by linking it to a scope and then
+--- immediately closing it, causing alien-signals to unlink the computed value
+--- from its dependencies and allow it to be garbage collected.
+local function unlink(getter: () -> ())
+ effectScope(function()
+ getter()
+ end)()
+end
+
+test("should release computed cache", function()
+ local srcRef = weakRef({})
+ local src = atom(srcRef.value)
+ local c = computed(function()
+ return src()
+ end)
+
+ c() -- cache src value
+ src(nil) -- release value
+ c = unlink(c) -- release computed
+
+ gc()
+ assert(srcRef.value == nil, "should release computed cache")
+end)
+
+test("should not release effect for atom", function()
+ local effectCalls = 0
+ local src = atom(0)
+
+ effect(function()
+ effectCalls += 1
+ src()
+ end)
+ assert(effectCalls == 1, "should call effect once")
+
+ gc()
+ src(src() + 1)
+ assert(effectCalls == 2, "should call effect again")
+end)
+
+return {}
diff --git a/packages/charm/test/listen.test.luau b/packages/charm/src/__tests__/listen.spec.luau
similarity index 56%
rename from packages/charm/test/listen.test.luau
rename to packages/charm/src/__tests__/listen.spec.luau
index 5d9c335..f3098d1 100644
--- a/packages/charm/test/listen.test.luau
+++ b/packages/charm/src/__tests__/listen.spec.luau
@@ -1,8 +1,10 @@
-local Charm = require("../src")
-local test = require("../../../tests/test")
+local Charm = require("../")
+local suite = require("../../../../test/suite")
local signal = Charm.signal
local listen = Charm.listen
+local effect = Charm.effect
+local test = suite.test
test("should listen to selector", function()
local source, setSource = signal(0)
@@ -25,23 +27,32 @@ test("should listen to selector", function()
assert(current == 2 and previous == 1, "should not run after unsub")
end)
-test("should run in untracked", function()
+test("should run in peek", function()
local runs = 0
+ local cleanups = 0
local a, setA = signal(0)
local b, setB = signal(0)
listen(a, function()
runs += 1
b()
+ effect(function()
+ return function()
+ cleanups += 1
+ end
+ end)
end)
assert(runs == 1, "should run once on init")
+ assert(cleanups == 0, "should not cleanup yet")
+
setA(1)
- assert(runs == 2, "should run once on change")
+ assert(runs == 2, "should run on a change")
+ assert(cleanups == 1, "should cleanup first call")
+
setB(1)
assert(runs == 2, "should not run on b change")
- setA(2)
- assert(runs == 3, "should run on a change")
+ assert(cleanups == 1, "should not cleanup second call")
end)
test("should run if first value is nil", function()
@@ -57,6 +68,36 @@ test("should run if first value is nil", function()
assert(runs == 2, "should run once on change")
end)
+test("should track inner subs", function()
+ local order = {}
+ local a, setA = signal(1)
+
+ local unsub = listen(a, function()
+ table.insert(order, 1)
+ listen(a, function()
+ table.insert(order, 2)
+ end)
+ listen(a, function()
+ table.insert(order, 3)
+ end)
+ end)
+
+ assert(table.concat(order) == "123", `bad mount order: {table.concat(order)}`)
+
+ table.clear(order)
+ setA(a() + 1)
+ assert(table.concat(order) == "123", `bad order: {table.concat(order)}`)
+
+ table.clear(order)
+ setA(a() + 1)
+ assert(table.concat(order) == "123", `bad order: {table.concat(order)}`)
+
+ table.clear(order)
+ unsub()
+ setA(a() + 1)
+ assert(table.concat(order) == "", `bad order: {table.concat(order)}`)
+end)
+
test("should skip redundant updates", function()
local order = {}
local a, setA = signal(0)
diff --git a/packages/charm/test/mapped.test.luau b/packages/charm/src/__tests__/mapped.spec.luau
similarity index 78%
rename from packages/charm/test/mapped.test.luau
rename to packages/charm/src/__tests__/mapped.spec.luau
index 33620d4..b812aea 100644
--- a/packages/charm/test/mapped.test.luau
+++ b/packages/charm/src/__tests__/mapped.spec.luau
@@ -1,8 +1,9 @@
-local Charm = require("../src")
-local test = require("../../../tests/test")
+local Charm = require("../")
+local suite = require("../../../../test/suite")
local signal = Charm.signal
local mapped = Charm.mapped
+local test = suite.test
test("should map state", function()
local src, setSrc = signal({ 1, 2, 3 })
@@ -104,25 +105,4 @@ test("should map keys", function()
assert(state.D == 4, "should add d")
end)
-test("should remove old key if mapper returns a new key", function()
- local src, setSrc = signal({ 1 })
- local map = mapped(src, function(value: number, index: number)
- return true, `{index}={value}`
- end)
-
- local state = map()
- assert(state["1=1"], "should have mapped first element to key 1=1")
-
- setSrc({ 2 })
- state = map()
- assert(state["1=1"] == nil, "should have removed old key 1=1")
- assert(state["1=2"], "should have remapped element to key 1=2")
-
- setSrc({ 3, 1 })
- state = map()
- assert(state["1=2"] == nil, "should have removed old key 1=2")
- assert(state["1=3"], "should have remapped element to key 1=3")
- assert(state["2=1"], "should have mapped second element to key 2=1")
-end)
-
return {}
diff --git a/packages/charm/test/observe.test.luau b/packages/charm/src/__tests__/observe.spec.luau
similarity index 79%
rename from packages/charm/test/observe.test.luau
rename to packages/charm/src/__tests__/observe.spec.luau
index 64bbd7d..f308477 100644
--- a/packages/charm/test/observe.test.luau
+++ b/packages/charm/src/__tests__/observe.spec.luau
@@ -1,18 +1,19 @@
-local Charm = require("../src")
-local test = require("../../../tests/test")
+local Charm = require("../")
+local suite = require("../../../../test/suite")
local signal = Charm.signal
local computed = Charm.computed
local effect = Charm.effect
local effectScope = Charm.effectScope
local observe = Charm.observe
+local test = suite.test
test("should observe items", function()
local items, setItems = signal({ 0, 0, 0 })
local updates = {}
local cleanups = {}
- observe(items, function(_, key: number)
+ observe(items, function(_, key)
local get = computed(function()
return items()[key]
end)
@@ -91,11 +92,11 @@ end)
test("should handle recursive additions and removals", function()
local items, setItems = signal({ 1 })
- local edits: { string } = {}
+ local edits = {}
observe(items, function(item)
if item < 5 then
- setItems(function(state: { number })
+ setItems(function(state)
state = table.clone(state)
table.insert(state, item + 1)
return state
@@ -114,10 +115,10 @@ end)
test("should handle self removal", function()
local items, setItems = signal({ 1, 2, 3, 4 })
- local edits: { string } = {}
+ local edits = {}
observe(items, function(item, index)
- setItems(function(state: { number })
+ setItems(function(state)
state = table.clone(state)
state[index] = nil
return state
@@ -134,7 +135,7 @@ end)
test("should allow disposal during addition", function()
local items, setItems = signal({})
local edits = {}
- local dispose: () -> ()
+ local dispose
dispose = observe(items, function(item)
if item == 1 then
@@ -153,7 +154,7 @@ end)
test("should allow disposal during removal", function()
local items, setItems = signal({ 1, 2, 3, 4 })
local edits = {}
- local dispose: () -> ()
+ local dispose
dispose = observe(items, function(item)
table.insert(edits, `+{item}`)
@@ -167,32 +168,4 @@ test("should allow disposal during removal", function()
assert(table.concat(edits) == "+1+2+3+4-1-2-3-4", `bad edits: {table.concat(edits)}`)
end)
-test("should not hang on error after recursive update", function()
- local items, setItems = signal({})
-
- observe(items, function()
- setItems({ {} })
- error("test error")
- end)
-
- local success, err = pcall(function()
- setItems({ {} })
- end)
- assert(not success, "should error")
- assert(string.find(tostring(err), "test error"), `bad error: {err}`)
-end)
-
-test("should not hang on error after recursive init", function()
- local items, setItems = signal({ {} })
-
- local success, err = pcall(function()
- observe(items, function()
- setItems({ {} })
- error("test error")
- end)
- end)
- assert(not success, "should error")
- assert(string.find(tostring(err), "test error"), `bad error: {err}`)
-end)
-
return {}
diff --git a/packages/charm/src/__tests__/peek.spec.luau b/packages/charm/src/__tests__/peek.spec.luau
new file mode 100644
index 0000000..b786135
--- /dev/null
+++ b/packages/charm/src/__tests__/peek.spec.luau
@@ -0,0 +1,147 @@
+local Charm = require("../")
+local suite = require("../../../../test/suite")
+
+local signal = Charm.signal
+local computed = Charm.computed
+local effect = Charm.effect
+local peek = Charm.peek
+local test = suite.test
+
+test("should not run detached", function()
+ local a, setA = signal(1)
+ local spyCalls = 0
+
+ effect(function()
+ a()
+ peek(function()
+ effect(function()
+ return function()
+ spyCalls += 1
+ end
+ end)
+ end)
+ end)
+
+ assert(spyCalls == 0, "should not be called yet")
+ setA(2)
+ assert(spyCalls == 1, "should be called once")
+ setA(3)
+ assert(spyCalls == 2, "should be called twice")
+end)
+
+test("should block tracking inside effects", function()
+ local a, setA = signal(1)
+ local b, setB = signal(2)
+ local spyCalls = 0
+
+ effect(function()
+ peek(function()
+ spyCalls += 1
+ return a() + b()
+ end)
+ end)
+ assert(spyCalls == 1, "should be called once")
+
+ setA(10)
+ setB(20)
+ assert(spyCalls == 1, "should be called once")
+end)
+
+test("should block tracking even when run inside effect run inside peek", function()
+ local s, setS = signal(1)
+ local spyCalls = 0
+
+ peek(function()
+ effect(function()
+ peek(function()
+ spyCalls += 1
+ return s()
+ end)
+ end)
+ end)
+ assert(spyCalls == 1, "should be called once")
+
+ setS(2)
+ assert(spyCalls == 1, "should be called once")
+end)
+
+test("should not cause signal assignments to throw", function()
+ local a, setA = signal(1)
+ local aChangedTime, setAChangedTime = signal(0)
+
+ local dispose = effect(function()
+ a()
+ peek(function()
+ setAChangedTime(aChangedTime() + 1)
+ end)
+ end)
+
+ setA(2)
+ assert(aChangedTime() == 2, "aChangedTime should be 2")
+ setA(3)
+ assert(aChangedTime() == 3, "aChangedTime should be 3")
+
+ dispose()
+end)
+
+test("should block tracking inside computed signals", function()
+ local a, setA = signal(1)
+ local b, setB = signal(2)
+ local spyCalls = 0
+ local c = computed(function()
+ return peek(function()
+ spyCalls += 1
+ return a() + b()
+ end)
+ end)
+
+ assert(spyCalls == 0, "should not be called yet")
+ assert(c() == 3, "c should be 3")
+ setA(10)
+ c()
+ setB(20)
+ c()
+ assert(spyCalls == 1, "should be called once")
+ assert(c() == 3, "c should be 3")
+end)
+
+test("should track outside peek", function()
+ local a, setA = signal(1)
+ local b, setB = signal(2)
+ local c, setC = signal(3)
+ local spyCalls = 0
+
+ effect(function()
+ a()
+ peek(c)
+ b()
+ spyCalls += 1
+ end)
+
+ assert(spyCalls == 1, "should be called once")
+ setA(10)
+ assert(spyCalls == 2, "should be called twice")
+ setB(20)
+ assert(spyCalls == 3, "should be called three times")
+ setC(30)
+ assert(spyCalls == 3, "should not run on c change")
+end)
+
+test("should stack peek", function()
+ local a, setA = signal(1)
+ local spyCalls = 0
+
+ effect(function()
+ peek(function()
+ peek(a)
+ return a()
+ end)
+ spyCalls += 1
+ end)
+
+ assert(spyCalls == 1, "should be called once")
+ setA(2)
+ assert(spyCalls == 1, "should not be called twice")
+end)
+
+return {}
diff --git a/packages/charm/test/signal.test.luau b/packages/charm/src/__tests__/signal.spec.luau
similarity index 62%
rename from packages/charm/test/signal.test.luau
rename to packages/charm/src/__tests__/signal.spec.luau
index 21c1746..a9693f6 100644
--- a/packages/charm/test/signal.test.luau
+++ b/packages/charm/src/__tests__/signal.spec.luau
@@ -1,12 +1,13 @@
-local Charm = require("../src")
-local test = require("../../../tests/test")
+local Charm = require("../")
+local suite = require("../../../../test/suite")
local signal = Charm.signal
local effect = Charm.effect
+local test = suite.test
test("should accept equality function", function()
local calls = {}
- local src, setSrc = signal(0, function(current: number, incoming: number)
+ local src, setSrc = signal(0, function(current, incoming)
return current == incoming or incoming % 2 == 1
end)
@@ -46,22 +47,4 @@ test("should not freeze values with metatables", function()
)
end)
-test("should setter return same value as getter even if equal", function()
- local src, setSrc = signal(0, function()
- return true
- end)
- assert(setSrc(1) == src(), `setSrc(1) expected 0, got {setSrc(1)}`)
-end)
-
-test("should setter return latest value", function()
- local src, setSrc = signal(0)
- effect(function()
- if src() == 1 then
- setSrc(2)
- end
- end)
- assert(setSrc(1) == 2, `setSrc(1) expected 2, got {setSrc(1)}`)
- assert(src() == 2, `src() expected 2, got {src()}`)
-end)
-
return {}
diff --git a/packages/charm/src/__tests__/spec.luau b/packages/charm/src/__tests__/spec.luau
new file mode 100644
index 0000000..b95c070
--- /dev/null
+++ b/packages/charm/src/__tests__/spec.luau
@@ -0,0 +1,43 @@
+local suite = require("../../../../test/suite")
+
+suite.describe("computed", function()
+ require("./computed.spec")
+end)
+suite.describe("effect", function()
+ require("./effect.spec")
+end)
+suite.describe("effectScope", function()
+ require("./effectScope.spec")
+end)
+suite.describe("gc", function()
+ require("./gc.spec")
+end)
+suite.describe("listen", function()
+ require("./listen.spec")
+end)
+suite.describe("mapped", function()
+ require("./mapped.spec")
+end)
+suite.describe("observe", function()
+ require("./observe.spec")
+end)
+suite.describe("peek", function()
+ require("./peek.spec")
+end)
+suite.describe("signal", function()
+ require("./signal.spec")
+end)
+suite.describe("subscribe", function()
+ require("./subscribe.spec")
+end)
+suite.describe("topology", function()
+ require("./topology.spec")
+end)
+suite.describe("trigger", function()
+ require("./trigger.spec")
+end)
+suite.describe("untracked", function()
+ require("./untracked.spec")
+end)
+
+return {}
diff --git a/packages/charm/test/subscribe.test.luau b/packages/charm/src/__tests__/subscribe.spec.luau
similarity index 61%
rename from packages/charm/test/subscribe.test.luau
rename to packages/charm/src/__tests__/subscribe.spec.luau
index 10ab932..938931b 100644
--- a/packages/charm/test/subscribe.test.luau
+++ b/packages/charm/src/__tests__/subscribe.spec.luau
@@ -1,9 +1,12 @@
-local Charm = require("../src")
-local test = require("../../../tests/test")
+local Charm = require("../")
+local suite = require("../../../../test/suite")
local signal = Charm.signal
local computed = Charm.computed
local subscribe = Charm.subscribe
+local listen = Charm.listen
+local effect = Charm.effect
+local test = suite.test
test("should subscribe to selector", function()
local source, setSource = signal(0)
@@ -26,27 +29,40 @@ test("should subscribe to selector", function()
assert(current == 2 and previous == 1, "should not run after unsub")
end)
-test("should run in untracked", function()
+test("should run in peek", function()
local runs = 0
+ local cleanups = 0
local a, setA = signal(0)
local b, setB = signal(0)
subscribe(a, function()
runs += 1
b()
+ effect(function()
+ return function()
+ cleanups += 1
+ end
+ end)
end)
assert(runs == 0, "should not run once on init")
+ assert(cleanups == 0, "should not cleanup yet")
+
setA(1)
assert(runs == 1, "should run once on change")
+ assert(cleanups == 0, "should not cleanup yet")
+
setB(1)
assert(runs == 1, "should not run on b change")
+ assert(cleanups == 0, "should not cleanup first call")
+
setA(2)
assert(runs == 2, "should run on a change")
+ assert(cleanups == 1, "should cleanup first call")
end)
test("should ensure correct execution order", function()
- local order: { number } = {}
+ local order = {}
local n1, setN1 = signal(0)
local n2, setN2 = signal(0)
local sum = computed(function()
@@ -71,8 +87,35 @@ test("should ensure correct execution order", function()
assert(table.concat(order) == "123", `bad order: {table.concat(order)}`)
end)
+test("should track inner subs", function()
+ local order = {}
+ local a, setA = signal(1)
+
+ local unsub = subscribe(a, function()
+ table.insert(order, 1)
+ listen(a, function()
+ table.insert(order, 2)
+ end)
+ listen(a, function()
+ table.insert(order, 3)
+ end)
+ end)
+
+ setA(a() + 1)
+ assert(table.concat(order) == "123", `bad order: {table.concat(order)}`)
+
+ table.clear(order)
+ setA(a() + 1)
+ assert(table.concat(order) == "123", `bad order: {table.concat(order)}`)
+
+ table.clear(order)
+ unsub()
+ setA(a() + 1)
+ assert(table.concat(order) == "", `bad order: {table.concat(order)}`)
+end)
+
test("should skip redundant updates", function()
- local order: { "even" | "odd" } = {}
+ local order = {}
local a, setA = signal(0)
subscribe(function(): "even" | "odd"
diff --git a/packages/charm/test/topology.test.luau b/packages/charm/src/__tests__/topology.spec.luau
similarity index 95%
rename from packages/charm/test/topology.test.luau
rename to packages/charm/src/__tests__/topology.spec.luau
index 3675b3d..6f1e1e0 100644
--- a/packages/charm/test/topology.test.luau
+++ b/packages/charm/src/__tests__/topology.spec.luau
@@ -1,9 +1,11 @@
-local Charm = require("../src")
-local test = require("../../../tests/test")
+local Charm = require("../")
+local suite = require("../../../../test/suite")
local atom = Charm.atom
local computed = Charm.computed
local effect = Charm.effect
+local describe = suite.describe
+local test = suite.test
--[[
Tests adopted with thanks from preact-core implementation at
@@ -32,7 +34,7 @@ local effect = Charm.effect
SOFTWARE
]]
-test.describe("graph updates", function()
+describe("graph updates", function()
test("should drop A->B->A updates", function()
-- A
-- / |
@@ -439,19 +441,21 @@ test.describe("graph updates", function()
end)
end)
-test.describe("error handling", function()
+describe("error handling", function()
test("should keep graph consistent on errors during activation", function()
local a = atom(0)
- local b = computed(function(): nil
+ local b = computed(function()
error("fail")
end)
local c = computed(function()
return a()
end)
- local success, result = pcall(b)
+ local success, result = pcall(function()
+ b()
+ end)
assert(not success, "should throw")
- assert(string.match(tostring(result), "fail"), "should throw fail")
+ assert(string.match(result, "fail"), "should throw fail")
a(1)
assert(c() == 1, "c should recover and return 1")
@@ -459,7 +463,7 @@ test.describe("error handling", function()
test("should keep graph consistent on errors in computeds", function()
local a = atom(0)
- local b = computed(function(): number
+ local b = computed(function()
if a() == 1 then
error("fail")
end
@@ -472,9 +476,11 @@ test.describe("error handling", function()
assert(c() == 0, "c should be 0")
a(1)
- local success, result = pcall(b)
+ local success, result = pcall(function()
+ b()
+ end)
assert(not success, "should throw")
- assert(string.match(tostring(result), "fail"), "should throw fail")
+ assert(string.match(result, "fail"), "should throw fail")
a(2)
assert(c() == 2, "c should recover and return 2")
diff --git a/packages/charm/test/trigger.test.luau b/packages/charm/src/__tests__/trigger.spec.luau
similarity index 69%
rename from packages/charm/test/trigger.test.luau
rename to packages/charm/src/__tests__/trigger.spec.luau
index 20ebf32..4055cbe 100644
--- a/packages/charm/test/trigger.test.luau
+++ b/packages/charm/src/__tests__/trigger.spec.luau
@@ -1,18 +1,20 @@
-local Charm = require("../src")
-local test = require("../../../tests/test")
+local Charm = require("../")
+local suite = require("../../../../test/suite")
local computed = Charm.computed
local effect = Charm.effect
-local listen = Charm.listen
local signal = Charm.signal
local subscribe = Charm.subscribe
local trigger = Charm.trigger
+local test = suite.test
+local beforeEach = suite.beforeEach
+local afterEach = suite.afterEach
-test.beforeEach(function()
+beforeEach(function()
Charm.flags.frozen = false
end)
-test.afterEach(function()
+afterEach(function()
Charm.flags.frozen = true
end)
@@ -67,22 +69,6 @@ test("should trigger effect once", function()
assert(triggers == 2, "effect should run twice")
end)
-test("should not notify the trigger function sub", function()
- local src1 = signal({} :: { number })
- local src2 = computed(function()
- return src1()
- end)
-
- effect(function()
- src1()
- src2()
- end)
- trigger(function()
- src1()
- src2()
- end)
-end)
-
test("should trigger updates for subscribe", function()
local src = signal({} :: { number })
local triggers = 0
@@ -91,21 +77,9 @@ test("should trigger updates for subscribe", function()
triggers += 1
end)
+ table.insert(src(), 1)
trigger(src)
assert(triggers == 1, "subscribe callback should run after trigger")
end)
-test("should trigger updates for listen", function()
- local src = signal({} :: { number })
- local triggers = 0
-
- listen(src, function()
- triggers += 1
- end)
- assert(triggers == 1, "listen callback should run once initially")
-
- trigger(src)
- assert(triggers == 2, "listen callback should run after trigger")
-end)
-
return {}
diff --git a/packages/charm/test/untracked.test.luau b/packages/charm/src/__tests__/untracked.spec.luau
similarity index 96%
rename from packages/charm/test/untracked.test.luau
rename to packages/charm/src/__tests__/untracked.spec.luau
index 3c7fea4..d4bfc4c 100644
--- a/packages/charm/test/untracked.test.luau
+++ b/packages/charm/src/__tests__/untracked.spec.luau
@@ -1,11 +1,12 @@
-local Charm = require("../src")
-local test = require("../../../tests/test")
+local Charm = require("../")
+local suite = require("../../../../test/suite")
local signal = Charm.signal
local computed = Charm.computed
local effect = Charm.effect
local effectScope = Charm.effectScope
local untracked = Charm.untracked
+local test = suite.test
test("should run detached from effect", function()
local a, setA = signal(1)
@@ -38,7 +39,7 @@ end)
test("should run detached from effect scope", function()
local effects = 0
- local stopEffect: () -> ()
+ local stopEffect
local stopScope = effectScope(function()
untracked(function()
diff --git a/packages/charm/src/index.d.ts b/packages/charm/src/index.d.ts
index e4689e5..cc27c44 100644
--- a/packages/charm/src/index.d.ts
+++ b/packages/charm/src/index.d.ts
@@ -1,15 +1,15 @@
type Key = string | number | symbol;
-
type Cleanup = () => void;
export enum ReactiveFlags {
- None = 0b000000,
- Mutable = 0b000001,
- Watching = 0b000010,
- RecursedCheck = 0b000100,
- Recursed = 0b001000,
- Dirty = 0b010000,
- Pending = 0b100000,
+ None = 0b0000000,
+ Mutable = 0b0000001,
+ Watching = 0b0000010,
+ RecursedCheck = 0b0000100,
+ Recursed = 0b0001000,
+ Dirty = 0b0010000,
+ Pending = 0b0100000,
+ Peeking = 0b1000000,
}
export interface ReactiveNode {
@@ -31,15 +31,11 @@ export interface Link {
}
export interface Atom {
- (update: Update): T;
+ (newValue: T | ((currentValue: T) => T)): T;
(): T;
}
-export type Getter = () => T;
-
-export type Setter = (update: Update) => T;
-
-export type Update = ((currentValue: T) => T) | T;
+export type Setter = (newValue: T | ((currentValue: T) => T)) => T;
export type Equals = (current: T, incoming: T) => boolean;
@@ -50,12 +46,12 @@ export const flags: {
/**
* Enforces synchronous, non-yielding behavior in signals and effects.
* Also enables state validation in Charm Sync to catch sync errors early.
- * Enabled by default in Roblox Studio.
+ * Enabled if the optimization level is below 2, which is true in studio.
*/
strict: boolean;
/**
* Enforces immutability of tables by deep-freezing table values. Enabled
- * by default in Roblox Studio.
+ * if the optimization level is below 2, which is true in studio.
*/
frozen: boolean;
/**
@@ -75,8 +71,13 @@ export const flags: {
* @returns A tuple containing the getter and setter functions for the signal.
* @see https://github.com/littensy/charm?tab=readme-ov-file#signalinitialValue-equals
*/
-export function signal(initialValue: T, equals?: Equals): LuaTuple<[getter: Getter, setter: Setter]>;
-export function signal(): LuaTuple<[getter: Getter, setter: Setter]>;
+export function signal(
+ initialValue: T,
+ equals?: Equals,
+): LuaTuple<[getter: () => T, setter: (newValue: T | ((currentValue: T) => T)) => T]>;
+export function signal(): LuaTuple<
+ [getter: () => T | undefined, setter: (newValue: T | ((currentValue?: T) => T | undefined)) => T | undefined]
+>;
/**
* Creates a reactive atom that acts as both a signal getter and setter.
@@ -98,7 +99,7 @@ export function atom(): Atom;
* @param getter A function that produces the next value.
* @see https://github.com/littensy/charm?tab=readme-ov-file#computedgetter
*/
-export function computed(getter: (previousValue?: T) => T): Getter;
+export function computed(getter: (previousValue?: T) => T): () => T;
/**
* Creates an effect that runs a callback in response to signal state changes.
@@ -149,12 +150,10 @@ export function trigger(fn: () => void): void;
export function onCleanup(fn: Cleanup, failSilently?: boolean): void;
/**
- * Runs the function without subscribing to signal updates and prevents the
- * current effect or scope from tracking inner effects created within the
- * function.
+ * Runs the function without subscribing to signal updates or capturing inner
+ * effects in the parent effect or scope.
*
- * To avoid tracking signals while still allowing inner effects to be tracked
- * by parent effects/scopes, use `effectScope()` instead.
+ * For an alternative that still cleans up inner effects, use `peek` instead.
*
* @param fn A function that may read signals or create effects.
* @param args Arguments to pass to the function.
@@ -163,6 +162,19 @@ export function onCleanup(fn: Cleanup, failSilently?: boolean): void;
*/
export function untracked(fn: (...args: Args) => Result, ...args: Args): Result;
+/**
+ * Runs the function without subscribing to signal updates, but still captures
+ * inner effects so that they dispose with the parent effect.
+ *
+ * To avoid capturing inner effects, use `untracked` instead.
+ *
+ * @param fn A function that may read signals.
+ * @param args Arguments to pass to the function.
+ * @returns The return value of the function.
+ * @see https://github.com/littensy/charm?tab=readme-ov-file#peekcallback
+ */
+export function peek(fn: (...args: Args) => Result, ...args: Args): Result;
+
/**
* Combines multiple updates into a single "commit" that runs effects and
* computed signals after the provided function finishes running.
@@ -179,51 +191,54 @@ export function batch(fn: (...args: Args) => Result,
* when the value returned by the getter changes. The callback receives the new
* value and the previous value as arguments.
*
+ * Effects created within the callback are cleaned up when the listener runs
+ * or the listener is disposed.
+ *
* @param getter A function that returns the value to watch.
- * @param callback The function to run when the value changes.
+ * @param fn The function to run when the value changes.
* @returns A function for disposing the effect.
* @see https://github.com/littensy/charm?tab=readme-ov-file#listengetter-callback
*/
-export function listen(getter: Getter, callback: (value: T, previousValue: T | undefined) => void): Cleanup;
+export function listen(getter: () => T, fn: (value: T, previousValue: T | undefined) => void): Cleanup;
/**
* Creates an effect that only runs the callback when the value returned by the
* getter changes. The callback receives the new value and the previous value
* as arguments.
*
+ * Effects created within the callback are cleaned up when the listener runs
+ * or the listener is disposed.
+ *
* @param getter A function that returns the value to watch.
- * @param callback The function to run when the value changes.
+ * @param fn The function to run when the value changes.
* @returns A function for disposing the effect.
* @see https://github.com/littensy/charm?tab=readme-ov-file#subscribegetter-callback
*/
-export function subscribe(getter: Getter, callback: (value: T, previousValue: T) => void): Cleanup;
+export function subscribe(getter: () => T, fn: (value: T, previousValue: T) => void): Cleanup;
/**
* Creates a new read-only signal that is computed by mapping over the entries
- * of the source table. If the key is omitted from the transform function's
- * return value, the original key is preserved.
+ * of an existing atom. If the key is omitted from the mapper's return value,
+ * the original key is preserved.
*
- * This function is optimized to minimize unnecessary updates by only calling
- * the transform function for entries that have changed since the last run.
- *
- * @param getter A signal or function that returns a table
- * @param transform Function that maps each entry from the source to a new entry in the mapped table
- * @see https://github.com/littensy/charm?tab=readme-ov-file#mappedgetter-transform
+ * @param getter Returns the table to map over.
+ * @param mapper Transforms each value-key pair from the source into a new value or value-key pair for the resulting table.
+ * @see https://github.com/littensy/charm?tab=readme-ov-file#mappedgetter-mapper
*/
export function mapped(
- getter: Getter