Skip to content

Commit

Permalink
fix(lib): fixed polling middleware
Browse files Browse the repository at this point in the history
  • Loading branch information
SaulMoro committed May 26, 2021
1 parent 7581a55 commit 4b49a90
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 23 deletions.
32 changes: 11 additions & 21 deletions projects/ngrx-rtk-query/src/lib/build-metareducer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,34 +5,24 @@ import { AngularHooksModule, AngularHooksModuleOptions } from './module';

export function buildMetaReducer({
api,
moduleOptions: { useDispatch: dispatch, getState },
moduleOptions: { useDispatch: dispatch },
}: {
api: any;
moduleOptions: Required<Pick<AngularHooksModuleOptions, 'useDispatch' | 'getState'>>;
moduleOptions: Required<Pick<AngularHooksModuleOptions, 'useDispatch'>>;
}): MetaReducer<any> {
const anyApi = api as Api<any, Record<string, any>, string, string, AngularHooksModule | CoreModule>;
const { unsubscribeQueryResult } = anyApi.internalActions;

return function (reducer: ActionReducer<any>): ActionReducer<any> {
return function (state: any, action: Action) {
const nextState = reducer(state, action);
let getNextState = () => nextState;

// Query inside forFeature (Code splitting)
if (!nextState[anyApi.reducerPath]) {
getNextState = () => ({ [anyApi.reducerPath]: nextState });
}
let nextState: any;
const getState = () =>
// Query inside forFeature (Code splitting)
!nextState || nextState[anyApi.reducerPath] ? nextState : { [anyApi.reducerPath]: nextState };

anyApi.middleware({
dispatch,
/**
* The unsuscriptQueryResult Action to remove unsubscriptions (handleUnsubscribe)
* dispatch a function with setTimeout. This function needs the state of the store after the
* tiemout so we have to pass the reference of the state with getState.
*/
getState: unsubscribeQueryResult.match(action) ? getState : getNextState,
})(getNextState)(action);
const middleware = anyApi.middleware({ dispatch, getState })(getState);

return function (reducer: ActionReducer<any>): ActionReducer<any> {
return function (state: any, action: Action) {
nextState = reducer(state, action);
middleware(action);
return nextState;
};
};
Expand Down
2 changes: 1 addition & 1 deletion projects/ngrx-rtk-query/src/lib/module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ export const angularHooksModule = ({
api,
moduleOptions: { useDispatch, useSelector, getState },
});
const metareducer: MetaReducer<any> = buildMetaReducer({ api, moduleOptions: { useDispatch, getState } });
const metareducer: MetaReducer<any> = buildMetaReducer({ api, moduleOptions: { useDispatch } });
safeAssign(anyApi, { usePrefetch });
safeAssign(anyApi, { metareducer });

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ import { useDeletePostMutation, useGetPostQuery, useUpdatePostMutation } from '.
changeDetection: ChangeDetectionStrategy.OnPush,
})
export class PostDetailComponent {
postQuery$ = useGetPostQuery(+this.route.snapshot.params.id).pipe(
postQuery$ = useGetPostQuery(+this.route.snapshot.params.id, { pollingInterval: 5000 }).pipe(
tap((result) => this.postFormControl.setValue(result.data?.name ?? '')),
);
updatePostMutation = useUpdatePostMutation();
Expand Down

0 comments on commit 4b49a90

Please sign in to comment.