|
1 | 1 | # xstate |
2 | 2 |
|
| 3 | +## 5.15.0 |
| 4 | + |
| 5 | +### Minor Changes |
| 6 | + |
| 7 | +- [#4976](https://github.com/statelyai/xstate/pull/4976) [`452bce71e`](https://github.com/statelyai/xstate/commit/452bce71e56fb28bfd85294b44138cf1bd5a9608) Thanks [@with-heart](https://github.com/with-heart)! - Added exports for actor logic-specific `ActorRef` types: `CallbackActorRef`, `ObservableActorRef`, `PromiseActorRef`, and `TransitionActorRef`. |
| 8 | + |
| 9 | + Each type represents `ActorRef` narrowed to the corresponding type of logic (the type of `self` within the actor's logic): |
| 10 | + |
| 11 | + - `CallbackActorRef`: actor created by [`fromCallback`](https://stately.ai/docs/actors#fromcallback) |
| 12 | + |
| 13 | + ```ts |
| 14 | + import { fromCallback, createActor } from 'xstate'; |
| 15 | + |
| 16 | + /** The events the actor receives. */ |
| 17 | + type Event = { type: 'someEvent' }; |
| 18 | + /** The actor's input. */ |
| 19 | + type Input = { name: string }; |
| 20 | + |
| 21 | + /** Actor logic that logs whenever it receives an event of type `someEvent`. */ |
| 22 | + const logic = fromCallback<Event, Input>(({ self, input, receive }) => { |
| 23 | + self; |
| 24 | + // ^? CallbackActorRef<Event, Input> |
| 25 | + |
| 26 | + receive((event) => { |
| 27 | + if (event.type === 'someEvent') { |
| 28 | + console.log(`${input.name}: received "someEvent" event`); |
| 29 | + // logs 'myActor: received "someEvent" event' |
| 30 | + } |
| 31 | + }); |
| 32 | + }); |
| 33 | + |
| 34 | + const actor = createActor(logic, { input: { name: 'myActor' } }); |
| 35 | + // ^? CallbackActorRef<Event, Input> |
| 36 | + ``` |
| 37 | + |
| 38 | + - `ObservableActorRef`: actor created by [`fromObservable`](https://stately.ai/docs/actors#fromobservable) and [`fromEventObservable`](https://stately.ai/docs/actors#fromeventobservable) |
| 39 | + |
| 40 | + ```ts |
| 41 | + import { fromObservable, createActor } from 'xstate'; |
| 42 | + import { interval } from 'rxjs'; |
| 43 | +
|
| 44 | + /** The type of the value observed by the actor's logic. */ |
| 45 | + type Context = number; |
| 46 | + /** The actor's input. */ |
| 47 | + type Input = { period?: number }; |
| 48 | +
|
| 49 | + /** |
| 50 | + * Actor logic that observes a number incremented every `input.period` |
| 51 | + * milliseconds (default: 1_000). |
| 52 | + */ |
| 53 | + const logic = fromObservable<Context, Input>(({ input, self }) => { |
| 54 | + self; |
| 55 | + // ^? ObservableActorRef<Event, Input> |
| 56 | +
|
| 57 | + return interval(input.period ?? 1_000); |
| 58 | + }); |
| 59 | +
|
| 60 | + const actor = createActor(logic, { input: { period: 2_000 } }); |
| 61 | + // ^? ObservableActorRef<Event, Input> |
| 62 | + ``` |
| 63 | + |
| 64 | + - `PromiseActorRef`: actor created by [`fromPromise`](https://stately.ai/docs/actors#actors-as-promises) |
| 65 | + |
| 66 | + ```ts |
| 67 | + import { fromPromise, createActor } from 'xstate'; |
| 68 | +
|
| 69 | + /** The actor's resolved output. */ |
| 70 | + type Output = string; |
| 71 | + /** The actor's input. */ |
| 72 | + type Input = { message: string }; |
| 73 | +
|
| 74 | + /** Actor logic that fetches the url of an image of a cat saying `input.message`. */ |
| 75 | + const logic = fromPromise<Output, Input>(async ({ input, self }) => { |
| 76 | + self; |
| 77 | + // ^? PromiseActorRef<Output, Input> |
| 78 | +
|
| 79 | + const data = await fetch(`https://cataas.com/cat/says/${input.message}`); |
| 80 | + const url = await data.json(); |
| 81 | + return url; |
| 82 | + }); |
| 83 | + |
| 84 | + const actor = createActor(logic, { input: { message: 'hello world' } }); |
| 85 | + // ^? PromiseActorRef<Output, Input> |
| 86 | + ``` |
| 87 | +
|
| 88 | + - `TransitionActorRef`: actor created by [`fromTransition`](https://stately.ai/docs/actors#fromtransition) |
| 89 | +
|
| 90 | + ```ts |
| 91 | + import { fromTransition, createActor, type AnyActorSystem } from 'xstate'; |
| 92 | + |
| 93 | + /** The actor's stored context. */ |
| 94 | + type Context = { |
| 95 | + /** The current count. */ |
| 96 | + count: number; |
| 97 | + /** The amount to increase `count` by. */ |
| 98 | + step: number; |
| 99 | + }; |
| 100 | + /** The events the actor receives. */ |
| 101 | + type Event = { type: 'increment' }; |
| 102 | + /** The actor's input. */ |
| 103 | + type Input = { step?: number }; |
| 104 | + |
| 105 | + /** |
| 106 | + * Actor logic that increments `count` by `step` when it receives an event of |
| 107 | + * type `increment`. |
| 108 | + */ |
| 109 | + const logic = fromTransition<Context, Event, AnyActorSystem, Input>( |
| 110 | + (state, event, actorScope) => { |
| 111 | + actorScope.self; |
| 112 | + // ^? TransitionActorRef<Context, Event> |
| 113 | + |
| 114 | + if (event.type === 'increment') { |
| 115 | + return { |
| 116 | + ...state, |
| 117 | + count: state.count + state.step |
| 118 | + }; |
| 119 | + } |
| 120 | + return state; |
| 121 | + }, |
| 122 | + ({ input, self }) => { |
| 123 | + self; |
| 124 | + // ^? TransitionActorRef<Context, Event> |
| 125 | + |
| 126 | + return { |
| 127 | + count: 0, |
| 128 | + step: input.step ?? 1 |
| 129 | + }; |
| 130 | + } |
| 131 | + ); |
| 132 | + |
| 133 | + const actor = createActor(logic, { input: { step: 10 } }); |
| 134 | + // ^? TransitionActorRef<Context, Event> |
| 135 | + ``` |
| 136 | +
|
| 137 | +- [#4949](https://github.com/statelyai/xstate/pull/4949) [`8aa4c2b90`](https://github.com/statelyai/xstate/commit/8aa4c2b90c6a07a4678202181420b5ddd33edacf) Thanks [@davidkpiano](https://github.com/davidkpiano)! - The TypeGen-related types have been removed from XState, simplifying the internal types without affecting normal XState usage. |
| 138 | +
|
3 | 139 | ## 5.14.0 |
4 | 140 |
|
5 | 141 | ### Minor Changes |
|
0 commit comments