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

Enable quick and complete cancellation of running searches #642

Merged

Conversation

Jaifroid
Copy link
Member

This is proof-of-concept code for the first part of the PR title (cancellation of running searches). It implements #637 and is in draft, for testing.

Because I believe the techniques and solution are the same, I intend to address #426 in this PR as well.

The code currently contains console log reports to prove that it cancels title search completely. They will be removed at a later stage.

@Jaifroid Jaifroid added this to the v2.9 milestone Jul 13, 2020
@Jaifroid Jaifroid self-assigned this Jul 13, 2020
@Jaifroid
Copy link
Member Author

Here is the output of console log running the above on two title searches in succession. The first is for "unesco world heritage site"; after a short time this is quickly replaced with "unesco world heritage". The former search is complex with many variations searched, and it is replaced by a simpler search.

Asterisks indicate where the first search was cancelled, and where the cancellation was registered - note that it is after the global search object has been changed, which proves that the running search object in zimArchive.js updates its status property to "cancelled" when it is modified in the global search object. When the global search object is completely replaced, the zimArchive search continues to point to the old search object that it was launched with (which has a modified status of "cancelled").

Hence, we have an effective signalling mechanism without recourse to "super-global" objects, and it is sufficient to pass all the needed information to zimArchive in a single appstate.search object.

image

@Jaifroid
Copy link
Member Author

Jaifroid commented Sep 3, 2020

Having looked back over this, I now believe it is too ambitious to address #426 in this PR. However, it implements #637 and could be merged as an easy PR (after removal of console.log entries).

@Jaifroid Jaifroid changed the title Enable quick and complete cancellation of running searches and asset loading Enable quick and complete cancellation of running searches Sep 3, 2020
@Jaifroid
Copy link
Member Author

Jaifroid commented Sep 8, 2020

I'm listing here the stages I mentioned in #637. I believe they have all been implemented, but I'll tick them off as I check through them before removing Draft status for this PR:

  • Move globalstate object from init.js to app.js; possibly rename appstate;
  • Pass the appstate.search object to zimArchive.js;
  • When a search is cancelled, set the appstate.search.status property to 'cancelled';
  • When a new search is undertaken, also set appstate.search.status to 'cancelled': this will be reflected in the search object passed to zimArchive.js because they both still point to the same underlying object;
  • Instantiate a new search object, and set appstate.search to this new object; the one passed to zimArchive.js will still point to the old object according to Javascript Pointer Reference craziness;
  • Insert an early cancellation at the very start of the binary search loop: if a search status is cancelled, return 0 instead of -1 or +1 -- this causes the loop to exit early;
  • Once all Promises have returned, the old search object is no longer referenced, and should be garbage collected automatically in due course.

@Jaifroid Jaifroid marked this pull request as ready for review September 8, 2020 17:15
@Jaifroid
Copy link
Member Author

Jaifroid commented Sep 8, 2020

@mossroy I have checked over the code (see checkboxes above) and added a short technical explanation of how the object assignment works between the different scopes, as it may be useful to my future self or other future devs encountering the code. But otherwise, this is ready for review. It effectively completes #617 (which was merged despite the known issue) by implementing #637.

NB I have left in (for now) console.log output that verifies precisely what is happening and that the old search is cancelled in its own scope as soon as possible. If you wish to test, follow this procedure: open a large ZIM like Wikipedia English all, and search for something that will take a fairly long time to search for in all its permutations, e.g. "unesco world heritage site". (use all lowercase) After a few seconds quickly backspace in the search field to delete "site" so that a new search is launched. Observe console.log entries as per screenshot above.

I will delete diagnostic console logging before squash/merge once the code is approved.

@Jaifroid Jaifroid requested a review from mossroy September 8, 2020 17:30
@mossroy
Copy link
Contributor

mossroy commented Sep 19, 2020

I'm not forgetting this PR, and will review it as soon as I can.
But it's hard for me to find some time

@Jaifroid
Copy link
Member Author

No rush, I sympathize -- things going crazy here too.

This was referenced Sep 29, 2020
@Jaifroid Jaifroid modified the milestones: v3.0, v3.1 Oct 3, 2020
Copy link
Contributor

@mossroy mossroy left a comment

Choose a reason for hiding this comment

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

It seems to be working well on my FirefoxOS device.
I added a few minor remarks

www/js/app.js Outdated Show resolved Hide resolved
www/js/app.js Outdated Show resolved Hide resolved
www/js/init.js Show resolved Hide resolved
@Jaifroid Jaifroid merged commit cff7dec into master Oct 25, 2020
@Jaifroid Jaifroid deleted the Enable-quick-and-complete-cancellation-of-running-searches branch October 25, 2020 17:38
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Optimize title search so that cancelled binary search Promises return at the earliest possible opportunity
2 participants