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

feat: useController() #1048

Merged
merged 2 commits into from
Jul 18, 2021
Merged

feat: useController() #1048

merged 2 commits into from
Jul 18, 2021

Conversation

ntucker
Copy link
Collaborator

@ntucker ntucker commented Jul 18, 2021

Motivation

Consolidate, simplify hooks

Consistent interface between managers and hooks

Global referential equality available everywhere (managers and updaters)

Simplify and consolidate TTL and error concepts

Less code in hooks = less work on rendering leaf nodes

Icing on cake: ez migration to EndpointInterface and flexible args support for hooks

Future breaking changes can allow ez migration with version strings sent to useController('v2')

Solution

We're introducing fetch, invalidate, resetEntireStore today into experimental.

Full future plan:

const controller = useController();

// actions
controller.fetch(UserResource.detail(), { id }) // sideEffects means no throttle, otherwise throttle
controller.receive(payload, UserResource.detail(), { id })
controller.invalidate(UserResource.detail(), { id })
controller.resetEntireStore()
controller.subscribe(UserResource.detail(), { id })
controller.unsubscribe(UserResource.detail(), { id })
// posisble new
controller.abort(UserResource.detail(), { id }) // only aborts if in flight
// note: to force fetch of sideEffect: undefined - call abort first
// this should enable good offline/online managers

// retrieval
const state = useContext(StateContext);
const [value, expiresAt] = controller.getResponse(state, UserResource.detail(), { id })
const error = controller.getError(state, UserResource.detail(), { id })

});
dispatch(action);

return action.meta.promise as any;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
return action.meta.promise as any;
return action.meta.promise as Promise<unknown>;

packages/experimental/src/useController.ts Outdated Show resolved Hide resolved
Co-authored-by: Jordan Harband <ljharb@gmail.com>
@ntucker ntucker merged commit a485782 into master Jul 18, 2021
@ntucker ntucker deleted the feat/usecontroller branch July 18, 2021 14:47
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