| 165 | * @see {@link https://stately.ai/docs/input | Input docs} for more information about how input is passed |
| 166 | */ |
| 167 | export function fromTransition< |
| 168 | TContext, |
| 169 | TEvent extends EventObject, |
| 170 | TSystem extends AnyActorSystem, |
| 171 | TInput extends NonReducibleUnknown, |
| 172 | TEmitted extends EventObject = EventObject |
| 173 | >( |
| 174 | transition: ( |
| 175 | snapshot: TContext, |
| 176 | event: TEvent, |
| 177 | actorScope: ActorScope< |
| 178 | TransitionSnapshot<TContext>, |
| 179 | TEvent, |
| 180 | TSystem, |
| 181 | TEmitted |
| 182 | > |
| 183 | ) => TContext, |
| 184 | initialContext: |
| 185 | | TContext |
| 186 | | (({ |
| 187 | input, |
| 188 | self |
| 189 | }: { |
| 190 | input: TInput; |
| 191 | self: TransitionActorRef<TContext, TEvent>; |
| 192 | }) => TContext) // TODO: type |
| 193 | ): TransitionActorLogic<TContext, TEvent, TInput, TEmitted> { |
| 194 | return { |
| 195 | config: transition, |
| 196 | transition: (snapshot, event, actorScope) => { |
| 197 | return { |
| 198 | ...snapshot, |
| 199 | context: transition(snapshot.context, event, actorScope as any) |
| 200 | }; |
| 201 | }, |
| 202 | getInitialSnapshot: (_, input) => { |
| 203 | return { |
| 204 | status: 'active', |
| 205 | output: undefined, |
| 206 | error: undefined, |
| 207 | context: |
| 208 | typeof initialContext === 'function' |
| 209 | ? (initialContext as any)({ input }) |
| 210 | : initialContext |
| 211 | }; |
| 212 | }, |
| 213 | getPersistedSnapshot: (snapshot) => snapshot, |
| 214 | restoreSnapshot: (snapshot: any) => snapshot |
| 215 | }; |
| 216 | } |