Skip to content

Commit 0db0340

Browse files
committed
feat: make second callback arg useful (#42)
- Documenting new features - Rearranging tests a bit to take advantage of new features BREAKING CHANGE: previously the second arg to the callback function had a single `data` property on it representing the last `State` object seen by the top-level machine. Now it has `state` (same as data was previously), and some bound APIs for interacting with the statechart: `.hasTag()`, `.broadcast()`, and `.matches()`. These are the same APIs available on the `ComponentTree` instance but made available through the callback args for convenience.
1 parent c64065d commit 0db0340

File tree

15 files changed

+897
-61
lines changed

15 files changed

+897
-61
lines changed

README.md

Lines changed: 42 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ Machine({
4141
},
4242
});
4343
```
44+
4445
Props for the components are also supported via the `props` key.
4546

4647
```js
@@ -108,7 +109,7 @@ new ComponentTree(service, (tree) => {
108109
});
109110
```
110111

111-
This data structure can also contain components from any child statecharts you created using `invoke`, they will be correctly walked & monitored for transitions.
112+
This data structure can also contain components from any child statecharts you created using `invoke`, they will be correctly walked & monitored for transitions and appear in their expected position within the hierarchy. This lets you compose a larger statechart from several smaller ones and still have them all contribute components to the app.
112113

113114
## Advanced Usage
114115

@@ -208,24 +209,61 @@ import { component } from "xstate-component-tree/component";
208209

209210
## API
210211

211-
### `new ComponentTree(interpreter, callback, [options])`
212+
### `ComponentTree`
213+
214+
#### `new ComponentTree(interpreter, callback, [options])`
212215

213216
- `interpreter`, and instance of a xstate interpreter
214217
- `callback`, a function that will be executed each time a new tree of components is ready
215218
- `options`, an optional object containing [configuration values](#options) for the library.
216219

220+
The `callback` functions receives two arguments, the first is your assembled tree of components & props. The second is an object with some useful information on it:
221+
222+
- `.state`, the returned xstate `State` object for the root machine
223+
- `.matches()`, a bound version of the [`.matches()` API documented below](#matchesstatename)
224+
- `.hasTag()`, a bound version of the [`.hasTag()` API documented below](#hastagtag)
225+
- `.broadcast()`, a bound version of the [`.broadcast()` API documented below](#broadcasteventname--eventobject-payload)
226+
217227
#### `options`
218228

219229
- `cache` (default `true`), a boolean determining whether or not the value of `load()` functions should be cached. This can be overriden by setting `meta.cache` on any state in the tree where caching needs to be disabled.
220230

221231
- `stable` (default: `false`), tells the library to sort states alphabetically before walking them at each tier to help ensure that the component output is more consistent between state transitions.
222232

223-
### `component(Component | () => {}, [node])`
233+
- `verbose` (default: `false`), logs out info about the internal workings & decisions.
234+
235+
### `ComponentTree` instance methods
236+
237+
#### `.broadcast(eventName | eventObject, payload)`
238+
239+
Calls the xstate `.send()` method on every running interpreter in the hierarchy. This is especially useful to avoid the use of the `autoforward` option on all of your invoked child machines.
240+
241+
- `eventName` is a string event to be sent
242+
- `eventObject` is an object with a `type` property of the event name, along with other optional fields
243+
- `payload` is an object of optional fields to be added to the event object
244+
245+
#### `.matches(stateName)`
246+
247+
- `stateName` is a full or partial state value specified as a string
248+
249+
Calls the [xstate `.matches()` method](https://xstate.js.org/docs/guides/states.html#state-matches-parentstatevalue) against all the running machines and returns the result, stopping at the first successful match.
250+
251+
#### `.hasTag(tag)`
252+
253+
- `tag` is a string, which can be defined on states using the `tags` property
254+
255+
Calls the [xstate `.hasTag()` method](https://xstate.js.org/docs/guides/states.html#state-hastag-tag) against all the running machines and returns the result, stopping at the first successful match.
256+
257+
### `component()` helper
258+
259+
The `component` helper returns an `xstate` node as an object literal, it is solely a convenience method for statechart authors.
260+
261+
#### `component(Component | () => {}, [node])`
224262

225263
- `Component` is either a component or an arrow function that will be executed. It supports functions that return either a component or a `Promise`.
226264
- `node` is a valid xstate node, the `meta` object will be created and mixed-in by the `component()`.
227265

228-
### `component({ component : Component | () => {}, props : {...} | () => {} })`
266+
#### `component({ component : Component | () => {}, props : {...} | () => {} })`
229267

230268
- `component` is either a raw Component or an arrow function that will be executed. It supports returning either a value or a `Promise`.
231269
- `props` is either a props object or a function that will be executed. It supports function returning either a value or a `Promise`.

0 commit comments

Comments
 (0)