vigetlabs / microcosm

Flux with actions at center stage. Write optimistic updates, cancel requests, and track changes with ease.
http://code.viget.com/microcosm/
MIT License
487 stars 29 forks source link

Micromanage inception #488

Closed nhunzaker closed 6 years ago

nhunzaker commented 6 years ago

This PR starts micromanage, what I consider to be the missing piece of Microcosm: a data management and querying layer. Domains work reasonably well enough as data-bags, however I'm not happy with how low level their data management operations are. Consider:

import { Domain } from 'microcosm'

function getWidget (id) {
  return fetch(`/widgets/${id}`).then(res => res.json)
}

class Widgets extends Domain {
  getInitialState() {
    return []
  }  
  addWidget(widgets, widget) {
    return widgets.concat(widget)
  }
  register() {
    return {
      [getWidget]: this.addWidget
    }
  }  
}

class WidgetShow extends Presenter {
  getModel(repo, { id }) {
    return {
      widget: repo.domains.widgets.map(widgets => widgets.find(w => w.id === id))
    }
  }

  ready(repo, props) {
    repo.push(getWidget, props.id)
  }

  update(repo, props) {
    if (props.id !== this.props.id) {
      repo.push(getWidget, props.id)
    }
  }
}

* Important: This is the microcosm 13 API.

This is too low level:

We can do better. These are not valuable uses of project time. What if this were something like:

import { Entity, Collection } from 'micromanage'

class Widget extends Entity {
  static schema = {
    id: String,
    name: String,
    weight: Number
  }
}

class Widgets extends Collection(Widget) {
  // Intentionally left blank, but you could include your own stuff
}

class WidgetShow extends Presenter {
  getModel(repo, { id }) {
    return {
      widget: repo.domains.widgets.find(1)
    }
  }
}

Querying for an entity is never null. repo.domains.widgets.find above could return:

So here goes! This PR includes an initial outline of what I'd like. It's pretty high level, but I thought I'd send it out for transparency.

codecov-io commented 6 years ago

Codecov Report

Merging #488 into master will increase coverage by 0.14%. The diff coverage is n/a.

Impacted file tree graph

@@           Coverage Diff            @@
##           master   #488      +/-   ##
========================================
+ Coverage   97.86%    98%   +0.14%     
========================================
  Files          29     29              
  Lines         702    702              
  Branches      137    137              
========================================
+ Hits          687    688       +1     
+ Misses         13     12       -1     
  Partials        2      2
Impacted Files Coverage Δ
packages/microcosm-dom/src/utilities.js 100% <0%> (+7.14%) :arrow_up:

Continue to review full report at Codecov.

Legend - Click here to learn more Δ = absolute <relative> (impact), ø = not affected, ? = missing data Powered by Codecov. Last update 537c4a4...1f1a60c. Read the comment docs.

efatsi commented 6 years ago

Exciting!