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

[microcosm] Split Subject.hash into separate class #514

Merged
merged 1 commit into from
May 1, 2018

Conversation

nhunzaker
Copy link
Contributor

But it was getting pretty massive, and I didn't like having it as a static method on Subject. This commit moves this function into a separate class that extends from Subject.

What is Subject.hash

Subject.hash is a method used in a few places to reduce an object of nested asynchronous values into a POJO. This is extremely useful for building view models and for generally reducing down any complex async type in Microcosm down into something usable to the outside world:

import { http } from 'microcosm-http'

let answer = Subject.hash({
  users: http.get('/users'),
  postsWithComments: Subject.hash({
    posts: http.get('/posts'),
    comments: http.get('/comments')
  }).map(mergePostsWithComments)
})

// This will emit a stream of updates as it gets answers
answer.subscribe({
  next() {}, // Partially loaded. You might have users, but not posts
  complete() { }, // All users, posts, and comments are loaded
  error () {} // Something went wrong with this entire behavior
})

// Or say you stop caring:
// with microcosm-http, this would cancel all requests
answer.cancel()

This request behavior is additionally illustrated in the file-uploads example.

Why call it SubjectHash?

I don't know. I think this is the closest thing we have to the concept of a ViewModel, but it covers a broad use case of behavior. SubjectTree also came to mind, or something that doesn't use the word Subject, which is pretty vague. I have an issue to track that in #511

@nhunzaker nhunzaker force-pushed the async-subject-updates branch from 1fc7829 to b727c7c Compare April 26, 2018 14:02
@nhunzaker nhunzaker force-pushed the async-subject-hash-updates branch from 9939945 to e2ca281 Compare April 26, 2018 14:02
@nhunzaker nhunzaker changed the base branch from async-subject-updates to async April 26, 2018 20:14
@nhunzaker nhunzaker force-pushed the async-subject-hash-updates branch from e2ca281 to 286c399 Compare April 26, 2018 20:58
@nhunzaker nhunzaker changed the base branch from async to master April 26, 2018 20:58
Copy link

@zporter zporter left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

code looks great and I love that this has been extracted into its own thing, but I also think that SubjectHash is maybe a bit too generic of a name. I wasn't sure if Hash was referring to a scrambled value (like encryption) or if it was similar to a Hash in another language like Ruby. Since it's converting to an object, how about SubjectObj or an ObjectMapper.from_subject function? Just some more thoughts that may or may not help you out.

@nhunzaker nhunzaker force-pushed the async-subject-hash-updates branch 2 times, most recently from f0c6d41 to fd21b2c Compare May 1, 2018 11:33
The Subject.hash method was getting pretty massive, and I didn't like
having it as a static method on Subject. This commit moves this
function into a separate class that extends from Subject.
@nhunzaker nhunzaker force-pushed the async-subject-hash-updates branch from fd21b2c to a8acb59 Compare May 1, 2018 11:42
@codecov-io
Copy link

codecov-io commented May 1, 2018

Codecov Report

Merging #514 into master will decrease coverage by 0.01%.
The diff coverage is 100%.

Impacted file tree graph

@@            Coverage Diff             @@
##           master     #514      +/-   ##
==========================================
- Coverage   98.79%   98.78%   -0.02%     
==========================================
  Files          29       30       +1     
  Lines         745      738       -7     
  Branches      149      147       -2     
==========================================
- Hits          736      729       -7     
  Misses          9        9
Impacted Files Coverage Δ
packages/microcosm/src/observable.js 96.07% <ø> (-0.99%) ⬇️
packages/microcosm/src/coroutine.js 100% <100%> (ø) ⬆️
packages/microcosm-dom/src/action-form.js 100% <100%> (ø) ⬆️
packages/microcosm-dom/src/presenter.js 100% <100%> (ø) ⬆️
packages/microcosm-dom/src/action-button.js 100% <100%> (ø) ⬆️
packages/microcosm/src/subject.js 100% <100%> (+0.99%) ⬆️
packages/microcosm/src/microcosm.js 100% <100%> (ø) ⬆️
packages/microcosm/src/subject-map.js 100% <100%> (ø)

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 0372345...a8acb59. Read the comment docs.

@nhunzaker
Copy link
Contributor Author

@zporter Renamed this to SubjectMap. I'm going to keep jamming on a name, but I'm hopeful that I'll hit some clarity once I've better distinguished the responsibility of this component in Presenters vs Microcosm itself.

Thanks for the review!

@nhunzaker nhunzaker merged commit 01d1f19 into master May 1, 2018
@nhunzaker nhunzaker deleted the async-subject-hash-updates branch May 1, 2018 11:50
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants