-
-
Notifications
You must be signed in to change notification settings - Fork 15.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Don't use getAtom(), pass state on each dispatch instead
- Loading branch information
Showing
8 changed files
with
142 additions
and
37 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
import React, { PropTypes } from 'react'; | ||
import DOMify from 'react-domify'; | ||
|
||
export default class Transactions { | ||
static propTypes = { | ||
status: PropTypes.object.isRequired, | ||
commit: PropTypes.func.isRequired, | ||
begin: PropTypes.func.isRequired, | ||
rollback: PropTypes.func.isRequired | ||
}; | ||
|
||
render() { | ||
const { status, commit, begin, rollback } = this.props; | ||
return ( | ||
<p> | ||
<DOMify value={status} /> | ||
{' '} | ||
<button onClick={begin}>Begin</button> | ||
{' '} | ||
<button onClick={commit}>Commit</button> | ||
{' '} | ||
<button onClick={rollback}>Rollback</button> | ||
</p> | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
import React, { PropTypes } from 'react'; | ||
import { Connector } from 'redux'; | ||
import Transactions from '../components/Transactions'; | ||
|
||
const transactorShape = PropTypes.shape({ | ||
getStatus: PropTypes.func.isRequired, | ||
begin: PropTypes.func.isRequired, | ||
commit: PropTypes.func.isRequired, | ||
rollback: PropTypes.func.isRequired | ||
}); | ||
|
||
export default class TransactionsApp { | ||
static propTypes = { | ||
transactor: transactorShape.isRequired | ||
}; | ||
|
||
render() { | ||
return ( | ||
// TODO: Need a way to subscribe to all dispatches, without memoization | ||
<Connector select={() => ({ lol: {} })}> | ||
{::this.renderChild} | ||
</Connector> | ||
); | ||
} | ||
|
||
renderChild() { | ||
const { transactor: { getStatus, begin, commit, rollback } } = this.props; | ||
|
||
return ( | ||
<Transactions status={getStatus()} | ||
commit={commit} | ||
begin={begin} | ||
rollback={rollback} /> | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,28 +1,45 @@ | ||
function noop() {} | ||
|
||
class Transactor { | ||
inProgress = false; | ||
actions = []; | ||
|
||
middleware(getAtom, store) { | ||
middleware(store) { | ||
this.store = store; | ||
this.getAtom = getAtom; | ||
this.headState = getAtom(); | ||
|
||
return next => { | ||
this.next = next; | ||
|
||
return action => { | ||
if (this.inProgress) { | ||
this.actions.push(action); | ||
return next(this.reduceActions(this.headState, this.actions)); | ||
} else { | ||
const state = this.store(this.getAtom(), action); | ||
this.headState = state; | ||
return next(state); | ||
} | ||
|
||
return (state, dispatch) => { | ||
this.dispatch = dispatch; | ||
this.currentState = state; | ||
|
||
if (this.isCapturingState) { | ||
this.isCapturingState = false; | ||
return () => () => this.next(state); | ||
} | ||
|
||
return next => { | ||
this.next = next; | ||
|
||
return action => { | ||
if (this.inProgress) { | ||
this.actions.push(action); | ||
const nextState = this.reduceActions(this.headState, this.actions); | ||
return this.next(nextState); | ||
} else { | ||
const nextState = this.store(this.currentState, action); | ||
return this.next(nextState); | ||
} | ||
}; | ||
}; | ||
}; | ||
} | ||
|
||
// Get the current state atom by running a dummy dispatch. | ||
getCurrentState() { | ||
this.isCapturingState = true; | ||
this.dispatch(); | ||
return this.currentState; | ||
} | ||
|
||
reduceActions(initialState, actions) { | ||
return actions.reduce( | ||
(state, action) => this.store(state, action), | ||
|
@@ -31,24 +48,30 @@ class Transactor { | |
} | ||
|
||
begin() { | ||
this.inProgress = true; | ||
this.dirty = false; | ||
this.actions = []; | ||
if (!this.inProgress) { | ||
this.inProgress = true; | ||
this.actions = []; | ||
this.headState = this.getCurrentState(); | ||
} | ||
} | ||
|
||
commit() { | ||
this.inProgress = false; | ||
this.dirty = false; | ||
this.actions = []; | ||
this.headState = this.getAtom(); | ||
if (this.inProgress) { | ||
this.inProgress = false; | ||
this.actions = []; | ||
delete this.headState; | ||
This comment has been minimized.
Sorry, something went wrong.
This comment has been minimized.
Sorry, something went wrong.
acdlite
Author
Collaborator
|
||
this.next(this.getCurrentState()); | ||
} | ||
} | ||
|
||
rollback() { | ||
const { headState } = this; | ||
this.inProgress = false; | ||
this.dirty = false; | ||
this.actions = []; | ||
return this.next(headState); | ||
if (this.inProgress) { | ||
this.inProgress = false; | ||
this.actions = []; | ||
this.currentState = this.headState; | ||
delete this.headState; | ||
return this.next(this.currentState); | ||
} | ||
} | ||
|
||
getStatus() { | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
export default function createReducer(store) { | ||
return state => next => action => | ||
next(store(state, action)); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Don't do that. It will mutate the hidden class.