(
observableCreator: ({
input,
system,
self
}: {
input: TInput;
system: AnyActorSystem;
self: ObservableActorRef<TContext>;
emit: (emitted: TEmitted) => void;
}) => Subscribable<TContext>
)
| 116 | * @see {@link Subscribable} interface in XState, which is based on and compatible with RxJS Observable. |
| 117 | */ |
| 118 | export function fromObservable< |
| 119 | TContext, |
| 120 | TInput extends NonReducibleUnknown, |
| 121 | TEmitted extends EventObject = EventObject |
| 122 | >( |
| 123 | observableCreator: ({ |
| 124 | input, |
| 125 | system, |
| 126 | self |
| 127 | }: { |
| 128 | input: TInput; |
| 129 | system: AnyActorSystem; |
| 130 | self: ObservableActorRef<TContext>; |
| 131 | emit: (emitted: TEmitted) => void; |
| 132 | }) => Subscribable<TContext> |
| 133 | ): ObservableActorLogic<TContext, TInput, TEmitted> { |
| 134 | // TODO: add event types |
| 135 | const logic: ObservableActorLogic<TContext, TInput, TEmitted> = { |
| 136 | config: observableCreator, |
| 137 | transition: (snapshot, event) => { |
| 138 | if (snapshot.status !== 'active') { |
| 139 | return snapshot; |
| 140 | } |
| 141 | |
| 142 | switch (event.type) { |
| 143 | case XSTATE_OBSERVABLE_NEXT: { |
| 144 | const newSnapshot = { |
| 145 | ...snapshot, |
| 146 | context: event.data as TContext |
| 147 | }; |
| 148 | return newSnapshot; |
| 149 | } |
| 150 | case XSTATE_OBSERVABLE_ERROR: |
| 151 | return { |
| 152 | ...snapshot, |
| 153 | status: 'error', |
| 154 | error: (event as any).data, |
| 155 | input: undefined, |
| 156 | _subscription: undefined |
| 157 | }; |
| 158 | case XSTATE_OBSERVABLE_COMPLETE: |
| 159 | return { |
| 160 | ...snapshot, |
| 161 | status: 'done', |
| 162 | input: undefined, |
| 163 | _subscription: undefined |
| 164 | }; |
| 165 | case XSTATE_STOP: |
| 166 | snapshot._subscription!.unsubscribe(); |
| 167 | return { |
| 168 | ...snapshot, |
| 169 | status: 'stopped', |
| 170 | input: undefined, |
| 171 | _subscription: undefined |
| 172 | }; |
| 173 | default: |
| 174 | return snapshot; |
| 175 | } |
no test coverage detected