Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

@rbxts/charm

Package Overview
Dependencies
Maintainers
1
Versions
25
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@rbxts/charm - npm Package Compare versions

Comparing version 0.1.0 to 0.2.0

2

package.json
{
"name": "@rbxts/charm",
"version": "0.1.0",
"version": "0.2.0",
"description": "Atomic state management for Roblox",

@@ -5,0 +5,0 @@ "main": "src/init.lua",

@@ -21,10 +21,19 @@ <p align="center">

**Charm** is an atomic state management library inspired by [Jotai](https://jotai.org). Designed to be a unique alternative to [Reflex](https://littensy.github.io/reflex), Charm aims to provide a simple and composable interface for your game's state.
**Charm** is an atomic state management library inspired by [Jotai](https://jotai.org) and [Nanostores](https://github.com/nanostores/nanostores). Designed to be a more composable alternative to Reflex, Charm aims to address common criticisms of Rodux-like state containers to better address certain use cases.
> [!NOTE]
> Charm is incomplete and not ready for production use.
## 🍀 Features
- ⚛️ **Manage state with _atoms_.** Decompose state into small, distinct containers called _atoms_, as opposed to combining them into a single store.
- 💪 **Minimal, yet powerful.** No concept of actions or middleware — write simple functions to read from and write to state.
- 🔬 **Immediate updates.** Listeners run asynchronously by default, avoiding the cascading effects of deferred updates and improving responsiveness.
- 🦄 **Like magic.** Selector functions can be subscribed to as-is — their memoization and dependencies are resolved for you.
---
## 📦 Setup
### TypeScript
Install Charm for roblox-ts using your package manager of choice.

@@ -37,6 +46,4 @@ ```sh

### Wally
Alternatively, add `littensy/charm` to your `wally.toml` file.
Add `littensy/charm` to your `wally.toml` file.
```toml

@@ -47,36 +54,348 @@ [dependencies]

## 📚 API Reference
---
### Charm
## 📚 Reference
#### `atom(state, options?)`
### `atom(state, options?)`
#### `effect(callback)`
Call `atom` to create a state container with the value `state`.
#### `subscribe(atom, listener)`
```ts
const nameAtom = atom("John");
const todosAtom = atom<string[]>([]);
```
#### `computed(atom, options?)`
#### Parameters
#### `observe(atom, observer)`
- `state`: The value to assign to the atom initially.
#### `mapped(atom, mapper)`
- **optional** `options`: An object that configures the behavior of this atom.
#### `peek(atom)`
- **optional** `equals`: An equality function to determine whether the state has changed. By default, strict equality (`===`) is used.
### React
#### Returns
#### `useAtom(atom, dependencies?)`
The `atom` constructor returns an atom function with two possible operations:
### Sync
1. **Read the state.** Call the atom without arguments to get the current state.
2. **Set the state.** Pass a new value or an updater function to change the state.
#### `sync.client(options)`
```ts
function newTodo() {
nameAtom("Jane");
nameAtom(); // "Jane"
todosAtom((todos) => [...todos, "New todo"]);
}
```
#### `Client.sync(payload)`
---
#### `sync.server(options)`
### `subscribe(atom, listener)`
#### `Server.connect(callback)`
Call `subscribe` to listen for changes to an atom. Changes to the atom will immediately notify all subscribers, passing the new state and the previous state as arguments.
#### `Server.hydrate(player)`
```ts
const nameAtom = atom("John");
subscribe(nameAtom, (name, prevName) => {
print(name);
});
```
You may also pass a _molecule_, or a function that derives a value from one or more atoms. The molecule will be memoized and only re-run when its dependencies change.
```ts
const getUppercase = () => nameAtom().upper();
subscribe(getUppercase, (name) => {
print(name);
});
```
#### Parameters
- `atom`: An atom or molecule that you want to subscribe to. This can be an atom, or a function that reads from one or more atoms.
- `listener`: A function that will be called whenever the atom changes. The listener will receive the new state and the previous state as arguments.
#### Returns
`subscribe` returns a cleanup function.
---
### `effect(callback)`
Call `effect` to declare a side effect that runs when any atom that it depends on changes. The effect will run immediately and whenever its dependencies change.
```ts
const nameAtom = atom("John");
effect(() => {
print(nameAtom());
return () => {
print("Changing name!");
};
});
```
#### Parameters
- `callback`: The function that runs your effect. The function is called once to retrieve its dependencies, and then again whenever they change. Your callback may return a cleanup function to run when the effect is removed or about to re-run.
#### Returns
`effect` returns a cleanup function.
#### Caveats
- Dependencies are captured when the effect is created, but not when it re-runs. This means that atoms referenced conditionally might be "skipped" in the dependency list.
---
### `computed(molecule, options?)`
Call `computed` when you want to derive an expensive value from one or more atoms. The derived value will be memoized and only re-run when its dependencies change.
```ts
const todosAtom = atom<string[]>([]);
const mapToUppercase = computed(() => {
return todosAtom().map((todo) => todo.upper());
});
```
`computed` is useful when you have multiple subscribers that depend on the same derived value. By memoizing the value, you can avoid re-calculating it for each subscriber.
#### Parameters
- `molecule`: A function that reads from one or more atoms and returns a derived value.
- **optional** [`options`](#parameters): An object that configures the behavior of this atom.
#### Returns
`computed` returns a read-only atom.
---
### `observe(atom, factory)`
Call `observe` to run the factory when a key is added to the atom's state. Your factory can return a cleanup function to run when the key is removed or the observer is disposed.
```ts
const todosAtom = atom<{ [Id in string]?: Todo }>({});
observe(todosAtom, (todo, key) => {
print(todo);
return () => {
print("Removing todo!");
};
});
```
#### Parameters
- `atom`: An atom or molecule that you want to observe. This can be a primitive atom, or a function that reads from one or more atoms. The atom should return a dictionary or an array of objects.
- `factory`: A function called for each key in the atom's state. The factory will receive the entry as an argument and should return a cleanup function.
#### Returns
`observe` returns a cleanup function.
#### Caveats
- The factory will only run when a key is added or removed, not when the value at that key changes. If your data is not keyed by a unique and stable identifier, consider using `mapped` to transform it into a keyed object before passing it to `observe`.
---
### `mapped(atom, mapper)`
Call `mapped` to transform the key-value pairs of an atom's state. The mapper function will be called for each key-value pair in the atom's state, and the result will be stored in a new atom.
```ts
const todosAtom = atom<Todo[]>([]);
const todosById = mapped(todosAtom, (todo, index) => {
return $tuple(todo, todo.id);
});
```
#### Parameters
- `atom`: An atom or molecule that you want to map. This can be a primitive atom, or a function that reads from one or more atoms. The atom should return a dictionary or an array of objects.
- `mapper`: A function called for each key-value pair in the atom's state. The mapper will receive the entry as an argument and should return a new key-value pair. The value you return determines the type of the mapped atom:
- If you return a tuple, the mapped atom returns a dictionary with the first element as the value and the second element as the key.
- If you return a value without a key, the mapped atom returns an array of the given values in the order they were mapped.
- If the first element is `undefined`, the entry will be omitted from the mapped atom.
#### Returns
`mapped` returns a read-only atom.
---
### `peek(value)`
Call `peek` to get the current state of an atom without tracking it as a dependency in functions like `effect` and `subscribe`.
```ts
const nameAtom = atom("John");
const ageAtom = atom(25);
effect(() => {
const name = nameAtom();
const age = peek(ageAtom);
});
```
#### Parameters
- `value`: Any value. If the value is an atom, `peek` will return the current state of the atom without tracking it. Otherwise, it will return the value as-is.
- **optional** `...args`: Additional arguments to pass to the value if it is a function.
#### Returns
`peek` returns the current state of the atom. If the value is not a function, it will return the value as-is.
---
### `batch(callback)`
Call `batch` to group multiple state changes into a single update. The callback will run immediately and listeners will only be notified once all changes have been applied.
```ts
const nameAtom = atom("John");
const ageAtom = atom(25);
batch(() => {
nameAtom("Jane");
ageAtom(26);
});
```
#### Parameters
- `callback`: A function that makes multiple state changes. The changes will be batched together and listeners will only be notified once all changes have been applied.
#### Returns
`batch` does not return anything.
---
## 📘 React
### `useAtom(atom, dependencies?)`
Call `useAtom` at the top-level of a React component to read from an atom.
```tsx
import { useAtom } from "@rbxts/charm";
import { todosAtom } from "./todos-atom";
function TodosApp() {
const todos = useAtom(todosAtom);
// ...
}
```
By default, the atom is subscribed to once when the component initially mounts. Optionally, you may pass an array of dependencies to `useAtom` if your atom should be memoized based on other values.
```tsx
const todos = useAtom(searchTodos(filter), [filter]);
```
#### Parameters
- `atom`: An atom or molecule that you want to read from. This can be an atom, or a function that reads from one or more atoms.
- **optional** `dependencies`: An array of values that the atom depends on. If the dependencies change, the atom will be re-subscribed to.
#### Returns
`useAtom` returns the current state of the atom.
---
## 📗 Charm Sync
### `sync.client(options)`
Call `sync.client` to create a client sync object. The object will sync the client's copy of the state with the server's state.
```ts
import { sync } from "@rbxts/charm";
const client = sync.client({ atoms: atomsToSync });
remotes.syncState.connect((payload) => {
client.sync(payload);
});
remotes.requestState.fire();
```
#### Parameters
- `options`: An object to configure the client syncer.
- `atoms`: An object containing the atoms to sync. The keys should match the keys on the server.
#### Returns
`sync.client` returns a client sync object. The sync object has the following methods:
- `client.sync(payload)` applies a state update from the server.
#### Caveats
- The client sync object does not handle network communication. You must implement your own network layer to send and receive state updates. This includes requesting the initial state, which is implemented via `requestState` in the example above.
---
### `sync.server(options)`
Call `sync.server` to create a server sync object. The object handles sending state updates to clients at a specified interval, and hydrating clients with the initial state.
```ts
import { sync } from "@rbxts/charm";
const server = sync.server({ atoms: atomsToSync });
server.connect((player, payload) => {
remotes.syncState.fire(player, payload);
});
remotes.requestState.connect((player) => {
server.hydrate(player);
});
```
#### Parameters
- `options`: An object to configure the server syncer.
- `atoms`: An object containing the atoms to sync. The keys should match the keys on the client.
- `interval`: The interval at which to send state updates to clients. Defaults to `0`, meaning updates are sent on the next frame.
#### Returns
`sync.server` returns a server sync object. The sync object has the following methods:
- `server.connect(callback)` registers a callback to send state updates to clients. The callback will receive the player and the payload to send, and should send the payload to the client. The payload should not be mutated, so changes should be applied to a copy of the payload.
- `server.hydrate(player)` sends the initial state to a player, calling the callback passed to `connect` with a payload containing the initial state.
#### Caveats
- The server sync object does not handle network communication. You must implement your own network layer to send and receive state updates. This includes sending the initial state, which is implemented via `requestState` in the example above.
---
## 🚀 Examples

@@ -90,4 +409,7 @@

const counterAtom = atom(0);
// Create a derived atom that returns double the counter value
const doubleCounterAtom = () => counterAtom() * 2;
// Runs after counterAtom is updated and prints double the new value
subscribe(doubleCounterAtom, (value) => {

@@ -97,4 +419,4 @@ print(value);

counterAtom(1);
counterAtom((count) => count + 1);
counterAtom(1); // 2
counterAtom((count) => count + 1); // 4
```

@@ -107,6 +429,6 @@

import { useAtom } from "@rbxts/charm";
import { counterAtom } from "./counter-atom";
import { counterAtom, incrementCounter } from "./counter-atom";
function Counter() {
const [count, setCount] = useAtom(counterAtom);
const count = useAtom(counterAtom);

@@ -118,3 +440,3 @@ return (

Event={{
Activated: () => setCount(count + 1),
Activated: () => incrementCounter(),
}}

@@ -126,2 +448,49 @@ />

### Server-client sync
Charm provides client and server objects for synchronizing state between the server and clients. Start by defining a module (or creating an object) exporting the atoms you want to sync:
```ts
export { counterAtom } from "./counter-atom";
export { writerAtom } from "./writer-atom";
```
Then, on the server, create a server sync object and pass in the atoms to sync. Use remote events to broadcast state updates and send initial state to clients upon request.
```ts
import { sync } from "@rbxts/charm";
import { remotes } from "./remotes";
import * as atoms from "./atoms";
const server = sync.server({ atoms });
// Broadcast a state update to a specific player
server.connect((player, payload) => {
remotes.syncState.fire(player, payload);
});
// Send initial state to a player upon request
remotes.requestState.connect((player) => {
server.hydrate(player);
});
```
Finally, on the client, create a client sync object and apply incoming state changes.
```ts
import { sync } from "@rbxts/charm";
import { remotes } from "./remotes";
import * as atoms from "./atoms";
const client = sync.client({ atoms });
// Listen for incoming state changes from the server
remotes.syncState.connect((payload) => {
client.sync(payload);
});
// Request initial state from the server
remotes.requestState.fire();
```
---

@@ -128,0 +497,0 @@

@@ -12,31 +12,134 @@ export = Charm;

declare namespace Charm {
/**
* A primitive state container that can be read from and written to. When the
* state changes, all subscribers are notified.
*
* @template State The type of the state.
* @param state The next state or a function that produces the next state.
* @returns The current state, if no arguments are provided.
*/
interface Atom<State> extends Molecule<State> {
/**
* @deprecated This property is not meant to be accessed directly.
*/
readonly __nominal: unique symbol;
/**
* @param state The next state or a function that produces the next state.
* @returns The current state, if no arguments are provided.
*/
(state: State | ((prev: State) => State)): void;
}
/**
* A function that depends on one or more atoms and produces a state. Can be
* used to derive state from atoms.
*
* @template State The type of the state.
* @returns The current state.
*/
type Molecule<State> = () => State;
interface AtomOptions<State> {
/**
* A function that determines whether the state has changed. By default,
* a strict equality check (`===`) is used.
*/
equals?: (prev: State, next: State) => boolean;
}
/**
* Creates a new atom with the given state.
*
* @template State The type of the state.
* @param state The initial state.
* @param options Optional configuration.
* @returns A new atom.
*/
function atom<State>(state: State, options?: AtomOptions<State>): Atom<State>;
/**
* Creates a read-only atom that derives its state from one or more atoms.
* Used to avoid unnecessary recomputations if multiple listeners depend on
* the same molecule.
*
* @param molecule The function that produces the state.
* @param options Optional configuration.
* @returns A new read-only atom.
*/
function computed<State>(molecule: Molecule<State>, options?: AtomOptions<State>): Molecule<State>;
/**
* Subscribes to changes in the given atom or molecule. The callback is
* called with the current state and the previous state immediately after a
* change occurs.
*
* @param molecule The atom or molecule to subscribe to.
* @param callback The function to call when the state changes.
* @returns A function that unsubscribes the callback.
*/
function subscribe<State>(molecule: Molecule<State>, callback: (state: State, prev: State) => void): Cleanup;
function effect(callback: () => void): Cleanup;
/**
* Runs the given callback immediately and whenever any atom it depends on
* changes. Returns a cleanup function that unsubscribes the callback.
*
* Note that dependencies are only evaluated once when the effect is created,
* so conditional dependencies may not work as expected.
*
* @param callback The function to run.
* @returns A function that unsubscribes the callback.
*/
function effect(callback: () => Cleanup | void): Cleanup;
/**
* Returns the result of the function without subscribing to changes. If a
* non-function value is provided, it is returned as is.
*
* @param molecule The atom or molecule to get the state of.
* @param args Arguments to pass to the molecule.
* @returns The current state.
*/
function peek<State, Args extends unknown[]>(molecule: State | ((...args: Args) => State), ...args: Args): State;
/**
* Returns whether the given value is an atom.
*
* @param value The value to check.
* @returns `true` if the value is an atom, otherwise `false`.
*/
function isAtom(value: unknown): value is Atom<any>;
/**
* Runs the given function and schedules listeners to be notified only once
* after the function has completed. Useful for batching multiple changes.
*
* @param callback The function to run.
*/
function batch(callback: () => void): void;
/**
* Captures all atoms that are read during the function call and returns them
* along with the result of the function. Useful for tracking dependencies.
*
* @param molecule The function to run.
* @returns A tuple containing the captured atoms and the result of the function.
*/
function capture<State>(molecule: Molecule<State>): LuaTuple<[captured: Set<Atom<unknown>>, state: State]>;
/**
* Notifies all subscribers of the given atom that the state has changed.
*
* @param atom The atom to notify.
*/
function notify(atom: Atom<unknown>): void;
/**
* Creates an instance of `factory` for each item in the atom's state, and
* cleans up the instance when the item is removed. Returns a cleanup function
* that unsubscribes all instances.
*
* @param molecule The atom or molecule to observe.
* @param factory The function that tracks the lifecycle of each item.
* @returns A function that unsubscribes all instances.
*/
function observe<Item>(

@@ -52,2 +155,12 @@ molecule: Molecule<readonly Item[]>,

/**
* Maps each entry in the atom's state to a new key-value pair. If the `mapper`
* function returns `undefined`, the entry is omitted from the resulting map.
* When the atom changes, the `mapper` is called for each entry in the state
* to compute the new state.
*
* @param molecule The atom or molecule to map.
* @param mapper The function that maps each entry.
* @returns A new atom with the mapped state.
*/
function mapped<V0, K1, V1>(

@@ -68,16 +181,45 @@ molecule: Molecule<readonly V0[]>,

// React
/**
* A hook that subscribes to changes in the given atom or molecule. The
* component is re-rendered whenever the state changes.
*
* If the `dependencies` array is provided, the subscription to the atom or
* molecule is re-created whenever the dependencies change. Otherwise, the
* subscription is created once when the component is mounted.
*
* @param molecule The atom or molecule to subscribe to.
* @param dependencies An array of values that the subscription depends on.
* @returns The current state.
*/
function useAtom<State>(molecule: Molecule<State>, dependencies?: unknown[]): State;
// Sync
/**
* Synchronizes state between the client and server. The server sends patches
* to the client, which applies them to its local state.
*/
const sync: {
/**
* Creates a `ClientSyncer` object that receives patches from the server and
* applies them to the local state.
*
* @param options The atoms to synchronize with the server.
* @returns A `ClientSyncer` object.
*/
client: <T extends Record<string, Atom<any>>>(options: ClientOptions<T>) => ClientSyncer<T>;
/**
* Creates a `ServerSyncer` object that sends patches to the client and
* hydrates the client's state.
*
* @param options The atoms to synchronize with the client.
* @returns A `ServerSyncer` object.
*/
server: <T extends Record<string, Atom<any>>>(options: ServerOptions<T>) => ServerSyncer<T>;
collect: (root: Instance) => Record<string, Atom<any>>;
};
/**
* A special value that denotes the absence of a value. Used to represent
* undefined values in patches.
*/
interface None {
__none: "__none";
readonly __none: "__none";
}

@@ -89,5 +231,12 @@

/**
* A payload that can be sent from the server to the client to synchronize
* state between the two.
*/
type SyncPayload<T> = { type: "init"; data: T } | { type: "patch"; data: SyncPatch<T> };
interface ClientOptions<T extends Record<string, Atom<any>>> {
/**
* The atoms to synchronize with the server.
*/
atoms: T;

@@ -97,3 +246,11 @@ }

interface ServerOptions<T extends Record<string, Atom<any>>> {
/**
* The atoms to synchronize with the client.
*/
atoms: T;
/**
* The interval at which to send patches to the client, in seconds.
* Defaults to `0` (patches are sent up to once per frame). Set to a
* negative value to disable automatic syncing.
*/
interval?: number;

@@ -103,2 +260,8 @@ }

interface ClientSyncer<T extends Record<string, Atom<any>>> {
/**
* Applies a patch or initializes the state of the atoms with the given
* payload from the server.
*
* @param payload The patch or hydration payload to apply.
*/
sync(payload: SyncPayload<T>): void;

@@ -108,5 +271,22 @@ }

interface ServerSyncer<T extends Record<string, Atom<any>>> {
/**
* Sets up a subscription to each atom that schedules a patch to be sent to
* the client whenever the state changes. When a change occurs, the `callback`
* is called with the player and the payload to send.
*
* Note that the `payload` object should not be mutated. If you need to
* modify the payload, apply the changes to a copy of the object.
*
* @param callback The function to call when the state changes.
* @returns A cleanup function that unsubscribes all listeners.
*/
connect(callback: (player: Player, payload: SyncPayload<T>) => void): Cleanup;
/**
* Hydrates the client's state with the server's state. This should be
* called when a player joins the game and requires the server's state.
*
* @param player The player to hydrate.
*/
hydrate(player: Player): void;
}
}

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc