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

Make state an internal property of Store #354

Merged
merged 6 commits into from Oct 12, 2018
Merged

Conversation

mokagio
Copy link
Member

@mokagio mokagio commented Jun 5, 2018

This PR removes the State property from the StoreType protocol, and makes it private(set) internal in the Store implementation.

The aim of this change is to promoted a better usage of ReSwift, preventing consumers from falling into the anti-pattern of reading the State directly from the Store, rather than conforming to StoreSubscriber. See the discussion on #264.

Having the State value accessible in the Store can lead to misuses of
ReSwift, where state is accessed by reading it from the store rather
than subscribing to it.
This way only internals of ReSwift can access it, e.g. for testing
purposes, while consumers of the framework can't.

This should promoted a better usage of ReSwift, where the only way to
read the State and its changes is to subscribe to the Store.
@codecov-io
Copy link

codecov-io commented Jun 5, 2018

Codecov Report

Merging #354 into master will not change coverage.
The diff coverage is n/a.

Impacted file tree graph

@@           Coverage Diff           @@
##           master     #354   +/-   ##
=======================================
  Coverage   99.43%   99.43%           
=======================================
  Files           5        5           
  Lines         176      176           
=======================================
  Hits          175      175           
  Misses          1        1
Impacted Files Coverage Δ
ReSwift/CoreTypes/Store.swift 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 4f99933...83df438. Read the comment docs.

Copy link
Member

@mjarvis mjarvis left a comment

Choose a reason for hiding this comment

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

Along with this PR, ReSwift-Recorder should have a PR posted before this is merged, updating the documentation to indicate that it is currently deprecated and non-functional without changes.

@@ -18,9 +18,6 @@ public protocol StoreType: DispatchingStoreType {

associatedtype State: StateType

/// The current state stored in the store.
var state: State! { get }
Copy link
Member

Choose a reason for hiding this comment

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

I don't think this change should be included in this PR. There are valid reasons for making state unreadable, but there are also valid reasons for reading state outside of a subscriber (IE one-time reads). Whether or not it stays I think this should be discussed & dealt with separately from the change to removing write access.

Choose a reason for hiding this comment

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

I agree with that. Making the state totally private from the store could be a source of a lot of problem in some architectures.

Copy link
Contributor

Choose a reason for hiding this comment

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

The original Redux implementation in JavaScript has a getState() method to query the state of the store at any time, so that should be enough, IMO!

Copy link
Contributor

Choose a reason for hiding this comment

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

Instead of Java-style getters like getState(), making the property puplicly readable via public private(set) could work.

Choose a reason for hiding this comment

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

Would be perfect. Setter can and should be private anyway.

Copy link
Contributor

Choose a reason for hiding this comment

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

Yeah, they probably do it because of JavaScript, but in Swift we could just use the read-only variable. It's the same as in the middleware, you get both the dispatch function and getState.

@mokagio
Copy link
Member Author

mokagio commented Sep 26, 2018

Hey folks. Thanks for all the great comments. Unfortunately I haven't had and still don't have the bandwidth to follow this up 😞 .

Feel free to pick up the branch if you want, or close this PR and start a new one.

@mjarvis mjarvis merged commit b24ee18 into master Oct 12, 2018
@mjarvis mjarvis deleted the private-store-state branch October 12, 2018 23:36
mjarvis pushed a commit that referenced this pull request Mar 15, 2019
This reverts commit b24ee18, reversing
changes made to 4f99933.
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.

None yet

6 participants