Giter VIP home page Giter VIP logo

Comments (3)

davidkpiano avatar davidkpiano commented on June 8, 2024 1

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.

davidkpiano avatar davidkpiano commented on June 8, 2024 1

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.

ivankoleda avatar ivankoleda commented on June 8, 2024

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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.