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

Feature/promisify #293

Merged
merged 9 commits into from Apr 14, 2020
Merged

Feature/promisify #293

merged 9 commits into from Apr 14, 2020

Conversation

dxinteractive
Copy link
Collaborator

@dxinteractive dxinteractive commented Apr 8, 2020

Addresses #286

dataparcels

  • Added promisify to replace asyncValue and asyncChange. The new effect / async change reducer make this really easy.
    • Promises responses are queued so the order of promises being resolved / rejected is predicable
  • Can be used in:
    • useParcel onChange for async saving
    • useParcel source for async loading
    • useParcel / useBuffer / Boundary derive for async derivation such as validation
    • any modifyUp
import promisify from 'react-dataparcels/promisify';
import useParcel from 'react-dataparcels/useParcel';

let personParcel = useParcel({
    source: () => ({
        value: 123
    }),
    onChange: promisify({
        key: 'save',
        effect: async ({value, meta}) => {
            await sendRequest(value);
            // you can return {value, meta} from here if you want to make a further update
        }
        // optionally set "last: true" if you only want to take the response from the last effect to be fired. Can be useful if you have new promises being created before old ones have resolved and you only care about the one based off the most recent value e.g. async validation of a text input
        // optionally set "revert: true" if you want it to revert changes when a promise is rejected. Useful for form submissions, as this will indicate to the buffer that it should retry sending the same changes again next time a form is submitted + any new changes
    })
});

personParcel.meta.saveStatus = 'pending' | 'resolved' | 'rejected';
personParcel.meta.saveError = latest error result, only when rejected

@dxinteractive dxinteractive merged commit 1eab575 into release/strict-swift Apr 14, 2020
@dxinteractive dxinteractive deleted the feature/promisify branch April 14, 2020 04:23
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

2 participants