From 1b011556b8aef2b429fbe959c35a59cd67f87a08 Mon Sep 17 00:00:00 2001 From: Evyatar Date: Mon, 25 Sep 2023 17:11:28 -0400 Subject: [PATCH] Subscribe docs --- packages/vest/src/suite/createSuite.ts | 5 +++-- website/docs/api_reference.md | 2 ++ website/docs/writing_your_suite/vests_suite.md | 11 +++++++++++ 3 files changed, 16 insertions(+), 2 deletions(-) diff --git a/packages/vest/src/suite/createSuite.ts b/packages/vest/src/suite/createSuite.ts index f19e791b3..30d7c43c1 100644 --- a/packages/vest/src/suite/createSuite.ts +++ b/packages/vest/src/suite/createSuite.ts @@ -65,7 +65,8 @@ function createSuite< // We do this within the VestRuntime so that the suite methods // will be bound to the suite's stateRef and be able to access it. return VestRuntime.Run(stateRef, () => { - const { subscribe } = useInitVestBus(); + // @vx-allow use-use + const VestBus = useInitVestBus(); return assign( // We're also binding the suite to the stateRef, so that the suite @@ -80,7 +81,7 @@ function createSuite< reset: Bus.usePrepareEmitter(Events.RESET_SUITE), resetField: Bus.usePrepareEmitter(Events.RESET_FIELD), resume: VestRuntime.persist(useLoadSuite), - subscribe: subscribe, + subscribe: VestBus.subscribe, ...bindSuiteSelectors(VestRuntime.persist(useCreateSuiteResult)), ...getTypedMethods(), } diff --git a/website/docs/api_reference.md b/website/docs/api_reference.md index 79ea419d7..80258d487 100644 --- a/website/docs/api_reference.md +++ b/website/docs/api_reference.md @@ -44,6 +44,7 @@ keywords: promisify, compose, staticSuite, + subscrib, ] --- @@ -59,6 +60,7 @@ Below is a list of all the API functions exposed by Vest. - [suite.remove](./writing_your_suite/vests_suite.md#removing-a-single-field-from-the-suite-state) - Removes a single field from the suite. - [suite.reset](./writing_your_suite/vests_suite.md#cleaning-up-our-validation-state) - Resets the suite to its initial state. - [suite.resetField](./writing_your_suite/vests_suite.md#cleaning-up-our-validation-state) - Resets a single field to an untested state. + - [suite.subscribe](./writing_your_suite/vests_suite.md#subscribing-to-suite-state-changes) - Subscribes to suite state changes. - [staticSuite](./server_side_validations.md) - creates a stateless suite that is used for server side validations. diff --git a/website/docs/writing_your_suite/vests_suite.md b/website/docs/writing_your_suite/vests_suite.md index a521b3312..0c13ab8af 100644 --- a/website/docs/writing_your_suite/vests_suite.md +++ b/website/docs/writing_your_suite/vests_suite.md @@ -87,3 +87,14 @@ To reset the validity of a single field, you can call `suite.resetField(fieldNam In some cases, you may want to remove a field from the suite state. For example, when the user removes a dynamically added field. In this case, you can call `suite.remove(fieldName)` to remove the field from the state and cancel any pending async validations that might still be running. Note that you don't need to use `suite.remove` very often, as most users can simply use `reset` and `omitWhen`. + +## Subscribing to Suite State Changes + +You can subscribe to changes in the suite state by calling `suite.subscribe(callback)`. The callback will be called whenever the suite state changes internally. + +```js +suite.subscribe(() => { + const result = suite.get(); + // ... Do something with the result +}); +```