search/
Start typing to search packages!
package_2
state
By @femphilic
Roblox
MirroredState Library
A reactive state management library for Luau with type safety and performance optimizations.
Features
- Type-safe reactive state
- Automatic connection cleanup
- Debug mode with detailed logging
- Performance optimizations
- Comprehensive error handling
- Memory leak prevention
Installation
You can either copy the module directly into your Rojo codebase and require it like so:
local State = require(path.to.State)
...or use Wally to install the library automatically:
# In wally.toml...
# Other Wally properties
state = "femphilic/state@1.1.2" # Change the version as necessary and put this under dependencies
# Other Wally properties
wally install
local State = require(game.ReplicatedStorage.Packages.state) -- Depends on where your "packages" are located in Studio, the name of your `Packages` folder, and the property name in the wally.toml file (e.g. with the example above it would be "state" like so)
Basic Usage
-- Create a state
local counter = State.new(0)
-- Connect to changes
local connection = counter:connect(function(value)
print("Counter changed to:", value)
end)
-- Update state
counter:set(5)
-- Disconnect when done with certain connection
connection:disconnect()
-- Destroy state when done with state
state:destroy()
Advanced Usage
-- Debug mode
State.setDebugMode(true)
-- Custom connection limits
State.setMaxConnections(500)
-- Named states for debugging
local playerHealth = State.new(100, "PlayerHealth")
API Reference
Static Functions
State.new<T>(initialValue: T, debugName?: string): State<T>- Creates a new
stateobject. If attempting to debug,debugNamehas to be set to a string
- Creates a new
State.setDebugMode(enabled: boolean): ()- Sets debug mode to
enabled(default:false)
- Sets debug mode to
State.getDebugMode(): boolean- Gets the current debug mode configuration (default:
false)
- Gets the current debug mode configuration (default:
State.setMaxConnections(max: number): ()- Allows you to set the maximum number of connections each state can have. It is highly recommended to keep this below
1000(default:100)
- Allows you to set the maximum number of connections each state can have. It is highly recommended to keep this below
State.getMaxConnections(): number- Gets the current max connection configuration (default:
100)
- Gets the current max connection configuration (default:
Class Functions
It is assumed that the self type is equal to State<T> in all situations
state:set(value: T): ()- Sets the state to
value, firing all connected callbacks
- Sets the state to
state:connect(callback: (value: T) -> ()): Connection- Connects
callbackto the state, firing whenever said state is set. Returns a connection (seeconnectionbelow)
- Connects
state:destroy(): ()- Destroys the state, meaning that all connections are disconnected, the connection counter is set to
0, and the state is rendered unusable.
- Destroys the state, meaning that all connections are disconnected, the connection counter is set to
state:isDestroyed(): boolean- Returns the state's destroyed state
state:setDebugName(name: string?): ()- Sets the state's debug name to
name. This can also be set tonilif you would like to disable debugging for that state.
- Sets the state's debug name to
state:getDebugName(): string?- Get the current state's debug name, which can potentially be
nil.
- Get the current state's debug name, which can potentially be
Connection functions
Connections are returned when calling state:connect(callback).
It is assumed that the self type is equal to Connection in all situations
connection:disconnect(): ()- Disconnects the connection callback, meaning it'll no longer be called when
state:set(value)is called
- Disconnects the connection callback, meaning it'll no longer be called when
connection:isConnected(): boolean- Returns whether the connection callback is connected to the state.
Package Details
Install command (Click to copy)
Version
1.1.3
License
MIT
Safe for commercial use
Automated license review — not legal advice.
