@@ -58,6 +58,12 @@ export type UsableStatechart<
5858 | MachineConfig < TContext , TStateSchema , TEvent >
5959 | StateMachine < TContext , TStateSchema , TEvent , TTypestate > ;
6060
61+ /**
62+ * An object that makes an XState-[Interpreter](https://xstate.js.org/docs/guides/interpretation.html)
63+ * accessible to the outside world and allows `send`ing events to it.
64+ *
65+ * This is actually the return value of {@link InterpreterService.state}.
66+ */
6167export type InterpreterUsable <
6268 TContext ,
6369 TStateSchema extends StateSchema ,
@@ -134,6 +140,13 @@ export type UseMachineBucket<
134140 } ;
135141} ;
136142
143+ /**
144+ * The actual usable that gets started when a {@link ConfigurableMachineDefinition} is `@use`d.
145+ *
146+ * On initial access this class will setup an XState-[Interpreter](https://xstate.js.org/docs/guides/interpretation.html)
147+ * and make its state available to the outside world.
148+ *
149+ */
137150export class InterpreterService <
138151 TContext ,
139152 TStateSchema extends StateSchema ,
@@ -159,6 +172,25 @@ export class InterpreterService<
159172 this . onTransition = onTransition ;
160173 }
161174
175+ /**
176+ * The object that gets returned when you access the Usable.
177+ *
178+ * ```js
179+ * import Component from '@glimmer/component';
180+ * import buttonMachine from '../machines/button';
181+ *
182+ * export default ButtonComponent extends Component {
183+ * @use statechart = useMachine(buttonMachine)
184+ *
185+ * @action
186+ * buttonClicked() {
187+ * // acessing `statechart` will return the return value of this getter
188+ * this.statechart.send('CLICK');
189+ * }
190+ * }
191+ *
192+ * ```
193+ */
162194 get state ( ) : {
163195 state : State < TContext , TEvent , TStateSchema , TTypestate > ;
164196 send : Send < TContext , TStateSchema , TEvent , TTypestate > ;
@@ -323,6 +355,81 @@ const createMachineInterpreterManager = () => {
323355const MANAGED_INTERPRETER = { } ;
324356setUsableManager ( MANAGED_INTERPRETER , createMachineInterpreterManager ) ;
325357
358+ /**
359+ * A function that lets you define a {@link ConfigurableMachineDefinition} that can be
360+ * `@use`d.
361+ *
362+ *
363+ * ```ts
364+ * import Component from '@glimmer/component';
365+ * import buttonMachine from '../machines/button';
366+ *
367+ * export default ButtonComponent extends Component {
368+ * @use statechart = useMachine(buttonMachine)
369+ *
370+ * @action
371+ * buttonClicked() {
372+ * this.statechart.send('CLICK');
373+ * }
374+ * }
375+ * ```
376+ *
377+ * The {@link ConfigurableMachineDefinition} can be used to override properties
378+ * of the {@link UsableStatechart} that was passed to `useMachine`.
379+ *
380+ * ```ts
381+ * import Component from '@glimmer/component';
382+ * import formMachine from '../machines/form';
383+ * // ...
384+ *
385+ * export default FormComponent extends Component {
386+ * // ...
387+ * @use statechart = useMachine(formMachine)
388+ * .withContext({
389+ * // we can override the machine's context here
390+ * formObject: this.formObject
391+ * })
392+ * .withConfig({
393+ * services: {
394+ * // to override an async function that can be invoked
395+ * submitForm: this.submitForm
396+ * },
397+ * actions: {
398+ * notifyFormSubmission: this.notifyFormSubmission
399+ * },
400+ * guards: {
401+ * // ...
402+ * }
403+ * })
404+ * .onTransition((state) => {
405+ * // when you want to react to transitions manually
406+ * })
407+ * .update(({ send }) => {
408+ * // decide how to handle properties passed to `useMachine` changing
409+ * send('MY_EVENT_TO_HANDLE_ARGS_CHANGES');
410+ * })
411+ *
412+ * @action
413+ * submitForm(context, event) {
414+ * return this.onSubmitForm(context.formObject);
415+ * }
416+ *
417+ * @action
418+ * notifyFormSubmission() {
419+ * this.notifications.notify('Form was submitted');
420+ * }
421+ * }
422+ * ```
423+ *
424+ * A {@link ConfigurableMachineDefinition} is only used to configure how the
425+ * interpreted {@link UsableStatechart} will behave when interacting with it.
426+ *
427+ * When accessing the `@use`d value you are actually dealing with a {@link
428+ * InterpreterUsable} - i.e. an object that gives you access to the
429+ * `state`-property of an XState-[Interpreter](https://xstate.js.org/docs/guides/interpretation.html)
430+ * and a `send`-function that can be used to send events to the interpreter.
431+ *
432+ */
326433export default function useMachine <
327434 TContext ,
328435 TStateSchema extends StateSchema ,
0 commit comments