Skip to content

Commit

Permalink
Remove unnecessary reduxify() annotation and replace with connect()
Browse files Browse the repository at this point in the history
  • Loading branch information
mythz committed Apr 6, 2016
1 parent 9f022dc commit 0760efc
Show file tree
Hide file tree
Showing 35 changed files with 83 additions and 166 deletions.
31 changes: 10 additions & 21 deletions README.md
Expand Up @@ -1466,23 +1466,13 @@ and add the option manually:
```
When enabled this lets you create and use decorators that despite being simple functions provide an easy way
to compose behavior, dramatically reduce repetitive boilerplate and improve readability as seen with the
[@reduxify()](https://github.com/ServiceStackApps/typescript-redux/blob/661f9fcc1ce6c4a7b66064ce3511033d37a26d99/src/TypeScriptRedux/src/example08/core.ts#L18)
decorator which just delegates to Redux connect():
to compose behavior, dramatically reduce repetitive boilerplate and improve readability where we're able to
get the ideal declarative API we want for defining Redux-connected Components in a single cohesive unit,
with property mapping functions declared above the class declaration and all interim artifacts abstracted away
by using Redux's `connect()` as a decorator instead:
```typescript
export function reduxify(mapStateToProps?: MapStateToProps,
mapDispatchToProps?: MapDispatchToPropsFunction | MapDispatchToPropsObject) {
return target => connect(mapStateToProps, mapDispatchToProps)(target);
}
```
With just this simple change we get the ideal declarative API we want for defining Redux-connected Components
which are now defined in a single unit, with property mapping functions declared above the class declaration
and all interim artifacts abstracted away:
```typescript
@reduxify(
@connect(
(state) => ({ color: state.color }),
(dispatch) => ({ setColor: (color) => dispatch({ type: 'COLOR_CHANGE', color }) })
)
Expand Down Expand Up @@ -1722,8 +1712,6 @@ Surprisingly most of the code to make this happen is encapsulated within the Rea
/// <reference path='../../typings/browser.d.ts'/>

import * as React from 'react';
import { connect } from 'react-redux';
import { reduxify } from './core';
import 'jquery';
import 'ss-utils';

Expand All @@ -1737,12 +1725,12 @@ export default class Connect extends React.Component<any, any> {
channels: ["home"], currentUser: null, users: [],
connectedToUserId: null, connectedUserActions: [], connectedStateIndex: -1
};
var source = new EventSource(serverEventsUrl());
var source = new EventSource("/event-stream?channels=home");
$(source).handleServerEvents({
handlers: {
onConnect: (currentUser) => {
currentUser.usersChannel = userChannel(currentUser.userId);
this.setState({currentUser, users: filterUsers(this.state.users,currentUser.userId)});
this.setState({ currentUser, users: filterUsers(this.state.users, currentUser.userId) });
this.props.onConnect(currentUser);
},
onJoin: () => this.refreshUsers(),
Expand All @@ -1757,8 +1745,9 @@ export default class Connect extends React.Component<any, any> {
$.ss.postJSON(`/send-user/${o.replyTo}?selector=cmd.onState`, state);
},
onState: (json, e) => {
this.props.store.dispatch({
type:'LOAD', state:json ? JSON.parse(json) : this.props.defaultState });
this.props.store.dispatch({
type: 'LOAD', state: json ? JSON.parse(json) : this.props.defaultState
});
},
publishAction: (json, e) => {
var action = JSON.parse(json);
Expand Down
14 changes: 7 additions & 7 deletions src/TypeScriptRedux/app.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion src/TypeScriptRedux/app.js.map

Large diffs are not rendered by default.

10 changes: 5 additions & 5 deletions src/TypeScriptRedux/src/example08/Counter.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion src/TypeScriptRedux/src/example08/Counter.js.map

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 1 addition & 2 deletions src/TypeScriptRedux/src/example08/Counter.tsx
Expand Up @@ -2,7 +2,6 @@

import * as React from 'react';
import { connect } from 'react-redux';
import { reduxify } from './core';

interface IHelloWorldProps {
field: string;
Expand All @@ -12,7 +11,7 @@ interface IHelloWorldProps {
decr?: Function;
}

@reduxify(
@connect(
(state, props) => ({ counter: (state[props.field] || 0) }),
(dispatch) => ({
incr: (field, step) => {
Expand Down
12 changes: 6 additions & 6 deletions src/TypeScriptRedux/src/example08/ShapeMaker.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion src/TypeScriptRedux/src/example08/ShapeMaker.js.map

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 1 addition & 2 deletions src/TypeScriptRedux/src/example08/ShapeMaker.tsx
Expand Up @@ -3,9 +3,8 @@
import * as React from 'react';
import { connect } from 'react-redux';
import { isDark } from './ColorPicker';
import { reduxify } from './core';

@reduxify(
@connect(
(state) => ({
width: state.width, height: state.height, color: state.color,
top: state.nextShapeId * 10, left: state.nextShapeId * 10
Expand Down
12 changes: 6 additions & 6 deletions src/TypeScriptRedux/src/example08/ShapeViewer.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 0760efc

Please sign in to comment.