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

Refactor code for better typing, naming, and RouteMapping support. #36

Merged
merged 28 commits into from
Dec 10, 2015

Conversation

inamiy
Copy link
Member

@inamiy inamiy commented Nov 14, 2015

This is a BREAKING CHANGE and release candidate for next version 4.0.0,
greatly improved typing and RouteMapping support (handling states/events with associated values).

See Also: #34

Simple Class Names

I renamed many verbose class names e.g. StateTransition to simple Transition.
(Even if the name collides with other frameworks, we can just simply add framework-prefix e.g. SwiftState.Transition to avoid it.)

Better Typing

As requested in #34 by @frogcjn, current version 3.x needs to use StateType/EventType which conforms to NilLiteralConvertible to represent .Any state/event, but it was quite ugly because we needed to add extra case Any in their enums.

To alleviate this, I added extra enum wrappers (State<S> & Event<E>) so that users no longer need to implement .Any anymore.

RouteMapping

RouteMapping and StateRouteMapping are new ways of adding routes which has a closure type

public typealias RouteMapping = (event: E?, fromState: S, userInfo: Any?) -> S?
public typealias StateRouteMapping = (fromState: S, userInfo: Any?) -> [S]?

to navigate StateMachine to preferred toState without using traditional arrow style, e.g. fromState => toState.
These closures help resolving states & events with associated values.

Please see FrogcjnTest.swift#L124-L180 RouteMappingTests.swift#L77-L133 for more details.

Simple Machine class (works like Redux)

StateMachine (which can tryState() as well as tryEvent()) is now a subclass of simpler class Machine which is capable of tryEvent() only.
By splitting these codes, along with RouteMapping, Machine will now behave like rackt/redux in JavaScript, a safe state container.
For more information on Redux, see http://redux.js.org/ .

Easy Disposable of routes & handlers

There were previously RouteID and HandlerID being passed as return value for asking StateMachine to unregister, but now Disposable (derived from ReactiveCocoa) will take care of it.

Others

  • Support watchOS & tvOS.

@inamiy inamiy added this to the 4.0.0 milestone Nov 14, 2015
@inamiy
Copy link
Member Author

inamiy commented Nov 14, 2015

Please ping me for any thoughts on this 😉

@frogcjn
Copy link

frogcjn commented Nov 15, 2015

Cool! FrogcjnTest.swift#L124-L180 this is just what I needs. Thanks!

@inamiy
Copy link
Member Author

inamiy commented Nov 15, 2015

@frogcjn Thanks! I need to improve tests too, so it may take a bit more time to finish this up. So please let me get this right ;)

…hine`.

- Add `Machine` (event-only-driven state machine).
- Add `Disposable` derived from ReactiveCocoa.
- Add Package.swift & change directory structure for Swift-Package-Manager.
@inamiy
Copy link
Member Author

inamiy commented Dec 5, 2015

I applied a 2nd big refactor in f7ce748 to add following features:

  • Add Machine (event-only-driven state machine).
  • Add Disposable derived from ReactiveCocoa. (no more IDs for removal of routes & handlers)
  • Add Package.swift & change directory structure for apple/swift-package-manager.

There are now 2 machine classes, Machine and its subclass StateMachine.
StateMachine behaves just as same as old version 3.x and below.
On the other hand, Machine is more simpler version, and works very similar to rackt/redux in JavaScript, a safe state container.

@inamiy
Copy link
Member Author

inamiy commented Dec 5, 2015

See also (strict directory structure): swiftlang/swift-package-manager#16

@inamiy
Copy link
Member Author

inamiy commented Dec 5, 2015

Ref: swift-package-manager + SwiftState 4.0.0-RC (e9c1bf3) build failure in Ubuntu 14.04
https://gist.github.com/inamiy/b75cba8916716c81dd7d

@mxcl
Copy link

mxcl commented Dec 7, 2015

Hey, we don't want to force anyone to have a directory structure they don't want. We will add configurability to the Package.swift so you can have the layout you prefer. We didn't have time to do that for the initial release.

@inamiy
Copy link
Member Author

inamiy commented Dec 8, 2015

@mxcl
Changing directory structure in this pull request is no problem because there are many refactoring including file name changes. As long as Sources and Tests keep their dirnames canonical, I don't even have to add extra exclusion lines in Package.swift, and I just love it 😉

@inamiy inamiy changed the title [WIP] Refactor code for better typing, naming, and routeMapping support. Refactor code for better typing, naming, and RouteMapping support. Dec 10, 2015
@inamiy
Copy link
Member Author

inamiy commented Dec 10, 2015

Ready to :shipit:

inamiy added a commit that referenced this pull request Dec 10, 2015
Refactor code for better typing, naming, and RouteMapping support.
@inamiy inamiy merged commit 67b9d35 into swift/2.0 Dec 10, 2015
@susieyy
Copy link

susieyy commented Dec 11, 2015

🎉🎉🎉

@inamiy inamiy deleted the refactor branch December 15, 2015 03:18
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.

4 participants