diff --git a/CHANGELOG.md b/CHANGELOG.md index 3900dad..2a576af 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -17,6 +17,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/). - Reactive scopes created within reactive scopes are now destroyed on rerun. - `untrack()` can be called outside of reactive scopes. +- `changed()` will also run its callback with the initial property value. ### Fixed diff --git a/docs/api/creation.md b/docs/api/creation.md index f3f51c8..b4576b8 100644 --- a/docs/api/creation.md +++ b/docs/api/creation.md @@ -154,3 +154,23 @@ instances. changed("Text", output) } ``` + +## changed() + +A wrapper for `action()` to listen for property changes. + +- **Type** + + ```lua + function changed(property: string, callback: (...unknown) -> ()): Action + ``` + +- **Details** + + Will run the given callback any time the property is changed, as well as + when the action is initially run. + + The changed connection is disconnected when the reactive scope the action is + ran in is destroyed. + + Runs with an action priority of 1. diff --git a/src/changed.luau b/src/changed.luau index 5d439c6..519a554 100644 --- a/src/changed.luau +++ b/src/changed.luau @@ -12,6 +12,8 @@ local function changed(property: string, callback: (T) -> ()) cleanup(function() con:Disconnect() end) + + callback((instance :: any)[property]) end) end diff --git a/test/tests.luau b/test/tests.luau index 7e95cca..a82712b 100644 --- a/test/tests.luau +++ b/test/tests.luau @@ -1806,7 +1806,7 @@ TEST("changed()", wrap_root(function() changed("Text", output) } - --CHECK(output() == "a") + CHECK(output() == "a") text.Text = "b" CHECK(output() == "b") end