Skip to content

Navigation Menu

Sign in
Sign up

Add thunk support - #109

Open
eschaefer wants to merge 2 commits into
developit:master from
eschaefer:feature/thunk-support
Open

Add thunk support #109
eschaefer wants to merge 2 commits into
developit:master from
eschaefer:feature/thunk-support

Conversation

@eschaefer

@eschaefer eschaefer commented Aug 17, 2018
edited
Loading

Copy link
Copy Markdown

Maybe this opens a can of worms. I don't know. I found it useful. Real middleware would make it cooler.

let helpers = {
 setUser: (val) => ({ user: val }),
 setMessage: (val) => ({ message: val }),
};
let actions = store => ({
 simpleAction: (state, value) => ({ count: state.count + 1 }),
 thunkAction: (state, value) => async (action) => {
 let response = await fetch('/foo.json').then(res => res.json());
 action(helpers.setUser(response));
 action(helpers.setMessage('Thanks!');
 },
});

ptrwllrt, developit, renanliberato, orzechowskid, cj, ddoice, pauliusef, and Meenal9936 reacted with thumbs up emoji eduardoborges reacted with heart emoji
@cj

cj commented Sep 30, 2018

Copy link
Copy Markdown
Contributor

@developit I saw you thumbs upped this, would it be able to get merged in?

eschaefer, cj, and eduardoborges reacted with heart emoji

Copy link
Copy Markdown

Can't this already be done with Unistore's existing async action functions?
Here's a rewrite of your example above (I've kept the function name thunkAction):

let helpers = {
 setUser: (val) => ({ user: val }),
 setMessage: (val) => ({ message: val }),
};
let actions = store => ({
 simpleAction: (state, value) => ({ count: state.count + 1 }),
 async thunkAction(state, value) {
 let response = await fetch('/foo.json').then(res => res.json());
 const updatedState = {
 ...helpers.setUser(response),
 ...helpers.setMessage('Thanks!'),
 };
 return updatedState; // or with store.setState(updatedState)
 },
});

Isn't the async flow equivalent to this PR's "thunk'ed" approach?

Also, in your example action is invoked twice (for setUser and setMessage), so I am wondering if this is to make a particular point, or just a random example? (as you can see in my modified code snippet above, I combined the state update into a single object)

korzhyk and silenceisgolden reacted with thumbs up emoji

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Reviewers

No reviews

Assignees

No one assigned

Labels

None yet

Projects

None yet

Milestone

No milestone

Development

Successfully merging this pull request may close these issues.

AltStyle によって変換されたページ (->オリジナル) /