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

Move search function to use Promise interface #32609

Closed
wants to merge 8 commits into from

Conversation

kgrz
Copy link

@kgrz kgrz commented Aug 16, 2017

Draft to fix #20650

Initial draft of changes for moving the search function inside FileSearch and
TextSearch modules to ause a promise interface instead of callbacks.

I have a couple of problems with the changes I made:

  1. The complete action's object shape is very different from that of
    progress. So much so that the usage doesn't feel natural to me.

    As @roblourens pointed out, may be it's time to have different interfaces
    for text and file search.

  2. This doesn't clean up code related to ripgrepsearch yet.

@mention-bot
Copy link

@kgrz, thanks for your PR! By analyzing the history of the files in this pull request, we identified @roblourens and @bpasero to be potential reviewers.

Copy link
Member

@roblourens roblourens left a comment

Choose a reason for hiding this comment

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

Thanks for tackling this! It looks like a great start to me.

The complete action's object shape is very different from that of progress. So much so that the usage doesn't feel natural to me.

Is that a problem? I would expect them to be different and carry different information

});
search(): PPromise<ISerializedSearchComplete, ISearchProgress<IRawFileMatch>> {
return new PPromise<ISerializedSearchComplete, ISearchProgress<IRawFileMatch>>((complete, error, progress) => {
this.walker.walk(this.folderQueries, this.extraFiles,
Copy link
Member

Choose a reason for hiding this comment

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

I think it would make sense for walk to return a PPromise as well.

Copy link
Author

Choose a reason for hiding this comment

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

Oh! yeah, I forgot to mention this in the PR description. This usage was very odd. Will try migrating this too.

@roblourens
Copy link
Member

Looks like there is a search-related test failing.

@kgrz
Copy link
Author

kgrz commented Aug 17, 2017

Weird. I didn't see this test run output when I ran on my local machine (OSX). Will recheck.

@microsoft microsoft deleted a comment from msftclas Sep 26, 2017
@microsoft microsoft deleted a comment from msftclas Sep 26, 2017
limitHit: isLimitHit,
stats: this.walker.getStats()
});
search(): PPromise<ISerializedSearchComplete, ISearchProgress<IRawFileMatch>> {

Choose a reason for hiding this comment

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

You forgot access modificator

Copy link
Author

Choose a reason for hiding this comment

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

Oh! thanks for pointing this out. Corrected.

search(): PPromise<ISerializedSearchComplete, ISearchProgress<IRawFileMatch>> {
return new PPromise<ISerializedSearchComplete, ISearchProgress<IRawFileMatch>>((complete, error, progress) => {
this.walker.walk(this.folderQueries, this.extraFiles,
(result: IRawFileMatch) => {

Choose a reason for hiding this comment

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

Why you use function body? You can write is like: (result: IRawFileMatch) => progress({ results: result });
In one line.

Copy link
Author

Choose a reason for hiding this comment

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

There's a subtle difference between calling a function and returning the result, especially in the case of promises. I wanted to keep the old structure.

engine.search((matches) => {
const totalMatches = matches.reduce((acc, m) => acc + m.numMatches, 0);
collector.addItems(matches, totalMatches);
engine.search().then((success) => {

Choose a reason for hiding this comment

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

Can you specify type for success if you don't specify type you aren't needed parentheses

} else {
c(stats);
}
const results = progress.results;

Choose a reason for hiding this comment

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

Which type of const results. Can you specify this type?

Copy link
Author

Choose a reason for hiding this comment

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

Same question as below. I'm leaning towards not specifying these implicit types everywhere.

c(stats);
}
const results = progress.results;
const totalMatches = results.reduce((acc, m) => acc + m.numMatches, 0);

Choose a reason for hiding this comment

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

Which type of const totalMatches. Can you specify this?
I think within parentheses you can specify type.

Copy link
Author

Choose a reason for hiding this comment

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

Do we need to specify types for automatically inferred primitives? Because we aren't implementing reduce, and the return type of that will never change (by our actions).

Copy link
Member

Choose a reason for hiding this comment

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

No, I don't specify the type when it isn't necessary. This is fine.

@roblourens
Copy link
Member

Thanks, but do you know why the tests are failing?

TODO: There's a bit of duplication inside the `complete` and `error`
callbacks.
@kgrz
Copy link
Author

kgrz commented Nov 12, 2017

@roblourens I'm sort of reworking this today. Will update once done.

@kgrz
Copy link
Author

kgrz commented Nov 12, 2017

Tests are failing because I made a mess 😐 I didn't change some of the search functions in tests to the promise format.

@kgrz
Copy link
Author

kgrz commented Nov 13, 2017

@roblourens I've reworked this changeset, this time preserving the old search and walk APIs for comparison. There are three unit tests failing, all related to the extraFiles config param in fileSearch tests. I'm unable figure this failure out. I tried increasing the timeouts, I did as much printf-debugging as possible and can confirm that the progress callback is being called. This leaves one possibility that this might be some race condition—either in the code itself, or the way the test is written. I might need a bit of help or pointers here.

To be more precise, the onResult inside the this.matchFile gets called, but this line in searchP never gets called. May be my mental modelling for this API is wrong.

@roblourens
Copy link
Member

Thanks, I promise I'll look at this when I have a minute.

@roblourens
Copy link
Member

roblourens commented Jan 3, 2018

Sorry for the delay. The tests are failing because of a tricky point with PPromise - if the progress callback is called synchronously inside the Promise constructor, then the progress call will be lost, because the caller's progress callback will not be attached. To work around this, you can wait a tick to give the caller a chance to attach the progress callback. We do this in several places. Here's one example: https://github.com/Microsoft/vscode/blob/4173c6478d4ac630fa4064485ecec610a02b8b5c/src/vs/workbench/services/search/node/searchService.ts#L88

That should be it. Besides that, there are just a couple things to do before I merge this -

  • Fix merge conflict - It doesn't look serious
  • Remove the previous implementations of these methods, we don't need both
  • Rename the new implementations to have the same name as the previous - e.g. searchP -> search

@roblourens
Copy link
Member

I'm largely rewriting this code as part of #47058

@roblourens roblourens closed this May 15, 2018
@github-actions github-actions bot locked and limited conversation to collaborators Mar 27, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Use promises in SearchEngine
6 participants