Comments (3)
This is expected behavior because the actor needs to be in some initial state when it is started. In the observable actor's case, the initial state is that nothing was produced yet from the actor, which is represented by snapshot.context === undefined
.
Also, if you were to read the snapshot of the actor before it produced anything, then you would expect undefined
.
If you weren't using XState, this would be analogous to this:
let currentValue = undefined; // we don't know what the first value is!
interval$.subscribe(val => {
currentValue = val;
});
currentValue; // undefined
from xstate.
The problem is that observables are conceptually async; even if the first value comes back immediately, you must treat it as async. If observables had a notion of a synchronous initial value, we could use that (e.g. some sort of obs$.getInitialValue()
, but this is not a concept in observables.
A potential idea may be to add something like this to XState:
// PSEUDOCODE
const logic = fromObservable(() => interval$, { startWith: 0 });
I'll continue to think about this.
from xstate.
Thanks for the explanation.
So if I understand correctly, there is no way to have a different initial value given an approach with setup
function.
setup({
actors: {
intervals: fromObservable(() => interval(1000)),
},
}).createMachine({
invoke: {
src: 'intervals',
onSnapshot: [], // undefined will always be initial value and there has to be a runtime check
},
});
Can see the confirmation to this in type definitions:
export type ObservableSnapshot<TContext, TInput extends NonReducibleUnknown> = Snapshot<undefined> & {
context: TContext | undefined;
input: TInput | undefined;
_subscription: Subscription | undefined;
};
I'm probably stating the obvious. Just thought I missed something in docs and there was a way to not have a runtime check for undefined
in onSnapshot
handlers.
from xstate.
Related Issues (20)
- Bug: typegen.ts ist incomplete when moving state node configs to separate files HOT 3
- Bug: Promise actor error handling doesn't work as documented HOT 3
- Bug: types broken with assign HOT 2
- Bug: supplying generic input argument to setup actors for stubbing breaks invoke type inference
- Bug: When child state transitioned to final state, did not trigger onDone event and transition to next state
- Bug: [@xstate/store] typings broken HOT 2
- Bug: Bug with After event on restoring snapshot
- Bug: [Typescript] event target doesn't get restricted to declared state HOT 3
- Bug: HOT 3
- Methods like `provide` are lost when implementing Higher Level Actor Logic HOT 1
- Bug: system.get(id) not working after restoring a snapshot HOT 2
- Bug: Importing Xstate in a React-Native android project errors HOT 1
- Bug: History state in parallel machines is changing states unexpectedly HOT 3
- Bug: State entry action's actor value mismatch HOT 9
- Bug: [Typescript] Child actor send method event argument is an intersection of all child actor events
- CreateActor Requires Input with Snapshot
- Bug: @xstate/store does not export any types
- Bug: [Typescript] `enqueueActions` throws a type error when declared within setup actions HOT 2
- `actor.getSnapshot().value` does not contain `compound` state types
- Bug: _xstate_react__WEBPACK_IMPORTED_MODULE_1__.useInterpret is not a function HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from xstate.