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: receive each advisory info separatly #32

Conversation

baruchiro
Copy link
Collaborator

@baruchiro baruchiro commented Mar 15, 2023

Fix #26
Depends on #30

First, I moved from message communication to port communication, as described here.

Then, I split the advisories fetching to send a message when each advisory info is fetched, instead of aggregating them all together.

Comment on lines +4 to +28
const listener = async ({ type, detail }, port) => {
if (type === REQUEST_PACKAGE_INFO_EVENT) {
const promises = await advisories(detail);
Object.entries(promises).forEach(([part, promise]) => {
promise.then((info) => {
port.postMessage({
type: RESPONSE_PACKAGE_INFO_EVENT,
detail: {
packageId: detail,
part,
info,
},
});
});
});
}

return true;
};

export const listen = () => {
chrome.runtime.onConnect.addListener((port) => {
port.onMessage.addListener(listener);
});
};
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

The code became a deep indentation, and also I'm struggling with the objects that passed between the events, each one requires a particular property for its use case.

I'm open to suggestions.

@baruchiro baruchiro force-pushed the baruchiro/get-package-info---from-single-response-to-async-responses-event-based branch from 2e91914 to fe6518f Compare March 15, 2023 15:31
Comment on lines +13 to 32
export default (packageID) => {
const depsDev = handleAsyncError(fetchDepsDev, packageID);
const info = depsDev.then((depsDevInfo) => {
const { latestVersion, licenses, stars } = depsDevInfo.data;
return {
latest: latestVersion,
licenses,
stars,
};
});

return { debricked, depsDev, openbase, snyk, socket };
return {
debricked: handleAsyncError(fetchDebricked, packageID),
depsDev,
info,
openbase: handleAsyncError(fetchOpenbase, packageID),
snyk: handleAsyncError(fetchSnyk, packageID),
socket: handleAsyncError(fetchSocket, packageID),
};
};
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Here is the trick. Instead of returning a complete result, I returned a Promise for each key.

This allows me to add a .then to each promise, for registering a port.sendMessage at the end of each promise.

@baruchiro baruchiro requested a review from jossef March 15, 2023 15:34
@baruchiro baruchiro marked this pull request as ready for review March 15, 2023 15:35
…esponse-to-async-responses-event-based

# Conflicts:
#	src/content.stackoverflow.js
@jossef jossef merged commit cebdb97 into master Mar 19, 2023
@jossef jossef deleted the baruchiro/get-package-info---from-single-response-to-async-responses-event-based branch March 19, 2023 07:37
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.

get package info - from single response to async responses (event based)
2 participants