Mobx-state-tree: Question: Is there any model creation completed hook

Created on 4 Feb 2019  路  8Comments  路  Source: mobxjs/mobx-state-tree

Here's some code to illustrate my case:

const BaseModel = types
  .model({
    code: types.number,
    description: types.string,
  })
  .actions(self => {
    let anyInternalValue = 0;

    const internalComputing = () => {
      ++anyInternalValue;
    }

    return {
      afterCreate: () => {
        console.log('afterCreate called...');
        internalComputing(); // ERROR Failed to resolve...
      },
      afterAttach: () => {
        console.log('afterAttach called...');
        internalComputing();
      }
    }
  })
  ;

const RootModel = types
  .model({
    baseInstance: types.maybe(BaseModel)
  })
  .actions(self => {
    return {
      createBaseInstance: (code, description) => {
        self.baseInstance = BaseModel.create({ code, description });
      }
    }
  })
  ;

aModel = RootModel.create();

Now, if I run the following:

aModel.createBaseInstance(100, 'baseModel instance');

console looks like:
_afterCreate called...
afterAttach called..._
_>> anyInternalValue === 2 (expected 1)_

Then, if I create an instance of BaseModel, but standalone (no parent):

let bModel = BaseModel.create( { code: 100, description: 'baseModel instance' })

console looks like:
afterCreate called...
_>> anyInternalValue === 1 (expectation ok)_

Since I don't know if the user of my model will ever use BaseModel standalone (rely on afterCreate) or within a parent (rely on afterAttach), is there any hook I'm not aware of that I could use for that purpose like "afterCreationCompletedSortOf" ?

thanx !

question stale

All 8 comments

Internally there's a afterCreationFinalization hook (which I named finalize on the proposal here https://github.com/mobxjs/mobx-state-tree/issues/1151), but it's not currently exposed right now

Internally there's a afterCreationFinalization hook (which I named finalize on the proposal here #1151), but it's not currently exposed right now

Is there any way I could detect if my current model instance (self) is being attached to a parent, I tried using getParent, hasParentOfType, getRoot, hasParent, but within afterCreate hook, it seems self has no clue about it's current parent. I'm just looking for a workaround until we get the finalize hook...

thanx!

Not until that hook is added I'm afraid, since afterCreate runs when the node has been created but not yet attached to a parent, and afterAttach only runs if it actually has a parent.

Just wondering, what are you trying to achieve?

Not until that hook is added I'm afraid, since afterCreate runs when the node has been created but not yet attached to a parent, and afterAttach only runs if it actually has a parent.

Just wondering, what are you trying to achieve?

Actually, I need to compute some code on creation of a model instance to which a reference is attached. I use the value of that reference inside the "initialization code", but since references are not resolved until attachment, I cannot rely on afterCreate. I have to wait within afterAttach instead. But at runtime, I cannot know if my model instance will be standalone or attached to a parent and I cannot put the initialization code into both afterCreate or afterAttach. I'm facing the chicken or the egg dilema. The initialization code is made from a "base model" that I compose with the user model.

But I finally work around this by providing the "base model" with a specific property _standaloneInstance that I set to false by defaut. So if my user wants to use his model standalone, he must set the _standaloneInstance = true. So from inside my "base model" I know from where to call the initialization code (_standaloneInstance = true ? call from afterCreate, _standaloneInstance = false ? call from afterAttach)

Once I get your new finalize hook, I will simply get rid of the _standaloneInstance property from my baseModel and I will call the initialization code from within finalize hook. With version of MST 3.3.0, references were able to get resolved before attachment gets completed, so I was able to access a reference from within afterCreate but this is not supported anymore and I perfectly get it why.

@xaviergonz doesn't a finalizeHook have exactly the same limitation? Unless .create is never called internally anymore, and .create is called only from userland; in that case that hook could be run on a full depth traversal of the created tree

a finalize hook would be kind of like an attach hook, except that it would run also when there's no parent, so it would run once after the node is created and the whole tree is ready. combined with a "eager" creation mode (from the proposal) it would mean that it would always trigger no matter if the node is accessed or not (unless I'm missing something which I might :) )

This issue has been automatically marked as stale because it has not had recent activity in the last 10 days. It will be closed in 4 days if no further activity occurs. Thank you for your contributions.

This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs or questions.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

xgenvn picture xgenvn  路  3Comments

misantronic picture misantronic  路  3Comments

donatoaz picture donatoaz  路  3Comments

tahv0 picture tahv0  路  3Comments

mshibl picture mshibl  路  3Comments