Forest Logo
search
package_2

observable-value

By @jdevon

Roblox

Mirrored from Wally

ObservableValue

A typed value you can read, set, and observe. Adding a listener returns a cleanup function that's safe to call more than once.

Full API reference at jdevon.github.io/observable-value.

Install

badges/github badges/wally

Install with Wally by adding it to your wally.toml:

[dependencies]
ObservableValue = "jdevon/observable-value@0.1.0"

Usage

local ObservableValue = require(ReplicatedStorage.Packages.ObservableValue)

local health = ObservableValue.new(100)

local cleanup = health:onChanged(function(newHealth, oldHealth)
	print(`health went from {oldHealth} to {newHealth}`)
end)

health:set(80) --> health went from 100 to 80
health:update(function(current)
	return current - 10
end) --> health went from 80 to 70

cleanup()

-- yields until health drops to 0 or below, then continues with the final value
local finalHealth = health:promiseValue(function(current)
	return current <= 0, current
end):expect()
print(`defeated at {finalHealth} health`)

API

Each member links to its full entry in the API reference.

Types

  • Disconnect - the cleanup function returned by every listener method

Properties

  • version - increases each time the value changes

Functions

  • .new - creates an ObservableValue holding an initial value
  • :get - returns the current value
  • :set - sets the value
  • :update - sets the value from the current one
  • :onChanged - calls back whenever the value changes
  • :onSet - calls back on every set
  • :observe - calls back now with the current value, then on every change
  • :promiseValue - resolves once the value passes a check
  • :Destroy - disconnects every listener (:destroy also works)

Package Details

Install command (Click to copy)


Version

0.1.0

License

MIT

check_circle

Safe for commercial use

Automated license review — not legal advice.