microstates icon indicating copy to clipboard operation
microstates copied to clipboard

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

Open brandynbennett opened this issue 6 years ago • 5 comments

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 avatar Dec 03 '18 19:12 brandynbennett

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 avatar Dec 03 '18 20:12 taras

Awesome @taras Thanks for the pointer.

brandynbennett avatar Dec 03 '18 21:12 brandynbennett

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

taras avatar Dec 04 '18 08:12 taras

Has there been any progress on this issue?

joshgillies avatar Mar 13 '19 02:03 joshgillies

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

cowboyd avatar Mar 15 '19 21:03 cowboyd