MCPcopy
hub / github.com/statelyai/xstate / fromObservable

Function fromObservable

packages/core/src/actors/observable.ts:118–223  ·  view source on GitHub ↗
(
  observableCreator: ({
    input,
    system,
    self
  }: {
    input: TInput;
    system: AnyActorSystem;
    self: ObservableActorRef<TContext>;
    emit: (emitted: TEmitted) => void;
  }) => Subscribable<TContext>
)

Source from the content-addressed store, hash-verified

116 * @see {@link Subscribable} interface in XState, which is based on and compatible with RxJS Observable.
117 */
118export 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 }

Callers 10

input.test.tsFile · 0.90
actor.test.tsFile · 0.90
actorLogic.test.tsFile · 0.90
system.test.tsFile · 0.90
emit.test.tsFile · 0.90
spawnChild.test.tsFile · 0.90
invoke.test.tsFile · 0.90
useActor.test.tsxFile · 0.90

Calls 2

unsubscribeMethod · 0.65
subscribeMethod · 0.65

Tested by

no test coverage detected