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

updateExistingStream: Reduce api calls on changing stream details. #3224

Closed
wants to merge 2 commits into from

Conversation

jainkuniya
Copy link
Member

Before inidividual api calls were made for each stream property which
is changed. But on server, this api is capable of handeling multiple
propery at a time. So just call api once with all the properties which
are changed.

Fixes: #3222

};

export default (auth: Auth, id: number, newValues: ParamsType): Promise<ApiResponse> =>
apiPatch(auth, `streams/${id}`, res => res, { ...newValues });
Copy link
Contributor

Choose a reason for hiding this comment

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

Do you need to spread the values?
```{ ...newValues }`` vs just newValues

Copy link
Member Author

@jainkuniya jainkuniya Dec 18, 2018

Choose a reason for hiding this comment

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

I tried with newValues , then flow is giving error.

Cannot call apiPatch with newValues bound to params because:
 • string [1] is incompatible with boolean [2] in property description.
 • string [1] is incompatible with number [3] in property description.
 • boolean [4] is incompatible with string [5] in property is_private.
 • boolean [4] is incompatible with number [3] in property is_private.
 • string [6] is incompatible with boolean [2] in property new_name.
 • string [6] is incompatible with number [3] in property new_name.

           src/api/streams/updateStream.js
       [6]  6│   new_name?: string,
       [1]  7│   description?: string,
       [4]  8│   is_private?: boolean,
            9│ };
           10│
           11│ export default (auth: Auth, id: number, newValues: ParamsType): Promise<ApiResponse> =>
           12│   apiPatch(auth, `streams/${id}`, res => res, newValues);
           13│

           src/utils/url.js
 [5][2][3] 16│ export type UrlParams = { [string]: string | boolean | number };



Found 6 errors

In toggleMobilePushSettings also we are creating new object.

export default async ({
  auth,
  opp,
  value,
}: {
  auth: Auth,
  opp: string,
  value: boolean,
}): Promise<ApiResponse> =>
  apiPatch(auth, 'settings/notifications', res => res, {
    ...getRequestBody(opp, value),
  });

So I thought of doing same 😅

export type ParamsType = {
new_name?: string,
description?: string,
is_private?: boolean,
Copy link
Contributor

Choose a reason for hiding this comment

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

Is this an exhaustive list?

Copy link
Member Author

Choose a reason for hiding this comment

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

yup! stream have limited properties.

Copy link
Contributor

Choose a reason for hiding this comment

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

ParamsType does not sound like concrete or a good name.
First, we don't name types *Type :)
Also, Param? It is a parameter/argument to a function but you can do better.

Copy link
Member Author

Choose a reason for hiding this comment

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

any name you want to suggest? :)
ApiBody, ApiParameters??

@@ -26,21 +27,20 @@ export const updateExistingStream = (
initialValues: Object,
newValues: Object,
) => async (dispatch: Dispatch, getState: GetState) => {
const apiParams: UpdateStreamApiParamsType = {};
Copy link
Contributor

Choose a reason for hiding this comment

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

This looks like a candidate for a utility function.
The chances are that we will add more fields and we will need this logic in other places.
What if you do create a currentValues object with the values needed.
Then we can diff it against the current version (via a utility function)

Copy link
Member Author

Choose a reason for hiding this comment

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

Then we need to do some refactors, like

  • currently we have name key (for stream name) in initialValues and newValues
    But at the time of sending parameters to API, we want key new_name, so again a if/else will be required.
    Suggestions?

Copy link
Contributor

Choose a reason for hiding this comment

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

I would suggest you do an object diffing function and then pass the resulting object (if it is not empty!) to this new function:
https://github.com/zulip/zulip-mobile/blob/master/src/api/apiFetch.js#L14

Copy link
Member Author

@jainkuniya jainkuniya left a comment

Choose a reason for hiding this comment

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

(#3140 is blocked on this)

Before inidividual api calls were made for each stream property which
is changed. But on server, this api is capable of handeling multiple
propery at a time. So just call api once with all the properties which
are changed.

Fixes: zulip#3222
Copy link
Member Author

@jainkuniya jainkuniya left a comment

Choose a reason for hiding this comment

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

@borisyankov Updated 👍

@gnprice
Copy link
Member

gnprice commented Jun 17, 2022

We ended up making this change in be12766 / #5341, with a slightly more explicit implementation.

@gnprice gnprice closed this Jun 17, 2022
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.

refactor updateExistingStream: Reduce api calls on changing stream details.
3 participants