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

Introduce lifecycle hooks for models #76

Closed
mweststrate opened this issue Apr 7, 2017 · 5 comments
Closed

Introduce lifecycle hooks for models #76

mweststrate opened this issue Apr 7, 2017 · 5 comments
Labels
brainstorming/wild idea Brainstorming / Wild idea

Comments

@mweststrate
Copy link
Member

suggestions:

  • beforeDispose
  • afterCreate
  • beforeApplySnapshot
  • afterApplySnapshot
  • afterCreateSnapshot
  • beforeChange (like intercept)

..But only if there are good use cased :-D

@mweststrate mweststrate added the brainstorming/wild idea Brainstorming / Wild idea label Apr 7, 2017
@mattiamanzati
Copy link
Contributor

+1

afterCreateInstance to setup reactions! :D

@mweststrate
Copy link
Member Author

Proposal:

  • preprocessSnapshot(snapshot) => snapshot (called before applying a snapshot, both during create and later on)
  • postprocessSnapshot(snapshot) => snapshot (called when creating a snapshot)
  • didCreate() called after instantation, but not during reconcilation
  • didAttach() called when node gets attached to a tree (not called if attached before creation)
  • willDetach() called before detaching from a tree
  • willDestroy() called before a node is killed

@RainerAtSpirit
Copy link
Contributor

Naming convention before/after resonates better with me.

@mattiamanzati
Copy link
Contributor

@mweststrate Maybe add a preprocess and postprocess for patches too?

@mweststrate
Copy link
Member Author

Merged by #98 and released as 0.3.3.

Following lifecycle hooks are now available:

  • afterCreate
  • beforeDestroy
  • afterAttach
  • beforeDetach

More lifecycle hooks should be demand / use case driven. E.g. processing snapshots.

Also introduced the convenience method addDisposer which is useful in afterCreate, avoid the need to setup a beforeDestroy just to fire disposers.

E.g.:

const Todo = types.model({
   afterCreate() {
       addDisposer(this, reaction(() => getSnapshot(this), s => submitSnapshotToServer(s)))
   }
})

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
brainstorming/wild idea Brainstorming / Wild idea
Projects
None yet
Development

No branches or pull requests

3 participants