Skip to content
This repository has been archived by the owner on Mar 13, 2018. It is now read-only.

Commit

Permalink
Handle case where PointerGestures is loaded asynchronously
Browse files Browse the repository at this point in the history
Part of Polymer/platform-dev#11
  • Loading branch information
dfreedm committed Mar 2, 2014
1 parent f635375 commit 296fb7c
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 11 deletions.
3 changes: 2 additions & 1 deletion build.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,6 @@
"src/track.js",
"src/flick.js",
"src/pinch.js",
"src/tap.js"
"src/tap.js",
"src/registerScopes.js"
]
17 changes: 7 additions & 10 deletions src/dispatcher.js
Original file line number Diff line number Diff line change
Expand Up @@ -206,9 +206,11 @@
}
};
dispatcher.boundHandler = dispatcher.eventHandler.bind(dispatcher);
// recognizers call into the dispatcher and load later
// solve the chicken and egg problem by having registerScopes module run last
dispatcher.registerQueue = [];
dispatcher.immediateRegister = false;
scope.dispatcher = dispatcher;
var registerQueue = [];
var immediateRegister = false;
/**
* Enable gesture events for a given scope, typically
* [ShadowRoots](https://dvcs.w3.org/hg/webcomponents/raw-file/tip/spec/shadow/index.html#shadow-root-object).
Expand All @@ -219,20 +221,15 @@
* support on.
*/
scope.register = function(inScope) {
if (immediateRegister) {
if (dispatcher.immediateRegister) {
var pe = window.PointerEventsPolyfill;
if (pe) {
pe.register(inScope);
}
scope.dispatcher.registerTarget(inScope);
} else {
registerQueue.push(inScope);
dispatcher.registerQueue.push(inScope);
}
};
// wait to register scopes until recognizers load
document.addEventListener('DOMContentLoaded', function() {
immediateRegister = true;
registerQueue.push(document);
registerQueue.forEach(scope.register);
});
scope.register(document);
})(window.PointerGestures);
30 changes: 30 additions & 0 deletions src/registerScopes.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/*
* Copyright 2013 The Polymer Authors. All rights reserved.
* Use of this source code is governed by a BSD-style
* license that can be found in the LICENSE file.
*/

/**
* Because recognizers are loaded after dispatcher, we have to wait to register
* scopes until after all the recognizers.
*/
(function(scope) {
var dispatcher = scope.dispatcher;
function registerScopes() {
dispatcher.immediateRegister = true;
var rq = dispatcher.registerQueue;
rq.forEach(scope.register);
rq.length = 0;
}
if (document.readyState === 'complete') {
registerScopes();
} else {
// register scopes after a steadystate is reached
// less MutationObserver churn
document.addEventListener('readystatechange', function() {
if (document.readyState === 'complete') {
registerScopes();
}
});
}
})(window.PointerGestures);

0 comments on commit 296fb7c

Please sign in to comment.