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 recipe for using Microstates with Ember Concurrency #76

Open
taras opened this issue Nov 27, 2018 · 0 comments
Open

Add recipe for using Microstates with Ember Concurrency #76

taras opened this issue Nov 27, 2018 · 0 comments
Assignees
Labels
docs Missing documentation good first issue Issues that are a good place for beginners to start

Comments

@taras
Copy link
Member

taras commented Nov 27, 2018

There is nothing really tricky about using Microstates with Ember Concurrency, but we do want to make this clearer with a good example. For all intents and purposes, Microstates behave like Computed Properties. You can write values to them and the result will be computed synchronously when the value is read. Microstates also debounce no-op write operations.

To use Microstates with EC, you would probably want to define the state on the component rather than in the template. It's possible to send a microstate as an argument to the task, so that could be an option for microstates defined in the template.

import Component from '@ember/microstates';
import { state } from '@microstates/ember';
import { task } from 'ember-concurrency';
import fetch from 'ember-network';

class Person {
  name = String;
  age = String;

  get summary() {
   return `${this.name.state} is ${this.age.state}`;
  }
}

export default Component.extend({
  person: state(Person),

  fetchPerson: task(function *() {

    let response = yield fetch('person');
    let person = yield response.json();

    this.get('person').set(person);

    return this.get('person.summary');
  })
});
@taras taras added good first issue Issues that are a good place for beginners to start docs Missing documentation labels Nov 27, 2018
@sivakumar-kailasam sivakumar-kailasam self-assigned this Nov 27, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
docs Missing documentation good first issue Issues that are a good place for beginners to start
Projects
None yet
Development

No branches or pull requests

2 participants