Skip to content

Add recipe for using Microstates with Ember Concurrency #76

@taras

Description

@taras

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');
  })
});

Metadata

Metadata

Labels

docsMissing documentationgood first issueIssues that are a good place for beginners to start

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions