Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Calling transition on child object with intialize transition does not return parent microstate #291

Open
brandynbennett opened this issue Dec 3, 2018 · 5 comments

Comments

@brandynbennett
Copy link

brandynbennett commented Dec 3, 2018

Given a child class with an initialize transition

class Pagination {
  page = Number;
  itemsPerPage = Number;

  initialize() {
    let pagination = this;

    if (!pagination.page.state) {
      pagination = this.page.set(1);
    }

    if (!pagination.itemsPerPage.state) {
      pagination = this.itemsPerPage.set(25);
    }

    return pagination;
  }
}

and a parent class that uses it

class Table {
  pagination = Pagination;
}

Calling a transition on the child property returns the child property not the parent property:

table.pagination.page.set(5) // Returns page microstate not table microstate

You can see an example here: https://codesandbox.io/s/y7v1v8op8j

@brandynbennett brandynbennett changed the title Calling transition on child object Calling transition on child object with intialize transition does not return parent microstate Dec 3, 2018
@taras
Copy link
Member

taras commented Dec 3, 2018

Hi @brandynbennett, thank you for catching another bug for us. I was able to reproduce this in runkit https://runkit.com/taras/initialization-test. It's highlighting area of functionality where we don't have sufficient test coverage.

While we sort this out, you can use create to provide default values.

const { create, valueOf } = require('microstates');

class Pagination {
  page = create(Number, 1)
  itemsPerPage = create(Number, 25)
}

class Table {
  pagination = Pagination
}

let table = create(Table);

table.pagination.page.set(5) instanceof Table
//> true

You can see it working here https://runkit.com/taras/initialization-test-create-default-value

@taras taras added the bug label Dec 3, 2018
@triage-new-issues triage-new-issues bot removed the triage label Dec 3, 2018
@brandynbennett
Copy link
Author

Awesome @taras Thanks for the pointer.

@taras
Copy link
Member

taras commented Dec 4, 2018

Here is a branch with a failing case for this use case tm/initializing-nested-node

@joshgillies
Copy link

Has there been any progress on this issue?

@cowboyd
Copy link
Member

cowboyd commented Mar 15, 2019

We're still working on it, but it is definitely a priority. I'd expect a solution in the next two weeks.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants