ftlabs / fastclick

Polyfill to remove click delays on browsers with touch UIs
MIT License
18.66k stars 3.22k forks source link

Strange issue with fastclick and angularJS in cordova/phonegap in iOS with the bind function #466

Open pauloya opened 8 years ago

pauloya commented 8 years ago

Hi, We were getting errors with a callstack that looked like this:

2095) 2016-04-13 15:05:50,911 ERROR - Error: [$rootScope:inprog] $digest already in progress http://errors.angularjs.org/1.5.0/$rootScope/inprog?p0=%24digest https://aaa.com/web/common/frameworks/angular.js:68:32 beginPhase@https://aaa.com/web/common/frameworks/angular.js:17178:31 $apply@https://aaa.com/web/common/frameworks/angular.js:16918:21 https://aaa.com/web/common/frameworks/angular.js:24551:29 dispatch@https://aaa.com/web/scripts/frameworks/jquery-2.1.1.min.js:3:6409 handle@https://aaa.com/web/scripts/frameworks/jquery-2.1.1.min.js:3:3184 dispatchEvent@[native code] sendClick@https://aaa.com/web/scripts/frameworks/fastclick.js:295:30 onTouchEnd@https://aaa.com/web/scripts/frameworks/fastclick.js:589:18 https://aaa.com/web/scripts/frameworks/fastclick.js:105:43 send@[native code] send@https://aaa.com/web/scripts/frameworks/jquery-2.1.1.min.js:4:14959 ajax@https://aaa.com/web/scripts/frameworks/jquery-2.1.1.min.js:4:10583 ajax@https://aaa.com/fins.api/Scripts/jquery.signalR-2.2.0.min.js:8:16074 ajaxSend@https://aaa.com/fins.api/Scripts/jquery.signalR-2.2.0.min.js:8:18424 send@https://aaa.com/fins.api/Scripts/jquery.signalR-2.2.0.min.js:8:26147 send@https://aaa.com/fins.api/Scripts/jquery.signalR-2.2.0.min.js:8:11696 invoke@https://aaa.com/fins.api/Scripts/jquery.signalR-2.2.0.min.js:8:34506 subscribeModel@https://aaa.com/fins.api/signalr/hubs:105:62 subscribeModelWithPromise@https://aaa.com/web/common/services/ModelCacheService.js:99:58 https://aaa.com/web/common/services/ModelCacheService.js:40:52 each@https://aaa.com/web/scripts/frameworks/jquery-2.1.1.min.js:2:2938 https://aaa.com/web/common/services/ModelCacheService.js:39:23 $emit@https://aaa.com/web/common/frameworks/angular.js:17070:38 https://aaa.com/web/common/services/SignalRService.js:214:43 processQueue@https://aaa.com/web/common/frameworks/angular.js:15552:30 https://aaa.com/web/common/frameworks/angular.js:15568:39 $eval@https://aaa.com/web/common/frameworks/angular.js:16820:28 $digest@https://aaa.com/web/common/frameworks/angular.js:16636:36 $apply@https://aaa.com/web/common/frameworks/angular.js:16928:31 https://aaa.com/web/common/frameworks/angular.js:18753:42 completeOutstandingRequest@https://aaa.com/web/common/frameworks/angular.js:5804:15 https://aaa.com/web/common/frameworks/angular.js:6081:33

The strange thing here is that ajax native send seems to call the onTouchEnd function. We could replicate the problem by touching the screen while an ajax call was pending (I'm not completely sure of the internals, but we could always replicate by touching the screen at a point where ajax would run).

This didn't happen in the browser, happened only within the cordova app in iOS.

We were able to fix it by renaming the function bind to fcBind. I believe this code somehow hooked event handlers with events it wasn't supposed to.

for (var i = 0, l = methods.length; i < l; i++) {
    context[methods[i]] = bind(context[methods[i]], context);
}

Like I said, renaming the function bind() to fcBind() fixed it. I thought I would leave this here in case someone else has a similar issue.

TombolaShepless commented 8 years ago

@pauloya We're seeing the same issue. Thanks for posting this, will try the fix!

TombolaShepless commented 8 years ago

@pauloya We took a slightly different approach if you're interested:

FastClick.prototype.sendClick = function (targetElement, event) {
     window.setTimeout(originalFunction.bind(this, targetElement, event));
}
pauloya commented 8 years ago

Hi, I don't think our fix is relevant, because we minify and the function is renamed anyway, and we still get errors. We did a similar fix on our side, making it asynchronous.

I'm still confused as to why an ajax send would trigger fastclick. Sounds like doing the async call doesn't solve the real problem, but just the symptom.

There is a similar issue reported here: https://stackoverflow.com/questions/29992980/debugging-digest-already-in-progress-error/36839750#36839750

briananderson1222 commented 8 years ago

Any word on whether the above fix might find its way into the source?

briananderson1222 commented 8 years ago

anyone mind sharing more details around how this problem was "fixed" did you just overwrite the implementation of sendClick to wrap the dispatchEvent in a setTimeout function?

michaelabuckley commented 7 years ago

I've published a PR at #533.