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

Weird behavior on transport function override when onSuccess and onError are not called #212

Open
simplenotezy opened this issue Jan 27, 2020 · 1 comment

Comments

@simplenotezy
Copy link

simplenotezy commented Jan 27, 2020

I have detected some unusual behavior when overriding the transport property of the remote option.

Basically, I do some logic before determining if the response should be made, like so:

var engine = new Bloodhound({
	datumTokenizer: Bloodhound.tokenizers.obj.whitespace('value'),
	queryTokenizer: Bloodhound.tokenizers.whitespace,
	remote: {
		url: 'mysite.com?searchPlugins&q=%QUERY',
		wildcard: '%QUERY',
		cache: true,
		transport: function (options, onSuccess, onError) {
			var query = input.val(); // get the query

			if(query.startsWith('something')) {
				// make the request
				// abort previous requests made by typeahead

				options.url = options.url.replace('something', ''); // remove "something" from query

				$.ajax(options)
				.done(function(data, textStatus, request) { onSuccess(data); })
				.fail(function(request, textStatus, errorThrown) { onError(errorThrown); });
			}
		}
	}
});

However, if the if(query.startsWith('something')) { does not fire, resulting in neither onSuccess or onError are being called, then it might break the request from every being sent again. It only happen after X amount of requests sent, so the next request might work, but then after that, they will never fire.

I have debugged this issue, somewhat successfully, by removing the if(query.startsWith('something')) { statement. In this case, requests will always be sent, despite it being against the usecase.

I only want the request to be sent if the query contains something, otherwise not.

I am not sure if anything in the codebase depends on onSuccess or onError always being called?

@simplenotezy
Copy link
Author

simplenotezy commented Jan 27, 2020

My workaround, that fixes the issue, is below code.

If the query string is not present, the request is aborted just after being executed. That results in onError being executed. I have noticed onError will execute the function cb(true).

It's not pretty, but it at least doesn't prevent future remote responses from being fired, and it prevents unnecessary overhead for the server, processing requests, it shouldn't have received in the first place.

var engine = new Bloodhound({
	datumTokenizer: Bloodhound.tokenizers.obj.whitespace('value'),
	queryTokenizer: Bloodhound.tokenizers.whitespace,
	remote: {
		url: 'mysite.com?searchPlugins&q=%QUERY',
		wildcard: '%QUERY',
		cache: true,
		transport: function (options, onSuccess, onError) {
			var query = input.val(); // get the query

			// make the request
			// abort previous requests made by typeahead

			options.url = options.url.replace('something', ''); // remove "something" from query

			var request = $.ajax(options)
				.done(function(data, textStatus, request) { onSuccess(data); })
				.fail(function(request, textStatus, errorThrown) { onError(errorThrown); });
			
			if(!query.startsWith('something')) {
				request.abort();
			}
		}
	}
});

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

No branches or pull requests

1 participant