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

Add Parket example #43

Closed
RusinovAnton opened this issue May 14, 2018 · 2 comments
Closed

Add Parket example #43

RusinovAnton opened this issue May 14, 2018 · 2 comments
Labels
help wanted Extra attention is needed

Comments

@RusinovAnton
Copy link
Contributor

https://github.com/ForsakenHarmony/parket

A library to manage application state, heavily inspired by mobx-state-tree

Basic example

import model from 'parket';
// model returns a "constructor" function
const Person = model('Person', { // name is used internally for serialization
  initial: () => ({
    firstname: null,
    lastname: null,
    nested: null,
  }),
  actions: state => ({
    setFirstName (first) {
      state.firstname = first; // no set state, no returns to merge, it's reactive™
    },
    setLastName (last) {
      state.lastname = last;
    },
    setNested (nested) {
      state.nested = nested;
    },
  }),
  views: state => ({
    fullname: () => `${state.firstname} ${state.lastname}`, // views are computed properties
  }),
});

// merge an object with the initial state
const instance = Person({ firstname: 'Tom' });

// you can subscribe to actions, patches (state updates) and snapshots (full state after actions)
const unsubscribe = instance.onSnapshot(console.log);

// you can unsubscribe by calling the function returned by the listener
// unsubscribe();

instance.setLastName('Clancy');

// views turn into cached getters
console.log(instance.fullname); // 'Tom Clancy'

// nested models also bubble up events to the parent
instance.setNested(Person());

instance.nested.setFirstName('wow');

// you can get a snapshot of the state at any time
// { firstname: 'Tom', lastname: 'Clancy',  nested: { firstname: 'wow', lastname: null, nested: null } }
console.log(instance.getSnapshot());
@GantMan GantMan added the help wanted Extra attention is needed label May 16, 2018
@GantMan
Copy link
Owner

GantMan commented May 16, 2018

PRs welcome!

@juandc
Copy link
Contributor

juandc commented May 27, 2018

Working on!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
help wanted Extra attention is needed
Projects
None yet
Development

No branches or pull requests

3 participants